Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement agent service register & deregister. fix request parse json error #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,31 @@ pub struct AgentService {
pub ModifyIndex: u64,
}

#[derive(Eq, Default, PartialEq, Serialize, Deserialize, Debug)]
#[serde(default)]
pub struct AgentServiceRegistration {
pub ID: String,
pub Name: String,
pub Tags: Option<Vec<String>>,
pub Port: u16,
pub Address: String,
pub EnableTagOverride: bool,
}

impl AgentServiceRegistration {
pub fn new(name: String) -> AgentServiceRegistration {
AgentServiceRegistration {
Name: name,
..Default::default()
}
}
}

//I haven't implemetned https://www.consul.io/api/agent.html#read-configuration
//I haven't implemetned https://www.consul.io/api/agent.html#stream-logs
pub trait Agent {
fn service_register(&self, asr: &AgentServiceRegistration) -> Result<()>;
fn service_deregister(&self, service_id: &str) -> Result<()>;
fn checks(&self) -> Result<HashMap<String, AgentCheck>>;
fn members(&self, wan: bool) -> Result<AgentMember>;
fn reload(&self) -> Result<()>;
Expand All @@ -59,6 +81,29 @@ pub trait Agent {
}

impl Agent for Client {
/// https://www.consul.io/api-docs/agent/service#register-service
fn service_register(&self, asr: &AgentServiceRegistration) -> Result<()> {
put(
"/v1/agent/service/register",
Some(asr),
&self.config,
HashMap::new(),
None,
)
.map(|x| x.0)
}
/// https://www.consul.io/api-docs/agent/service#deregister-service
fn service_deregister(&self, service_id: &str) -> Result<()> {
let path = "/v1/agent/service/deregister/".to_owned() + service_id;
put(
path.as_str(),
None as Option<&()>,
&self.config,
HashMap::new(),
None,
)
.map(|x| x.0)
}
/// https://www.consul.io/api/agent/check.html#list-checks
fn checks(&self) -> Result<HashMap<String, AgentCheck>> {
get("/v1/agent/checks", &self.config, HashMap::new(), None).map(|x| x.0)
Expand Down
17 changes: 13 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn get<R: DeserializeOwned>(
})
}

pub fn delete<R: DeserializeOwned>(
pub fn delete<R: DeserializeOwned + Default>(
path: &str,
config: &Config,
params: HashMap<String, String>,
Expand All @@ -166,7 +166,7 @@ pub fn post<T: Serialize, R: DeserializeOwned>(path: &str,
write_with_body(path, body, config, options, req)
}
*/
pub fn put<T: Serialize, R: DeserializeOwned>(
pub fn put<T: Serialize, R: DeserializeOwned + Default>(
path: &str,
body: Option<&T>,
config: &Config,
Expand All @@ -177,7 +177,7 @@ pub fn put<T: Serialize, R: DeserializeOwned>(
write_with_body(path, body, config, params, options, req)
}

fn write_with_body<T: Serialize, R: DeserializeOwned, F>(
fn write_with_body<T: Serialize, R: DeserializeOwned + Default, F>(
path: &str,
body: Option<&T>,
config: &Config,
Expand Down Expand Up @@ -210,7 +210,16 @@ where
builder
.send()
.chain_err(|| "HTTP request to consul failed")
.and_then(|x| x.json().chain_err(|| "Failed to parse JSON"))
.and_then(|x| match x.text().chain_err(|| "Failed to get text") {
Ok(text) => {
if text.is_empty() {
Ok(Default::default())
} else {
serde_json::from_str(&text).chain_err(|| "Failed to parse JSON")
}
}
Err(err) => Err(err),
})
.map(|x| {
(
x,
Expand Down