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);
9 use PVE
::Network
::SDN
::Zones
;
10 use PVE
::Network
::SDN
::Controllers
;
11 use PVE
::Network
::SDN
::Controllers
::Plugin
;
12 use PVE
::Network
::SDN
::Controllers
::EvpnPlugin
;
13 use PVE
::Network
::SDN
::Controllers
::FaucetPlugin
;
15 use Storable
qw(dclone);
16 use PVE
::JSONSchema
qw(get_standard_option);
17 use PVE
::RPCEnvironment
;
21 use base
qw(PVE::RESTHandler);
23 my $sdn_controllers_type_enum = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup_types();
25 my $api_sdn_controllers_config = sub {
28 my $scfg = dclone
(PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id));
29 $scfg->{controller
} = $id;
30 $scfg->{digest
} = $cfg->{digest
};
35 __PACKAGE__-
>register_method ({
39 description
=> "SDN controllers index.",
41 description
=> "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/controllers/<controller>'",
45 additionalProperties
=> 0,
48 description
=> "Only list sdn controllers of specific type",
50 enum
=> $sdn_controllers_type_enum,
59 properties
=> { controller
=> { type
=> 'string'},
60 type
=> { type
=> 'string'},
63 links
=> [ { rel
=> 'child', href
=> "{controller}" } ],
68 my $rpcenv = PVE
::RPCEnvironment
::get
();
69 my $authuser = $rpcenv->get_user();
72 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
74 my @sids = PVE
::Network
::SDN
::Controllers
::sdn_controllers_ids
($cfg);
76 foreach my $id (@sids) {
77 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
78 next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1);
80 my $scfg = &$api_sdn_controllers_config($cfg, $id);
81 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
83 my $plugin_config = $cfg->{ids
}->{$id};
84 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
91 __PACKAGE__-
>register_method ({
93 path
=> '{controller}',
95 description
=> "Read sdn controller configuration.",
97 check
=> ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']],
101 additionalProperties
=> 0,
103 controller
=> get_standard_option
('pve-sdn-controller-id'),
106 returns
=> { type
=> 'object' },
110 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
112 return &$api_sdn_controllers_config($cfg, $param->{controller
});
115 __PACKAGE__-
>register_method ({
120 description
=> "Create a new sdn controller object.",
122 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
124 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>createSchema(),
125 returns
=> { type
=> 'null' },
129 my $type = extract_param
($param, 'type');
130 my $id = extract_param
($param, 'controller');
132 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($type);
133 my $opts = $plugin->check_config($id, $param, 1, 1);
135 PVE
::Network
::SDN
::Controllers
::lock_sdn_controllers_config
(
138 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
141 if ($scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id, 1)) {
142 die "sdn controller object ID '$id' already defined\n";
145 $cfg->{ids
}->{$id} = $opts;
146 $plugin->on_update_hook($id, $cfg);
148 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
150 }, "create sdn controller object failed");
155 __PACKAGE__-
>register_method ({
156 name
=> 'revert_configuration',
160 description
=> "Revert sdn controller changes.",
162 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
165 additionalProperties
=> 0,
167 returns
=> { type
=> 'null' },
171 die "no sdn controllers changes to revert" if !-e
"/etc/pve/sdn/controllers.cfg.new";
172 unlink "/etc/pve/sdn/controllers.cfg.new";
177 __PACKAGE__-
>register_method ({
180 path
=> '{controller}',
182 description
=> "Update sdn controller object configuration.",
184 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
186 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>updateSchema(),
187 returns
=> { type
=> 'null' },
191 my $id = extract_param
($param, 'controller');
192 my $digest = extract_param
($param, 'digest');
194 PVE
::Network
::SDN
::Controllers
::lock_sdn_controllers_config
(
197 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
199 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
201 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
203 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
204 my $opts = $plugin->check_config($id, $param, 0, 1);
206 foreach my $k (%$opts) {
207 $scfg->{$k} = $opts->{$k};
210 $plugin->on_update_hook($id, $cfg);
212 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
214 }, "update sdn controller object failed");
219 __PACKAGE__-
>register_method ({
222 path
=> '{controller}',
224 description
=> "Delete sdn controller object configuration.",
226 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
229 additionalProperties
=> 0,
231 controller
=> get_standard_option
('pve-sdn-controller-id', {
232 completion
=> \
&PVE
::Network
::SDN
::Controllers
::complete_sdn_controllers
,
236 returns
=> { type
=> 'null' },
240 my $id = extract_param
($param, 'controller');
242 PVE
::Network
::SDN
::Controllers
::lock_sdn_controllers_config
(
245 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
247 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
249 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
251 my $transport_cfg = PVE
::Network
::SDN
::Zones
::config
();
253 $plugin->on_delete_hook($id, $transport_cfg);
255 delete $cfg->{ids
}->{$id};
256 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
258 }, "delete sdn controller object failed");