]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Storage/Config.pm
config: add missing whitespace
[pve-storage.git] / PVE / API2 / Storage / Config.pm
CommitLineData
b6cf0a66
DM
1package PVE::API2::Storage::Config;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
1dc01b9f 7use PVE::Tools qw(extract_param);
b6cf0a66
DM
8use PVE::Cluster qw(cfs_read_file cfs_write_file);
9use PVE::Storage;
1dc01b9f 10use PVE::Storage::Plugin;
304344ce 11use PVE::Storage::LVMPlugin;
a9db2ca8 12use PVE::Storage::CIFSPlugin;
b6cf0a66
DM
13use HTTP::Status qw(:constants);
14use Storable qw(dclone);
15use PVE::JSONSchema qw(get_standard_option);
5f642f73 16use PVE::RPCEnvironment;
b6cf0a66
DM
17
18use PVE::RESTHandler;
19
20use base qw(PVE::RESTHandler);
21
22my @ctypes = qw(images vztmpl iso backup);
23
1dc01b9f 24my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
b6cf0a66
DM
25
26my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
28
1dc01b9f 29 my $scfg = dclone(PVE::Storage::storage_config($cfg, $storeid));
b6cf0a66 30 $scfg->{storage} = $storeid;
b6cf0a66 31 $scfg->{digest} = $cfg->{digest};
1dc01b9f 32 $scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
b6cf0a66
DM
33
34 if ($scfg->{nodes}) {
1dc01b9f 35 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
b6cf0a66
DM
36 }
37
38 return $scfg;
39};
40
41__PACKAGE__->register_method ({
37ab64f3 42 name => 'index',
b6cf0a66
DM
43 path => '',
44 method => 'GET',
45 description => "Storage index.",
37ab64f3 46 permissions => {
5f642f73
DM
47 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
48 user => 'all',
49 },
b6cf0a66 50 parameters => {
37ab64f3 51 additionalProperties => 0,
b6cf0a66 52 properties => {
37ab64f3 53 type => {
b6cf0a66 54 description => "Only list storage of specific type",
37ab64f3 55 type => 'string',
b6cf0a66
DM
56 enum => $storage_type_enum,
57 optional => 1,
58 },
b6cf0a66
DM
59 },
60 },
61 returns => {
62 type => 'array',
63 items => {
64 type => "object",
65 properties => { storage => { type => 'string'} },
66 },
67 links => [ { rel => 'child', href => "{storage}" } ],
68 },
69 code => sub {
70 my ($param) = @_;
71
5f642f73
DM
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
83d7192f 75 my $cfg = PVE::Storage::config();
b6cf0a66 76
5f642f73 77 my @sids = PVE::Storage::storage_ids($cfg);
b6cf0a66
DM
78
79 my $res = [];
80 foreach my $storeid (@sids) {
5f642f73
DM
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83
b6cf0a66
DM
84 my $scfg = &$api_storage_config($cfg, $storeid);
85 next if $param->{type} && $param->{type} ne $scfg->{type};
86 push @$res, $scfg;
87 }
88
89 return $res;
90 }});
91
92__PACKAGE__->register_method ({
37ab64f3 93 name => 'read',
b6cf0a66
DM
94 path => '{storage}',
95 method => 'GET',
96 description => "Read storage configuration.",
37ab64f3 97 permissions => {
5f642f73
DM
98 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 },
b6cf0a66 100 parameters => {
37ab64f3 101 additionalProperties => 0,
b6cf0a66
DM
102 properties => {
103 storage => get_standard_option('pve-storage-id'),
104 },
105 },
8b3d5c1f 106 returns => { type => 'object' },
b6cf0a66
DM
107 code => sub {
108 my ($param) = @_;
109
83d7192f 110 my $cfg = PVE::Storage::config();
b6cf0a66
DM
111
112 return &$api_storage_config($cfg, $param->{storage});
113 }});
114
115__PACKAGE__->register_method ({
116 name => 'create',
117 protected => 1,
37ab64f3 118 path => '',
b6cf0a66
DM
119 method => 'POST',
120 description => "Create a new storage.",
37ab64f3 121 permissions => {
5f642f73
DM
122 check => ['perm', '/storage', ['Datastore.Allocate']],
123 },
1dc01b9f 124 parameters => PVE::Storage::Plugin->createSchema(),
b6cf0a66
DM
125 returns => { type => 'null' },
126 code => sub {
127 my ($param) = @_;
128
1dc01b9f
DM
129 my $type = extract_param($param, 'type');
130 my $storeid = extract_param($param, 'storage');
b6cf0a66 131
a4a9405d
WL
132 # revent an empty nodelist.
133 # fix me in section config create never need an empty entity.
134 delete $param->{nodes} if !$param->{nodes};
135
d7b70762
TL
136 my $password;
137 # always extract pw, else it gets written to the www-data readable scfg
138 if (my $tmp_pw = extract_param($param, 'password')) {
271fe394 139 if (($type eq 'pbs') || ($type eq 'cifs' && $param->{username})) {
d7b70762
TL
140 $password = $tmp_pw;
141 } else {
142 warn "ignore password parameter\n";
143 }
144 }
a4a9405d 145
1dc01b9f
DM
146 my $plugin = PVE::Storage::Plugin->lookup($type);
147 my $opts = $plugin->check_config($storeid, $param, 1, 1);
b6cf0a66
DM
148
149 PVE::Storage::lock_storage_config(
150 sub {
151
83d7192f 152 my $cfg = PVE::Storage::config();
b6cf0a66 153
1dc01b9f 154 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
b6cf0a66
DM
155 die "storage ID '$storeid' already defined\n";
156 }
157
158 $cfg->{ids}->{$storeid} = $opts;
159
3932ca0d
TL
160 $plugin->on_add_hook($storeid, $opts, password => $password);
161
a4a9405d
WL
162 eval {
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);
167 }
168 };
187e32ce 169 if (my $err = $@) {
3932ca0d
TL
170 eval { $plugin->on_delete_hook($storeid, $opts) };
171 warn "$@\n" if $@;
a4a9405d 172 die $err;
b6cf0a66
DM
173 }
174
83d7192f 175 PVE::Storage::write_config($cfg);
37ab64f3 176
b6cf0a66
DM
177 }, "create storage failed");
178
1dc01b9f 179 return undef;
b6cf0a66
DM
180 }});
181
182__PACKAGE__->register_method ({
183 name => 'update',
184 protected => 1,
185 path => '{storage}',
186 method => 'PUT',
187 description => "Update storage configuration.",
37ab64f3 188 permissions => {
5f642f73
DM
189 check => ['perm', '/storage', ['Datastore.Allocate']],
190 },
1dc01b9f 191 parameters => PVE::Storage::Plugin->updateSchema(),
b6cf0a66
DM
192 returns => { type => 'null' },
193 code => sub {
194 my ($param) = @_;
195
1dc01b9f
DM
196 my $storeid = extract_param($param, 'storage');
197 my $digest = extract_param($param, 'digest');
4273e3ac 198 my $delete = extract_param($param, 'delete');
b6cf0a66 199
37ab64f3 200 PVE::Storage::lock_storage_config(sub {
b6cf0a66 201
83d7192f 202 my $cfg = PVE::Storage::config();
b6cf0a66 203
1dc01b9f 204 PVE::SectionConfig::assert_if_modified($cfg, $digest);
b6cf0a66 205
1dc01b9f 206 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
0ff4cfea
DM
207 my $type = $scfg->{type};
208
209 my $password;
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})) {
213 $password = $tmp_pw;
214 } else {
215 warn "ignore password parameter\n";
216 }
217 }
b6cf0a66 218
0ff4cfea 219 my $plugin = PVE::Storage::Plugin->lookup($type);
1dc01b9f 220 my $opts = $plugin->check_config($storeid, $param, 0, 1);
b6cf0a66 221
0ff4cfea
DM
222 my $delete_password = 0;
223
4273e3ac 224 if ($delete) {
0ff4cfea 225 my $options = $plugin->private()->{options}->{$type};
4273e3ac
TL
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});
232
233 delete $scfg->{$k};
0ff4cfea
DM
234
235 $delete_password = 1 if $k eq 'password';
4273e3ac
TL
236 }
237 }
238
0ff4cfea
DM
239 if ($delete_password || defined($password)) {
240 $plugin->on_update_hook($storeid, $opts, password => $password);
241 } else {
242 $plugin->on_update_hook($storeid, $opts);
243 }
244
91f42b33 245 for my $k (keys %$opts) {
b6cf0a66
DM
246 $scfg->{$k} = $opts->{$k};
247 }
248
83d7192f 249 PVE::Storage::write_config($cfg);
b6cf0a66 250
37ab64f3 251 }, "update storage failed");
b6cf0a66
DM
252
253 return undef;
254 }});
255
256__PACKAGE__->register_method ({
257 name => 'delete',
258 protected => 1,
259 path => '{storage}', # /storage/config/{storage}
260 method => 'DELETE',
261 description => "Delete storage configuration.",
37ab64f3 262 permissions => {
5f642f73
DM
263 check => ['perm', '/storage', ['Datastore.Allocate']],
264 },
b6cf0a66 265 parameters => {
37ab64f3
TL
266 additionalProperties => 0,
267 properties => {
f3bd890d
DM
268 storage => get_standard_option('pve-storage-id', {
269 completion => \&PVE::Storage::complete_storage,
270 }),
b6cf0a66
DM
271 },
272 },
273 returns => { type => 'null' },
274 code => sub {
275 my ($param) = @_;
276
1dc01b9f
DM
277 my $storeid = extract_param($param, 'storage');
278
b6cf0a66
DM
279 PVE::Storage::lock_storage_config(
280 sub {
281
83d7192f 282 my $cfg = PVE::Storage::config();
b6cf0a66 283
5a39d0a1 284 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
402df80b 285
b6cf0a66 286 die "can't remove storage - storage is used as base of another storage\n"
1dc01b9f 287 if PVE::Storage::storage_is_used($cfg, $storeid);
b6cf0a66 288
3932ca0d
TL
289 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
290
291 $plugin->on_delete_hook($storeid, $scfg);
292
1dc01b9f 293 delete $cfg->{ids}->{$storeid};
b6cf0a66 294
83d7192f 295 PVE::Storage::write_config($cfg);
b6cf0a66
DM
296
297 }, "delete storage failed");
2a2cf20a
AG
298
299 PVE::AccessControl::remove_storage_access($storeid);
300
b6cf0a66
DM
301 return undef;
302 }});
303
3041;