Used to add a service certificate to a particular hosted service so that it can be used in remote desktop or SSL
Inheritance: Elastacloud.AzureManagement.Fluent.Commands.Services.ServiceCommand
 /// <summary>
 /// Used to create a hosted service with a service certificate if specified
 /// </summary>
 public void Create()
 {
     // create the hosted service here
     var service = new CreateHostedServiceCommand(_manager.HostedServiceName, _manager.Description,
                                                  _manager.Location)
                       {
                           Certificate = _manager.ManagementCertificate,
                           SubscriptionId = _manager.SubscriptionId
                       };
     service.Execute();
     if (_manager.ServiceCertificate == null)
         return;
     // first of all upload the service certificate
     byte[] export = _manager.ServiceCertificate.Certificate.Export(X509ContentType.Pkcs12,
                                                                    _manager.ServiceCertificate.PvkPassword);
     var command = new AddServiceCertificateCommand(export, _manager.ServiceCertificate.PvkPassword,
                                                    _manager.HostedServiceName)
                       {
                           Certificate = _manager.ManagementCertificate,
                           SubscriptionId = _manager.SubscriptionId
                       };
     command.Execute();
 }
        /// <summary>
        /// Uploads a certificate given a valid service certificate and password
        /// </summary>
        /// <param name="certificate">The certificate being uploaded</param>
        /// <param name="password">The .pfx password for the certificate</param>
        /// <param name="includePrivateKey">The .pfx password for the certificate</param>
        public void UploadServiceCertificate(X509Certificate2 certificate, string password = "", bool includePrivateKey = false)
        {
            var certBytes = includePrivateKey
                                ? certificate.Export(X509ContentType.Pkcs12, password)
                                : certificate.Export(X509ContentType.Cert);

            var cert = new AddServiceCertificateCommand(certBytes, password, Name)
            {
                SubscriptionId = SubscriptionId,
                Certificate = ManagementCertificate
            };
            cert.Execute();
        }
        /// <summary>
        /// Creates a deplopment based on the specific criteria specified in the DeploymentManager
        /// </summary>
        /// <param name="packageLocation">Where the package is left</param>
        private void CreateDeployment(string packageLocation)
        {
            bool deleted = false;

            if(!_blobClient.CheckStorageAccountHasResolved())
                throw new ApplicationException("unable to proceed storage account cannot be resolved after default 5 minute timeout");

            if (!_manager.UseExistingHostedService)
            {
                var hostedServiceCreate = new CreateCloudServiceCommand(_manager.HostedServiceName, _manager.Description ?? "Deployed by Fluent Management", _manager.Location)
                                              {
                                                  Certificate = _manager.ManagementCertificate,
                                                  SubscriptionId = _manager.SubscriptionId
                                              };
                hostedServiceCreate.Execute();
                _manager.WriteComplete(EventPoint.HostedServiceCreated, "Hosted service with name " + _manager.HostedServiceName + " created");
            }

            // send up service certificate - whatever happens we want the certificate up there - sometimes we may get a request but not need to alter the config of the SSL
            if (_manager.EnableSsl)
            {
                byte[] export = _manager.ServiceCertificate.Certificate.Export(X509ContentType.Pkcs12, _manager.ServiceCertificate.PvkPassword);

                var addCertificate =
                    new AddServiceCertificateCommand(export, _manager.ServiceCertificate.PvkPassword, _manager.HostedServiceName)
                        {
                            SubscriptionId = _manager.SubscriptionId,
                            Certificate = _manager.ManagementCertificate
                        };
                addCertificate.Execute();
            }

            // read in the enum value for the additional params
            bool startImmediately = true, treatErrorsAsWarnings = false;
            if (_manager.DeploymentParams.HasValue)
            {
                startImmediately = (_manager.DeploymentParams.Value & DeploymentParams.StartImmediately) == DeploymentParams.StartImmediately;
                treatErrorsAsWarnings = (_manager.DeploymentParams.Value & DeploymentParams.WarningsAsErrors) == DeploymentParams.WarningsAsErrors;
            }
            // read in the config and convert Base64
            var deployment = new CreateDeploymentCommand(_manager.HostedServiceName, _manager.DeploymentName,packageLocation,
                                                         _manager.Base64CsfgFile, _manager.DeploymentSlot, startImmediately, treatErrorsAsWarnings)
                                 {
                                     Certificate = _manager.ManagementCertificate,
                                     SubscriptionId = _manager.SubscriptionId
                                 };

            try
            {
                deployment.Execute();
                _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
            }
            catch (Exception)
            {
                DeleteDeployment();
                deleted = true;
            }
            finally
            {
                if (deleted)
                {
                    deployment.Execute();
                    _manager.WriteComplete(EventPoint.DeploymentCreated, "Deployment " + _manager.DeploymentName + " created");
                }
                // check here and execute on a timer to see if the role are ready and running
                if (_manager.WaitUntilAllRoleInstancesAreRunning)
                {
                    var command = new GetAggregateDeploymentStatusCommand(_manager.HostedServiceName, _manager.DeploymentSlot)
                                      {
                                          Certificate = _manager.ManagementCertificate,
                                          SubscriptionId = _manager.SubscriptionId
                                      };
                    while (!command.AllDeploymentNodesRunning)
                    {
                        command.Execute();
                        // TODO: put a 5 second timer in here for now but replace with a timeout and exception method if over a certain value
                        Thread.Sleep(5000);
                    }
                }
            }
        }
 /// <summary>
 /// Uploads a certificate given a valid service certificate and password
 /// </summary>
 /// <param name="certificate">The certificate being uploaded</param>
 /// <param name="password">The .pfx password for the certificate</param>
 public void UploadServiceCertificate(X509Certificate2 certificate, string password)
 {
     var certBytes = certificate.Export(X509ContentType.Pkcs12, password);
     var cert = new AddServiceCertificateCommand(certBytes, password, Name)
     {
         SubscriptionId = SubscriptionId,
         Certificate = ManagementCertificate
     };
     cert.Execute();
 }