]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
1 package PVE
::API2
::Storage
::Config
;
7 use PVE
::Tools
qw(extract_param);
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 __PACKAGE__-
>register_method ({
120 description
=> "Create a new storage.",
122 check
=> ['perm', '/storage', ['Datastore.Allocate']],
124 parameters
=> PVE
::Storage
::Plugin-
>createSchema(),
125 returns
=> { type
=> 'null' },
129 my $type = extract_param
($param, 'type');
130 my $storeid = extract_param
($param, 'storage');
132 # revent an empty nodelist.
133 # fix me in section config create never need an empty entity.
134 delete $param->{nodes
} if !$param->{nodes
};
137 # always extract pw, else it gets written to the www-data readable scfg
138 if (my $tmp_pw = extract_param
($param, 'password')) {
139 if (($type eq 'pbs') || ($type eq 'cifs' && $param->{username
})) {
142 warn "ignore password parameter\n";
146 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
147 my $opts = $plugin->check_config($storeid, $param, 1, 1);
149 PVE
::Storage
::lock_storage_config
(
152 my $cfg = PVE
::Storage
::config
();
154 if (my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid, 1)) {
155 die "storage ID '$storeid' already defined\n";
158 $cfg->{ids
}->{$storeid} = $opts;
160 $plugin->on_add_hook($storeid, $opts, password
=> $password);
163 # try to activate if enabled on local node,
164 # we only do this to detect errors/problems sooner
165 if (PVE
::Storage
::storage_check_enabled
($cfg, $storeid, undef, 1)) {
166 PVE
::Storage
::activate_storage
($cfg, $storeid);
170 eval { $plugin->on_delete_hook($storeid, $opts) };
175 PVE
::Storage
::write_config
($cfg);
177 }, "create storage failed");
182 __PACKAGE__-
>register_method ({
187 description
=> "Update storage configuration.",
189 check
=> ['perm', '/storage', ['Datastore.Allocate']],
191 parameters
=> PVE
::Storage
::Plugin-
>updateSchema(),
192 returns
=> { type
=> 'null' },
196 my $storeid = extract_param
($param, 'storage');
197 my $digest = extract_param
($param, 'digest');
198 my $delete = extract_param
($param, 'delete');
200 PVE
::Storage
::lock_storage_config
(sub {
202 my $cfg = PVE
::Storage
::config
();
204 PVE
::SectionConfig
::assert_if_modified
($cfg, $digest);
206 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
207 my $type = $scfg->{type
};
210 # always extract pw, else it gets written to the www-data readable scfg
211 if (my $tmp_pw = extract_param
($param, 'password')) {
212 if (($type eq 'pbs') || ($type eq 'cifs' && $param->{username
})) {
215 warn "ignore password parameter\n";
219 my $plugin = PVE
::Storage
::Plugin-
>lookup($type);
220 my $opts = $plugin->check_config($storeid, $param, 0, 1);
222 my $delete_password = 0;
225 my $options = $plugin->private()->{options
}->{$type};
226 foreach my $k (PVE
::Tools
::split_list
($delete)) {
227 my $d = $options->{$k} || die "no such option '$k'\n";
228 die "unable to delete required option '$k'\n" if !$d->{optional
};
229 die "unable to delete fixed option '$k'\n" if $d->{fixed
};
230 die "cannot set and delete property '$k' at the same time!\n"
231 if defined($opts->{$k});
235 $delete_password = 1 if $k eq 'password';
239 if ($delete_password || defined($password)) {
240 $plugin->on_update_hook($storeid, $opts, password
=> $password);
242 $plugin->on_update_hook($storeid, $opts);
245 for my $k (keys %$opts) {
246 $scfg->{$k} = $opts->{$k};
249 PVE
::Storage
::write_config
($cfg);
251 }, "update storage failed");
256 __PACKAGE__-
>register_method ({
259 path
=> '{storage}', # /storage/config/{storage}
261 description
=> "Delete storage configuration.",
263 check
=> ['perm', '/storage', ['Datastore.Allocate']],
266 additionalProperties
=> 0,
268 storage
=> get_standard_option
('pve-storage-id', {
269 completion
=> \
&PVE
::Storage
::complete_storage
,
273 returns
=> { type
=> 'null' },
277 my $storeid = extract_param
($param, 'storage');
279 PVE
::Storage
::lock_storage_config
(
282 my $cfg = PVE
::Storage
::config
();
284 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
286 die "can't remove storage - storage is used as base of another storage\n"
287 if PVE
::Storage
::storage_is_used
($cfg, $storeid);
289 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
291 $plugin->on_delete_hook($storeid, $scfg);
293 delete $cfg->{ids
}->{$storeid};
295 PVE
::Storage
::write_config
($cfg);
297 }, "delete storage failed");
299 PVE
::AccessControl
::remove_storage_access
($storeid);