]> git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN/Vnets.pm
remove .new files
[pve-network.git] / PVE / API2 / Network / SDN / Vnets.pm
1 package PVE::API2::Network::SDN::Vnets;
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::Vnets;
10 use PVE::Network::SDN::VnetPlugin;
11
12 use Storable qw(dclone);
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::RPCEnvironment;
15
16 use PVE::RESTHandler;
17
18 use base qw(PVE::RESTHandler);
19
20 my $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 => {
36 description => "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/vnets/<vnet>'",
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) {
62 my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
63 next if !$rpcenv->check_any($authuser, "/sdn/vnets/$id", $privs, 1);
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.",
77 permissions => {
78 check => ['perm', '/sdn/vnets/{vnet}', ['SDN.Allocate']],
79 },
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.",
104 permissions => {
105 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
106 },
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
115 # create /etc/pve/sdn directory
116 PVE::Cluster::check_cfs_quorum();
117 mkdir("/etc/pve/sdn");
118
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
140 __PACKAGE__->register_method ({
141 name => 'update',
142 protected => 1,
143 path => '{vnet}',
144 method => 'PUT',
145 description => "Update sdn vnet object configuration.",
146 permissions => {
147 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
148 },
149 parameters => PVE::Network::SDN::VnetPlugin->updateSchema(),
150 returns => { type => 'null' },
151 code => sub {
152 my ($param) = @_;
153
154 my $id = extract_param($param, 'vnet');
155 my $digest = extract_param($param, 'digest');
156
157 PVE::Network::SDN::Vnets::lock_sdn_vnets_config(
158 sub {
159
160 my $cfg = PVE::Network::SDN::Vnets::config();
161
162 PVE::SectionConfig::assert_if_modified($cfg, $digest);
163
164 my $opts = PVE::Network::SDN::VnetPlugin->check_config($id, $param, 0, 1);
165 $cfg->{ids}->{$id} = $opts;
166
167 PVE::Network::SDN::VnetPlugin->on_update_hook($id, $cfg);
168
169 PVE::Network::SDN::Vnets::write_config($cfg);
170
171 }, "update sdn vnet object failed");
172
173 return undef;
174 }});
175
176 __PACKAGE__->register_method ({
177 name => 'delete',
178 protected => 1,
179 path => '{vnet}',
180 method => 'DELETE',
181 description => "Delete sdn vnet object configuration.",
182 permissions => {
183 check => ['perm', '/sdn/vnets', ['SDN.Allocate']],
184 },
185 parameters => {
186 additionalProperties => 0,
187 properties => {
188 vnet => get_standard_option('pve-sdn-vnet-id', {
189 completion => \&PVE::Network::SDN::Vnets::complete_sdn_vnets,
190 }),
191 },
192 },
193 returns => { type => 'null' },
194 code => sub {
195 my ($param) = @_;
196
197 my $id = extract_param($param, 'vnet');
198
199 PVE::Network::SDN::Vnets::lock_sdn_vnets_config(
200 sub {
201
202 my $cfg = PVE::Network::SDN::Vnets::config();
203
204 my $scfg = PVE::Network::SDN::Vnets::sdn_vnets_config($cfg, $id);
205
206 my $vnet_cfg = PVE::Network::SDN::Vnets::config();
207
208 PVE::Network::SDN::VnetPlugin->on_delete_hook($id, $vnet_cfg);
209
210 delete $cfg->{ids}->{$id};
211 PVE::Network::SDN::Vnets::write_config($cfg);
212
213 }, "delete sdn vnet object failed");
214
215
216 return undef;
217 }});
218
219 1;