]>
Commit | Line | Data |
---|---|---|
4140be9e AD |
1 | package PVE::API2::Network::SDN::Controllers; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Tools qw(extract_param); | |
8 | use PVE::Cluster qw(cfs_read_file cfs_write_file); | |
f9bc9640 | 9 | use PVE::Network::SDN; |
4140be9e AD |
10 | use PVE::Network::SDN::Zones; |
11 | use PVE::Network::SDN::Controllers; | |
12 | use PVE::Network::SDN::Controllers::Plugin; | |
fa253735 | 13 | use PVE::Network::SDN::Controllers::EvpnPlugin; |
4140be9e AD |
14 | use PVE::Network::SDN::Controllers::FaucetPlugin; |
15 | ||
16 | use Storable qw(dclone); | |
17 | use PVE::JSONSchema qw(get_standard_option); | |
18 | use PVE::RPCEnvironment; | |
19 | ||
20 | use PVE::RESTHandler; | |
21 | ||
22 | use base qw(PVE::RESTHandler); | |
23 | ||
24 | my $sdn_controllers_type_enum = PVE::Network::SDN::Controllers::Plugin->lookup_types(); | |
25 | ||
26 | my $api_sdn_controllers_config = sub { | |
27 | my ($cfg, $id) = @_; | |
28 | ||
29 | my $scfg = dclone(PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id)); | |
30 | $scfg->{controller} = $id; | |
31 | $scfg->{digest} = $cfg->{digest}; | |
32 | ||
33 | return $scfg; | |
34 | }; | |
35 | ||
36 | __PACKAGE__->register_method ({ | |
37 | name => 'index', | |
38 | path => '', | |
39 | method => 'GET', | |
40 | description => "SDN controllers index.", | |
41 | permissions => { | |
3551b612 | 42 | description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/controllers/<controller>'", |
4140be9e AD |
43 | user => 'all', |
44 | }, | |
45 | parameters => { | |
46 | additionalProperties => 0, | |
47 | properties => { | |
48 | type => { | |
49 | description => "Only list sdn controllers of specific type", | |
50 | type => 'string', | |
51 | enum => $sdn_controllers_type_enum, | |
52 | optional => 1, | |
53 | }, | |
54 | }, | |
55 | }, | |
56 | returns => { | |
57 | type => 'array', | |
58 | items => { | |
59 | type => "object", | |
0f700635 | 60 | properties => { controller => { type => 'string'}, |
4140be9e AD |
61 | type => { type => 'string'}, |
62 | }, | |
63 | }, | |
64 | links => [ { rel => 'child', href => "{controller}" } ], | |
65 | }, | |
66 | code => sub { | |
67 | my ($param) = @_; | |
68 | ||
69 | my $rpcenv = PVE::RPCEnvironment::get(); | |
70 | my $authuser = $rpcenv->get_user(); | |
71 | ||
72 | ||
73 | my $cfg = PVE::Network::SDN::Controllers::config(); | |
74 | ||
75 | my @sids = PVE::Network::SDN::Controllers::sdn_controllers_ids($cfg); | |
76 | my $res = []; | |
77 | foreach my $id (@sids) { | |
3551b612 AD |
78 | my $privs = [ 'SDN.Audit', 'SDN.Allocate' ]; |
79 | next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1); | |
4140be9e AD |
80 | |
81 | my $scfg = &$api_sdn_controllers_config($cfg, $id); | |
82 | next if $param->{type} && $param->{type} ne $scfg->{type}; | |
83 | ||
84 | my $plugin_config = $cfg->{ids}->{$id}; | |
85 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type}); | |
86 | push @$res, $scfg; | |
87 | } | |
88 | ||
89 | return $res; | |
90 | }}); | |
91 | ||
92 | __PACKAGE__->register_method ({ | |
93 | name => 'read', | |
94 | path => '{controller}', | |
95 | method => 'GET', | |
96 | description => "Read sdn controller configuration.", | |
3551b612 AD |
97 | permissions => { |
98 | check => ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']], | |
99 | }, | |
4140be9e AD |
100 | |
101 | parameters => { | |
102 | additionalProperties => 0, | |
103 | properties => { | |
104 | controller => get_standard_option('pve-sdn-controller-id'), | |
105 | }, | |
106 | }, | |
107 | returns => { type => 'object' }, | |
108 | code => sub { | |
109 | my ($param) = @_; | |
110 | ||
111 | my $cfg = PVE::Network::SDN::Controllers::config(); | |
112 | ||
113 | return &$api_sdn_controllers_config($cfg, $param->{controller}); | |
114 | }}); | |
115 | ||
116 | __PACKAGE__->register_method ({ | |
117 | name => 'create', | |
118 | protected => 1, | |
119 | path => '', | |
120 | method => 'POST', | |
121 | description => "Create a new sdn controller object.", | |
3551b612 AD |
122 | permissions => { |
123 | check => ['perm', '/sdn/controllers', ['SDN.Allocate']], | |
124 | }, | |
4140be9e AD |
125 | parameters => PVE::Network::SDN::Controllers::Plugin->createSchema(), |
126 | returns => { type => 'null' }, | |
127 | code => sub { | |
128 | my ($param) = @_; | |
129 | ||
130 | my $type = extract_param($param, 'type'); | |
131 | my $id = extract_param($param, 'controller'); | |
132 | ||
133 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($type); | |
134 | my $opts = $plugin->check_config($id, $param, 1, 1); | |
135 | ||
45c3f15c AD |
136 | # create /etc/pve/sdn directory |
137 | PVE::Cluster::check_cfs_quorum(); | |
138 | mkdir("/etc/pve/sdn"); | |
139 | ||
f9bc9640 | 140 | PVE::Network::SDN::lock_sdn_config( |
4140be9e AD |
141 | sub { |
142 | ||
56cdcac9 | 143 | my $controller_cfg = PVE::Network::SDN::Controllers::config(); |
4140be9e AD |
144 | |
145 | my $scfg = undef; | |
56cdcac9 | 146 | if ($scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id, 1)) { |
4140be9e AD |
147 | die "sdn controller object ID '$id' already defined\n"; |
148 | } | |
149 | ||
56cdcac9 AD |
150 | $controller_cfg->{ids}->{$id} = $opts; |
151 | $plugin->on_update_hook($id, $controller_cfg); | |
4140be9e | 152 | |
56cdcac9 | 153 | PVE::Network::SDN::Controllers::write_config($controller_cfg); |
4140be9e AD |
154 | |
155 | }, "create sdn controller object failed"); | |
156 | ||
157 | return undef; | |
158 | }}); | |
159 | ||
4140be9e AD |
160 | __PACKAGE__->register_method ({ |
161 | name => 'update', | |
162 | protected => 1, | |
163 | path => '{controller}', | |
164 | method => 'PUT', | |
165 | description => "Update sdn controller object configuration.", | |
3551b612 AD |
166 | permissions => { |
167 | check => ['perm', '/sdn/controllers', ['SDN.Allocate']], | |
168 | }, | |
4140be9e AD |
169 | parameters => PVE::Network::SDN::Controllers::Plugin->updateSchema(), |
170 | returns => { type => 'null' }, | |
171 | code => sub { | |
172 | my ($param) = @_; | |
173 | ||
174 | my $id = extract_param($param, 'controller'); | |
175 | my $digest = extract_param($param, 'digest'); | |
176 | ||
f9bc9640 | 177 | PVE::Network::SDN::lock_sdn_config( |
4140be9e AD |
178 | sub { |
179 | ||
56cdcac9 | 180 | my $controller_cfg = PVE::Network::SDN::Controllers::config(); |
4140be9e | 181 | |
56cdcac9 | 182 | PVE::SectionConfig::assert_if_modified($controller_cfg, $digest); |
4140be9e | 183 | |
56cdcac9 | 184 | my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id); |
4140be9e AD |
185 | |
186 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type}); | |
187 | my $opts = $plugin->check_config($id, $param, 0, 1); | |
188 | ||
189 | foreach my $k (%$opts) { | |
190 | $scfg->{$k} = $opts->{$k}; | |
191 | } | |
192 | ||
56cdcac9 | 193 | $plugin->on_update_hook($id, $controller_cfg); |
4140be9e | 194 | |
56cdcac9 | 195 | PVE::Network::SDN::Controllers::write_config($controller_cfg); |
4140be9e | 196 | |
f9bc9640 | 197 | |
4140be9e AD |
198 | }, "update sdn controller object failed"); |
199 | ||
200 | return undef; | |
201 | }}); | |
202 | ||
203 | __PACKAGE__->register_method ({ | |
204 | name => 'delete', | |
205 | protected => 1, | |
206 | path => '{controller}', | |
207 | method => 'DELETE', | |
208 | description => "Delete sdn controller object configuration.", | |
3551b612 AD |
209 | permissions => { |
210 | check => ['perm', '/sdn/controllers', ['SDN.Allocate']], | |
211 | }, | |
4140be9e AD |
212 | parameters => { |
213 | additionalProperties => 0, | |
214 | properties => { | |
215 | controller => get_standard_option('pve-sdn-controller-id', { | |
216 | completion => \&PVE::Network::SDN::Controllers::complete_sdn_controllers, | |
217 | }), | |
218 | }, | |
219 | }, | |
220 | returns => { type => 'null' }, | |
221 | code => sub { | |
222 | my ($param) = @_; | |
223 | ||
224 | my $id = extract_param($param, 'controller'); | |
225 | ||
f9bc9640 | 226 | PVE::Network::SDN::lock_sdn_config( |
4140be9e AD |
227 | sub { |
228 | ||
229 | my $cfg = PVE::Network::SDN::Controllers::config(); | |
230 | ||
231 | my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id); | |
232 | ||
233 | my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type}); | |
234 | ||
56cdcac9 | 235 | my $zone_cfg = PVE::Network::SDN::Zones::config(); |
4140be9e | 236 | |
56cdcac9 | 237 | $plugin->on_delete_hook($id, $zone_cfg); |
4140be9e AD |
238 | |
239 | delete $cfg->{ids}->{$id}; | |
240 | PVE::Network::SDN::Controllers::write_config($cfg); | |
241 | ||
242 | }, "delete sdn controller object failed"); | |
243 | ||
244 | ||
245 | return undef; | |
246 | }}); | |
247 | ||
248 | 1; |