Skip to content

Commit

Permalink
providers/openstack: initial implementation
Browse files Browse the repository at this point in the history
this implements getting metadata from the openstack metadata service
  • Loading branch information
Derek Gonyeo authored and sdemos committed Oct 18, 2017
1 parent 4e83bf5 commit 2ee7e10
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn fetch_metadata(provider: &str) -> Result<Metadata> {
"digitalocean" => digitalocean::fetch_metadata(),
"ec2" => ec2::fetch_metadata(),
"gce" => gce::fetch_metadata(),
"openstack" => openstack::fetch_metadata(),
"openstack-metadata" => openstack::network::fetch_metadata(),
"oracle-oci" => oracle::fetch_metadata(),
"packet" => packet::fetch_metadata(),
"vagrant-virtualbox" => vagrant_virtualbox::fetch_metadata(),
Expand Down
8 changes: 1 addition & 7 deletions src/providers/openstack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@

//! openstack metadata fetcher

use metadata::Metadata;

use errors::*;

pub fn fetch_metadata() -> Result<Metadata> {
unimplemented!();
}
pub mod network;
44 changes: 44 additions & 0 deletions src/providers/openstack/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! openstack metadata fetcher

use errors::*;
use metadata::Metadata;
use retry;

const URL: &'static str = "http://169.254.169.254/latest/meta-data";

fn url_for_key(key: &str) -> String {
format!("{}/{}", URL, key)
}

pub fn fetch_metadata() -> Result<Metadata> {
let client = retry::Client::new()
.chain_err(|| "openstack: failed to create http client")?;

let hostname: Option<String> = client.get(retry::Raw, url_for_key("hostname")).send()?;

Ok(Metadata::builder()
.add_attribute_if_exists("OPENSTACK_INSTANCE_ID".to_owned(), client.get(retry::Raw, url_for_key("instance-id")).send()?)
.add_attribute_if_exists("OPENSTACK_IPV4_LOCAL".to_owned(), client.get(retry::Raw, url_for_key("local-ipv4")).send()?)
.add_attribute_if_exists("OPENSTACK_IPV4_PUBLIC".to_owned(), client.get(retry::Raw, url_for_key("public-ipv4")).send()?)
.add_attribute_if_exists("OPENSTACK_HOSTNAME".to_owned(), hostname.clone())
.set_hostname_if_exists(hostname)
.add_ssh_keys(fetch_keys(&client)?)?
.build())
}

fn fetch_keys(client: &retry::Client) -> Result<Vec<String>> {
let keys_list: Option<String> = client.get(retry::Raw, url_for_key("public-keys")).send()?;
let mut keys = Vec::new();
if let Some(keys_list) = keys_list {
for l in keys_list.lines() {
let tokens: Vec<&str> = l.split('=').collect();
if tokens.len() != 2 {
return Err("error parsing keyID".into());
}
let key: String = client.get(retry::Raw, url_for_key(&format!("public-keys/{}/openssh-key", tokens[0]))).send()?
.ok_or("missing ssh key")?;
keys.push(key);
}
}
Ok(keys)
}

0 comments on commit 2ee7e10

Please sign in to comment.