/// <summary>
 /// Creates a new cloud service 
 /// </summary>
 /// <param name="location">the data centre location of the cloud service</param>
 /// <param name="description">The description of the cloud service</param>
 public void CreateNewCloudService(string location, string description = "Fluent Management created cloud service")
 {
     var hostedServiceCreate = new CreateHostedServiceCommand(Name, description, location)
     {
         Certificate = ManagementCertificate,
         SubscriptionId = SubscriptionId
     };
     hostedServiceCreate.Execute();
 }
 /// <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>
        /// 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 (!_manager.UseExistingHostedService)
            {
                var hostedServiceCreate = new CreateHostedServiceCommand(_manager.HostedServiceName,
                                                                         _manager.Description, _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);
                    }
                }
            }
        }