#![allow(clippy::too_many_arguments)]
use uuid::Uuid;
use crate::AxiamError;
use crate::client::AxiamClient;
use crate::management::models;
use crate::management::page::{Page, PageRequest, collect_pages};
use crate::management::request::{Call, Verb};
use crate::management::scope::Scope;
#[derive(Clone, Copy)]
pub struct Network<'c> {
pub(crate) client: &'c AxiamClient,
pub(crate) scope: Scope,
}
impl std::fmt::Debug for Network<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Network")
.field("scope", &self.scope)
.finish()
}
}
impl<'c> Network<'c> {
pub(crate) fn new(client: &'c AxiamClient) -> Self {
Self {
client,
scope: Scope::default(),
}
}
pub async fn list_segments(
&self,
site: Option<&str>,
page: PageRequest,
) -> Result<Page<models::NetworkSegmentResponse>, AxiamError> {
let mut query = page.query();
if let Some(v) = site {
query.push(("site", v.to_string()));
}
let call = Call {
operation: "network.list_segments",
verb: Verb::Get,
path_template: "/api/v1/network/segments",
path: "/api/v1/network/segments".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::NetworkSegmentResponse>>(call, None::<&()>)
.await
}
pub async fn list_segments_all(
&self,
site: Option<&str>,
start: PageRequest,
) -> Result<Vec<models::NetworkSegmentResponse>, AxiamError> {
collect_pages(start, |p| self.list_segments(site, p)).await
}
pub async fn create_segment(
&self,
body: &models::CreateNetworkSegmentRequest,
) -> Result<models::NetworkSegmentResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.create_segment",
verb: Verb::Post,
path_template: "/api/v1/network/segments",
path: "/api/v1/network/segments".to_string(),
query: &query,
};
self.client
.management_send::<_, models::NetworkSegmentResponse>(call, Some(body))
.await
}
pub async fn get_segment(
&self,
id: Uuid,
) -> Result<models::NetworkSegmentResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.get_segment",
verb: Verb::Get,
path_template: "/api/v1/network/segments/{id}",
path: format!("/api/v1/network/segments/{id}"),
query: &query,
};
self.client
.management_send::<_, models::NetworkSegmentResponse>(call, None::<&()>)
.await
}
pub async fn replace_segment(
&self,
id: Uuid,
body: &models::ReplaceNetworkSegmentRequest,
) -> Result<models::NetworkSegmentResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.replace_segment",
verb: Verb::Put,
path_template: "/api/v1/network/segments/{id}",
path: format!("/api/v1/network/segments/{id}"),
query: &query,
};
self.client
.management_send::<_, models::NetworkSegmentResponse>(call, Some(body))
.await
}
pub async fn delete_segment(&self, id: Uuid, version: i64) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = vec![("version", version.to_string())];
let call = Call {
operation: "network.delete_segment",
verb: Verb::Delete,
path_template: "/api/v1/network/segments/{id}",
path: format!("/api/v1/network/segments/{id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn list_profiles(
&self,
page: PageRequest,
) -> Result<Page<models::NetworkReplyProfile>, AxiamError> {
let query = page.query();
let call = Call {
operation: "network.list_profiles",
verb: Verb::Get,
path_template: "/api/v1/network/profiles",
path: "/api/v1/network/profiles".to_string(),
query: &query,
};
self.client
.management_send::<_, Page<models::NetworkReplyProfile>>(call, None::<&()>)
.await
}
pub async fn list_profiles_all(
&self,
start: PageRequest,
) -> Result<Vec<models::NetworkReplyProfile>, AxiamError> {
collect_pages(start, |p| self.list_profiles(p)).await
}
pub async fn create_profile(
&self,
body: &models::CreateNetworkReplyProfileRequest,
) -> Result<models::NetworkReplyProfile, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.create_profile",
verb: Verb::Post,
path_template: "/api/v1/network/profiles",
path: "/api/v1/network/profiles".to_string(),
query: &query,
};
self.client
.management_send::<_, models::NetworkReplyProfile>(call, Some(body))
.await
}
pub async fn get_profile(
&self,
segment_id: Uuid,
) -> Result<models::NetworkReplyProfile, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.get_profile",
verb: Verb::Get,
path_template: "/api/v1/network/profiles/{segment_id}",
path: format!("/api/v1/network/profiles/{segment_id}"),
query: &query,
};
self.client
.management_send::<_, models::NetworkReplyProfile>(call, None::<&()>)
.await
}
pub async fn replace_profile(
&self,
segment_id: Uuid,
body: &models::ReplaceNetworkReplyProfileRequest,
) -> Result<models::NetworkReplyProfile, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.replace_profile",
verb: Verb::Put,
path_template: "/api/v1/network/profiles/{segment_id}",
path: format!("/api/v1/network/profiles/{segment_id}"),
query: &query,
};
self.client
.management_send::<_, models::NetworkReplyProfile>(call, Some(body))
.await
}
pub async fn delete_profile(&self, segment_id: Uuid, version: i64) -> Result<(), AxiamError> {
let query: Vec<(&'static str, String)> = vec![("version", version.to_string())];
let call = Call {
operation: "network.delete_profile",
verb: Verb::Delete,
path_template: "/api/v1/network/profiles/{segment_id}",
path: format!("/api/v1/network/profiles/{segment_id}"),
query: &query,
};
self.client
.management_send_no_content(call, None::<&()>)
.await
}
pub async fn get_policy(&self) -> Result<models::NetworkPolicyResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.get_policy",
verb: Verb::Get,
path_template: "/api/v1/network/policy",
path: "/api/v1/network/policy".to_string(),
query: &query,
};
self.client
.management_send::<_, models::NetworkPolicyResponse>(call, None::<&()>)
.await
}
pub async fn replace_policy(
&self,
body: &models::ReplaceNetworkPolicyRequest,
) -> Result<models::NetworkPolicyResponse, AxiamError> {
let query: Vec<(&'static str, String)> = Vec::new();
let call = Call {
operation: "network.replace_policy",
verb: Verb::Put,
path_template: "/api/v1/network/policy",
path: "/api/v1/network/policy".to_string(),
query: &query,
};
self.client
.management_send::<_, models::NetworkPolicyResponse>(call, Some(body))
.await
}
}