]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Config.pm
api/config update: indentation and whitespace fixes
[pve-storage.git] / PVE / API2 / Storage / Config.pm
1 package PVE::API2::Storage::Config;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param);
8 use PVE::Cluster qw(cfs_read_file cfs_write_file);
9 use PVE::Storage;
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;
17
18 use PVE::RESTHandler;
19
20 use base qw(PVE::RESTHandler);
21
22 my @ctypes = qw(images vztmpl iso backup);
23
24 my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
25
26 my $api_storage_config = sub {
27 my ($cfg, $storeid) = @_;
28
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});
33
34 if ($scfg->{nodes}) {
35 $scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
36 }
37
38 return $scfg;
39 };
40
41 __PACKAGE__->register_method ({
42 name => 'index',
43 path => '',
44 method => 'GET',
45 description => "Storage index.",
46 permissions => {
47 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
48 user => 'all',
49 },
50 parameters => {
51 additionalProperties => 0,
52 properties => {
53 type => {
54 description => "Only list storage of specific type",
55 type => 'string',
56 enum => $storage_type_enum,
57 optional => 1,
58 },
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
72 my $rpcenv = PVE::RPCEnvironment::get();
73 my $authuser = $rpcenv->get_user();
74
75 my $cfg = PVE::Storage::config();
76
77 my @sids = PVE::Storage::storage_ids($cfg);
78
79 my $res = [];
80 foreach my $storeid (@sids) {
81 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
82 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
83
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 ({
93 name => 'read',
94 path => '{storage}',
95 method => 'GET',
96 description => "Read storage configuration.",
97 permissions => {
98 check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
99 },
100 parameters => {
101 additionalProperties => 0,
102 properties => {
103 storage => get_standard_option('pve-storage-id'),
104 },
105 },
106 returns => { type => 'object' },
107 code => sub {
108 my ($param) = @_;
109
110 my $cfg = PVE::Storage::config();
111
112 return &$api_storage_config($cfg, $param->{storage});
113 }});
114
115 __PACKAGE__->register_method ({
116 name => 'create',
117 protected => 1,
118 path => '',
119 method => 'POST',
120 description => "Create a new storage.",
121 permissions => {
122 check => ['perm', '/storage', ['Datastore.Allocate']],
123 },
124 parameters => PVE::Storage::Plugin->createSchema(),
125 returns => { type => 'null' },
126 code => sub {
127 my ($param) = @_;
128
129 my $type = extract_param($param, 'type');
130 my $storeid = extract_param($param, 'storage');
131
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
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')) {
139 if ($type eq 'cifs' && $param->{username}) {
140 $password = $tmp_pw;
141 } else {
142 warn "ignore password parameter\n";
143 }
144 }
145
146 my $plugin = PVE::Storage::Plugin->lookup($type);
147 my $opts = $plugin->check_config($storeid, $param, 1, 1);
148
149 PVE::Storage::lock_storage_config(
150 sub {
151
152 my $cfg = PVE::Storage::config();
153
154 if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
155 die "storage ID '$storeid' already defined\n";
156 }
157
158 $cfg->{ids}->{$storeid} = $opts;
159
160 $plugin->on_add_hook($storeid, $opts, password => $password);
161
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 };
169 if(my $err = $@) {
170 eval { $plugin->on_delete_hook($storeid, $opts) };
171 warn "$@\n" if $@;
172 die $err;
173 }
174
175 PVE::Storage::write_config($cfg);
176
177 }, "create storage failed");
178
179 return undef;
180 }});
181
182 __PACKAGE__->register_method ({
183 name => 'update',
184 protected => 1,
185 path => '{storage}',
186 method => 'PUT',
187 description => "Update storage configuration.",
188 permissions => {
189 check => ['perm', '/storage', ['Datastore.Allocate']],
190 },
191 parameters => PVE::Storage::Plugin->updateSchema(),
192 returns => { type => 'null' },
193 code => sub {
194 my ($param) = @_;
195
196 my $storeid = extract_param($param, 'storage');
197 my $digest = extract_param($param, 'digest');
198 my $delete = extract_param($param, 'delete');
199
200 PVE::Storage::lock_storage_config(sub {
201
202 my $cfg = PVE::Storage::config();
203
204 PVE::SectionConfig::assert_if_modified($cfg, $digest);
205
206 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
207
208 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
209 my $opts = $plugin->check_config($storeid, $param, 0, 1);
210
211 if ($delete) {
212 my $options = $plugin->private()->{options}->{$scfg->{type}};
213 foreach my $k (PVE::Tools::split_list($delete)) {
214 my $d = $options->{$k} || die "no such option '$k'\n";
215 die "unable to delete required option '$k'\n" if !$d->{optional};
216 die "unable to delete fixed option '$k'\n" if $d->{fixed};
217 die "cannot set and delete property '$k' at the same time!\n"
218 if defined($opts->{$k});
219
220 delete $scfg->{$k};
221 }
222 }
223
224 for my $k (keys %$opts) {
225 $scfg->{$k} = $opts->{$k};
226 }
227
228 PVE::Storage::write_config($cfg);
229
230 }, "update storage failed");
231
232 return undef;
233 }});
234
235 __PACKAGE__->register_method ({
236 name => 'delete',
237 protected => 1,
238 path => '{storage}', # /storage/config/{storage}
239 method => 'DELETE',
240 description => "Delete storage configuration.",
241 permissions => {
242 check => ['perm', '/storage', ['Datastore.Allocate']],
243 },
244 parameters => {
245 additionalProperties => 0,
246 properties => {
247 storage => get_standard_option('pve-storage-id', {
248 completion => \&PVE::Storage::complete_storage,
249 }),
250 },
251 },
252 returns => { type => 'null' },
253 code => sub {
254 my ($param) = @_;
255
256 my $storeid = extract_param($param, 'storage');
257
258 PVE::Storage::lock_storage_config(
259 sub {
260
261 my $cfg = PVE::Storage::config();
262
263 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
264
265 die "can't remove storage - storage is used as base of another storage\n"
266 if PVE::Storage::storage_is_used($cfg, $storeid);
267
268 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
269
270 $plugin->on_delete_hook($storeid, $scfg);
271
272 delete $cfg->{ids}->{$storeid};
273
274 PVE::Storage::write_config($cfg);
275
276 }, "delete storage failed");
277
278 PVE::AccessControl::remove_storage_access($storeid);
279
280 return undef;
281 }});
282
283 1;