]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
ea655c5805256264b6c8d6a41b792947bf460bc8
1 package PVE
::API2
::Storage
::Config
;
7 use PVE
::Tools
qw(extract_param extract_sensitive_params);
8 use PVE
::Cluster
qw(cfs_read_file cfs_write_file);
10 use PVE
::Storage
::Plugin
;
11 use PVE
::Storage
::LVMPlugin
;
12 use PVE
::Storage
::CIFSPlugin
;
13 use HTTP
::Status
qw(:constants);
14 use Storable
qw(dclone);
15 use PVE
::JSONSchema
qw(get_standard_option);
16 use PVE
::RPCEnvironment
;
20 use base
qw(PVE::RESTHandler);
22 my @ctypes = qw(images vztmpl iso backup);
24 my $storage_type_enum = PVE
::Storage
::Plugin-
>lookup_types();
26 my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
29 my $scfg = dclone
(PVE
::Storage
::storage_config
($cfg, $storeid));
30 $scfg->{storage
} = $storeid;
31 $scfg->{digest
} = $cfg->{digest
};
32 $scfg->{content
} = PVE
::Storage
::Plugin-
>encode_value($scfg->{type
}, 'content', $scfg->{content
});
35 $scfg->{nodes
} = PVE
::Storage
::Plugin-
>encode_value($scfg->{type
}, 'nodes', $scfg->{nodes
});
41 __PACKAGE__-
>register_method ({
45 description
=> "Storage index.",
47 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
51 additionalProperties
=> 0,
54 description
=> "Only list storage of specific type",
56 enum
=> $storage_type_enum,
65 properties
=> { storage
=> { type
=> 'string'} },
67 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
72 my $rpcenv = PVE
::RPCEnvironment
::get
();
73 my $authuser = $rpcenv->get_user();
75 my $cfg = PVE
::Storage
::config
();
77 my @sids = PVE
::Storage
::storage_ids
($cfg);
80 foreach my $storeid (@sids) {
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type
} && $param->{type
} ne $scfg->{type
};
92 __PACKAGE__-
>register_method ({
96 description
=> "Read storage configuration.",
98 check
=> ['perm', '/storage/{storage}', ['Datastore.Allocate']],
101 additionalProperties
=> 0,
103 storage
=> get_standard_option
('pve-storage-id'),
106 returns
=> { type
=> 'object' },
110 my $cfg = PVE
::Storage
::config
();
112 return &$api_storage_config($cfg, $param->{storage
});
115 my $sensitive_params = [qw(password encryption-key master-pubkey)];
117 __PACKAGE__-
>register_method ({
122 description
=> "Create a new storage.",
124 check
=> ['perm', '/storage', ['Datastore.Allocate']],
126 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
131 description
=> "The ID of the created storage.",
135 description
=> "The type of the created storage.",
137 enum
=> $storage_type_enum,
140 description
=> "Partial, possible server generated, configuration properties.",
143 additionalProperties
=> 1,
145 'encryption-key' => {
146 description
=> "The, possible auto-generated, encryption-key.",
157 my $type = extract_param
($param, 'type');
158 my $storeid = extract_param
($param, 'storage');
160 # revent an empty nodelist.
161 # fix me in section config create never need an empty entity.
162 delete $param->{nodes
} if !$param->{nodes
};
164 my $sensitive = extract_sensitive_params
($param, $sensitive_params, []);
166 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
167 my $opts = $plugin->check_config($storeid, $param, 1, 1);
170 PVE
::Storage
::lock_storage_config
(sub {
171 my $cfg = PVE
::Storage
::config
();
173 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
174 die "storage ID '$storeid' already defined\n";
177 $cfg->{ids
}->{$storeid} = $opts;
179 $returned_config = $plugin->on_add_hook($storeid, $opts, %$sensitive);
182 # try to activate if enabled on local node,
183 # we only do this to detect errors/problems sooner
184 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
185 PVE
::Storage
::activate_storage
($cfg, $storeid);
189 eval { $plugin->on_delete_hook($storeid, $opts) };
194 PVE
::Storage
::write_config
($cfg);
196 }, "create storage failed");
202 $res->{config
} = $returned_config if $returned_config;
206 __PACKAGE__-
>register_method ({
211 description
=> "Update storage configuration.",
213 check
=> ['perm', '/storage', ['Datastore.Allocate']],
215 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
220 description
=> "The ID of the created storage.",
224 description
=> "The type of the created storage.",
226 enum
=> $storage_type_enum,
229 description
=> "Partial, possible server generated, configuration properties.",
232 additionalProperties
=> 1,
234 'encryption-key' => {
235 description
=> "The, possible auto-generated, encryption-key.",
246 my $storeid = extract_param
($param, 'storage');
247 my $digest = extract_param
($param, 'digest');
248 my $delete = extract_param
($param, 'delete');
252 $delete = [ PVE
::Tools
::split_list
($delete) ];
256 PVE
::Storage
::lock_storage_config
(sub {
257 my $cfg = PVE
::Storage
::config
();
259 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
261 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
262 $type = $scfg->{type
};
264 my $sensitive = extract_sensitive_params
($param, $sensitive_params, $delete);
266 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
267 my $opts = $plugin->check_config($storeid, $param, 0, 1);
270 my $options = $plugin->private()->{options
}->{$type};
271 foreach my $k (@$delete) {
272 my $d = $options->{$k} || die "no such option '$k'\n";
273 die "unable to delete required option '$k'\n" if !$d->{optional
};
274 die "unable to delete fixed option '$k'\n" if $d->{fixed
};
275 die "cannot set and delete property '$k' at the same time!\n"
276 if defined($opts->{$k});
282 $returned_config = $plugin->on_update_hook($storeid, $opts, %$sensitive);
284 for my $k (keys %$opts) {
285 $scfg->{$k} = $opts->{$k};
288 PVE
::Storage
::write_config
($cfg);
290 }, "update storage failed");
296 $res->{config
} = $returned_config if $returned_config;
300 __PACKAGE__-
>register_method ({
303 path
=> '{storage}', # /storage/config/{storage}
305 description
=> "Delete storage configuration.",
307 check
=> ['perm', '/storage', ['Datastore.Allocate']],
310 additionalProperties
=> 0,
312 storage
=> get_standard_option
('pve-storage-id', {
313 completion
=> \
&PVE
::Storage
::complete_storage
,
317 returns
=> { type
=> 'null' },
321 my $storeid = extract_param
($param, 'storage');
323 PVE
::Storage
::lock_storage_config
(sub {
324 my $cfg = PVE
::Storage
::config
();
326 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
328 die "can't remove storage - storage is used as base of another storage\n"
329 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
331 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
333 $plugin->on_delete_hook($storeid, $scfg);
335 delete $cfg->{ids
}->{$storeid};
337 PVE
::Storage
::write_config
($cfg);
339 }, "delete storage failed");
341 PVE
::AccessControl
::remove_storage_access
($storeid);