]>
git.proxmox.com Git - pve-manager.git/blob - lib/PVE/API2/Network.pm
1 package PVE
::API2
::Network
;
6 use PVE
::Tools
qw(extract_param);
9 use PVE
::Exception
qw(raise_param_exc);
11 use PVE
::RPCEnvironment
;
12 use PVE
::JSONSchema
qw(get_standard_option);
13 use PVE
::AccessControl
;
16 use base
qw(PVE::RESTHandler);
18 my $iflockfn = "/etc/network/.pve-interfaces.lock";
20 my $bond_mode_enum = [
32 description
=> "Automatically start interface on boot.",
37 description
=> "Specify the iterfaces you want to add to your bridge.",
39 type
=> 'string', format
=> 'pve-iface-list',
42 description
=> "Specify the interfaces used by the bonding device.",
44 type
=> 'string', format
=> 'pve-iface-list',
47 description
=> "Bonding mode.",
49 type
=> 'string', enum
=> $bond_mode_enum,
52 description
=> 'Default gateway address.',
53 type
=> 'string', format
=> 'ipv4',
57 description
=> 'Network mask.',
58 type
=> 'string', format
=> 'ipv4mask',
62 description
=> 'IP address.',
63 type
=> 'string', format
=> 'ipv4',
65 requires
=> 'netmask',
69 sub json_config_properties
{
72 foreach my $opt (keys %$confdesc) {
73 $prop->{$opt} = $confdesc->{$opt};
79 __PACKAGE__-
>register_method({
83 permissions
=> { user
=> 'all' },
84 description
=> "List available networks",
87 additionalProperties
=> 0,
89 node
=> get_standard_option
('pve-node'),
91 description
=> "Only list specific interface types.",
93 enum
=> ['bond', 'bridge', 'alias', 'eth'],
104 links
=> [ { rel
=> 'child', href
=> "{iface}" } ],
109 my $config = PVE
::INotify
::read_file
('interfaces');
111 delete $config->{lo
}; # do not list the loopback device
113 if ($param->{type
}) {
114 foreach my $k (keys %$config) {
115 delete $config->{$k} if $param->{type
} ne $config->{$k}->{type
};
119 return PVE
::RESTHandler
::hash_to_array
($config, 'iface');
123 my $check_duplicate_gateway = sub {
124 my ($config, $newiface) = @_;
126 foreach my $iface (keys %$config) {
127 raise_param_exc
({ gateway
=> "Default gateway already exists on interface '$iface'." })
128 if ($newiface ne $iface) && $config->{$iface}->{gateway
};
133 __PACKAGE__-
>register_method({
134 name
=> 'create_network',
138 path
=> '/nodes/{node}',
139 privs
=> [ 'Sys.Modify' ],
141 description
=> "Create network device configuration",
145 additionalProperties
=> 0,
146 properties
=> json_config_properties
({
147 node
=> get_standard_option
('pve-node'),
148 iface
=> get_standard_option
('pve-iface')}),
150 returns
=> { type
=> 'null' },
154 my $node = extract_param
($param, 'node');
155 my $iface = extract_param
($param, 'iface');
158 my $config = PVE
::INotify
::read_file
('interfaces');
160 raise_param_exc
({ iface
=> "interface already exists" })
161 if $config->{$iface};
163 &$check_duplicate_gateway($config, $iface)
164 if $param->{gateway
};
166 $param->{method} = $param->{address
} ?
'static' : 'manual';
168 $config->{$iface} = $param;
170 PVE
::INotify
::write_file
('interfaces', $config);
173 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
179 __PACKAGE__-
>register_method({
180 name
=> 'update_network',
184 path
=> '/nodes/{node}',
185 privs
=> [ 'Sys.Modify' ],
187 description
=> "Update network device configuration",
191 additionalProperties
=> 0,
192 properties
=> json_config_properties
({
193 node
=> get_standard_option
('pve-node'),
194 iface
=> get_standard_option
('pve-iface'),
196 type
=> 'string', format
=> 'pve-configid-list',
197 description
=> "A list of settings you want to delete.",
201 returns
=> { type
=> 'null' },
205 my $node = extract_param
($param, 'node');
206 my $iface = extract_param
($param, 'iface');
207 my $delete = extract_param
($param, 'delete');
210 my $config = PVE
::INotify
::read_file
('interfaces');
212 raise_param_exc
({ iface
=> "interface does not exist" })
213 if !$config->{$iface};
215 foreach my $k (PVE
::Tools
::split_list
($delete)) {
216 delete $config->{$iface}->{$k};
219 &$check_duplicate_gateway($config, $iface)
220 if $param->{gateway
};
222 $param->{method} = $param->{address
} ?
'static' : 'manual';
224 foreach my $k (keys %$param) {
225 $config->{$iface}->{$k} = $param->{$k};
228 PVE
::INotify
::write_file
('interfaces', $config);
231 PVE
::Tools
::lock_file
($iflockfn, 10, $code);
237 __PACKAGE__-
>register_method({
238 name
=> 'network_config',
242 path
=> '/nodes/{node}',
243 privs
=> [ 'Sys.Audit' ],
245 description
=> "Read network device configuration",
248 additionalProperties
=> 0,
250 node
=> get_standard_option
('pve-node'),
251 iface
=> get_standard_option
('pve-iface'),
268 my $config = PVE
::INotify
::read_file
('interfaces');
270 raise_param_exc
({ iface
=> "interface does not exist" })
271 if !$config->{$param->{iface
}};
273 return $config->{$param->{iface
}};
276 __PACKAGE__-
>register_method({
277 name
=> 'delete_network',
281 path
=> '/nodes/{node}',
282 privs
=> [ 'Sys.Modify' ],
284 description
=> "Delete network device configuration",
288 additionalProperties
=> 0,
290 node
=> get_standard_option
('pve-node'),
291 iface
=> get_standard_option
('pve-iface'),
294 returns
=> { type
=> 'null' },
299 my $config = PVE
::INotify
::read_file
('interfaces');
301 raise_param_exc
({ iface
=> "interface does not exist" })
302 if !$config->{$param->{iface
}};
304 delete $config->{$param->{iface
}};
306 PVE
::INotify
::write_file
('interfaces', $config);
309 PVE
::Tools
::lock_file
($iflockfn, 10, $code);