Skip to main content

Crate network_device

Crate network_device 

Source
Expand description

A dependency-free, strongly typed eDSL and base contracts for network device operations and declarative infrastructure resources.

The imperative API runs operations directly through Driver and Execute. The declarative API describes desired resources for an IacBackend. These models are intentionally separate.

§Direct execution

use network_device::{
    AdministrativeState, Driver, Execute, InterfaceName, Run,
    SetInterfaceState, operation,
};
use std::convert::Infallible;
use std::future::Future;

struct Device;

impl Driver for Device {
    type Error = Infallible;
}

impl Execute<SetInterfaceState> for Device {
    type Output = ();

    fn execute<'a>(
        &'a self,
        _operation: SetInterfaceState,
    ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send + 'a {
        async { Ok(()) }
    }
}

let name = InterfaceName::try_from("Ethernet1/1").expect("valid interface name");
operation(SetInterfaceState::new(name, AdministrativeState::Enabled))
    .run(&Device)
    .await?;

§IaC backend

use network_device::{
    AdministrativeState, IacBackend, InterfaceConfiguration, InterfaceName,
};
use std::convert::Infallible;
use std::future::Future;

struct LocalBackend;

impl IacBackend<InterfaceConfiguration> for LocalBackend {
    type Error = Infallible;
    type Plan = usize;
    type ApplyResult = ();

    fn plan<'a>(
        &'a self,
        desired: &'a InterfaceConfiguration,
    ) -> impl Future<Output = Result<Self::Plan, Self::Error>> + Send + 'a {
        async move { Ok(usize::from(desired.administrative_state.is_some())) }
    }

    fn apply<'a>(
        &'a self,
        _desired: &'a InterfaceConfiguration,
    ) -> impl Future<Output = Result<Self::ApplyResult, Self::Error>> + Send + 'a {
        async { Ok(()) }
    }
}

let desired = InterfaceConfiguration::new(
    InterfaceName::try_from("Ethernet1/1").expect("valid interface name"),
)
.administrative_state(AdministrativeState::Enabled);
let backend = LocalBackend;
let _plan = backend.plan(&desired).await?;
backend.apply(&desired).await?;

Structs§

EnsureStaticRoute
Imperative operation to ensure a static route exists.
InterfaceConfiguration
A declarative desired configuration for an interface.
InterfaceName
A validated, vendor-neutral interface identifier.
IpPrefix
A validated IP address and network prefix length.
IpPrefixError
Error returned when an IP prefix length exceeds its address family’s limit.
OperationStep
A typed step in a direct-operation program.
RemoveStaticRoute
Imperative operation to remove a static route.
Sequence
Two operation steps executed in order.
SetInterfaceDescription
Imperative operation to set an interface description.
SetInterfaceState
Imperative operation to set an interface’s administrative state.
StaticRoute
A static route’s destination and next-hop address.
StaticRouteConfiguration
A declarative desired configuration for a static route.

Enums§

AdministrativeState
Desired administrative state for an interface.
InterfaceNameError
Validation errors for InterfaceName.
IpPrefixParseError
Parsing errors for IpPrefix.
StaticRouteError
Validation error for StaticRoute.

Traits§

Driver
A device driver capable of executing one or more typed operations.
Execute
Executes a specific operation on a Driver.
IacBackend
Plans and applies a resource’s declarative desired state.
Run
Runs a DSL value against a statically selected driver.

Functions§

operation
Starts a typed operation step.