add get rpc calls

This commit is contained in:
Samuel Lorch 2023-10-28 19:47:29 +02:00
parent e7628e97fb
commit 21c1f303aa
5 changed files with 145 additions and 1 deletions

35
src/api/firewall.rs Normal file
View file

@ -0,0 +1,35 @@
use jsonrpsee::types::Params;
use crate::{
definitions::firewall::{DestinationNATRule, ForwardRule, SourceNATRule},
state::RpcState,
};
use super::ApiError;
pub fn get_forward_rules(_: Params, state: &RpcState) -> Result<Vec<ForwardRule>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.firewall
.forward_rules)
}
pub fn get_source_nat_rules(_: Params, state: &RpcState) -> Result<Vec<SourceNATRule>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.firewall
.source_nat_rules)
}
pub fn get_destination_nat_rules(
_: Params,
state: &RpcState,
) -> Result<Vec<DestinationNATRule>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.firewall
.destination_nat_rules)
}

View file

@ -1,4 +1,7 @@
mod firewall;
mod network; mod network;
mod object;
mod service;
mod system; mod system;
use crate::state::RpcState; use crate::state::RpcState;
@ -44,5 +47,47 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
.register_method("network.get_static_routes", network::get_static_routes) .register_method("network.get_static_routes", network::get_static_routes)
.unwrap(); .unwrap();
module
.register_method("network.get_interfaces", network::get_interfaces)
.unwrap();
module
.register_method("object.get_services", object::get_services)
.unwrap();
module
.register_method("object.get_addresses", object::get_addresses)
.unwrap();
module
.register_method("service.get_dhcp_servers", service::get_dhcp_servers)
.unwrap();
module
.register_method("service.get_dns_servers", service::get_dns_servers)
.unwrap();
module
.register_method("service.get_ntp_servers", service::get_ntp_servers)
.unwrap();
module
.register_method("firewall.get_forward_rules", firewall::get_forward_rules)
.unwrap();
module
.register_method(
"firewall.get_destination_nat_rules",
firewall::get_destination_nat_rules,
)
.unwrap();
module
.register_method(
"firewall.get_source_nat_rules",
firewall::get_source_nat_rules,
)
.unwrap();
module module
} }

View file

@ -1,6 +1,11 @@
use std::collections::HashMap;
use jsonrpsee::types::Params; use jsonrpsee::types::Params;
use crate::{definitions::network::StaticRoute, state::RpcState}; use crate::{
definitions::network::{NetworkInterface, StaticRoute},
state::RpcState,
};
use super::ApiError; use super::ApiError;
@ -11,3 +16,10 @@ pub fn get_static_routes(_: Params, state: &RpcState) -> Result<Vec<StaticRoute>
.network .network
.static_routes) .static_routes)
} }
pub fn get_interfaces(
_: Params,
state: &RpcState,
) -> Result<HashMap<String, NetworkInterface>, ApiError> {
Ok(state.config_manager.get_pending_config().network.interfaces)
}

18
src/api/object.rs Normal file
View file

@ -0,0 +1,18 @@
use std::collections::HashMap;
use jsonrpsee::types::Params;
use crate::{
definitions::object::{Address, Service},
state::RpcState,
};
use super::ApiError;
pub fn get_services(_: Params, state: &RpcState) -> Result<HashMap<String, Service>, ApiError> {
Ok(state.config_manager.get_pending_config().object.services)
}
pub fn get_addresses(_: Params, state: &RpcState) -> Result<HashMap<String, Address>, ApiError> {
Ok(state.config_manager.get_pending_config().object.addresses)
}

34
src/api/service.rs Normal file
View file

@ -0,0 +1,34 @@
use std::collections::HashMap;
use jsonrpsee::types::Params;
use crate::{
definitions::service::{DHCPServer, DNSServer, NTPServer},
state::RpcState,
};
use super::ApiError;
pub fn get_dhcp_servers(_: Params, state: &RpcState) -> Result<Vec<DHCPServer>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.service
.dhcp_servers)
}
pub fn get_dns_servers(_: Params, state: &RpcState) -> Result<Vec<DNSServer>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.service
.dns_servers)
}
pub fn get_ntp_servers(_: Params, state: &RpcState) -> Result<Vec<NTPServer>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.service
.ntp_servers)
}