]> git.proxmox.com Git - pve-network.git/blame - PVE/API2/Network/SDN/Ipams.pm
add IPAMs plugins
[pve-network.git] / PVE / API2 / Network / SDN / Ipams.pm
CommitLineData
70b03506
AD
1package PVE::API2::Network::SDN::Ipams;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Tools qw(extract_param);
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
9use PVE::Network::SDN;
10use PVE::Network::SDN::Ipams;
11use PVE::Network::SDN::Ipams::Plugin;
12use PVE::Network::SDN::Ipams::PVEPlugin;
13use PVE::Network::SDN::Ipams::PhpIpamPlugin;
14use PVE::Network::SDN::Ipams::NetboxPlugin;
15
16use Storable qw(dclone);
17use PVE::JSONSchema qw(get_standard_option);
18use PVE::RPCEnvironment;
19
20use PVE::RESTHandler;
21
22use base qw(PVE::RESTHandler);
23
24my $sdn_ipams_type_enum = PVE::Network::SDN::Ipams::Plugin->lookup_types();
25
26my $api_sdn_ipams_config = sub {
27 my ($cfg, $id) = @_;
28
29 my $scfg = dclone(PVE::Network::SDN::Ipams::sdn_ipams_config($cfg, $id));
30 $scfg->{ipam} = $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 ipams index.",
41 permissions => {
42 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/ipams/<ipam>'",
43 user => 'all',
44 },
45 parameters => {
46 additionalProperties => 0,
47 properties => {
48 type => {
49 description => "Only list sdn ipams of specific type",
50 type => 'string',
51 enum => $sdn_ipams_type_enum,
52 optional => 1,
53 },
54 },
55 },
56 returns => {
57 type => 'array',
58 items => {
59 type => "object",
60 properties => { ipam => { type => 'string'},
61 type => { type => 'string'},
62 },
63 },
64 links => [ { rel => 'child', href => "{ipam}" } ],
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::Ipams::config();
74
75 my @sids = PVE::Network::SDN::Ipams::sdn_ipams_ids($cfg);
76 my $res = [];
77 foreach my $id (@sids) {
78 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
79 next if !$rpcenv->check_any($authuser, "/sdn/ipams/$id", $privs, 1);
80
81 my $scfg = &$api_sdn_ipams_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::Ipams::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 => '{ipam}',
95 method => 'GET',
96 description => "Read sdn ipam configuration.",
97 permissions => {
98 check => ['perm', '/sdn/ipams/{ipam}', ['SDN.Allocate']],
99 },
100
101 parameters => {
102 additionalProperties => 0,
103 properties => {
104 ipam => get_standard_option('pve-sdn-ipam-id'),
105 },
106 },
107 returns => { type => 'object' },
108 code => sub {
109 my ($param) = @_;
110
111 my $cfg = PVE::Network::SDN::Ipams::config();
112
113 return &$api_sdn_ipams_config($cfg, $param->{ipam});
114 }});
115
116__PACKAGE__->register_method ({
117 name => 'create',
118 protected => 1,
119 path => '',
120 method => 'POST',
121 description => "Create a new sdn ipam object.",
122 permissions => {
123 check => ['perm', '/sdn/ipams', ['SDN.Allocate']],
124 },
125 parameters => PVE::Network::SDN::Ipams::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, 'ipam');
132
133 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($type);
134 my $opts = $plugin->check_config($id, $param, 1, 1);
135
136 # create /etc/pve/sdn directory
137 PVE::Cluster::check_cfs_quorum();
138 mkdir("/etc/pve/sdn");
139
140 PVE::Network::SDN::lock_sdn_config(
141 sub {
142
143 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
144 my $controller_cfg = PVE::Network::SDN::Controllers::config();
145
146 my $scfg = undef;
147 if ($scfg = PVE::Network::SDN::Ipams::sdn_ipams_config($ipam_cfg, $id, 1)) {
148 die "sdn ipam object ID '$id' already defined\n";
149 }
150
151 $ipam_cfg->{ids}->{$id} = $opts;
152
153 my $plugin_config = $opts;
154 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($plugin_config->{type});
155 $plugin->on_update_hook($plugin_config);
156
157 PVE::Network::SDN::Ipams::write_config($ipam_cfg);
158
159 }, "create sdn ipam object failed");
160
161 return undef;
162 }});
163
164__PACKAGE__->register_method ({
165 name => 'update',
166 protected => 1,
167 path => '{ipam}',
168 method => 'PUT',
169 description => "Update sdn ipam object configuration.",
170 permissions => {
171 check => ['perm', '/sdn/ipams', ['SDN.Allocate']],
172 },
173 parameters => PVE::Network::SDN::Ipams::Plugin->updateSchema(),
174 returns => { type => 'null' },
175 code => sub {
176 my ($param) = @_;
177
178 my $id = extract_param($param, 'ipam');
179 my $digest = extract_param($param, 'digest');
180
181 PVE::Network::SDN::lock_sdn_config(
182 sub {
183
184 my $ipam_cfg = PVE::Network::SDN::Ipams::config();
185
186 PVE::SectionConfig::assert_if_modified($ipam_cfg, $digest);
187
188 my $scfg = PVE::Network::SDN::Ipams::sdn_ipams_config($ipam_cfg, $id);
189
190 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($scfg->{type});
191 my $opts = $plugin->check_config($id, $param, 0, 1);
192
193 foreach my $k (%$opts) {
194 $scfg->{$k} = $opts->{$k};
195 }
196
197 $plugin->on_update_hook($scfg);
198
199 PVE::Network::SDN::Ipams::write_config($ipam_cfg);
200
201 }, "update sdn ipam object failed");
202
203 return undef;
204 }});
205
206__PACKAGE__->register_method ({
207 name => 'delete',
208 protected => 1,
209 path => '{ipam}',
210 method => 'DELETE',
211 description => "Delete sdn ipam object configuration.",
212 permissions => {
213 check => ['perm', '/sdn/ipams', ['SDN.Allocate']],
214 },
215 parameters => {
216 additionalProperties => 0,
217 properties => {
218 ipam => get_standard_option('pve-sdn-ipam-id', {
219 completion => \&PVE::Network::SDN::Ipams::complete_sdn_ipams,
220 }),
221 },
222 },
223 returns => { type => 'null' },
224 code => sub {
225 my ($param) = @_;
226
227 my $id = extract_param($param, 'ipam');
228
229 PVE::Network::SDN::lock_sdn_config(
230 sub {
231
232 my $cfg = PVE::Network::SDN::Ipams::config();
233
234 my $scfg = PVE::Network::SDN::Ipams::sdn_ipams_config($cfg, $id);
235
236 my $plugin = PVE::Network::SDN::Ipams::Plugin->lookup($scfg->{type});
237
238 my $vnet_cfg = PVE::Network::SDN::Vnets::config();
239
240 delete $cfg->{ids}->{$id};
241 PVE::Network::SDN::Ipams::write_config($cfg);
242
243 }, "delete sdn zone object failed");
244
245 return undef;
246 }});
247
2481;