diff options
| author | Alexandre Derumier <aderumier@odiso.com> | 2019-11-26 10:00:17 +0100 |
|---|---|---|
| committer | Thomas Lamprecht <t.lamprecht@proxmox.com> | 2019-11-26 12:33:40 +0100 |
| commit | 4140be9e7918d97f8b0a5da7856714dbd61ea083 (patch) | |
| tree | ccb3b3bc634502f23e00eab88862e6daf1bd9e08 /PVE/API2/Network/SDN/Zones.pm | |
| parent | f5eabba018956d5599edbc1578365bb15615ee26 (diff) | |
api2 : split vnets/zones/controllers
Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
Diffstat (limited to 'PVE/API2/Network/SDN/Zones.pm')
| -rw-r--r-- | PVE/API2/Network/SDN/Zones.pm | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/PVE/API2/Network/SDN/Zones.pm b/PVE/API2/Network/SDN/Zones.pm new file mode 100644 index 0000000..a447813 --- /dev/null +++ b/PVE/API2/Network/SDN/Zones.pm @@ -0,0 +1,291 @@ +package PVE::API2::Network::SDN::Zones; + +use strict; +use warnings; + +use PVE::SafeSyslog; +use PVE::Tools qw(extract_param); +use PVE::Cluster qw(cfs_read_file cfs_write_file); +use PVE::Network::SDN::Vnets; +use PVE::Network::SDN::Zones; +use PVE::Network::SDN::Zones::Plugin; +use PVE::Network::SDN::Zones::VlanPlugin; +use PVE::Network::SDN::Zones::QinQPlugin; +use PVE::Network::SDN::Zones::VxlanPlugin; +use PVE::Network::SDN::Zones::EvpnPlugin; +use PVE::Network::SDN::Zones::FaucetPlugin; + +use Storable qw(dclone); +use PVE::JSONSchema qw(get_standard_option); +use PVE::RPCEnvironment; + +use PVE::RESTHandler; + +use base qw(PVE::RESTHandler); + +my $sdn_zones_type_enum = PVE::Network::SDN::Zones::Plugin->lookup_types(); + +my $api_sdn_zones_config = sub { + my ($cfg, $id) = @_; + + my $scfg = dclone(PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id)); + $scfg->{zone} = $id; + $scfg->{digest} = $cfg->{digest}; + + return $scfg; +}; + +__PACKAGE__->register_method ({ + name => 'index', + path => '', + method => 'GET', + description => "SDN zones index.", + permissions => { + description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/cluster/sdn/zones/<zone>'", + user => 'all', + }, + parameters => { + additionalProperties => 0, + properties => { + type => { + description => "Only list sdn zones of specific type", + type => 'string', + enum => $sdn_zones_type_enum, + optional => 1, + }, + }, + }, + returns => { + type => 'array', + items => { + type => "object", + properties => { zone => { type => 'string'}, + type => { type => 'string'}, + }, + }, + links => [ { rel => 'child', href => "{zone}" } ], + }, + code => sub { + my ($param) = @_; + + my $rpcenv = PVE::RPCEnvironment::get(); + my $authuser = $rpcenv->get_user(); + + + my $cfg = PVE::Network::SDN::Zones::config(); + + my @sids = PVE::Network::SDN::Zones::sdn_zones_ids($cfg); + my $res = []; + foreach my $id (@sids) { +# my $privs = [ 'SDN.Audit', 'SDN.Allocate' ]; +# next if !$rpcenv->check_any($authuser, "/cluster/sdn/zones/$id", $privs, 1); + + my $scfg = &$api_sdn_zones_config($cfg, $id); + next if $param->{type} && $param->{type} ne $scfg->{type}; + + my $plugin_config = $cfg->{ids}->{$id}; + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($plugin_config->{type}); + push @$res, $scfg; + } + + return $res; + }}); + +__PACKAGE__->register_method ({ + name => 'read', + path => '{zone}', + method => 'GET', + description => "Read sdn zone configuration.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones/{zone}', ['SDN.Allocate']], +# }, + + parameters => { + additionalProperties => 0, + properties => { + zone => get_standard_option('pve-sdn-zone-id'), + }, + }, + returns => { type => 'object' }, + code => sub { + my ($param) = @_; + + my $cfg = PVE::Network::SDN::Zones::config(); + + return &$api_sdn_zones_config($cfg, $param->{zone}); + }}); + +__PACKAGE__->register_method ({ + name => 'create', + protected => 1, + path => '', + method => 'POST', + description => "Create a new sdn zone object.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']], +# }, + parameters => PVE::Network::SDN::Zones::Plugin->createSchema(), + returns => { type => 'null' }, + code => sub { + my ($param) = @_; + + my $type = extract_param($param, 'type'); + my $id = extract_param($param, 'zone'); + + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($type); + my $opts = $plugin->check_config($id, $param, 1, 1); + + PVE::Network::SDN::Zones::lock_sdn_zones_config( + sub { + + my $cfg = PVE::Network::SDN::Zones::config(); + + my $scfg = undef; + if ($scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id, 1)) { + die "sdn zone object ID '$id' already defined\n"; + } + + $cfg->{ids}->{$id} = $opts; + $plugin->on_update_hook($id, $cfg); + + PVE::Network::SDN::Zones::write_config($cfg); + + }, "create sdn zone object failed"); + + return undef; + }}); + +__PACKAGE__->register_method ({ + name => 'apply_configuration', + protected => 1, + path => '', + method => 'PUT', + description => "Apply sdn zone changes.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']], +# }, + parameters => { + additionalProperties => 0, + }, + returns => { type => 'null' }, + code => sub { + my ($param) = @_; + + die "no sdn zone changes to apply" if !-e "/etc/pve/sdn/zones.cfg.new"; + rename("/etc/pve/sdn/zones.cfg.new", "/etc/pve/sdn/zones.cfg") + || die "applying sdn/zones.cfg changes failed - $!\n"; + + + return undef; + }}); + +__PACKAGE__->register_method ({ + name => 'revert_configuration', + protected => 1, + path => '', + method => 'DELETE', + description => "Revert sdn zone changes.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']], +# }, + parameters => { + additionalProperties => 0, + }, + returns => { type => 'null' }, + code => sub { + my ($param) = @_; + + die "no sdn zones changes to revert" if !-e "/etc/pve/sdn/zones.cfg.new"; + unlink "/etc/pve/sdn/zones.cfg.new"; + + return undef; + }}); + +__PACKAGE__->register_method ({ + name => 'update', + protected => 1, + path => '{zone}', + method => 'PUT', + description => "Update sdn zone object configuration.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']], +# }, + parameters => PVE::Network::SDN::Zones::Plugin->updateSchema(), + returns => { type => 'null' }, + code => sub { + my ($param) = @_; + + my $id = extract_param($param, 'zone'); + my $digest = extract_param($param, 'digest'); + + PVE::Network::SDN::Zones::lock_sdn_zones_config( + sub { + + my $cfg = PVE::Network::SDN::Zones::config(); + + PVE::SectionConfig::assert_if_modified($cfg, $digest); + + my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id); + + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($scfg->{type}); + my $opts = $plugin->check_config($id, $param, 0, 1); + + foreach my $k (%$opts) { + $scfg->{$k} = $opts->{$k}; + } + + $plugin->on_update_hook($id, $cfg); + + PVE::Network::SDN::Zones::write_config($cfg); + + }, "update sdn zone object failed"); + + return undef; + }}); + +__PACKAGE__->register_method ({ + name => 'delete', + protected => 1, + path => '{zone}', + method => 'DELETE', + description => "Delete sdn zone object configuration.", +# permissions => { +# check => ['perm', '/cluster/sdn/zones', ['SDN.Allocate']], +# }, + parameters => { + additionalProperties => 0, + properties => { + zone => get_standard_option('pve-sdn-zone-id', { + completion => \&PVE::Network::SDN::Zones::complete_sdn_zones, + }), + }, + }, + returns => { type => 'null' }, + code => sub { + my ($param) = @_; + + my $id = extract_param($param, 'zone'); + + PVE::Network::SDN::Zones::lock_sdn_zones_config( + sub { + + my $cfg = PVE::Network::SDN::Zones::config(); + + my $scfg = PVE::Network::SDN::Zones::sdn_zones_config($cfg, $id); + + my $plugin = PVE::Network::SDN::Zones::Plugin->lookup($scfg->{type}); + + my $vnet_cfg = PVE::Network::SDN::Vnets::config(); + + $plugin->on_delete_hook($id, $vnet_cfg); + + delete $cfg->{ids}->{$id}; + PVE::Network::SDN::Zones::write_config($cfg); + + }, "delete sdn zone object failed"); + + + return undef; + }}); + +1; |
