/// <summary>
 /// The Update Deployment Status By Deployment Slot operation initiates
 /// a change in the running status of a deployment. The status of a
 /// deployment can be running or suspended. This operation is an
 /// asynchronous operation. To determine whether the Management
 /// service has finished processing the request, call Get Operation
 /// Status. For more information on asynchronous operations, see
 /// Tracking Asynchronous Service Management Requests at
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460791.aspx.
 /// (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IDeploymentOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The cloud service to swap deployments for.
 /// </param>
 /// <param name='deploymentSlot'>
 /// Required. The deployment slot.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Update Deployment Status By
 /// Deployment Slot 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 error information regarding
 /// the failure.
 /// </returns>
 public static Task<OperationStatusResponse> UpdateStatusByDeploymentSlotAsync(this IDeploymentOperations operations, string serviceName, DeploymentSlot deploymentSlot, DeploymentUpdateStatusParameters parameters)
 {
     return operations.UpdateStatusByDeploymentSlotAsync(serviceName, deploymentSlot, parameters, CancellationToken.None);
 }
        public void ExecuteCommand()
        {
            string configString = string.Empty;
            if (!string.IsNullOrEmpty(Configuration))
            {
                configString = GeneralUtilities.GetConfiguration(Configuration);
            }

            ExtensionConfiguration extConfig = null;
            if (ExtensionConfiguration != null)
            {
                string errorConfigInput = null;
                if (!ExtensionManager.Validate(ExtensionConfiguration, out errorConfigInput))
                {
                    throw new Exception(string.Format(Resources.ServiceExtensionCannotApplyExtensionsInSameType, errorConfigInput));
                }

                foreach (ExtensionConfigurationInput context in ExtensionConfiguration)
                {
                    if (context != null && context.X509Certificate != null)
                    {
                        ExecuteClientActionNewSM(
                            null,
                            string.Format(Resources.ServiceExtensionUploadingCertificate, CommandRuntime, context.X509Certificate.Thumbprint),
                            () => this.ComputeClient.ServiceCertificates.Create(this.ServiceName, CertUtilsNewSM.Create(context.X509Certificate)));
                    }
                }

                var slotType = (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), this.Slot, true);
                DeploymentGetResponse d = null;
                InvokeInOperationContext(() =>
                {
                    try
                    {
                        d = this.ComputeClient.Deployments.GetBySlot(this.ServiceName, slotType);
                    }
                    catch (CloudException ex)
                    {
                        if (ex.Response.StatusCode != HttpStatusCode.NotFound && IsVerbose() == false)
                        {
                            this.WriteExceptionDetails(ex);
                        }
                    }
                });

                ExtensionManager extensionMgr = new ExtensionManager(this, ServiceName);
                extConfig = extensionMgr.Add(d, ExtensionConfiguration, this.Slot);
            }

            // Upgrade Parameter Set
            if (string.Compare(ParameterSetName, "Upgrade", StringComparison.OrdinalIgnoreCase) == 0)
            {
                bool removePackage = false;
                var storageName = CurrentContext.Subscription.GetProperty(AzureSubscription.Property.StorageAccount);

                Uri packageUrl = null;
                if (Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||
                    Package.StartsWith(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
                {
                    packageUrl = new Uri(Package);
                }
                else
                {
                    if (string.IsNullOrEmpty(storageName))
                    {
                        throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
                    }

                    var progress = new ProgressRecord(0, Resources.WaitForUploadingPackage, Resources.UploadingPackage);
                    WriteProgress(progress);
                    removePackage = true;
                    InvokeInOperationContext(() => packageUrl = RetryCall(s => AzureBlob.UploadPackageToBlob(this.StorageClient, storageName, Package, null)));
                }

                DeploymentUpgradeMode upgradeMode;
                if (!Enum.TryParse<DeploymentUpgradeMode>(Mode, out upgradeMode))
                {
                    upgradeMode = DeploymentUpgradeMode.Auto;
                }

                var upgradeDeploymentInput = new DeploymentUpgradeParameters
                {
                    Mode = upgradeMode,
                    Configuration = configString,
                    ExtensionConfiguration = extConfig,
                    PackageUri = packageUrl,
                    Label = Label ?? ServiceName,
                    Force = Force.IsPresent
                };

                if (!string.IsNullOrEmpty(RoleName))
                {
                    upgradeDeploymentInput.RoleToUpgrade = RoleName;
                }

                InvokeInOperationContext(() =>
                {
                    try
                    {
                        ExecuteClientActionNewSM(
                            upgradeDeploymentInput,
                            CommandRuntime.ToString(),
                            () => this.ComputeClient.Deployments.UpgradeBySlot(
                                this.ServiceName,
                                (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), this.Slot, true),
                                upgradeDeploymentInput));

                        if (removePackage == true)
                        {
                            this.RetryCall(s =>
                            AzureBlob.DeletePackageFromBlob(
                                    this.StorageClient,
                                    storageName,
                                    packageUrl));
                        }
                    }
                    catch (CloudException ex)
                    {
                        this.WriteExceptionDetails(ex);
                    }
                });
            }
            else if (string.Compare(this.ParameterSetName, "Config", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Config parameter set
                var changeDeploymentStatusParams = new DeploymentChangeConfigurationParameters
                {
                    Configuration = configString,
                    ExtensionConfiguration = extConfig
                };

                ExecuteClientActionNewSM(
                    changeDeploymentStatusParams,
                    CommandRuntime.ToString(),
                    () => this.ComputeClient.Deployments.ChangeConfigurationBySlot(
                        this.ServiceName,
                        (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), this.Slot, true),
                        changeDeploymentStatusParams));
            }
            else
            {
                // Status parameter set
                var updateDeploymentStatusParams = new DeploymentUpdateStatusParameters
                {
                    Status = (UpdatedDeploymentStatus)Enum.Parse(typeof(UpdatedDeploymentStatus), this.NewStatus, true)
                };

                ExecuteClientActionNewSM(
                    null,
                    CommandRuntime.ToString(),
                    () => this.ComputeClient.Deployments.UpdateStatusByDeploymentSlot(
                    this.ServiceName,
                    (DeploymentSlot)Enum.Parse(typeof(DeploymentSlot), this.Slot, true),
                    updateDeploymentStatusParams));
            }
        }
 /// <summary>
 /// The Update Deployment Status By Deployment Slot operation initiates
 /// a change in the running status of a deployment. The status of a
 /// deployment can be running or suspended. This operation is an
 /// asynchronous operation. To determine whether the Management
 /// service has finished processing the request, call Get Operation
 /// Status. For more information on asynchronous operations, see
 /// Tracking Asynchronous Service Management Requests at
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460791.aspx.
 /// (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IDeploymentOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The cloud service to swap deployments for.
 /// </param>
 /// <param name='deploymentSlot'>
 /// Required. The deployment slot.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Update Deployment Status By
 /// Deployment Slot 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 error information regarding
 /// the failure.
 /// </returns>
 public static OperationStatusResponse UpdateStatusByDeploymentSlot(this IDeploymentOperations operations, string serviceName, DeploymentSlot deploymentSlot, DeploymentUpdateStatusParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDeploymentOperations)s).UpdateStatusByDeploymentSlotAsync(serviceName, deploymentSlot, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// The Begin Updating Deployment Status By Deployment Name operation
 /// initiates a change in the running status of a deployment. The
 /// status of a deployment can be running or suspended. This operation
 /// is an asynchronous operation. To determine whether the Management
 /// service has finished processing the request, call Get Operation
 /// Status. For more information on asynchronous operations, see
 /// Tracking Asynchronous Service Management Requests at
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460791.aspx.
 /// (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IDeploymentOperations.
 /// </param>
 /// <param name='serviceName'>
 /// Required. The cloud service to swap deployments for.
 /// </param>
 /// <param name='deploymentName'>
 /// Required. The name of your deployment.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Begin Updating Deployment
 /// Status By Deployment Name operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<AzureOperationResponse> BeginUpdatingStatusByDeploymentNameAsync(this IDeploymentOperations operations, string serviceName, string deploymentName, DeploymentUpdateStatusParameters parameters)
 {
     return operations.BeginUpdatingStatusByDeploymentNameAsync(serviceName, deploymentName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// The Update Deployment Status operation initiates a change in the
 /// running status of a deployment. The status of a deployment can be
 /// running or suspended.  The Update Deployment Status operation is
 /// an asynchronous operation. To determine whether the Management
 /// service has finished processing the request, call Get Operation
 /// Status. For more information on asynchronous operations, see
 /// Tracking Asynchronous Service Management Requests.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IDeploymentOperations.
 /// </param>
 /// <param name='serviceName'>
 /// The cloud service to swap deployments for.
 /// </param>
 /// <param name='deploymentSlot'>
 /// The deployment slot.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the Update Deployment Status operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse BeginUpdatingStatusByDeploymentSlot(this IDeploymentOperations operations, string serviceName, DeploymentSlot deploymentSlot, DeploymentUpdateStatusParameters parameters)
 {
     try
     {
         return operations.BeginUpdatingStatusByDeploymentSlotAsync(serviceName, deploymentSlot, parameters).Result;
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
 /// <summary>
 /// The Update Deployment Status operation initiates a change in the
 /// running status of a deployment. The status of a deployment can be
 /// running or suspended.  The Update Deployment Status operation is
 /// an asynchronous operation. To determine whether the Management
 /// service has finished processing the request, call Get Operation
 /// Status. For more information on asynchronous operations, see
 /// Tracking Asynchronous Service Management Requests.  (see
 /// http://msdn.microsoft.com/en-us/library/windowsazure/ee460808.aspx
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Compute.IDeploymentOperations.
 /// </param>
 /// <param name='serviceName'>
 /// The cloud service to swap deployments for.
 /// </param>
 /// <param name='deploymentName'>
 /// The name of your deployment.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the Update Deployment Status 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 ComputeOperationStatusResponse UpdateStatusByDeploymentName(this IDeploymentOperations operations, string serviceName, string deploymentName, DeploymentUpdateStatusParameters parameters)
 {
     try
     {
         return operations.UpdateStatusByDeploymentNameAsync(serviceName, deploymentName, parameters).Result;
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
        protected PSArgument[] CreateDeploymentUpdateStatusByDeploymentSlotParameters()
        {
            string serviceName = string.Empty;
            DeploymentSlot deploymentSlot = new DeploymentSlot();
            DeploymentUpdateStatusParameters parameters = new DeploymentUpdateStatusParameters();

            return ConvertFromObjectsToArguments(new string[] { "ServiceName", "DeploymentSlot", "Parameters" }, new object[] { serviceName, deploymentSlot, parameters });
        }