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
::FrrEvpnPlugin
;
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 '/cluster/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, "/cluster/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', '/cluster/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', '/cluster/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
=> 'apply_configuration',
160 description
=> "Apply sdn controller changes.",
162 # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']],
165 additionalProperties
=> 0,
167 returns
=> { type
=> 'null' },
171 die "no sdn controller changes to apply" if !-e
"/etc/pve/sdn/controllers.cfg.new";
172 rename("/etc/pve/sdn/controllers.cfg.new", "/etc/pve/sdn/controllers.cfg")
173 || die "applying sdn/controllers.cfg changes failed - $!\n";
179 __PACKAGE__-
>register_method ({
180 name
=> 'revert_configuration',
184 description
=> "Revert sdn controller changes.",
186 # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']],
189 additionalProperties
=> 0,
191 returns
=> { type
=> 'null' },
195 die "no sdn controllers changes to revert" if !-e
"/etc/pve/sdn/controllers.cfg.new";
196 unlink "/etc/pve/sdn/controllers.cfg.new";
201 __PACKAGE__-
>register_method ({
204 path
=> '{controller}',
206 description
=> "Update sdn controller object configuration.",
208 # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']],
210 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>updateSchema(),
211 returns
=> { type
=> 'null' },
215 my $id = extract_param
($param, 'controller');
216 my $digest = extract_param
($param, 'digest');
218 PVE
::Network
::SDN
::Controllers
::lock_sdn_controllers_config
(
221 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
223 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
225 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
227 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
228 my $opts = $plugin->check_config($id, $param, 0, 1);
230 foreach my $k (%$opts) {
231 $scfg->{$k} = $opts->{$k};
234 $plugin->on_update_hook($id, $cfg);
236 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
238 }, "update sdn controller object failed");
243 __PACKAGE__-
>register_method ({
246 path
=> '{controller}',
248 description
=> "Delete sdn controller object configuration.",
250 # check => ['perm', '/cluster/sdn/controllers', ['SDN.Allocate']],
253 additionalProperties
=> 0,
255 controller
=> get_standard_option
('pve-sdn-controller-id', {
256 completion
=> \
&PVE
::Network
::SDN
::Controllers
::complete_sdn_controllers
,
260 returns
=> { type
=> 'null' },
264 my $id = extract_param
($param, 'controller');
266 PVE
::Network
::SDN
::Controllers
::lock_sdn_controllers_config
(
269 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
271 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
273 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
275 my $transport_cfg = PVE
::Network
::SDN
::Zones
::config
();
277 $plugin->on_delete_hook($id, $transport_cfg);
279 delete $cfg->{ids
}->{$id};
280 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
282 }, "delete sdn controller object failed");