public override void ExecuteCmdlet()
        {
            ConfirmAction(Force.IsPresent,
                          Resources.RemoveWarningACR,
                          Resources.RemoveConfirmationACR,
                          string.Empty,
                          () =>
                          {
                              try
                              {
                                  StorageAccountCredentialResponse existingSac = null;
                                  string sacName = null;

                                  switch (ParameterSetName)
                                  {
                                      case StorSimpleCmdletParameterSet.IdentifyByName:
                                          var allSACs = StorSimpleClient.GetAllStorageAccountCredentials();
                                          existingSac = allSACs.Where(x => x.Name.Equals(StorageAccountName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                                          sacName = StorageAccountName;
                                          break;
                                      case StorSimpleCmdletParameterSet.IdentifyByObject:
                                          existingSac = SAC;
                                          sacName = SAC.Name;
                                          break;
                                  }
                                  if (existingSac == null)
                                  {
                                      throw new ArgumentException(string.Format(Resources.SACNotFoundWithName, sacName));
                                  }

                                  var serviceConfig = new ServiceConfiguration()
                                  {
                                      AcrChangeList = new AcrChangeList(),
                                      CredentialChangeList = new SacChangeList()
                                      {
                                          Added = new List<StorageAccountCredential>(),
                                          Deleted = new[] {existingSac.InstanceId},
                                          Updated = new List<StorageAccountCredential>()
                                      }
                                  };

                                    if (WaitForComplete.IsPresent)
                                    {
                                        WriteVerbose("About to run a task to remove your Storage Access Credential!");
                                        var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                                        HandleSyncTaskResponse(taskStatus, "delete");
                                    }
                                    else
                                    {
                                        WriteVerbose("About to create a task to remove your Storage Access Credential!");
                                        var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                                        HandleAsyncTaskResponse(taskResponse, "delete");
                                    }
                              }
                              catch (Exception exception)
                              {
                                  this.HandleException(exception);
                              }
                          });
        }
 /// <summary>
 /// The Begin Creating Storage Account operation creates a new storage
 /// account in Azure.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IServiceConfigurationOperations.
 /// </param>
 /// <param name='serviceConfiguration'>
 /// Required. Parameters supplied to the Begin Creating Storage Account
 /// operation.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// This is the Task Response for all Async Calls
 /// </returns>
 public static TaskResponse BeginCreating(this IServiceConfigurationOperations operations, ServiceConfiguration serviceConfiguration, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IServiceConfigurationOperations)s).BeginCreatingAsync(serviceConfiguration, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        public override void ExecuteCmdlet()
        {
            try
            {

                var allACRs = StorSimpleClient.GetAllAccessControlRecords();
                var existingAcr = allACRs.Where(x => x.Name.Equals(ACRName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (existingAcr == null)
                {
                    WriteVerbose(string.Format(Resources.NotFoundMessageACR,ACRName));
                    return;
                }
                
                var serviceConfig = new ServiceConfiguration()
                {
                    AcrChangeList = new AcrChangeList()
                    {
                        Added = new List<AccessControlRecord>(),
                        Deleted = new List<string>(),
                        Updated = new []
                        {
                            new AccessControlRecord()
                            {
                                GlobalId = existingAcr.GlobalId,
                                InitiatorName = IQNInitiatorName,
                                InstanceId = existingAcr.InstanceId,
                                Name = existingAcr.Name,
                                VolumeCount = existingAcr.VolumeCount
                            },
                        }
                    },
                    CredentialChangeList = new SacChangeList(),
                };

                if (WaitForComplete.IsPresent)
                {
                    WriteVerbose("About to run a task to update your Access Control Record!"); 
                    var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                    HandleSyncTaskResponse(taskStatus, "update");
                    if (taskStatus.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
                    {
                        var updatedAcr = StorSimpleClient.GetAllAccessControlRecords()
                                            .Where(x => x.Name.Equals(ACRName, StringComparison.InvariantCultureIgnoreCase));
                        WriteObject(updatedAcr);
                    }
                }
                else
                {
                    WriteVerbose("About to create a task to update your Access Control Record!");
                    var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                    HandleAsyncTaskResponse(taskResponse, "update");
                }
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
        public override void ExecuteCmdlet()
        {
            try
            {
                var serviceConfig = new ServiceConfiguration()
                {
                    AcrChangeList = new AcrChangeList()
                    {
                        Added = new[]
                        {
                            new AccessControlRecord()
                            {
                                GlobalId = null,
                                InitiatorName = IQNInitiatorName,
                                InstanceId = null,
                                Name = ACRName,
                                VolumeCount = 0
                            },
                        },
                        Deleted = new List<string>(),
                        Updated = new List<AccessControlRecord>()
                    },
                    CredentialChangeList = new SacChangeList(),
                };

                if (WaitForComplete.IsPresent)
                {
                    var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                    HandleSyncTaskResponse(taskStatus, "create");
                    if (taskStatus.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
                    {
                        var createdAcr = StorSimpleClient.GetAllAccessControlRecords()
                                            .Where(x => x.Name.Equals(ACRName, StringComparison.InvariantCultureIgnoreCase));
                        WriteObject(createdAcr);
                    }
                }
                else
                {
                    var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                    HandleAsyncTaskResponse(taskResponse, "create");
                }
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
        public void CanAddACRTest()
        {
            using (var context = UndoContext.Current)
            {
                context.Start();

                var client = GetServiceClient<StorSimpleManagementClient>();

                var serviceConfig = new ServiceConfiguration()
                {
                    AcrChangeList = new AcrChangeList()
                    {
                        Added = new []
                        {
                            new AccessControlRecord()
                            {
                                GlobalId = null,
                                InitiatorName = TestUtilities.GenerateName("InitiatorName"),
                                InstanceId = null,
                                Name = TestUtilities.GenerateName("AcrName"),
                                VolumeCount = 0
                            },
                        },
                        Deleted = new List<string>(),
                        Updated = new List<AccessControlRecord>()
                    },
                    CredentialChangeList = new SacChangeList(),
                };

                CustomRequestHeaders hdrs = new CustomRequestHeaders();
                hdrs.ClientRequestId = Guid.NewGuid().ToString();
                hdrs.Language = "en-us";

                var taskStatus = client.ServiceConfig.Create(serviceConfig, hdrs);

                Assert.NotNull(taskStatus);
                Assert.True(taskStatus.Status == AsyncTaskStatus.Completed);
                Assert.True(taskStatus.Result == AsyncTaskResult.Succeeded);
            }
        }
        public override void ExecuteCmdlet()
        {
            ConfirmAction(Force.IsPresent,
                          Resources.RemoveWarningACR,
                          Resources.RemoveConfirmationACR,
                          string.Empty,
                          () =>
                          {
                              try
                              {
                                  AccessControlRecord existingAcr = null;
                                  string acrName = null;
                                  switch (ParameterSetName)
                                  {
                                      case StorSimpleCmdletParameterSet.IdentifyByName:
                                          var allACRs = StorSimpleClient.GetAllAccessControlRecords();
                                          existingAcr = allACRs.Where(x => x.Name.Equals(ACRName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                                          acrName = ACRName;
                                          break;
                                      case StorSimpleCmdletParameterSet.IdentifyByObject:
                                          existingAcr = ACR;
                                          acrName = ACR.Name;
                                          break;
                                  }
                                  if (existingAcr == null)
                                  {
                                      WriteObject(null);
                                      WriteVerbose(string.Format(Resources.NotFoundMessageACR, acrName));
                                      return;
                                  }
                                  
                                    var serviceConfig = new ServiceConfiguration()
                                    {
                                        AcrChangeList = new AcrChangeList()
                                        {
                                            Added = new List<AccessControlRecord>(),
                                            Deleted = new[] { existingAcr.InstanceId },
                                            Updated = new List<AccessControlRecord>()
                                        },
                                        CredentialChangeList = new SacChangeList(),
                                    };

                                    if (WaitForComplete.IsPresent)
                                    {
                                        WriteVerbose("About to run a task to remove your ACR!");
                                        var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                                        HandleSyncTaskResponse(taskStatus, "delete");
                                    }
                                    else
                                    {
                                        WriteVerbose("About to create a task to remove your ACR!");
                                        var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                                        HandleAsyncTaskResponse(taskResponse, "delete");
                                    }
                              }
                              catch (Exception exception)
                              {
                                  this.HandleException(exception);
                              }
                          });
        }
        public override void ExecuteCmdlet()
        {
            try
            {
                string endpoint = string.IsNullOrEmpty(Endpoint) ? StorSimpleConstants.DefaultStorageAccountEndpoint : Endpoint;
                
                //validate storage account credentials
                bool storageAccountPresent;
                string encryptedKey;
                string thumbprint;
                string location = GetStorageAccountLocation(StorageAccountName, out storageAccountPresent);
                if (!storageAccountPresent
                    || !ValidateAndEncryptStorageCred(StorageAccountName, StorageAccountKey, endpoint, out encryptedKey, out thumbprint))
                {
                    return;
                }

                var serviceConfig = new ServiceConfiguration()
                {
                    AcrChangeList = new AcrChangeList(),
                    CredentialChangeList = new SacChangeList()
                    {
                        Added = new[]
                        {
                            new StorageAccountCredential()
                            {
                                CloudType = CloudType.Azure,
                                Hostname = GetHostnameFromEndpoint(endpoint),
                                Login = StorageAccountName,
                                Password = encryptedKey,
                                PasswordEncryptionCertThumbprint = thumbprint,
                                UseSSL = UseSSL,
                                Name = StorageAccountName,
                                Location = location
                            },
                        },
                        Deleted = new List<string>(),
                        Updated = new List<StorageAccountCredential>()
                    }
                };

                if (WaitForComplete.IsPresent)
                {
                    var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                    HandleSyncTaskResponse(taskStatus, "create");
                    if (taskStatus.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
                    {
                        var createdSac = StorSimpleClient.GetAllStorageAccountCredentials()
                                            .Where(x => x.Name.Equals(StorageAccountName, StringComparison.InvariantCultureIgnoreCase));
                        WriteObject(createdSac);
                    }
                }
                else
                {
                    var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                    HandleAsyncTaskResponse(taskResponse, "create");
                }
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
        public override void ExecuteCmdlet()
        {
            try
            {

                var allSACs = StorSimpleClient.GetAllStorageAccountCredentials();
                var existingSac = allSACs.Where(x => x.Name.Equals(StorageAccountName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (existingSac == null)
                {
                    WriteVerbose(string.Format(Resources.SACNotFoundWithName,StorageAccountName));
                    return;
                }

                string encryptedKey;
                string thumbprint;
                string endpoint = GetEndpointFromHostname(existingSac.Hostname);
                if (!ValidateAndEncryptStorageCred(StorageAccountName, StorageAccountKey, endpoint, out encryptedKey, out thumbprint))
                {
                    return;
                }

                var serviceConfig = new ServiceConfiguration()
                {
                    AcrChangeList = new AcrChangeList(),
                    CredentialChangeList = new SacChangeList()
                    {
                        Added = new List<StorageAccountCredential>(),
                        Deleted = new List<string>(),
                        Updated = new[]
                        {
                            new StorageAccountCredential()
                            {
                                InstanceId = existingSac.InstanceId,
                                CloudType = existingSac.CloudType,
                                Hostname = existingSac.Hostname,
                                Login = existingSac.Login,
                                Password = encryptedKey ?? existingSac.Password,
                                UseSSL = UseSSL ?? existingSac.UseSSL,
                                VolumeCount = existingSac.VolumeCount,
                                Name = existingSac.Name,
                                IsDefault = existingSac.IsDefault,
                                PasswordEncryptionCertThumbprint = thumbprint,
                                Location = existingSac.Location
                            },
                        }
                    }
                };

                if (WaitForComplete.IsPresent)
                {
                    WriteVerbose("About to run a task to update your Storage Access credential!"); 
                    var taskStatus = StorSimpleClient.ConfigureService(serviceConfig);
                    HandleSyncTaskResponse(taskStatus, "update");
                    if (taskStatus.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
                    {
                        var updatedSac = StorSimpleClient.GetAllStorageAccountCredentials()
                                            .Where(x => x.Name.Equals(StorageAccountName, StringComparison.InvariantCultureIgnoreCase));
                        WriteObject(updatedSac);
                    }
                }
                else
                {
                    WriteVerbose("About to create a task to update your Storage Access credential!");
                    var taskResponse = StorSimpleClient.ConfigureServiceAsync(serviceConfig);
                    HandleAsyncTaskResponse(taskResponse, "update");
                }
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
 public TaskResponse ConfigureServiceAsync(ServiceConfiguration serviceConfig)
 {
     return GetStorSimpleClient().ServiceConfig.BeginCreating(serviceConfig, GetCustomRequestHeaders());
 }
 public TaskStatusInfo ConfigureService(ServiceConfiguration serviceConfig)
 {
     return GetStorSimpleClient().ServiceConfig.Create(serviceConfig, GetCustomRequestHeaders());
 }
 /// <summary>
 /// The Begin Creating Storage Account operation creates a new storage
 /// account in Azure.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IServiceConfigurationOperations.
 /// </param>
 /// <param name='serviceConfiguration'>
 /// Required. Parameters supplied to the Begin Creating Storage Account
 /// operation.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// This is the Task Response for all Async Calls
 /// </returns>
 public static Task<TaskResponse> BeginCreatingAsync(this IServiceConfigurationOperations operations, ServiceConfiguration serviceConfiguration, CustomRequestHeaders customRequestHeaders)
 {
     return operations.BeginCreatingAsync(serviceConfiguration, customRequestHeaders, CancellationToken.None);
 }
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IServiceConfigurationOperations.
 /// </param>
 /// <param name='serviceConfiguration'>
 /// Required. Parameters supplied to the Create Storage Account
 /// operation.
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// Info about the async task
 /// </returns>
 public static Task<TaskStatusInfo> CreateAsync(this IServiceConfigurationOperations operations, ServiceConfiguration serviceConfiguration, CustomRequestHeaders customRequestHeaders)
 {
     return operations.CreateAsync(serviceConfiguration, customRequestHeaders, CancellationToken.None);
 }