]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/Cluster/Mapping/PCI.pm
1 package PVE
::API2
::Cluster
::Mapping
::PCI
;
6 use Storable
qw(dclone);
8 use PVE
::Mapping
::PCI
();
9 use PVE
::JSONSchema
qw(get_standard_option);
10 use PVE
::Tools
qw(extract_param);
12 use base
qw(PVE::RESTHandler);
14 __PACKAGE__-
>register_method ({
18 # only proxy if we give the 'check-node' parameter
19 proxyto_callback
=> sub {
20 my ($rpcenv, $proxyto, $param) = @_;
21 return $param->{'check-node'} // 'localhost';
23 description
=> "List PCI Hardware Mapping",
25 description
=> "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or".
26 " 'Mapping.Audit' permissions on '/mapping/pci/<id>'.",
30 additionalProperties
=> 0,
32 'check-node' => get_standard_option
('pve-node', {
33 description
=> "If given, checks the configurations on the given node for ".
34 "correctness, and adds relevant diagnostics for the devices to the response.",
46 description
=> "The logical ID of the mapping."
50 description
=> "The entries of the mapping.",
53 description
=> "A mapping for a node.",
58 description
=> "A description of the logical mapping.",
63 description
=> "A list of checks, only present if 'check_node' is set.",
69 enum
=> ['warning', 'error'],
70 description
=> "The severity of the error",
74 description
=> "The message of the error",
81 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
86 my $rpcenv = PVE
::RPCEnvironment
::get
();
87 my $authuser = $rpcenv->get_user();
89 my $check_node = $param->{'check-node'};
90 my $local_node = PVE
::INotify
::nodename
();
92 die "wrong node to check - $check_node != $local_node\n"
93 if defined($check_node) && $check_node ne 'localhost' && $check_node ne $local_node;
95 my $cfg = PVE
::Mapping
::PCI
::config
();
97 my $can_see_mapping_privs = ['Mapping.Modify', 'Mapping.Use', 'Mapping.Audit'];
100 for my $id (keys $cfg->{ids
}->%*) {
101 next if !$rpcenv->check_any($authuser, "/mapping/pci/$id", $can_see_mapping_privs, 1);
102 next if !$cfg->{ids
}->{$id};
104 my $entry = dclone
($cfg->{ids
}->{$id});
106 $entry->{digest
} = $cfg->{digest
};
108 if (defined($check_node)) {
109 $entry->{checks
} = [];
110 if (my $mappings = PVE
::Mapping
::PCI
::get_node_mapping
($cfg, $id, $check_node)) {
111 if (!scalar($mappings->@*)) {
112 push $entry->{checks
}->@*, {
113 severity
=> 'warning',
114 message
=> "No mapping for node $check_node.",
117 for my $mapping ($mappings->@*) {
118 eval { PVE
::Mapping
::PCI
::assert_valid
($id, $mapping) };
120 push $entry->{checks
}->@*, {
122 message
=> "Invalid configuration: $err",
136 __PACKAGE__-
>register_method ({
141 description
=> "Get PCI Mapping.",
144 ['perm', '/mapping/pci/{id}', ['Mapping.Use']],
145 ['perm', '/mapping/pci/{id}', ['Mapping.Modify']],
146 ['perm', '/mapping/pci/{id}', ['Mapping.Audit']],
150 additionalProperties
=> 0,
154 format
=> 'pve-configid',
158 returns
=> { type
=> 'object' },
162 my $cfg = PVE
::Mapping
::PCI
::config
();
163 my $id = $param->{id
};
165 my $entry = $cfg->{ids
}->{$id};
166 die "mapping '$param->{id}' not found\n" if !defined($entry);
168 my $data = dclone
($entry);
170 $data->{digest
} = $cfg->{digest
};
175 __PACKAGE__-
>register_method ({
180 description
=> "Create a new hardware mapping.",
182 check
=> ['perm', '/mapping/pci', ['Mapping.Modify']],
184 parameters
=> PVE
::Mapping
::PCI-
>createSchema(1),
191 my $id = extract_param
($param, 'id');
193 my $plugin = PVE
::Mapping
::PCI-
>lookup('pci');
194 my $opts = $plugin->check_config($id, $param, 1, 1);
196 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
197 my $cfg = PVE
::Mapping
::PCI
::config
();
199 die "pci ID '$id' already defined\n" if defined($cfg->{ids
}->{$id});
201 $cfg->{ids
}->{$id} = $opts;
203 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
205 }, "create hardware mapping failed");
211 __PACKAGE__-
>register_method ({
216 description
=> "Update a hardware mapping.",
218 check
=> ['perm', '/mapping/pci/{id}', ['Mapping.Modify']],
220 parameters
=> PVE
::Mapping
::PCI-
>updateSchema(),
227 my $digest = extract_param
($param, 'digest');
228 my $delete = extract_param
($param, 'delete');
229 my $id = extract_param
($param, 'id');
232 $delete = [ PVE
::Tools
::split_list
($delete) ];
235 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
236 my $cfg = PVE
::Mapping
::PCI
::config
();
238 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest) if defined($digest);
240 die "pci ID '$id' does not exist\n" if !defined($cfg->{ids
}->{$id});
242 my $plugin = PVE
::Mapping
::PCI-
>lookup('pci');
243 my $opts = $plugin->check_config($id, $param, 1, 1);
245 my $data = $cfg->{ids
}->{$id};
247 my $options = $plugin->private()->{options
}->{pci
};
248 PVE
::SectionConfig
::delete_from_config
($data, $options, $opts, $delete);
250 $data->{$_} = $opts->{$_} for keys $opts->%*;
252 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
254 }, "update hardware mapping failed");
260 __PACKAGE__-
>register_method ({
265 description
=> "Remove Hardware Mapping.",
267 check
=> [ 'perm', '/mapping/pci', ['Mapping.Modify']],
270 additionalProperties
=> 0,
274 format
=> 'pve-configid',
278 returns
=> { type
=> 'null' },
282 my $id = $param->{id
};
284 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
285 my $cfg = PVE
::Mapping
::PCI
::config
();
287 if ($cfg->{ids
}->{$id}) {
288 delete $cfg->{ids
}->{$id};
291 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
293 }, "delete pci mapping failed");