]>
git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Controllers.pm
1 package PVE
::API2
::Network
::SDN
::Controllers
;
7 use PVE
::Tools
qw(extract_param);
8 use PVE
::Cluster
qw(cfs_read_file cfs_write_file);
10 use PVE
::Network
::SDN
::Zones
;
11 use PVE
::Network
::SDN
::Controllers
;
12 use PVE
::Network
::SDN
::Controllers
::Plugin
;
13 use PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
14 use PVE
::Network
::SDN
::Controllers
::FaucetPlugin
;
16 use Storable
qw(dclone);
17 use PVE
::JSONSchema
qw(get_standard_option);
18 use PVE
::RPCEnvironment
;
22 use base
qw(PVE::RESTHandler);
24 my $sdn_controllers_type_enum = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup_types();
26 my $api_sdn_controllers_config = sub {
29 my $scfg = dclone
(PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id));
30 $scfg->{controller
} = $id;
31 $scfg->{digest
} = $cfg->{digest
};
36 __PACKAGE__-
>register_method ({
40 description
=> "SDN controllers index.",
42 description
=> "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/controllers/<controller>'",
46 additionalProperties
=> 0,
49 description
=> "Only list sdn controllers of specific type",
51 enum
=> $sdn_controllers_type_enum,
60 properties
=> { controller
=> { type
=> 'string'},
61 type
=> { type
=> 'string'},
64 links
=> [ { rel
=> 'child', href
=> "{controller}" } ],
69 my $rpcenv = PVE
::RPCEnvironment
::get
();
70 my $authuser = $rpcenv->get_user();
73 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
75 my @sids = PVE
::Network
::SDN
::Controllers
::sdn_controllers_ids
($cfg);
77 foreach my $id (@sids) {
78 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
79 next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1);
81 my $scfg = &$api_sdn_controllers_config($cfg, $id);
82 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
84 my $plugin_config = $cfg->{ids
}->{$id};
85 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
92 __PACKAGE__-
>register_method ({
94 path
=> '{controller}',
96 description
=> "Read sdn controller configuration.",
98 check
=> ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']],
102 additionalProperties
=> 0,
104 controller
=> get_standard_option
('pve-sdn-controller-id'),
107 returns
=> { type
=> 'object' },
111 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
113 return &$api_sdn_controllers_config($cfg, $param->{controller
});
116 __PACKAGE__-
>register_method ({
121 description
=> "Create a new sdn controller object.",
123 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
125 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>createSchema(),
126 returns
=> { type
=> 'null' },
130 my $type = extract_param
($param, 'type');
131 my $id = extract_param
($param, 'controller');
133 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($type);
134 my $opts = $plugin->check_config($id, $param, 1, 1);
136 # create /etc/pve/sdn directory
137 PVE
::Cluster
::check_cfs_quorum
();
138 mkdir("/etc/pve/sdn");
140 PVE
::Network
::SDN
::lock_sdn_config
(
143 my $controller_cfg = PVE
::Network
::SDN
::Controllers
::config
();
146 if ($scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($controller_cfg, $id, 1)) {
147 die "sdn controller object ID '$id' already defined\n";
150 $controller_cfg->{ids
}->{$id} = $opts;
151 $plugin->on_update_hook($id, $controller_cfg);
153 PVE
::Network
::SDN
::Controllers
::write_config
($controller_cfg);
155 }, "create sdn controller object failed");
160 __PACKAGE__-
>register_method ({
163 path
=> '{controller}',
165 description
=> "Update sdn controller object configuration.",
167 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
169 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>updateSchema(),
170 returns
=> { type
=> 'null' },
174 my $id = extract_param
($param, 'controller');
175 my $digest = extract_param
($param, 'digest');
177 PVE
::Network
::SDN
::lock_sdn_config
(
180 my $controller_cfg = PVE
::Network
::SDN
::Controllers
::config
();
182 PVE
::SectionConfig
::assert_if_modified
($controller_cfg, $digest);
184 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($controller_cfg, $id);
186 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
187 my $opts = $plugin->check_config($id, $param, 0, 1);
189 foreach my $k (%$opts) {
190 $scfg->{$k} = $opts->{$k};
193 $plugin->on_update_hook($id, $controller_cfg);
195 PVE
::Network
::SDN
::Controllers
::write_config
($controller_cfg);
198 }, "update sdn controller object failed");
203 __PACKAGE__-
>register_method ({
206 path
=> '{controller}',
208 description
=> "Delete sdn controller object configuration.",
210 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
213 additionalProperties
=> 0,
215 controller
=> get_standard_option
('pve-sdn-controller-id', {
216 completion
=> \
&PVE
::Network
::SDN
::Controllers
::complete_sdn_controllers
,
220 returns
=> { type
=> 'null' },
224 my $id = extract_param
($param, 'controller');
226 PVE
::Network
::SDN
::lock_sdn_config
(
229 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
231 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
233 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
235 my $zone_cfg = PVE
::Network
::SDN
::Zones
::config
();
237 $plugin->on_delete_hook($id, $zone_cfg);
239 delete $cfg->{ids
}->{$id};
240 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
242 }, "delete sdn controller object failed");