Skip to content

Commit

Permalink
Merge pull request #47 from get-convex/service-tags
Browse files Browse the repository at this point in the history
Deserialize tags into Service struct
  • Loading branch information
kushudai authored Jul 12, 2024
2 parents e18ce70 + 4bf2f81 commit 1b4141a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
env:
# We pass the config as a JSON here to simulate one service with 3 nodes.
# TODO: Ideally, we should use the same setup in local environment (`testdata/config.hcl`) in GHA test.
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003} ]}'
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001, "tags": ["first"]}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002, "tags": ["second"]}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003, "tags": ["third"]} ]}'

env:
CONSUL_HTTP_ADDR: http://consul:8500
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
env:
# We pass the config as a JSON here to simulate one service with 3 nodes.
# TODO: Ideally, we should use the same setup in local environment (`testdata/config.hcl`) in GHA test.
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003} ]}'
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001, "tags": ["first"]}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002, "tags": ["second"]}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003, "tags": ["third"]} ]}'
env:
CONSUL_HTTP_ADDR: http://consul:8500

Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,19 @@ mod tests {
.iter()
.all(|item| addresses.contains(item)));

let tags: Vec<String> = response
.iter()
.map(|sn| sn.service.tags.clone().into_iter())
.flatten()
.collect();
let expected_tags = vec![
"first".to_string(),
"second".to_string(),
"third".to_string(),
];
assert_eq!(expected_tags.len(), 3);
assert!(expected_tags.iter().all(|tag| tags.contains(tag)));

let _: Vec<_> = response
.iter()
.map(|sn| assert_eq!("dc1", sn.node.datacenter))
Expand Down Expand Up @@ -1116,13 +1129,15 @@ mod tests {
service: "node".to_string(),
address: "2.2.2.2".to_string(),
port: 32,
tags: vec!["foo".to_string(), "bar=baz".to_string()],
};

let empty_service = Service {
id: "".to_string(),
service: "".to_string(),
address: "".to_string(),
port: 32,
tags: vec![],
};

let sn = ServiceNode {
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ pub struct Service {
pub address: String,
/// The port of the instance.
pub port: u16,
/// Tags assigned to the service instance.
pub tags: Vec<String>,
}

pub(crate) fn serialize_duration_as_string<S>(
Expand Down
3 changes: 3 additions & 0 deletions testdata/config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services {
address = "1.1.1.1"
port = 20001
checks = []
tags = ["first"]
}

services {
Expand All @@ -19,6 +20,7 @@ services {
address = "2.2.2.2"
port = 20002
checks = []
tags = ["second"]
}

services {
Expand All @@ -27,4 +29,5 @@ services {
address = "3.3.3.3"
port = 20003
checks = []
tags = ["third"]
}

0 comments on commit 1b4141a

Please sign in to comment.