]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Config.pm
api/config update: indentation and whitespace fixes
[pve-storage.git] / PVE / API2 / Storage / Config.pm
index a697e8dafd8062b449d941d711018b288df289c1..e00dc95536f7535f1edf48561830bcf82b403836 100755 (executable)
@@ -8,11 +8,12 @@ use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::Storage;
 use PVE::Storage::Plugin;
+use PVE::Storage::LVMPlugin;
+use PVE::Storage::CIFSPlugin;
 use HTTP::Status qw(:constants);
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RPCEnvironment;
-use PVE::PTY;
 
 use PVE::RESTHandler;
 
@@ -37,33 +38,21 @@ my $api_storage_config = sub {
     return $scfg;
 };
 
-my $set_cifs_credentials = sub {
-    my ($password, $storeid) = @_;
-
-    my $cred_path = '/etc/pve/priv/';
-
-    my $cred_file = $cred_path.$storeid.".cred";
-
-    PVE::Tools::file_set_contents($cred_file, "password=$password\n");
-
-    return $cred_file;
-};
-
 __PACKAGE__->register_method ({
-    name => 'index', 
+    name => 'index',
     path => '',
     method => 'GET',
     description => "Storage index.",
-    permissions => { 
+    permissions => {
        description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
        user => 'all',
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
-           type => { 
+           type => {
                description => "Only list storage of specific type",
-               type => 'string', 
+               type => 'string',
                enum => $storage_type_enum,
                optional => 1,
            },
@@ -101,20 +90,20 @@ __PACKAGE__->register_method ({
     }});
 
 __PACKAGE__->register_method ({
-    name => 'read', 
+    name => 'read',
     path => '{storage}',
     method => 'GET',
     description => "Read storage configuration.",
-    permissions => { 
+    permissions => {
        check => ['perm', '/storage/{storage}', ['Datastore.Allocate']],
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            storage => get_standard_option('pve-storage-id'),
        },
     },
-    returns => {},
+    returns => { type => 'object' },
     code => sub {
        my ($param) = @_;
 
@@ -126,10 +115,10 @@ __PACKAGE__->register_method ({
 __PACKAGE__->register_method ({
     name => 'create',
     protected => 1,
-    path => '', 
+    path => '',
     method => 'POST',
     description => "Create a new storage.",
-    permissions => { 
+    permissions => {
        check => ['perm', '/storage', ['Datastore.Allocate']],
     },
     parameters => PVE::Storage::Plugin->createSchema(),
@@ -144,11 +133,14 @@ __PACKAGE__->register_method ({
        # fix me in section config create never need an empty entity.
        delete $param->{nodes} if !$param->{nodes};
 
-       my $password = extract_param($param, 'password')
-           if $type eq 'cifs' && $param->{username};
-
-       if ($param->{portal}) {
-           $param->{portal} = PVE::Storage::resolv_portal($param->{portal});
+       my $password;
+       # always extract pw, else it gets written to the www-data readable scfg
+       if (my $tmp_pw = extract_param($param, 'password')) {
+           if ($type eq 'cifs' && $param->{username}) {
+               $password = $tmp_pw;
+           } else {
+               warn "ignore password parameter\n";
+           }
        }
 
        my $plugin = PVE::Storage::Plugin->lookup($type);
@@ -165,43 +157,7 @@ __PACKAGE__->register_method ({
 
                $cfg->{ids}->{$storeid} = $opts;
 
-               if ($type eq 'lvm' && $opts->{base}) {
-
-                   my ($baseid, $volname) = PVE::Storage::parse_volume_id($opts->{base});
-
-                   my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1);
-                   die "base storage ID '$baseid' does not exist\n" if !$basecfg;
-       
-                   # we only support iscsi for now
-                   if (!($basecfg->{type} eq 'iscsi')) {
-                       die "unsupported base type '$basecfg->{type}'";
-                   }
-
-                   my $path = PVE::Storage::path($cfg, $opts->{base});
-
-                   PVE::Storage::activate_storage($cfg, $baseid);
-
-                   PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared});
-               } elsif ($type eq 'rbd' && !defined($opts->{monhost})) {
-                   my $ceph_admin_keyring = '/etc/pve/priv/ceph.client.admin.keyring';
-                   my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
-
-                   die "ceph authx keyring file for storage '$storeid' already exists!\n"
-                       if -e $ceph_storage_keyring;
-
-                   eval {
-                       mkdir '/etc/pve/priv/ceph';
-                       PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
-                   };
-                   if (my $err = $@) {
-                       unlink $ceph_storage_keyring;
-                       die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
-                   }
-               }
-               # create a password file in /etc/pve/priv,
-               # this file is used as a cert_file at mount time.
-               my $cred_file = &$set_cifs_credentials($password, $storeid)
-                   if defined($password);
+               $plugin->on_add_hook($storeid, $opts, password => $password);
 
                eval {
                    # try to activate if enabled on local node,
@@ -211,12 +167,13 @@ __PACKAGE__->register_method ({
                    }
                };
                if(my $err = $@) {
-                   unlink $cred_file if defined($cred_file);
+                   eval { $plugin->on_delete_hook($storeid, $opts) };
+                   warn "$@\n" if $@;
                    die $err;
                }
 
                PVE::Storage::write_config($cfg);
-           
+
            }, "create storage failed");
 
        return undef;
@@ -228,7 +185,7 @@ __PACKAGE__->register_method ({
     path => '{storage}',
     method => 'PUT',
     description => "Update storage configuration.",
-    permissions => { 
+    permissions => {
        check => ['perm', '/storage', ['Datastore.Allocate']],
     },
     parameters => PVE::Storage::Plugin->updateSchema(),
@@ -238,9 +195,9 @@ __PACKAGE__->register_method ({
 
        my $storeid = extract_param($param, 'storage');
        my $digest = extract_param($param, 'digest');
+       my $delete = extract_param($param, 'delete');
 
-        PVE::Storage::lock_storage_config(
-        sub {
+        PVE::Storage::lock_storage_config(sub {
 
            my $cfg = PVE::Storage::config();
 
@@ -251,13 +208,26 @@ __PACKAGE__->register_method ({
            my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
            my $opts = $plugin->check_config($storeid, $param, 0, 1);
 
-           foreach my $k (%$opts) {
+           if ($delete) {
+               my $options = $plugin->private()->{options}->{$scfg->{type}};
+               foreach my $k (PVE::Tools::split_list($delete)) {
+                   my $d = $options->{$k} || die "no such option '$k'\n";
+                   die "unable to delete required option '$k'\n" if !$d->{optional};
+                   die "unable to delete fixed option '$k'\n" if $d->{fixed};
+                   die "cannot set and delete property '$k' at the same time!\n"
+                       if defined($opts->{$k});
+
+                   delete $scfg->{$k};
+               }
+           }
+
+           for my $k (keys %$opts) {
                $scfg->{$k} = $opts->{$k};
            }
 
            PVE::Storage::write_config($cfg);
 
-           }, "update storage failed");
+       }, "update storage failed");
 
        return undef;
     }});
@@ -268,12 +238,12 @@ __PACKAGE__->register_method ({
     path => '{storage}', # /storage/config/{storage}
     method => 'DELETE',
     description => "Delete storage configuration.",
-    permissions => { 
+    permissions => {
        check => ['perm', '/storage', ['Datastore.Allocate']],
     },
     parameters => {
-       additionalProperties => 0,
-       properties => { 
+       additionalProperties => 0,
+       properties => {
            storage => get_standard_option('pve-storage-id', {
                 completion => \&PVE::Storage::complete_storage,
             }),
@@ -295,12 +265,9 @@ __PACKAGE__->register_method ({
                die "can't remove storage - storage is used as base of another storage\n"
                    if PVE::Storage::storage_is_used($cfg, $storeid);
 
-               if ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
-                   my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
-                   if (-f $ceph_storage_keyring) {
-                       unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";
-                   }
-               }
+               my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+
+               $plugin->on_delete_hook($storeid, $scfg);
 
                delete $cfg->{ids}->{$storeid};