nomad-client 0.0.1

A Rust client for HashiCorp Nomad's API.
Documentation
/*
 * Nomad
 *
 * Nomad OpenApi specification
 *
 * The version of the OpenAPI document: 0.11.0
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;

use crate::apis::ResponseContent;
use super::{Error, configuration};


/// struct for typed errors of method `deregister_volume`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeregisterVolumeError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `get_volume`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetVolumeError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `get_volumes`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetVolumesError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `register_volume`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RegisterVolumeError {
    UnknownValue(serde_json::Value),
}


pub async fn deregister_volume(configuration: &configuration::Configuration, volume_id: &str) -> Result<(), Error<DeregisterVolumeError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/volume/csi/{volume_id}", configuration.base_path, volume_id=crate::apis::urlencode(volume_id));
    let mut local_var_req_builder = local_var_client.delete(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        Ok(())
    } else {
        let local_var_entity: Option<DeregisterVolumeError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn get_volume(configuration: &configuration::Configuration, volume_id: &str) -> Result<crate::models::CsiVolume, Error<GetVolumeError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/volume/csi/{volume_id}", configuration.base_path, volume_id=crate::apis::urlencode(volume_id));
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetVolumeError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn get_volumes(configuration: &configuration::Configuration, _type: Option<&str>, node_id: Option<&str>, plugin_id: Option<&str>) -> Result<Vec<crate::models::CsiVolumeListStub>, Error<GetVolumesError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/volumes", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    if let Some(ref local_var_str) = _type {
        local_var_req_builder = local_var_req_builder.query(&[("type", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = node_id {
        local_var_req_builder = local_var_req_builder.query(&[("node_id", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = plugin_id {
        local_var_req_builder = local_var_req_builder.query(&[("plugin_id", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetVolumesError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

pub async fn register_volume(configuration: &configuration::Configuration, volume_id: &str, csi_volume_register_request: Option<crate::models::CsiVolumeRegisterRequest>) -> Result<(), Error<RegisterVolumeError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/volume/csi/{volume_id}", configuration.base_path, volume_id=crate::apis::urlencode(volume_id));
    let mut local_var_req_builder = local_var_client.put(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    local_var_req_builder = local_var_req_builder.json(&csi_volume_register_request);

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        Ok(())
    } else {
        let local_var_entity: Option<RegisterVolumeError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}