Exemplo n.º 1
0
        public override void ExecuteCmdlet()
        {
            try
            {
                string deviceid = StorSimpleClient.GetDeviceId(DeviceName);
                if (deviceid == null)
                {
                    WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
                    WriteObject(null);
                    return;
                }

                if (EncryptionEnabled == true && string.IsNullOrEmpty(EncryptionKey))
                {
                    throw new ArgumentNullException("EncryptionKey");
                }

                string encryptedKey = null;
                StorSimpleCryptoManager storSimpleCryptoManager = new StorSimpleCryptoManager(StorSimpleClient);
                if (EncryptionEnabled == true)
                {
                    WriteVerbose(Resources.EncryptionInProgressMessage);
                    storSimpleCryptoManager.EncryptSecretWithRakPub(EncryptionKey, out encryptedKey);
                }

                if (string.IsNullOrEmpty(PrimaryStorageAccountCredential.InstanceId))
                {
                    //The SAC needs to be created inline
                    WriteVerbose(Resources.InlineSacCreationMessage);

                    var sac = PrimaryStorageAccountCredential;

                    //validate storage account credentials
                    bool   storageAccountPresent;
                    string encryptedPassword;
                    string thumbprint;
                    string endpoint = GetEndpointFromHostname(sac.Hostname);
                    string location = GetStorageAccountLocation(sac.Name, out storageAccountPresent);
                    if (!storageAccountPresent ||
                        !ValidateAndEncryptStorageCred(sac.Name, sac.Password, endpoint, out encryptedPassword, out thumbprint))
                    {
                        return;
                    }

                    sac.Password = encryptedPassword;
                    sac.PasswordEncryptionCertThumbprint = thumbprint;
                    sac.Location = location;
                }

                var dc = new DataContainerRequest
                {
                    IsDefault                       = false,
                    Name                            = VolumeContainerName,
                    BandwidthRate                   = BandWidthRateInMbps,
                    IsEncryptionEnabled             = EncryptionEnabled ?? false,
                    EncryptionKey                   = encryptedKey,
                    VolumeCount                     = 0,
                    PrimaryStorageAccountCredential = PrimaryStorageAccountCredential,
                    SecretsEncryptionThumbprint     = storSimpleCryptoManager.GetSecretsEncryptionThumbprint()
                };

                if (WaitForComplete.IsPresent)
                {
                    var taskStatus = StorSimpleClient.CreateDataContainer(deviceid, dc);
                    HandleSyncTaskResponse(taskStatus, "create");
                    if (taskStatus.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
                    {
                        var createdDataContainer = StorSimpleClient.GetDataContainer(deviceid, VolumeContainerName);
                        WriteObject(createdDataContainer.DataContainerInfo);
                    }
                }

                else
                {
                    var taskstatus = StorSimpleClient.CreateDataContainerAsync(deviceid, dc);
                    HandleAsyncTaskResponse(taskstatus, "create");
                }
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }