Skip to content

Commit bf50b76

Browse files
committed
keylimectl: Fix agent add URL for API version 3.0
In version 3.0, the add operation should make a POST request to the /v3.0/agents/ without the ID of the agent being added. Signed-off-by: Anderson Toshiyuki Sasaki <[email protected]>
1 parent 9609be7 commit bf50b76

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

keylimectl/src/client/verifier.rs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,16 @@ impl VerifierClient {
549549
) -> Result<Value, KeylimectlError> {
550550
debug!("Adding agent {agent_uuid} to verifier");
551551

552-
let url = format!(
553-
"{}/v{}/agents/{}",
554-
self.base.base_url, self.api_version, agent_uuid
555-
);
552+
// API v3.0+ uses POST /v3.0/agents/ (without agent ID)
553+
// API v2.x uses POST /v2.x/agents/{agent_id}
554+
let url = if self.api_version.parse::<f32>().unwrap_or(2.1) >= 3.0 {
555+
format!("{}/v{}/agents/", self.base.base_url, self.api_version)
556+
} else {
557+
format!(
558+
"{}/v{}/agents/{}",
559+
self.base.base_url, self.api_version, agent_uuid
560+
)
561+
};
556562

557563
debug!(
558564
"POST {url} with data: {}",
@@ -2088,5 +2094,50 @@ mod tests {
20882094
assert!(expected_url.ends_with("/"));
20892095
assert!(!expected_url.contains("/agents"));
20902096
}
2097+
2098+
#[test]
2099+
fn test_add_agent_url_construction() {
2100+
// Test that add_agent URLs are constructed correctly for different API versions
2101+
let config = create_test_config();
2102+
let mut client =
2103+
VerifierClient::new_without_version_detection(&config)
2104+
.unwrap();
2105+
let base_url = &client.base.base_url;
2106+
let agent_uuid = "test-agent-uuid";
2107+
2108+
// Test API v2.x (includes agent UUID in URL)
2109+
client.api_version = "2.1".to_string();
2110+
let api_version_f32 =
2111+
client.api_version.parse::<f32>().unwrap_or(2.1);
2112+
let url_v2 = if api_version_f32 >= 3.0 {
2113+
format!("{base_url}/v{}/agents/", client.api_version)
2114+
} else {
2115+
format!(
2116+
"{base_url}/v{}/agents/{agent_uuid}",
2117+
client.api_version
2118+
)
2119+
};
2120+
assert_eq!(
2121+
url_v2,
2122+
format!("{base_url}/v2.1/agents/{agent_uuid}")
2123+
);
2124+
assert!(url_v2.contains(agent_uuid));
2125+
2126+
// Test API v3.0 (excludes agent UUID from URL)
2127+
client.api_version = "3.0".to_string();
2128+
let api_version_f32 =
2129+
client.api_version.parse::<f32>().unwrap_or(2.1);
2130+
let url_v3 = if api_version_f32 >= 3.0 {
2131+
format!("{base_url}/v{}/agents/", client.api_version)
2132+
} else {
2133+
format!(
2134+
"{base_url}/v{}/agents/{agent_uuid}",
2135+
client.api_version
2136+
)
2137+
};
2138+
assert_eq!(url_v3, format!("{base_url}/v3.0/agents/"));
2139+
assert!(!url_v3.contains(agent_uuid));
2140+
assert!(url_v3.ends_with("/agents/"));
2141+
}
20912142
}
20922143
}

0 commit comments

Comments
 (0)