Inheritance: IAzureManagementLowLevelApi
        public void ThrowsOnAnythingOtherThanAccepted()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.ServiceUnavailable, ""));

            var api = new AzureManagementLowLevelApi(http);
            Assert.That(() => api.BeginSuspend(TestDeploymentUri), Throws.TypeOf<UnhandledHttpException>());
        }
        public void Http409ConflictReturnsNull()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Conflict, ""));

            var api = new AzureManagementLowLevelApi(http);
            Assert.That(api.BeginSuspend(TestDeploymentUri), Is.Null, "RequestUri should be null on 409 conflict");
        }
        public void Http409ConflictReturnsNull()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Conflict, ""));

            var api = new AzureManagementLowLevelApi(http);
            var config = MockRepository.GenerateStub<IDeploymentConfiguration>();
            Assert.That(api.BeginCreate(TestDeploymentUri, config), Is.Null, "Should return null on 409 conflict");
        }
        public void SendsCorrectDeleteUri()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Accepted, ""));

            var api = new AzureManagementLowLevelApi(http);
            api.BeginDelete(TestDeploymentUri);

            Assert.That(http.LastDeleteUri, Is.EqualTo(TestDeploymentUri.ToString()), "deploymentUri parameter incorrect");
        }
        public void ReturnsRunningTransitioningIfRunningTransitioning()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.OK, "<xml><Status>RunningTransitioning</Status></xml>"));

            var api = new AzureManagementLowLevelApi(http);
            var status = api.CheckDeploymentStatus(TestDeploymentUri);

            Assert.That(status, Is.EqualTo(AzureDeploymentCheckOutcome.RunningTransitioning));
        }
        public void ReturnsNotFoundIfStatusCode404()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.NotFound, ""));

            var api = new AzureManagementLowLevelApi(http);
            var status = api.CheckDeploymentStatus(TestDeploymentUri);

            Assert.That(status, Is.EqualTo(AzureDeploymentCheckOutcome.NotFound));
        }
        public void ReturnsFailedOnWebExceptionsNotHandledByHttpGet()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => { throw new UnhandledHttpException(); });

            var api = new AzureManagementLowLevelApi(http);
            var status = api.CheckDeploymentStatus(TestDeploymentUri);

            Assert.That(status, Is.EqualTo(AzureDeploymentCheckOutcome.Failed));
        }
        public void CheckRequestStatus_Succeeded()
        {
            var http = new ScriptedHttpFake();
            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Accepted, "<Status>Succeeded</Status>"));
            var api = new AzureManagementLowLevelApi(http);

            var requestStatus = api.CheckRequestStatus(TestUrl);

            Assert.That(requestStatus, Is.EqualTo(AzureRequestStatus.Succeeded), "request status");
            Assert.That(http.LastGetUri, Is.EqualTo("https://management.core.windows.net/subId/operations/requestId"), "operation URI");
        }
        public void ThrowsOnAnythingOtherThanAcceptedAndConflict()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.InternalServerError, ""));

            var api = new AzureManagementLowLevelApi(http);
            var config = MockRepository.GenerateStub<IDeploymentConfiguration>();
            Assert.That(
                () => api.BeginCreate(TestDeploymentUri, config),
                Throws.TypeOf<UnhandledHttpException>());
        }
        public void SendsCorrectPostArguments()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Accepted, ""));

            var api = new AzureManagementLowLevelApi(http);
            api.BeginSuspend(TestDeploymentUri);

            Assert.That(http.LastPostUri, Is.EqualTo(TestDeploymentUri + "/?comp=status"), "deploymentUri parameter incorrect");
            Assert.That(http.LastPostContent, Contains.Substring("<UpdateDeploymentStatus "), "is an updateDeploymentStatus request");
            Assert.That(http.LastPostContent, Contains.Substring("<Status>Suspended</Status>"), "has the appropriate status");
        }
        public void SendsCorrectArguments()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.Accepted, "", "requestId"));

            var api = new AzureManagementLowLevelApi(http);

            var config = MockRepository.GenerateStub<IDeploymentConfiguration>();
            config.Stub(x => x.MakeCreateDeploymentMessage()).Return("foo");
            var requestUrl = api.BeginCreate(TestDeploymentUri, config);

            Assert.That(http.LastPostUri, Is.EqualTo(TestDeploymentUri.ToString()), "deploymentUri parameter incorrect");
            Assert.That(http.LastPostContent, Is.EqualTo("foo"), "expected xml was passed");

            var expectedRequestUri = TestDeploymentUri.ToRequestUri("requestId");
            Assert.That(requestUrl, Is.EqualTo(expectedRequestUri));
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: mg-alfa-dev/DeployToAzure
        static int Main(string[] args)
        {
            var consoleTraceListener = new OurConsoleTraceListener();
            OurTrace.AddListener(consoleTraceListener);
            OurTrace.Source.Switch.Level = SourceLevels.All;

            if(args.Length < 1)
            {
                Usage();
                return 0;
            }

            var tryToUseUpgradeDeployment = false;
            var fallbackToReplaceDeployment = false;
            var doNotRedeploy = false;
            if (args.Length > 1)
            {
                if (args.Contains("--try-to-use-upgrade-deployment"))
                    tryToUseUpgradeDeployment = true;
                if (args.Contains("--fallback-to-replace-deployment"))
                    fallbackToReplaceDeployment = true;
                if (args.Contains("--delete"))
                    doNotRedeploy = true;
            }

            var configuration = ConfigurationParser.ParseConfiguration(args[0]);
            var certificate = new X509Certificate2(configuration.CertFileName, configuration.CertPassword);
            var http = new Http(certificate);
            var azureDeploymentDeploymentLowLevelApi = new AzureManagementLowLevelApi(http);
            var managementApiWithRetries = new AzureManagementApiWithRetries(azureDeploymentDeploymentLowLevelApi, configuration.MaxRetries, TimeSpan.FromSeconds(configuration.RetryIntervalInSeconds));

            try
            {
                var deploymentSlotManager = new AzureDeploymentSlot(managementApiWithRetries, configuration.DeploymentSlotUri);
                if (doNotRedeploy)
                {
                    deploymentSlotManager.DeleteDeployment();
                    return 0;
                }

                var subscriptionId = configuration.DeploymentSlotUri.SubscriptionId;
                if (configuration.StorageAccountKey == null || configuration.StorageAccountName == null)
                {
                    OurTrace.TraceInfo("Attempting to guess account name and key based on certificate.");

                    if (string.IsNullOrWhiteSpace(configuration.StorageAccountName))
                    {
                        OurTrace.TraceInfo("Looking up storage accounts for the subscription.");
                        var storageAccounts = azureDeploymentDeploymentLowLevelApi.ListStorageAccounts(subscriptionId);
                        configuration.StorageAccountName = storageAccounts.FirstOrDefault();

                        if (string.IsNullOrWhiteSpace(configuration.StorageAccountName))
                        {
                            OurTrace.TraceError("Couldn't find any suitable storage accounts.");
                            throw new InvalidOperationException("No suitable storage accounts.");
                        }

                        if (!string.IsNullOrWhiteSpace(configuration.BlobPathToDeploy))
                        {
                            // don't allow BlobPathToDeploy if we're guessing the storage account.
                            OurTrace.TraceInfo("Ignoring BlobPathToDeploy because we're guessing the storage account.");
                            configuration.BlobPathToDeploy = null;
                        }
                    }

                    if (string.IsNullOrWhiteSpace(configuration.StorageAccountKey))
                    {
                        OurTrace.TraceInfo(string.Format("Looking up storage keys for account: {0}", configuration.StorageAccountName));
                        var storageKeys = azureDeploymentDeploymentLowLevelApi.GetStorageAccountKeys(subscriptionId, configuration.StorageAccountName);

                        configuration.StorageAccountKey = storageKeys.FirstOrDefault();

                        if (string.IsNullOrWhiteSpace(configuration.StorageAccountKey))
                        {
                            OurTrace.TraceError(string.Format("Couldn't find any keys for storage account: {0}", configuration.StorageAccountName));
                            throw new InvalidOperationException("No suitable storage account keys.");
                        }
                    }
                }

                var csPkg = configuration.PackageFileName;
                if (!string.IsNullOrWhiteSpace(configuration.ChangeVMSize))
                {
                    csPkg = Path.GetTempFileName();
                    File.Copy(configuration.PackageFileName, csPkg, true);
                    ChangeVmSize(csPkg, configuration.ChangeVMSize);
                }

                UploadBlob(csPkg, configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);
                if(!string.IsNullOrWhiteSpace(configuration.BlobPathToDeploy))
                    DeployBlobs(configuration.BlobPathToDeploy, configuration.StorageAccountName, configuration.StorageAccountKey);

                if (tryToUseUpgradeDeployment && managementApiWithRetries.DoesDeploymentExist(configuration.DeploymentSlotUri))
                {
                    try
                    {
                        deploymentSlotManager.UpgradeDeployment(configuration);
                    }
                    catch(BadRequestException ex)
                    {
                        OurTrace.TraceError(string.Format("Upgrade failed with message: {0}\r\n, **** {1}", ex, fallbackToReplaceDeployment ? "falling back to replace." : "exiting."));
                        // retry using CreateOrReplaceDeployment, since we might have tried to do something that isn't allowed with UpgradeDeployment.
                        if (fallbackToReplaceDeployment)
                            deploymentSlotManager.CreateOrReplaceDeployment(configuration);
                        else
                            throw;
                    }
                }
                else
                {
                    deploymentSlotManager.CreateOrReplaceDeployment(configuration);
                }

                DeleteBlob(configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);
            }
            catch(Exception ex)
            {
                OurTrace.TraceError(string.Format("exception!\n{0}", ex));
                return -1;
            }
            return 0;
        }
