/// <summary>
 /// Creates a Cloud services
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RecoveryServices.ICloudServiceOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The cloud service name.
 /// </param>
 /// <param name='cloudService'>
 /// Required. Parameters supplied to the Create cloud service operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static AzureOperationResponse BeginCreating(this ICloudServiceOperations operations, string cloudServiceName, CloudServiceCreateArgs cloudService)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((ICloudServiceOperations)s).BeginCreatingAsync(cloudServiceName, cloudService);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// Method to Either find or create the cloud service.
        /// </summary>
        /// <param name="cloudServiceName">name of the cloud service to be created</param>
        /// <param name="cloudServiceInput">cloud service input to create the service.</param>
        public void FindOrCreateCloudService(string cloudServiceName, CloudServiceCreateArgs cloudServiceInput)
        {
            bool cloudServicePresent = this.DoesCloudServiceExits(cloudServiceName);

            if (!cloudServicePresent)
            {
                this.GetRecoveryServicesClient.CloudServices.Create(cloudServiceName, cloudServiceInput);
            }
        }
        /// <summary>
        /// ProcessRecord of the command.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            try
            {
                this.WriteWarningWithTimestamp(
                    string.Format(
                        Properties.Resources.CmdletWillBeDeprecatedSoon,
                        this.MyInvocation.MyCommand.Name));

                string cloudServiceName = Utilities.GenerateCloudServiceName(this.Location);
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(cloudServiceName);
                string base64Label = Convert.ToBase64String(bytes);

                CloudServiceCreateArgs cloudServiceCreateArgs = new CloudServiceCreateArgs()
                {
                    GeoRegion = this.Location,
                    Label = base64Label,
                    Description = base64Label
                };

                RecoveryServicesClient.FindOrCreateCloudService(cloudServiceName, cloudServiceCreateArgs);

                VaultCreateArgs vaultCreateArgs = new VaultCreateArgs()
                {
                    Name = this.Name,
                    Plan = string.Empty,
                    ResourceProviderNamespace = Constants.ResourceNamespace,
                    Type = Constants.ASRVaultType,
                    ETag = Guid.NewGuid().ToString(),
                    SchemaVersion = Constants.RpSchemaVersion
                };

                RecoveryServicesOperationStatusResponse response = RecoveryServicesClient.CreateVault(cloudServiceName, this.Name, vaultCreateArgs);

                VaultOperationOutput output = new VaultOperationOutput()
                {
                    Response = response.StatusCode == HttpStatusCode.OK ? Resources.VaultCreationSuccessMessage : response.StatusCode.ToString()
                };

                this.WriteObject(output, true);
            }
            catch (Exception exception)
            {
                this.HandleException(exception);
            }
        }
 /// <summary>
 /// Creates a Cloud services
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RecoveryServices.ICloudServiceOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The cloud service name.
 /// </param>
 /// <param name='cloudService'>
 /// Required. Parameters supplied to the Create cloud service operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> BeginCreatingAsync(this ICloudServiceOperations operations, string cloudServiceName, CloudServiceCreateArgs cloudService)
 {
     return operations.BeginCreatingAsync(cloudServiceName, cloudService, CancellationToken.None);
 }
 /// <summary>
 /// Create a cloud service.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.RecoveryServices.ICloudServiceOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The cloud service name.
 /// </param>
 /// <param name='cloudService'>
 /// Required. Parameters supplied to the Create cloud service operation.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself.  If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request.  If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request, and also includes error
 /// information regarding the failure.
 /// </returns>
 public static Task<RecoveryServicesOperationStatusResponse> CreateAsync(this ICloudServiceOperations operations, string cloudServiceName, CloudServiceCreateArgs cloudService)
 {
     return operations.CreateAsync(cloudServiceName, cloudService, CancellationToken.None);
 }