]>
git.proxmox.com Git - pve-network.git/blob - PVE/API2/Network/SDN.pm
1 package PVE
::API2
::Network
::SDN
;
7 use PVE
::Tools
qw(extract_param);
8 use PVE
::Cluster
qw(cfs_read_file cfs_write_file);
10 use PVE
::Network
::SDN
::Plugin
;
11 use PVE
::Network
::SDN
::VlanPlugin
;
12 use PVE
::Network
::SDN
::VxlanPlugin
;
13 use PVE
::Network
::SDN
::VnetPlugin
;
14 use PVE
::Network
::SDN
::FaucetControllerPlugin
;
15 use PVE
::Network
::SDN
::FaucetPlugin
;
16 use PVE
::Network
::SDN
::EvpnControllerPlugin
;
17 use PVE
::Network
::SDN
::EvpnPlugin
;
19 use Storable
qw(dclone);
20 use PVE
::JSONSchema
qw(get_standard_option);
21 use PVE
::RPCEnvironment
;
25 use base
qw(PVE::RESTHandler);
27 my $sdn_type_enum = PVE
::Network
::SDN
::Plugin-
>lookup_types();
29 my $api_sdn_config = sub {
30 my ($cfg, $sdnid) = @_;
32 my $scfg = dclone
(PVE
::Network
::SDN
::sdn_config
($cfg, $sdnid));
33 $scfg->{sdn
} = $sdnid;
34 $scfg->{digest
} = $cfg->{digest
};
39 __PACKAGE__-
>register_method ({
43 description
=> "SDN index.",
45 description
=> "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/cluster/sdn/<sdn>'",
49 additionalProperties
=> 0,
52 description
=> "Only list sdn of specific type",
54 enum
=> $sdn_type_enum,
63 properties
=> { sdn
=> { type
=> 'string'} },
65 links
=> [ { rel
=> 'child', href
=> "{sdn}" } ],
70 my $rpcenv = PVE
::RPCEnvironment
::get
();
71 my $authuser = $rpcenv->get_user();
74 my $cfg = PVE
::Network
::SDN
::config
();
76 my @sids = PVE
::Network
::SDN
::sdn_ids
($cfg);
78 foreach my $sdnid (@sids) {
79 # my $privs = [ 'SDN.Audit', 'SDN.Allocate' ];
80 # next if !$rpcenv->check_any($authuser, "/cluster/sdn/$sdnid", $privs, 1);
82 my $scfg = &$api_sdn_config($cfg, $sdnid);
83 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
90 __PACKAGE__-
>register_method ({
94 description
=> "Read sdn configuration.",
96 # check => ['perm', '/cluster/sdn/{sdn}', ['SDN.Allocate']],
100 additionalProperties
=> 0,
102 sdn
=> get_standard_option
('pve-sdn-id'),
105 returns
=> { type
=> 'object' },
109 my $cfg = PVE
::Network
::SDN
::config
();
111 return &$api_sdn_config($cfg, $param->{sdn
});
114 __PACKAGE__-
>register_method ({
119 description
=> "Create a new sdn object.",
121 # check => ['perm', '/cluster/sdn', ['SDN.Allocate']],
123 parameters
=> PVE
::Network
::SDN
::Plugin-
>createSchema(),
124 returns
=> { type
=> 'null' },
128 my $type = extract_param
($param, 'type');
129 my $sdnid = extract_param
($param, 'sdn');
131 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($type);
132 my $opts = $plugin->check_config($sdnid, $param, 1, 1);
134 PVE
::Network
::SDN
::lock_sdn_config
(
137 my $cfg = PVE
::Network
::SDN
::config
();
140 if ($scfg = PVE
::Network
::SDN
::sdn_config
($cfg, $sdnid, 1)) {
141 die "sdn object ID '$sdnid' already defined\n";
144 $cfg->{ids
}->{$sdnid} = $opts;
145 $plugin->on_update_hook($sdnid, $cfg);
146 #also verify transport associated to vnet
147 if($scfg && $scfg->{type
} eq 'vnet') {
148 my $transportid = $scfg->{transportzone
};
149 die "missing transportzone" if !$transportid;
150 my $transport_cfg = $cfg->{ids
}->{$transportid};
151 my $transport_plugin = PVE
::Network
::SDN
::Plugin-
>lookup($transport_cfg->{type
});
152 $transport_plugin->on_update_hook($transportid, $cfg);
155 PVE
::Network
::SDN
::write_config
($cfg);
157 }, "create sdn object failed");
162 __PACKAGE__-
>register_method ({
163 name
=> 'apply_configuration',
167 description
=> "Apply sdn changes.",
169 # check => ['perm', '/cluster/sdn', ['SDN.Allocate']],
172 additionalProperties
=> 0,
174 returns
=> { type
=> 'null' },
178 die "no sdn changes to apply" if !-e
"/etc/pve/sdn.cfg.new";
179 rename("/etc/pve/sdn.cfg.new", "/etc/pve/sdn.cfg")
180 || die "applying sdn.cfg changes failed - $!\n";
186 __PACKAGE__-
>register_method ({
187 name
=> 'revert_configuration',
191 description
=> "Revert sdn changes.",
193 # check => ['perm', '/cluster/sdn', ['SDN.Allocate']],
196 additionalProperties
=> 0,
198 returns
=> { type
=> 'null' },
202 die "no sdn changes to revert" if !-e
"/etc/pve/sdn.cfg.new";
203 unlink "/etc/pve/sdn.cfg.new";
208 __PACKAGE__-
>register_method ({
213 description
=> "Update sdn object configuration.",
215 # check => ['perm', '/cluster/sdn', ['SDN.Allocate']],
217 parameters
=> PVE
::Network
::SDN
::Plugin-
>updateSchema(),
218 returns
=> { type
=> 'null' },
222 my $sdnid = extract_param
($param, 'sdn');
223 my $digest = extract_param
($param, 'digest');
225 PVE
::Network
::SDN
::lock_sdn_config
(
228 my $cfg = PVE
::Network
::SDN
::config
();
230 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
232 my $scfg = PVE
::Network
::SDN
::sdn_config
($cfg, $sdnid);
234 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($scfg->{type
});
235 my $opts = $plugin->check_config($sdnid, $param, 0, 1);
237 foreach my $k (%$opts) {
238 $scfg->{$k} = $opts->{$k};
241 $plugin->on_update_hook($sdnid, $cfg);
242 #also verify transport associated to vnet
243 if($scfg->{type
} eq 'vnet') {
244 my $transportid = $scfg->{transportzone
};
245 die "missing transportzone" if !$transportid;
246 my $transport_cfg = $cfg->{ids
}->{$transportid};
247 my $transport_plugin = PVE
::Network
::SDN
::Plugin-
>lookup($transport_cfg->{type
});
248 $transport_plugin->on_update_hook($transportid, $cfg);
250 PVE
::Network
::SDN
::write_config
($cfg);
252 }, "update sdn object failed");
257 __PACKAGE__-
>register_method ({
262 description
=> "Delete sdn object configuration.",
264 # check => ['perm', '/cluster/sdn', ['SDN.Allocate']],
267 additionalProperties
=> 0,
269 sdn
=> get_standard_option
('pve-sdn-id', {
270 completion
=> \
&PVE
::Network
::SDN
::complete_sdn
,
274 returns
=> { type
=> 'null' },
278 my $sdnid = extract_param
($param, 'sdn');
280 PVE
::Network
::SDN
::lock_sdn_config
(
283 my $cfg = PVE
::Network
::SDN
::config
();
285 my $scfg = PVE
::Network
::SDN
::sdn_config
($cfg, $sdnid);
287 my $plugin = PVE
::Network
::SDN
::Plugin-
>lookup($scfg->{type
});
288 $plugin->on_delete_hook($sdnid, $cfg);
290 delete $cfg->{ids
}->{$sdnid};
291 PVE
::Network
::SDN
::write_config
($cfg);
293 }, "delete sdn object failed");