Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
fix: fixes issue with creating dns records
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Jun 3, 2015
1 parent 1699de9 commit 2b3c2eb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 107 deletions.
57 changes: 13 additions & 44 deletions src/request/builder/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ doapi_enum! {
}
}

// type string The record type (A, MX, CNAME, etc). All Records
// name string The host name, alias, or service being defined by the record. A, AAAA, CNAME, TXT, SRV
// data string Variable data depending on record type. See the [Domain Records]() section for more detail on each record type. A, AAAA, CNAME, MX, TXT, SRV, NS
// priority nullable number The priority of the host (for SRV and MX records. null otherwise). MX, SRV
// port nullable number The port that the service is accessible on (for SRV records only. null otherwise). SRV
// weight nullable number The weight of records with the same priority (for SRV records only. null otherwise). SRV
#[derive(Serialize)]
pub struct DnsRecord {
#[serde(rename = "type")]
Expand Down Expand Up @@ -78,9 +84,9 @@ impl<'t> RequestBuilder<'t, response::DnsRecords> {
// "type" : "MX" // All records
// "name" : "alias" // A, AAAA, CNAME, TXT, SRV
// "data" : "varies" // A, AAAA, CNAME, MX, TXT, SRV, NS
// "priority" : "20" // MX, SRV
// "port" : "80" // SRV
// "weight" : "200" // SRV
// "priority" : 20 // MX, SRV
// "port" : 80 // SRV
// "weight" : 200 // SRV

// FIXME: Don't unwrap()
RequestBuilder {
Expand All @@ -90,38 +96,19 @@ impl<'t> RequestBuilder<'t, response::DnsRecords> {
resp_t: PhantomData,
body: Some(json::to_string(rec).ok().unwrap())
}


// let mut hm = HashMap::new();
// hm.insert("type", rec.rec_type.to_string());
// if let Some(ref n) = rec.name {
// hm.insert("name", n.to_owned());
// }
// if let Some(ref d) = rec.data {
// hm.insert("data", d.to_owned());
// }
// if let Some(p) = rec.priority {
// hm.insert("priority", p.to_string());
// }
// if let Some(p) = rec.port {
// hm.insert("name", p.to_string());
// }
// if let Some(w) = rec.weight {
// hm.insert("name", w.to_string());
// }
}
}

impl<'t> RequestBuilder<'t, response::DnsRecord> {
pub fn update(mut self, record: &DnsRecord) -> RequestBuilder<'t, response::DnsRecord> {
pub fn update(self, record: &DnsRecord) -> RequestBuilder<'t, response::DnsRecord> {
// PUT: "https://api.digitalocean.com/v2/domains/$DOMAIN/records/$ID"
// body:
// "type" : "MX" // All records
// "name" : "alias" // A, AAAA, CNAME, TXT, SRV
// "data" : "varies" // A, AAAA, CNAME, MX, TXT, SRV, NS
// "priority" : "20" // MX, SRV
// "port" : "80" // SRV
// "weight" : "200" // SRV
// "priority" : 20 // MX, SRV
// "port" : 80 // SRV
// "weight" : 200 // SRV
// FIXME: Don't unwrap()
RequestBuilder {
method: Method::Put,
Expand All @@ -130,24 +117,6 @@ impl<'t> RequestBuilder<'t, response::DnsRecord> {
resp_t: PhantomData,
body: Some(json::to_string(record).ok().unwrap())
}

// let mut hm = HashMap::new();
// hm.insert("type", record.rec_type.to_string());
// if let Some(ref n) = record.name {
// hm.insert("name", n.to_owned());
// }
// if let Some(ref d) = record.data {
// hm.insert("data", d.to_owned());
// }
// if let Some(p) = record.priority {
// hm.insert("priority", p.to_string());
// }
// if let Some(p) = record.port {
// hm.insert("name", p.to_string());
// }
// if let Some(w) = record.weight {
// hm.insert("name", w.to_string());
// }
}
pub fn delete(self) -> RequestBuilder<'t, response::HeaderOnly> {
// DELETE: "https://api.digitalocean.com/v2/domains/$id"
Expand Down
6 changes: 1 addition & 5 deletions src/request/builder/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ impl<'t> RequestBuilder<'t, response::Domains> {
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(format!("{{\"name\":\"{}\",\"ip_address\":\"{}\"}}", name, ip))
body: Some(format!("{{\"name\":{:?},\"ip_address\":{:?}}}", name, ip))
}

// let mut hm = HashMap::new();
// hm.insert("name", name.to_owned());
// hm.insert("ip_address", ip.to_owned());
}
}