コード例 #13
0
ファイル: Program.cs プロジェクト: endjin/DeployToAzure
        static int Main(string[] args)
        {
            var consoleTraceListener = new OurConsoleTraceListener();
            OurTrace.AddListener(consoleTraceListener);
            OurTrace.Source.Switch.Level = SourceLevels.All;

            if(args.Length < 1)
            {
                Usage();
                return 0;
            }

            var tryToUseUpgradeDeployment = false;
            var fallbackToReplaceDeployment = false;
            var doNotRedeploy = false;
            if (args.Length > 1)
            {
                if (args.Contains("--try-to-use-upgrade-deployment"))
                    tryToUseUpgradeDeployment = true;
                if (args.Contains("--fallback-to-replace-deployment"))
                    fallbackToReplaceDeployment = true;
                if (args.Contains("--delete"))
                    doNotRedeploy = true;
            }

            var configuration = ConfigurationParser.ParseConfiguration(args[0]);
            var certificate = new X509Certificate2(configuration.CertFileName, configuration.CertPassword);
            var http = new Http(certificate);
            var azureDeploymentDeploymentLowLevelApi = new AzureManagementLowLevelApi(http);
            var managementApiWithRetries = new AzureManagementApiWithRetries(azureDeploymentDeploymentLowLevelApi, configuration.MaxRetries, TimeSpan.FromSeconds(configuration.RetryIntervalInSeconds));

            try
            {
                var deploymentSlotManager = new AzureDeploymentSlot(managementApiWithRetries, configuration.DeploymentSlotUri);
                if (doNotRedeploy)
                {
                    deploymentSlotManager.DeleteDeployment();
                    return 0;
                }

                UploadBlob(configuration.PackageFileName, configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);

                if (tryToUseUpgradeDeployment && managementApiWithRetries.DoesDeploymentExist(configuration.DeploymentSlotUri))
                {
                    try
                    {
                        deploymentSlotManager.UpgradeDeployment(configuration);
                    }
                    catch(BadRequestException ex)
                    {
                        OurTrace.TraceError(string.Format("Upgrade failed with message: {0}\r\n, **** {1}", ex, fallbackToReplaceDeployment ? "falling back to replace." : "exiting."));
                        // retry using CreateOrReplaceDeployment, since we might have tried to do something that isn't allowed with UpgradeDeployment.
                        if (fallbackToReplaceDeployment)
                            deploymentSlotManager.CreateOrReplaceDeployment(configuration);
                        else
                            throw;
                    }
                }
                else
                {
                    deploymentSlotManager.CreateOrReplaceDeployment(configuration);
                }

                DeleteBlob(configuration.PackageUrl, configuration.StorageAccountName, configuration.StorageAccountKey);
            }
            catch(Exception ex)
            {
                OurTrace.TraceError(string.Format("exception!\n{0}", ex));
                return -1;
            }
            return 0;
        }
        public void SendsCorrectUri()
        {
            var http = new ScriptedHttpFake();

            http.Script.Add(() => http.NextResponse = new HttpResponse(HttpStatusCode.OK, "<xml><Status>Suspended</Status></xml>"));

            var api = new AzureManagementLowLevelApi(http);
            api.CheckDeploymentStatus(TestDeploymentUri);

            Assert.That(http.LastGetUri, Is.EqualTo(TestDeploymentUri.ToString()));
        }