src/api2/config/network.rs: implement delete property

This commit is contained in:
Dietmar Maurer 2020-04-22 12:19:31 +02:00
parent 27f2c23049
commit c016482c7a

View File

@ -1,5 +1,6 @@
use anyhow::{Error}; use anyhow::{Error};
use serde_json::{Value, to_value}; use serde_json::{Value, to_value};
use ::serde::{Deserialize, Serialize};
use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission}; use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, Permission};
@ -73,6 +74,25 @@ pub fn read_interface(name: String) -> Result<Value, Error> {
Ok(data) Ok(data)
} }
#[api()]
#[derive(Serialize, Deserialize)]
#[allow(non_camel_case_types)]
/// Deletable property name
pub enum DeletableProperty {
/// Delete the IPv4 address property.
address_v4,
/// Delete the IPv6 address property.
address_v6,
/// Delete the IPv4 gateway property.
gateway_v4,
/// Delete the IPv6 gateway property.
gateway_v6,
/// Delete the whole IPv4 configuration entry.
method_v4,
/// Delete the whole IPv6 configuration entry.
method_v6,
}
#[api( #[api(
protected: true, protected: true,
input: { input: {
@ -96,6 +116,14 @@ pub fn read_interface(name: String) -> Result<Value, Error> {
schema: IP_SCHEMA, schema: IP_SCHEMA,
optional: true, optional: true,
}, },
delete: {
description: "List of properties to delete.",
type: Array,
optional: true,
items: {
type: DeletableProperty,
}
},
digest: { digest: {
optional: true, optional: true,
schema: PROXMOX_CONFIG_DIGEST_SCHEMA, schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
@ -113,6 +141,7 @@ pub fn update_interface(
method_v6: Option<NetworkConfigMethod>, method_v6: Option<NetworkConfigMethod>,
address: Option<String>, address: Option<String>,
gateway: Option<String>, gateway: Option<String>,
delete: Option<Vec<DeletableProperty>>,
digest: Option<String>, digest: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -127,6 +156,19 @@ pub fn update_interface(
let interface = config.lookup_mut(&name)?; let interface = config.lookup_mut(&name)?;
if let Some(delete) = delete {
for name in delete {
match name {
DeletableProperty::address_v4 => { interface.cidr_v4 = None; },
DeletableProperty::address_v6 => { interface.cidr_v6 = None; },
DeletableProperty::gateway_v4 => { interface.gateway_v4 = None; },
DeletableProperty::gateway_v6 => { interface.gateway_v6 = None; },
DeletableProperty::method_v4 => { interface.method_v4 = None; },
DeletableProperty::method_v6 => { interface.method_v6 = None; },
}
}
}
if method_v4.is_some() { interface.method_v4 = method_v4; } if method_v4.is_some() { interface.method_v4 = method_v4; }
if method_v6.is_some() { interface.method_v6 = method_v6; } if method_v6.is_some() { interface.method_v6 = method_v6; }