Skip to content

Commit

Permalink
Merge pull request #10 from Lachstec/dev
Browse files Browse the repository at this point in the history
Release version 0.1.2
  • Loading branch information
Lachstec authored Feb 15, 2024
2 parents 2e46f46 + afdad0d commit 79eb9cd
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = """
An asynchrounous gNMI client to interact with and manage network devices.
"""
name = "ginmi"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
keywords = ["grpc", "async", "gnmi", "network-automation"]
license = "MIT OR Apache-2.0"
Expand All @@ -27,5 +27,8 @@ tower-service = "0.3.2"
http = "0.2.0"
tower = "0.4.13"

[dev-dependencies]
tokio-test = "0.4.3"

[build-dependencies]
tonic-build = "0.11.0"
115 changes: 115 additions & 0 deletions src/client/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use crate::Client;
use crate::gen::gnmi::CapabilityResponse;
use crate::gen::gnmi::ModelData;

pub use crate::gen::gnmi::Encoding;

/// Capabilities of a given gNMI Target device.
///
/// Contains information about the capabilities that supported by a gNMI Target device.
/// Obtained via [`Client::capabilities`].
#[derive(Debug, Clone)]
pub struct Capabilities(pub CapabilityResponse);

impl<'a> Capabilities {
/// Retrieve the gNMI Version that the target device supports.
///
/// # Examples
/// ```rust
/// # use ginmi::{Client, Capabilities};
/// # fn main() -> std::io::Result<()> {
/// # tokio_test::block_on(async {
/// # const CERT: &str = "CA Certificate";
/// let mut client = Client::builder("https://clab-srl01-srl:57400")
/// .tls(CERT, "clab-srl01-srl")
/// .credentials("admin", "admin")
/// .build()
/// .await
/// .unwrap();
///
/// let capabilities = client.capabilities().await.unwrap();
/// let version = capabilities.gnmi_version();
/// # });
/// # Ok(())
/// # }
/// ```
pub fn gnmi_version(&'a self) -> &'a str {
self.0.g_nmi_version.as_str()
}

/// Check if target device supports a given model.
///
/// # Arguments
/// - name: Name of the model
/// - organization: Organization publishing the model
/// - version: Version of the model
///
/// # Examples
/// ```rust
/// # use ginmi::{Client, Capabilities};
/// # fn main() -> std::io::Result<()> {
/// # tokio_test::block_on(async {
/// # const CERT: &str = "CA Certificate";
/// let mut client = Client::builder("https://clab-srl01-srl:57400")
/// .tls(CERT, "clab-srl01-srl")
/// .credentials("admin", "admin")
/// .build()
/// .await
/// .unwrap();
///
/// let capabilities = client.capabilities().await.unwrap();
/// let supports_aaa_nokia = capabilities.supports_model(
/// "urn:srl_nokia/aaa:srl_nokia-aaa",
/// "Nokia",
/// "2023-10-31"
/// );
/// # });
/// # Ok(())
/// # }
/// ```
pub fn supports_model(&self, name: &str, organization: &str, version: &str) -> bool {
self.0.supported_models.contains(&ModelData {
name: name.to_string(),
organization: organization.to_string(),
version: version.to_string()
})
}

/// Check if a target device supports a given [`Encoding`].
///
/// # Arguments
/// - encoding: The [`Encoding`] to check for.
///
/// # Examples
/// ```rust
/// # use ginmi::{Client, Capabilities, Encoding};
/// # fn main() -> std::io::Result<()> {
/// # tokio_test::block_on(async {
/// # const CERT: &str = "CA Certificate";
/// let mut client = Client::builder("https://clab-srl01-srl:57400")
/// .tls(CERT, "clab-srl01-srl")
/// .credentials("admin", "admin")
/// .build()
/// .await
/// .unwrap();
///
/// let capabilities = client.capabilities().await.unwrap();
/// let supports_json = capabilities.supports_encoding(Encoding::Json);
///
/// # });
/// # Ok(())
/// # }
/// ```
pub fn supports_encoding(&self, encoding: Encoding) -> bool {
let enc: i32 = match encoding {
Encoding::Json => 0,
Encoding::Bytes => 1,
Encoding::Proto => 2,
Encoding::Ascii => 3,
Encoding::JsonIetf => 4
};

self.0.supported_encodings.contains(&enc)
}
}

21 changes: 11 additions & 10 deletions src/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::auth::AuthService;
use crate::error::GinmiError;
use crate::gen::gnmi::g_nmi_client::GNmiClient;
use crate::gen::gnmi::{CapabilityRequest, CapabilityResponse};
use crate::gen::gnmi::CapabilityRequest;
use super::capabilities::Capabilities;
use http::HeaderValue;
use std::str::FromStr;
use std::sync::Arc;
Expand All @@ -25,23 +26,23 @@ impl<'a> Client {
///
/// # Examples
/// ```rust
/// # use ginmi::Client
/// # tokio_test::block_on({ async
/// # use ginmi::Client;
/// # tokio_test::block_on(async {
/// # const CERT: &str = "CA Certificate";
/// let mut client = Client::builder("https://clab-srl01-srl:57400")
/// .tls(CERT, "clab-srl01-srl")
/// .credentials("admin", "admin")
/// .build()
/// .await
/// .unwrap();
///
/// let capabilities = client.capabilities().await;
/// # }
/// let capabilities = client.capabilities().await.unwrap();
/// # });
/// ```
pub async fn capabilities(&mut self) -> CapabilityResponse {
pub async fn capabilities(&mut self) -> Result<Capabilities, GinmiError> {
let req = CapabilityRequest::default();
match self.inner.capabilities(req).await {
Ok(val) => val.into_inner(),
Err(e) => panic!("Error getting capabilities: {:?}", e),
}
let res = self.inner.capabilities(req).await?;
Ok(Capabilities(res.into_inner()))
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
mod client;
mod capabilities;

pub use client::{Client, ClientBuilder};
pub use client::{Client, ClientBuilder};

pub use capabilities::{
Capabilities,
Encoding
};
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ pub enum GinmiError {
InvalidUriError(String),
#[error("invalid header in grpc request: {}", .0)]
InvalidHeaderValue(#[from] http::header::InvalidHeaderValue),
#[error("error communicating with target device: {}", .0)]
GrpcError(#[from] tonic::Status),
}
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ mod auth;
mod client;
mod error;

pub use client::{Client, ClientBuilder};

pub use client::{
Client,
ClientBuilder,
Encoding,
Capabilities
};
pub use error::GinmiError;

pub(crate) mod gen {
Expand Down

0 comments on commit 79eb9cd

Please sign in to comment.