use client::{Client, GoogleAPIResponse};
use std::error::Error;
#[derive(Deserialize, Debug, Clone)]
pub enum InstanceStatus {
PROVISIONING,
STAGING,
RUNNING,
STOPPING,
STOPPED,
SUSPENDING,
SUSPENDED,
TERMINATED,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct InstanceItem {
pub id: String,
pub name: String,
pub status: InstanceStatus,
}
impl InstanceItem {
pub fn start(&self, client: &Client) -> Result<GoogleAPIResponse, Box<Error>> {
client.post(&format!("/instances/{}/start", self.name), vec![])
}
pub fn stop(&self, client: &Client) -> Result<GoogleAPIResponse, Box<Error>> {
client.post(&format!("/instances/{}/stop", self.name), vec![])
}
}
pub type InstanceItems = Vec<InstanceItem>;
pub fn empty_instance_items() -> InstanceItems {
vec![]
}
#[derive(Debug)]
pub struct InstanceOp<'a> {
pub client: &'a Client<'a>,
}
impl<'a> InstanceOp<'a> {
pub fn list(&self) -> Result<InstanceItems, Box<Error>> {
self.client.get("/instances", vec![]).and_then(|response| {
match response {
GoogleAPIResponse::GoogleAPISuccessResponse(resp) => Ok(resp.items),
_ => Err(From::from("Invalid response from GoogleAPI")),
}
})
}
}