]>
git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Controllers.pm
75beb6bef924e019c1821cfab0668f27ed9295cd
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,
57 description
=> "Display running config.",
62 description
=> "Display pending config.",
70 properties
=> { controller
=> { type
=> 'string' },
71 type
=> { type
=> 'string' },
72 state => { type
=> 'string', optional
=> 1 },
73 pending
=> { optional
=> 1},
76 links
=> [ { rel
=> 'child', href
=> "{controller}" } ],
81 my $rpcenv = PVE
::RPCEnvironment
::get
();
82 my $authuser = $rpcenv->get_user();
85 if($param->{pending
}) {
86 my $running_cfg = PVE
::Network
::SDN
::config
();
87 my $config = PVE
::Network
::SDN
::Controllers
::config
();
88 $cfg = PVE
::Network
::SDN
::pending_config
($running_cfg, $config, 'controllers');
89 } elsif ($param->{running
}) {
90 my $running_cfg = PVE
::Network
::SDN
::config
();
91 $cfg = $running_cfg->{controllers
};
93 $cfg = PVE
::Network
::SDN
::Controllers
::config
();
96 my @sids = PVE
::Network
::SDN
::Controllers
::sdn_controllers_ids
($cfg);
98 foreach my $id (@sids) {
99 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
100 next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1);
102 my $scfg = &$api_sdn_controllers_config($cfg, $id);
103 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
105 my $plugin_config = $cfg->{ids
}->{$id};
106 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($plugin_config->{type
});
113 __PACKAGE__-
>register_method ({
115 path
=> '{controller}',
117 description
=> "Read sdn controller configuration.",
119 check
=> ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']],
123 additionalProperties
=> 0,
125 controller
=> get_standard_option
('pve-sdn-controller-id'),
129 description
=> "Display running config.",
134 description
=> "Display pending config.",
138 returns
=> { type
=> 'object' },
143 if($param->{pending
}) {
144 my $running_cfg = PVE
::Network
::SDN
::config
();
145 my $config = PVE
::Network
::SDN
::Controllers
::config
();
146 $cfg = PVE
::Network
::SDN
::pending_config
($running_cfg, $config, 'controllers');
147 } elsif ($param->{running
}) {
148 my $running_cfg = PVE
::Network
::SDN
::config
();
149 $cfg = $running_cfg->{controllers
};
151 $cfg = PVE
::Network
::SDN
::Controllers
::config
();
154 return &$api_sdn_controllers_config($cfg, $param->{controller
});
157 __PACKAGE__-
>register_method ({
162 description
=> "Create a new sdn controller object.",
164 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
166 parameters
=> PVE
::Network
::SDN
::Controllers
::Plugin-
>createSchema(),
167 returns
=> { type
=> 'null' },
171 my $type = extract_param
($param, 'type');
172 my $id = extract_param
($param, 'controller');
174 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($type);
175 my $opts = $plugin->check_config($id, $param, 1, 1);
177 # create /etc/pve/sdn directory
178 PVE
::Cluster
::check_cfs_quorum
();
179 mkdir("/etc/pve/sdn");
181 PVE
::Network
::SDN
::lock_sdn_config
(
184 my $controller_cfg = PVE
::Network
::SDN
::Controllers
::config
();
187 if ($scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($controller_cfg, $id, 1)) {
188 die "sdn controller object ID '$id' already defined\n";
191 $controller_cfg->{ids
}->{$id} = $opts;
192 $plugin->on_update_hook($id, $controller_cfg);
194 PVE
::Network
::SDN
::Controllers
::write_config
($controller_cfg);
196 }, "create sdn controller object failed");
201 __PACKAGE__-
>register_method ({
204 path
=> '{controller}',
206 description
=> "Update sdn controller object configuration.",
208 check
=> ['perm', '/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
::lock_sdn_config
(
221 my $controller_cfg = PVE
::Network
::SDN
::Controllers
::config
();
223 PVE
::SectionConfig
::assert_if_modified
($controller_cfg, $digest);
225 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($controller_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, $controller_cfg);
236 PVE
::Network
::SDN
::Controllers
::write_config
($controller_cfg);
239 }, "update sdn controller object failed");
244 __PACKAGE__-
>register_method ({
247 path
=> '{controller}',
249 description
=> "Delete sdn controller object configuration.",
251 check
=> ['perm', '/sdn/controllers', ['SDN.Allocate']],
254 additionalProperties
=> 0,
256 controller
=> get_standard_option
('pve-sdn-controller-id', {
257 completion
=> \
&PVE
::Network
::SDN
::Controllers
::complete_sdn_controllers
,
261 returns
=> { type
=> 'null' },
265 my $id = extract_param
($param, 'controller');
267 PVE
::Network
::SDN
::lock_sdn_config
(
270 my $cfg = PVE
::Network
::SDN
::Controllers
::config
();
272 my $scfg = PVE
::Network
::SDN
::Controllers
::sdn_controllers_config
($cfg, $id);
274 my $plugin = PVE
::Network
::SDN
::Controllers
::Plugin-
>lookup($scfg->{type
});
276 my $zone_cfg = PVE
::Network
::SDN
::Zones
::config
();
278 $plugin->on_delete_hook($id, $zone_cfg);
280 delete $cfg->{ids
}->{$id};
281 PVE
::Network
::SDN
::Controllers
::write_config
($cfg);
283 }, "delete sdn controller object failed");