]>
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
::Cluster
qw(cfs_lock_file);
10 use PVE
::JSONSchema
qw(get_standard_option);
11 use PVE
::Tools
qw(extract_param);
15 use base
qw(PVE::RESTHandler);
17 __PACKAGE__-
>register_method ({
21 # only proxy if we give the 'check-node' parameter
22 proxyto_callback
=> sub {
23 my ($rpcenv, $proxyto, $param) = @_;
24 return $param->{'check-node'} // 'localhost';
26 description
=> "List PCI Hardware Mapping",
28 description
=> "Only lists entries where you have 'Mapping.Modify', 'Mapping.Use' or".
29 " 'Mapping.Audit' permissions on '/mapping/pci/<id>'.",
33 additionalProperties
=> 0,
35 'check-node' => get_standard_option
('pve-node', {
36 description
=> "If given, checks the configurations on the given node for ".
37 "correctness, and adds relevant errors to the devices.",
49 description
=> "The logical ID of the mapping."
53 description
=> "The entries of the mapping.",
56 description
=> "A mapping for a node.",
61 description
=> "A description of the logical mapping.",
64 description
=> "A list of errors when 'check_node' is given.",
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();
88 my $node = $param->{'check-node'};
90 die "Wrong node to check\n"
91 if defined($node) && $node ne 'localhost' && $node ne PVE
::INotify
::nodename
();
93 my $cfg = PVE
::Mapping
::PCI
::config
();
97 my $privs = ['Mapping.Modify', 'Mapping.Use', 'Mapping.Audit'];
99 for my $id (keys $cfg->{ids
}->%*) {
100 next if !$rpcenv->check_full($authuser, "/mapping/pci/$id", $privs, 1, 1);
101 next if !$cfg->{ids
}->{$id};
103 my $entry = dclone
($cfg->{ids
}->{$id});
105 $entry->{digest
} = $cfg->{digest
};
107 if (defined($node)) {
108 $entry->{errors
} = [];
109 if (my $mappings = PVE
::Mapping
::PCI
::get_node_mapping
($cfg, $id, $node)) {
110 if (!scalar($mappings->@*)) {
111 push $entry->{errors
}->@*, {
112 severity
=> 'warning',
113 message
=> "No mapping for node $node.",
116 for my $mapping ($mappings->@*) {
118 PVE
::Mapping
::PCI
::assert_valid
($id, $mapping);
121 push $entry->{errors
}->@*, {
123 message
=> "Invalid configuration: $err",
137 __PACKAGE__-
>register_method ({
142 description
=> "Get PCI Mapping.",
145 ['perm', '/mapping/pci/{id}', ['Mapping.Use']],
146 ['perm', '/mapping/pci/{id}', ['Mapping.Modify']],
147 ['perm', '/mapping/pci/{id}', ['Mapping.Audit']],
151 additionalProperties
=> 0,
155 format
=> 'pve-configid',
159 returns
=> { type
=> 'object' },
163 my $cfg = PVE
::Mapping
::PCI
::config
();
164 my $id = $param->{id
};
166 my $entry = $cfg->{ids
}->{$id};
167 die "mapping '$param->{id}' not found\n" if !defined($entry);
169 my $data = dclone
($entry);
171 $data->{digest
} = $cfg->{digest
};
176 __PACKAGE__-
>register_method ({
181 description
=> "Create a new hardware mapping.",
183 check
=> ['perm', '/mapping/pci', ['Mapping.Modify']],
185 parameters
=> PVE
::Mapping
::PCI-
>createSchema(1),
192 my $id = extract_param
($param, 'id');
194 my $plugin = PVE
::Mapping
::PCI-
>lookup('pci');
195 my $opts = $plugin->check_config($id, $param, 1, 1);
197 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
198 my $cfg = PVE
::Mapping
::PCI
::config
();
200 die "pci ID '$id' already defined\n" if defined($cfg->{ids
}->{$id});
202 $cfg->{ids
}->{$id} = $opts;
204 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
206 }, "create hardware mapping failed");
212 __PACKAGE__-
>register_method ({
217 description
=> "Update a hardware mapping.",
219 check
=> ['perm', '/mapping/pci/{id}', ['Mapping.Modify']],
221 parameters
=> PVE
::Mapping
::PCI-
>updateSchema(),
228 my $digest = extract_param
($param, 'digest');
229 my $delete = extract_param
($param, 'delete');
230 my $id = extract_param
($param, 'id');
233 $delete = [ PVE
::Tools
::split_list
($delete) ];
236 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
237 my $cfg = PVE
::Mapping
::PCI
::config
();
239 PVE
::Tools
::assert_if_modified
($cfg->{digest
}, $digest) if defined($digest);
241 die "pci ID '$id' does not exist\n" if !defined($cfg->{ids
}->{$id});
243 my $plugin = PVE
::Mapping
::PCI-
>lookup('pci');
244 my $opts = $plugin->check_config($id, $param, 1, 1);
246 my $data = $cfg->{ids
}->{$id};
248 my $options = $plugin->private()->{options
}->{pci
};
249 PVE
::SectionConfig
::delete_from_config
($data, $options, $opts, $delete);
251 $data->{$_} = $opts->{$_} for keys $opts->%*;
253 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
255 }, "update hardware mapping failed");
261 __PACKAGE__-
>register_method ({
266 description
=> "Remove Hardware Mapping.",
268 check
=> [ 'perm', '/mapping/pci', ['Mapping.Modify']],
271 additionalProperties
=> 0,
275 format
=> 'pve-configid',
279 returns
=> { type
=> 'null' },
283 my $id = $param->{id
};
285 PVE
::Mapping
::PCI
::lock_pci_config
(sub {
286 my $cfg = PVE
::Mapping
::PCI
::config
();
288 if ($cfg->{ids
}->{$id}) {
289 delete $cfg->{ids
}->{$id};
292 PVE
::Mapping
::PCI
::write_pci_config
($cfg);
294 }, "delete pci mapping failed");