ubi-rs 0.1.3

A Rust cli and library for ubicloud API
Documentation
use crate::client::{HTTPClient, encode_param};
use crate::errors::UbiClientError;
use crate::{make_json_request, make_request};
use serde::Deserialize;
use serde::Serialize;
use std::sync::Arc;

pub struct KCClient {
    http_client: Arc<HTTPClient>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct QueryParams {
    start_after: Option<String>,
    page_size: Option<u32>,
    order_column: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ReqClusterCreate {
    pub version: String,
    pub worker_size: String,
    pub cp_nodes: u32,
    pub worker_nodes: u32,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ResponseListKubernetesClusters {
    pub count: u32,
    pub items: Vec<ClusterItem>,
}

#[derive(Debug, Serialize, Deserialize, Default)]

pub struct ClusterItem {
    pub cp_node_count: u32,
    pub cp_vms: Option<Vec<VirtualMachine>>,
    pub display_state: String,
    pub id: String,
    pub location: String,
    pub name: String,
    pub node_size: String,
    pub nodepools: Option<Vec<NodePool>>,
    pub services_load_balancer_url: Option<String>,
    pub version: String,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct NodePool {
    pub kubernetes_cluster_id: String,
    pub id: String,
    pub name: String,
    pub node_count: u32,
    pub node_size: String,
    pub vms: Vec<VirtualMachine>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct VirtualMachine {
    pub id: String,
    pub ip4: String,
    pub ip4_enabled: bool,
    pub ip6: String,
    pub location: String,
    pub name: String,
    pub size: String,
    pub state: String,
    pub storage_size_gib: u32,
    pub unix_user: String,
}

impl KCClient {
    pub fn new(http_client: Arc<HTTPClient>) -> Self {
        KCClient { http_client }
    }

    /// List KubernetesClusters in a specific location of a project
    /// https://api.ubicloud.com/project/{project_id}/kubernetes-cluster
    ///
    /// project_id
    /// string
    /// required
    pub async fn list_kubernetes_clusters(
        &self,
        project_id: &str,
        query_params: Option<QueryParams>,
    ) -> Result<ResponseListKubernetesClusters, UbiClientError> {
        let url = &format!("project/{}/kubernetes-cluster", encode_param(project_id));

        let body_empty = serde_json::json!({});
        make_json_request!(
            self,
            reqwest::Method::GET,
            url,
            body_empty,
            query_params,
            ResponseListKubernetesClusters
        )
    }

    /// Create a new KubernetesCluster in a specific location of a project
    /// https://api.ubicloud.com/project/{project_id}/location/{location}/kubernetes-cluster/{kubernetes_cluster_reference}
    ///
    /// # Arguments:
    /// * `project_id` - The ID of the project.
    /// * `location` - The location/region of the Kubernetes cluster.
    /// * `kubernetes_cluster_reference` - The ID or name of the Kubernetes cluster.
    /// * `payload` - The request body containing the details of the Kubernetes cluster to create.
    ///
    /// This function sends a POST request to create a new Kubernetes cluster with the specified parameters.
    pub async fn create_kubernetes_cluster(
        &self,
        project_id: &str,
        location: &str,
        kubernetes_cluster_reference: &str,
        payload: &ReqClusterCreate,
    ) -> Result<ClusterItem, UbiClientError> {
        let url = &format!(
            "project/{}/location/{}/kubernetes-cluster/{}",
            encode_param(project_id),
            location,
            kubernetes_cluster_reference
        );

        let query_params = [(); 0]; // Option<QueryParams>
        make_json_request!(
            self,
            reqwest::Method::POST,
            url,
            payload,
            query_params,
            ClusterItem
        )
    }

    /// Delete a specific KubernetesCluster
    /// https://api.ubicloud.com/project/{project_id}/location/{location}/kubernetes-cluster/{kubernetes_cluster_reference}
    ///
    /// # Arguments:
    /// * `project_id` - The ID of the project.
    /// * `location` - The location/region of the Kubernetes cluster.
    /// * `kubernetes_cluster_reference` - The ID or name of the Kubernetes cluster.
    ///
    /// This function sends a DELETE request to remove the specified Kubernetes cluster.
    ///
    pub async fn delete_kubernetes_cluster(
        &self,
        project_id: &str,
        location: &str,
        kubernetes_cluster_reference: &str,
    ) -> Result<(), UbiClientError> {
        let url = &format!(
            "project/{}/location/{}/kubernetes-cluster/{}",
            encode_param(project_id),
            location,
            kubernetes_cluster_reference
        );

        make_request!(self, reqwest::Method::DELETE, url)
    }
}