aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/consul.rs
blob: fca4f517899cbb5e072d1066e9621f5ad03d2514 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};

use hyper::client::Client;
use hyper::StatusCode;
use hyper::{Body, Method, Request};
use serde::Deserialize;

use netapp::NodeID;

use garage_util::error::Error;

#[derive(Deserialize, Clone, Debug)]
struct ConsulEntry {
	#[serde(alias = "Address")]
	address: String,
	#[serde(alias = "ServicePort")]
	service_port: u16,
	#[serde(alias = "NodeMeta")]
	node_meta: HashMap<String, String>,
}

pub async fn get_consul_nodes(
	consul_host: &str,
	consul_service_name: &str,
) -> Result<Vec<(NodeID, SocketAddr)>, Error> {
	let url = format!(
		"http://{}/v1/catalog/service/{}",
		consul_host, consul_service_name
	);
	let req = Request::builder()
		.uri(url)
		.method(Method::GET)
		.body(Body::default())?;

	let client = Client::new();

	let resp = client.request(req).await?;
	if resp.status() != StatusCode::OK {
		return Err(Error::Message(format!("HTTP error {}", resp.status())));
	}

	let body = hyper::body::to_bytes(resp.into_body()).await?;
	let entries = serde_json::from_slice::<Vec<ConsulEntry>>(body.as_ref())?;

	let mut ret = vec![];
	for ent in entries {
		let ip = ent.address.parse::<IpAddr>().ok();
		let pubkey = ent
			.node_meta
			.get("pubkey")
			.map(|k| hex::decode(&k).ok())
			.flatten()
			.map(|k| NodeID::from_slice(&k[..]))
			.flatten();
		if let (Some(ip), Some(pubkey)) = (ip, pubkey) {
			ret.push((pubkey, SocketAddr::new(ip, ent.service_port)));
		} else {
			warn!(
				"Could not process node spec from Consul: {:?} (invalid IP or public key)",
				ent
			);
		}
	}
	debug!("Got nodes from Consul: {:?}", ret);

	Ok(ret)
}