]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Subnets.pm
add subnet plugin
[pve-network.git] / PVE / API2 / Network / SDN / Subnets.pm
1 package PVE::API2::Network::SDN::Subnets;
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);
9 use PVE::Network::SDN;
10 use PVE::Network::SDN::Subnets;
11 use PVE::Network::SDN::SubnetPlugin;
12
13 use Storable qw(dclone);
14 use PVE::JSONSchema qw(get_standard_option);
15 use PVE::RPCEnvironment;
16
17 use PVE::RESTHandler;
18
19 use base qw(PVE::RESTHandler);
20
21 my $api_sdn_subnets_config = sub {
22 my ($cfg, $id) = @_;
23
24 my $scfg = dclone(PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id));
25 $scfg->{subnet} = $id;
26 $scfg->{cidr} = $id =~ s/-/\//r;
27 $scfg->{digest} = $cfg->{digest};
28
29 return $scfg;
30 };
31
32 __PACKAGE__->register_method ({
33 name => 'index',
34 path => '',
35 method => 'GET',
36 description => "SDN subnets index.",
37 permissions => {
38 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/subnets/<subnet>'",
39 user => 'all',
40 },
41 parameters => {
42 additionalProperties => 0,
43 },
44 returns => {
45 type => 'array',
46 items => {
47 type => "object",
48 properties => {},
49 },
50 links => [ { rel => 'child', href => "{subnet}" } ],
51 },
52 code => sub {
53 my ($param) = @_;
54
55 my $rpcenv = PVE::RPCEnvironment::get();
56 my $authuser = $rpcenv->get_user();
57
58
59 my $cfg = PVE::Network::SDN::Subnets::config();
60
61 my @sids = PVE::Network::SDN::Subnets::sdn_subnets_ids($cfg);
62 my $res = [];
63 foreach my $id (@sids) {
64 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
65 next if !$rpcenv->check_any($authuser, "/sdn/subnets/$id", $privs, 1);
66
67 my $scfg = &$api_sdn_subnets_config($cfg, $id);
68 push @$res, $scfg;
69 }
70
71 return $res;
72 }});
73
74 __PACKAGE__->register_method ({
75 name => 'read',
76 path => '{subnet}',
77 method => 'GET',
78 description => "Read sdn subnet configuration.",
79 permissions => {
80 check => ['perm', '/sdn/subnets/{subnet}', ['SDN.Allocate']],
81 },
82
83 parameters => {
84 additionalProperties => 0,
85 properties => {
86 subnet => get_standard_option('pve-sdn-subnet-id', {
87 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
88 }),
89 },
90 },
91 returns => { type => 'object' },
92 code => sub {
93 my ($param) = @_;
94
95 my $cfg = PVE::Network::SDN::Subnets::config();
96
97 return &$api_sdn_subnets_config($cfg, $param->{subnet});
98 }});
99
100 __PACKAGE__->register_method ({
101 name => 'create',
102 protected => 1,
103 path => '',
104 method => 'POST',
105 description => "Create a new sdn subnet object.",
106 permissions => {
107 check => ['perm', '/sdn/subnets', ['SDN.Allocate']],
108 },
109 parameters => PVE::Network::SDN::SubnetPlugin->createSchema(),
110 returns => { type => 'null' },
111 code => sub {
112 my ($param) = @_;
113
114 my $type = extract_param($param, 'type');
115 my $cidr = extract_param($param, 'subnet');
116 my $id = $cidr =~ s/\//-/r;
117
118 # create /etc/pve/sdn directory
119 PVE::Cluster::check_cfs_quorum();
120 mkdir("/etc/pve/sdn") if ! -d '/etc/pve/sdn';
121
122 PVE::Network::SDN::lock_sdn_config(
123 sub {
124
125 my $cfg = PVE::Network::SDN::Subnets::config();
126 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 1, 1);
127
128 my $scfg = undef;
129 if ($scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id, 1)) {
130 die "sdn subnet object ID '$id' already defined\n";
131 }
132
133 $cfg->{ids}->{$id} = $opts;
134 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $cfg);
135 PVE::Network::SDN::Subnets::write_config($cfg);
136 PVE::Network::SDN::increase_version();
137
138 }, "create sdn subnet object failed");
139
140 return undef;
141 }});
142
143 __PACKAGE__->register_method ({
144 name => 'update',
145 protected => 1,
146 path => '{subnet}',
147 method => 'PUT',
148 description => "Update sdn subnet object configuration.",
149 permissions => {
150 check => ['perm', '/sdn/subnets', ['SDN.Allocate']],
151 },
152 parameters => PVE::Network::SDN::SubnetPlugin->updateSchema(),
153 returns => { type => 'null' },
154 code => sub {
155 my ($param) = @_;
156
157 my $id = extract_param($param, 'subnet');
158 my $digest = extract_param($param, 'digest');
159
160 PVE::Network::SDN::lock_sdn_config(
161 sub {
162
163 my $cfg = PVE::Network::SDN::Subnets::config();
164
165 PVE::SectionConfig::assert_if_modified($cfg, $digest);
166
167 my $opts = PVE::Network::SDN::SubnetPlugin->check_config($id, $param, 0, 1);
168 $cfg->{ids}->{$id} = $opts;
169
170 PVE::Network::SDN::SubnetPlugin->on_update_hook($id, $cfg);
171 PVE::Network::SDN::Subnets::write_config($cfg);
172 PVE::Network::SDN::increase_version();
173
174 }, "update sdn subnet object failed");
175
176 return undef;
177 }});
178
179 __PACKAGE__->register_method ({
180 name => 'delete',
181 protected => 1,
182 path => '{subnet}',
183 method => 'DELETE',
184 description => "Delete sdn subnet object configuration.",
185 permissions => {
186 check => ['perm', '/sdn/subnets', ['SDN.Allocate']],
187 },
188 parameters => {
189 additionalProperties => 0,
190 properties => {
191 subnet => get_standard_option('pve-sdn-subnet-id', {
192 completion => \&PVE::Network::SDN::Subnets::complete_sdn_subnets,
193 }),
194 },
195 },
196 returns => { type => 'null' },
197 code => sub {
198 my ($param) = @_;
199
200 my $id = extract_param($param, 'subnet');
201
202 PVE::Network::SDN::lock_sdn_config(
203 sub {
204
205 my $cfg = PVE::Network::SDN::Subnets::config();
206
207 my $scfg = PVE::Network::SDN::Subnets::sdn_subnets_config($cfg, $id);
208
209 my $subnet_cfg = PVE::Network::SDN::Subnets::config();
210
211 delete $cfg->{ids}->{$id};
212 PVE::Network::SDN::Subnets::write_config($cfg);
213 PVE::Network::SDN::increase_version();
214
215 }, "delete sdn subnet object failed");
216
217
218 return undef;
219 }});
220
221 1;