Expand Down
75 changes: 54 additions & 21 deletions src/request/builder/droplets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ struct DropletWithId {
impl DropletWithId {
fn from_droplet(d: &Droplet) -> DropletWithId {
DropletWithId {
name: d.name().clone(),
name: d.name.clone(),
region: d.region.clone(),
size: d.size.clone(),
image: &d.image[..].parse().ok().unwrap(),
image: d.image[..].parse().ok().unwrap(),
ssh_keys: d.ssh_keys.clone(),
backups: d.backups,
ipv6: d.ipv6,
Expand Down Expand Up @@ -87,7 +87,40 @@ impl fmt::Display for Droplet {
},
self.backups,
self.ipv6,
self.private_net,
self.private_networking,
if let Some(d) = self.user_data.clone() {
d
} else {
"None".to_owned()
}
)
}
}

impl fmt::Display for DropletWithId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"Name: {}\n\
Region: {}\n\
Size: {}\n\
Image: {}\n\
SSH Keys: {}\n\
Backups Enabled: {}\n\
IPv6 Enabled: {}\n\
Private Networking Enabled: {}\n\
User Data: {}\n",
self.name,
self.region,
self.size,
self.image,
if let Some(ref v) = self.ssh_keys {
v.iter().fold(String::new(), |acc, s| acc + &format!(" {},", s)[..])
} else {
"None".to_owned()
},
self.backups,
self.ipv6,
self.private_networking,
if let Some(d) = self.user_data.clone() {
d
} else {
Expand All @@ -111,7 +144,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"disable_backups\"}}"))
}
}
pub fn reboot(self) -> RequestBuilder<'t, response::Action> {
pub fn reboot(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "reboot"
Expand All @@ -124,7 +157,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"reboot\"}}"))
}
}
pub fn power_cycle(self) -> RequestBuilder<'t, response::Action> {
pub fn power_cycle(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "power_cycle"
Expand All @@ -137,7 +170,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"power_cycle\"}}"))
}
}
pub fn shutdown(self) -> RequestBuilder<'t, response::Action> {
pub fn shutdown(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "shutdown"
Expand All @@ -150,7 +183,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"shutdown\"}}"))
}
}
pub fn power_off(self) -> RequestBuilder<'t, response::Action> {
pub fn power_off(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "power_off"
Expand All @@ -163,7 +196,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"power_off\"}}"))
}
}
pub fn power_on(self) -> RequestBuilder<'t, response::Action> {
pub fn power_on(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "power_on"
Expand All @@ -176,14 +209,14 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"power_on\"}}"))
}
}
pub fn restore(self, img: &str) -> RequestBuilder<'t, response::Action> {
pub fn restore(mut self, img: &str) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "restore"
// "image": 12356 // number if ID, string if slug
self.url.push_str("/actions");
let image = match img.parse::<u64>() {
Ok(n) => img.to_string(),
Ok(_) => img.to_string(),
Err(_) => format!("\"{}\"", img)
};
RequestBuilder {
Expand All @@ -194,7 +227,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"restore\",\"image\":{}}}", image))
}
}
pub fn reset_password(self) -> RequestBuilder<'t, response::Action> {
pub fn reset_password(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "password_reset"
Expand All @@ -207,7 +240,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"password_reset\"}}"))
}
}
pub fn resize(self, size: &str, disk: bool) -> RequestBuilder<'t, response::Action> {
pub fn resize(mut self, size: &str, disk: bool) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "resize"
Expand All @@ -222,14 +255,14 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"resize\",\"disk\":{},\"size\":{:?}}}", disk.to_string(), size))
}
}
pub fn rebuild(self, img: &str) -> RequestBuilder<'t, response::Action> {
pub fn rebuild(mut self, img: &str) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "rebuild"
// "image": 12356 // number if ID, string if slug
self.url.push_str("/actions");
let image = match img.parse::<u64>() {
Ok(n) => img.to_string(),
Ok(_) => img.to_string(),
Err(_) => format!("\"{}\"", img)
};
RequestBuilder {
Expand All @@ -240,7 +273,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"rebuild\",\"image\":{}}}", image))
}
}
pub fn rename(self, name: &str) -> RequestBuilder<'t, response::Action> {
pub fn rename(mut self, name: &str) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "rename"
Expand All @@ -254,7 +287,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"rename\",\"name\":{:?}}}", name))
}
}
pub fn change_kernel(self, kernel: &str) -> RequestBuilder<'t, response::Action> {
pub fn change_kernel(mut self, kernel: &str) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "change_kernel"
Expand All @@ -268,7 +301,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"change_kernel\",\"kernel\":{}}}", kernel))
}
}
pub fn enable_ipv6(self) -> RequestBuilder<'t, response::Action> {
pub fn enable_ipv6(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "enable_ipv6"
Expand All @@ -281,7 +314,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"enable_ipv6\"}}"))
}
}
pub fn enable_private_networking(self) -> RequestBuilder<'t, response::Action> {
pub fn enable_private_networking(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "enable_private_networking"
Expand All @@ -294,7 +327,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"enable_private_networking\"}}"))
}
}
pub fn snapshot(self, name: &str) -> RequestBuilder<'t, response::Action> {
pub fn snapshot(mut self, name: &str) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "snapshot"
Expand All @@ -308,7 +341,7 @@ impl<'t> RequestBuilder<'t, response::Droplet> {
body: Some(format!("{{\"type\":\"snapshot\",\"name\":{:?}}}", name))
}
}
pub fn upgrade(self) -> RequestBuilder<'t, response::Action> {
pub fn upgrade(mut self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/droplets/$ID/actions"
// body:
// "type" : "upgrade"
Expand Down Expand Up @@ -383,7 +416,7 @@ impl<'t> RequestBuilder<'t, response::Droplets> {

// FIXME: don't unwrap()

if &droplet.image[..].parse::<u64>().is_ok() {
if droplet.image[..].parse::<u64>().is_ok() {
let d = DropletWithId::from_droplet(droplet);
return RequestBuilder {
method: Method::Post,
Expand Down
16 changes: 3 additions & 13 deletions src/request/builder/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ impl<'t> RequestBuilder<'t, response::Image> {
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(format!("{{\"type\":\"transfer\",\"region\":\"{}\"}}", region))
body: Some(format!("{{\"type\":\"transfer\",\"region\":{:?}}}", region))
}

// let mut hm = HashMap::new();
// hm.insert("type", "transfer".to_owned());
// hm.insert("region", region.to_owned());
}
pub fn convert(self) -> RequestBuilder<'t, response::Action> {
// POST: "https://api.digitalocean.com/v2/images/7938291/actions"
Expand All @@ -33,11 +29,8 @@ impl<'t> RequestBuilder<'t, response::Image> {
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(format!("{{\"type\":\"convert\"}}"))
body: Some(r#"{"type":"convert"}"#.to_owned())
}

// let mut hm = HashMap::new();
// hm.insert("type", "convert".to_owned());
}
pub fn actions(mut self) -> RequestBuilder<'t, response::Actions> {
// GET: https://api.digitalocean.com/v2/images/$SLUG
Expand Down Expand Up @@ -66,11 +59,8 @@ impl<'t> RequestBuilder<'t, response::Image> {
url: self.url,
auth: self.auth,
resp_t: PhantomData,
body: Some(format!("{{\"name\":\"{}\"}}", name))
body: Some(format!("{{\"name\":{:?}}}", name))
}

// let mut hm = HashMap::new();
// hm.insert("name", name.to_owned());
}
pub fn delete(self) -> RequestBuilder<'t, response::HeaderOnly> {
// DELETE: https://api.digitalocean.com/v2/images/$ID
Expand Down
11 changes: 2 additions & 9 deletions src/request/builder/ssh_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ impl<'t> RequestBuilder<'t, response::SshKeys> {
auth: self.auth,
url: self.url,
resp_t: PhantomData,
body: Some(format!("{{\"name\":\"{}\",\"public_key\":\"{}\"}}", name, pub_key))
body: Some(format!("{{\"name\":{:?},\"public_key\":{:?}}}", name, pub_key))
}

// let mut hm = HashMap::new();
// hm.insert("name", name.to_owned());
// hm.insert("public_key", pub_key.to_owned());
}
}

Expand All @@ -40,11 +36,8 @@ impl<'t> RequestBuilder<'t, response::SshKey> {
url: self.url,
auth: self.auth,
resp_t: PhantomData,
body: Some(format!("{{\"name\":\"{}\"}}", name))
body: Some(format!("{{\"name\":{:?}}}", name))
}

// let mut hm = HashMap::new();
// hm.insert("name", name.to_owned());
}
pub fn destroy(self) -> RequestBuilder<'t, response::HeaderOnly> {
// DELETE: "https://api.digitalocean.com/v2/account/keys/$ID"
Expand Down
Loading

0 comments on commit 2b3c2eb

Please sign in to comment.