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