public async Task<Microsoft.Azure.Management.Resources.Models.DeploymentExtended> CreateDeployment(
                        [Metadata("Id", "The Id of the resource group to deploy to.")]string resourceGroupId,
                        [Metadata("Parameters", "The parameters for the template.")]string parameters,
                        [Metadata("Template", "The deployment template to deploy.")]string template,
                        [Metadata("Deployment name", "A custom name for the deployment.", VisibilityType.Advanced)]string deploymentName = null
            )
        {
            var client = await ResourceUtilities.GetClient().ConfigureAwait(continueOnCapturedContext: false);

            var deployment = new Deployment()
            {
                Properties = new DeploymentProperties()
                {
                    Mode = DeploymentMode.Incremental,
                    Parameters = parameters,
                    Template = template
                }
            };

            if (deploymentName == null)
            {
                deploymentName = "AzureResourceConnector-" + Guid.NewGuid().ToString("n");
            }

            var result = await client.Deployments.CreateOrUpdateAsync(ResourceUtilities.GetResourceGroupFromId(resourceGroupId), deploymentName, deployment, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);

            return result.Deployment;
        }
Exemple #2
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken     = request.DataStore.GetJson("AzureToken")["access_token"].ToString();
            var subscription   = request.DataStore.GetJson("SelectedSubscription")["SubscriptionId"].ToString();
            var resourceGroup  = request.DataStore.GetValue("SelectedResourceGroup");
            var deploymentName = request.DataStore.GetValue("DeploymentName") ?? "AzureMLDeployment";

            var workspaceName      = request.DataStore.GetValue("WorkspaceName");
            var storageAccountName = request.DataStore.GetValue("StorageAccountName");
            var planName           = request.DataStore.GetValue("PlanName") ?? "azuremlplan";
            var skuName            = request.DataStore.GetValue("SkuName") ?? "S1";
            var skuTier            = request.DataStore.GetValue("SkuTier") ?? "Standard";
            var skuCapacity        = request.DataStore.GetValue("SkuCapacity") ?? "1";

            // Get email address

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("name", workspaceName);
            param.AddStringParam("resourcegroup", resourceGroup);
            param.AddStringParam("subscription", subscription);
            param.AddStringParam("newStorageAccountName", storageAccountName);
            param.AddStringParam("planName", planName);
            param.AddStringParam("skuName", skuName);
            param.AddStringParam("skuTier", skuTier);
            param.AddStringParam("skuCapacity", skuCapacity);
            param.AddStringParam("ownerEmail", AzureUtility.GetEmailFromToken(request.DataStore.GetJson("AzureToken")));

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/azureml.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            return(new ActionResponse(ActionStatus.Success));
        }
Exemple #3
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            var location      = request.DataStore.GetJson("SelectedLocation", "Name");

            var deploymentName         = request.DataStore.GetValue("DeploymentName");
            var functionAppHostingPlan = request.DataStore.GetValue("functionAppHostingPlan");
            var sitename = request.DataStore.GetValue("SiteName");

            var search = request.DataStore.GetValue("SearchQuery");
            var logicAppNameHistorical = request.DataStore.GetValue("LogicAppNameHistorical");

            search = search.StartsWith("@") ? "@" + search : search;

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("sitename", sitename);
            param.AddStringParam("resourcegroup", resourceGroup);
            param.AddStringParam("subscription", subscription);
            param.AddStringParam("search", search);
            param.AddStringParam("LogicAppName", logicAppNameHistorical);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/AzureArm/logicappHistorical.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, validate, null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            //Log logic app
            request.Logger.LogResource(request.DataStore, logicAppNameHistorical,
                                       DeployedResourceType.LogicApp, CreatedBy.BPST, DateTime.UtcNow.ToString("o"));

            return(new ActionResponse(ActionStatus.Success, deploymentItem));
        }
Exemple #4
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            var location      = request.DataStore.GetJson("SelectedLocation", "Name");

            var deploymentName    = request.DataStore.GetValue("DeploymentName");
            var name              = request.DataStore.GetValue("StorageAccountName");
            var accountType       = request.DataStore.GetValue("StorageAccountType");
            var encryptionEnabled = request.DataStore.GetValue("StorageAccountEncryptionEnabled");

            string storageAccountName = "solutiontemplate" + Path.GetRandomFileName().Replace(".", "").Substring(0, 8);

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("encryptionEnabled", encryptionEnabled);
            param.AddStringParam("accountType", accountType);
            param.AddStringParam("location", location);
            param.AddStringParam("name", name);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/storageAccount.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            request.Logger.LogResource(request.DataStore, name,
                                       DeployedResourceType.StorageAccount, CreatedBy.BPST, DateTime.UtcNow.ToString("o"), string.Empty, accountType);
            return(new ActionResponse(ActionStatus.Success, deploymentItem));
        }
Exemple #5
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var cognitiveServiceKey = request.DataStore.GetValue("CognitiveServiceKey");

            if (cognitiveServiceKey == "")
            {
                var azureToken    = request.DataStore.GetJson("AzureToken")["access_token"].ToString();
                var subscription  = request.DataStore.GetJson("SelectedSubscription")["SubscriptionId"].ToString();
                var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
                var location      = request.DataStore.GetJson("SelectedLocation")["Name"].ToString();

                var deploymentName       = request.DataStore.GetValue("DeploymentName");
                var cognitiveServiceName = request.DataStore.GetValue("CognitiveServiceName");
                var skuName = request.DataStore.GetValue("CognitiveSkuName");

                var param = new AzureArmParameterGenerator();
                param.AddStringParam("CognitiveServiceName", cognitiveServiceName);
                param.AddStringParam("skuName", skuName);


                SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);
                Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);
                var registeration = await client.Providers.RegisterAsync("Microsoft.CognitiveServices");

                var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/AzureArm/sentimentCognitiveService.json")));
                var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());
                armTemplate.Remove("parameters");
                armTemplate.Add("parameters", armParamTemplate["parameters"]);


                var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
                {
                    Properties = new DeploymentPropertiesExtended()
                    {
                        Template   = armTemplate.ToString(),
                        Parameters = JsonUtility.GetEmptyJObject().ToString()
                    }
                };

                var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

                if (!validate.IsValid)
                {
                    return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                              DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
                }

                var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

                return(new ActionResponse(ActionStatus.Success, deploymentItem));
            }

            return(new ActionResponse(ActionStatus.Success, string.Empty));
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            var location      = request.DataStore.GetJson("SelectedLocation", "Name");

            var deploymentName     = request.DataStore.GetValue("DeploymentName");
            var logicAppName       = request.DataStore.GetValue("LogicAppName");
            var searchQuery        = request.DataStore.GetValue("SearchQuery");
            var imageCacheLogicApp = request.DataStore.GetValue("ImageCacheLogicApp");
            var siteName           = request.DataStore.GetValue("FunctionName");

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("resourcegroup", resourceGroup);
            param.AddStringParam("subscription", subscription);
            param.AddStringParam("logicappname", logicAppName);
            param.AddStringParam("imagecachelogicapp", imageCacheLogicApp);
            param.AddStringParam("sitename", siteName);
            param.AddStringParam("searchquery", searchQuery);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/AzureArm/NewsTemplateLogicApp.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            return(new ActionResponse(ActionStatus.Success, deploymentItem));
        }
        protected override bool CreateOrUpdate()
        {
            var created = true;

            try
            {
                using (var client = new ResourceManagementClient(GetCredentials()))
                {
                    // Skip if exists
                    if (!CheckExistence())
                    {
                        // Build deployment parameters
                        var deployment = new Deployment
                        {
                            Properties = new DeploymentProperties
                            {
                                Mode = DeploymentMode.Incremental,
                                Template = GetTemplate(),
                            }
                        };

                        // Run deployment
                        var result = client.Deployments.CreateOrUpdateAsync(Parameters.Tenant.SiteName, "Microsoft.DocumentDB", deployment).Result;

                        // Wait for deployment to finish
                        for (var i = 0; i < 30; i++)
                        {
                            var deploymentStatus = client.Deployments.GetAsync(Parameters.Tenant.SiteName, "Microsoft.DocumentDB").Result;

                            if (deploymentStatus.Deployment.Properties.ProvisioningState == ProvisioningState.Succeeded.ToString())
                            {
                                break;
                            }

                            Thread.Sleep(30000);
                        }

                        Console.WriteLine(result.StatusCode);
                    }

                    // Find the Account Keys
                    SetAccountKeys();
                }
            }
            catch (Exception ex)
            {
                created = false;
                Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }

            return created;
        }
        public void CreateDummyDeploymentTemplateWorks()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };

            var dictionary = new Dictionary<string, object> {
                    {"string", new Dictionary<string, object>() {
                        {"value", "myvalue"},
                    }},
                    {"securestring", new Dictionary<string, object>() {
                        {"value", "myvalue"},
                    }},
                    {"int", new Dictionary<string, object>() {
                        {"value", 42},
                    }},
                    {"bool", new Dictionary<string, object>() {
                        {"value", true},
                    }}
                };
            var serializedDictionary = JsonConvert.SerializeObject(dictionary, new JsonSerializerSettings
            {
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeNameHandling = TypeNameHandling.None
            });

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);
                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(DummyTemplateUri)
                        },
                        Parameters = serializedDictionary,
                        Mode = DeploymentMode.Incremental,
                    }
                };

                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");
                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });
                client.Deployments.CreateOrUpdate(groupName, deploymentName, parameters);

                JObject json = JObject.Parse(handler.Request);

                Assert.Equal(HttpStatusCode.OK, client.Deployments.Get(groupName, deploymentName).StatusCode);
            }
        }
        /// <summary>
        /// Starts a template deployment.
        /// </summary>
        /// <param name="resourceManagementClient">The resource manager client.</param>
        /// <param name="resourceGroupName">The name of the resource group.</param>
        /// <param name="deploymentName">The name of the deployment.</param>
        /// <param name="templateFileContents">The template file contents.</param>
        /// <param name="parameterFileContents">The parameter file contents.</param>
        private static void DeployTemplate(ResourceManagementClient resourceManagementClient, string resourceGroupName, string deploymentName, JObject templateFileContents, JObject parameterFileContents)
        {
            Console.WriteLine(string.Format("Starting template deployment '{0}' in resource group '{1]'", deploymentName, resourceGroupName));
            var deployment = new Deployment();

            deployment.Properties = new DeploymentProperties
            {
                Mode = DeploymentMode.Incremental,
                Template = templateFileContents,
                Parameters = parameterFileContents
            };

            var deploymentResult = resourceManagementClient.Deployments.CreateOrUpdate(resourceGroupName, deploymentName, deployment);
            Console.WriteLine(string.Format("Deployment status: {0}", deploymentResult.Properties.ProvisioningState));
        }
        private void CloneSlots(string[] slotNames)
        {
            var hostingEnvironmentProfile = WebsitesClient.CreateHostingEnvironmentProfile(ResourceGroupName, AseResourceGroupName, AseName);
            var template = DeploymentTemplateHelper.CreateSlotCloneDeploymentTemplate(Location, AppServicePlan, Name, SourceWebApp.Id,
                                                                                      slotNames, hostingEnvironmentProfile, WebsitesClient.WrappedWebsitesClient.ApiVersion());

            var deployment = new ResourceManagerDeployment
            {
                Properties = new DeploymentProperties
                {
                    Mode     = DeploymentMode.Incremental,
                    Template = template
                }
            };

            var deploymentName = string.Format("CloneSlotsFor{0}", Name);

            ResourcesClient.ResourceManagementClient.Deployments.CreateOrUpdate(ResourceGroupName, deploymentName, deployment);
            var result = ResourcesClient.ProvisionDeploymentStatus(ResourceGroupName, deploymentName, deployment);

            WriteObject(result.ToPSResourceGroupDeployment(ResourceGroupName));
        }
Exemple #11
0
        public async Task <ActionResponse> DeployLogicApp(string subscription, string azureToken, string resourceGroup, JObject armTemplate, string deploymentName)
        {
            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(
                           ActionStatus.Failure,
                           JsonUtility.GetJObjectFromObject(validate),
                           null,
                           DefaultErrorCodes.DefaultErrorCode,
                           $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            var resp = await WaitForDeployment(client, resourceGroup, deploymentItem.Deployment.Name);

            if (!resp.IsSuccess)
            {
                return(resp);
            }

            return(await WaitForDeployment(client, resourceGroup, deploymentName));
        }
        public static async Task <ActionResponse> ValidateAndDeployArm(SubscriptionCloudCredentials creds, string resourceGroup, string deploymentName, string template)
        {
            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = template,
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            ResourceManagementClient client = new ResourceManagementClient(creds);
            var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            if (!validate.IsValid)
            {
                throw new ActionFailedException($"Azure:{validate.Error.Message} Details:{validate.Error.Details}");
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            return(new ActionResponse(ActionStatus.Success, deploymentItem));
        }
        public void CheckExistenceReturnsCorrectValue()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);
                string resourceName = TestUtilities.GenerateName("csmr");

                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(GoodWebsiteTemplateUri),
                        },
                        Parameters =
                            @"{ 'siteName': {'value': 'mctest0101'},'hostingPlanName': {'value': 'mctest0101'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",
                        Mode = DeploymentMode.Incremental,
                    }
                };
                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");
                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });

                var checkExistenceFirst = client.Deployments.CheckExistence(groupName, deploymentName);
                Assert.False(checkExistenceFirst.Exists);

                var deploymentCreateResult = client.Deployments.CreateOrUpdate(groupName, deploymentName, parameters);

                var checkExistenceSecond = client.Deployments.CheckExistence(groupName, deploymentName);

                Assert.True(checkExistenceSecond.Exists);
            }
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");

            var location     = request.DataStore.GetLastValue("SqlLocation") ?? "westus";
            var databaseTier = request.DataStore.GetLastValue("SqlSku") ?? "S1";

            string server   = request.DataStore.GetJson("SqlCredentials").SelectToken("Server")?.ToString();
            string user     = request.DataStore.GetJson("SqlCredentials").SelectToken("User")?.ToString();
            string password = request.DataStore.GetJson("SqlCredentials").SelectToken("Password")?.ToString();
            var    database = request.DataStore.GetJson("SqlCredentials").SelectToken("Database")?.ToString();

            string serverWithoutExtension = server.Replace(".database.windows.net", string.Empty);

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("SqlServerName", serverWithoutExtension);
            param.AddStringParam("SqlDatabaseName", database);
            param.AddStringParam("Username", user);
            param.AddParameter("Password", "securestring", password);
            param.AddStringParam("Sku", databaseTier);
            param.AddStringParam("SqlLocation", location);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/sqlserveranddatabase.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds  = new TokenCloudCredentials(subscription, azureToken);
            ResourceManagementClient     client = new ResourceManagementClient(creds);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            string deploymentName = "SqlDatabaseDeployment";

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(
                           ActionStatus.Failure,
                           JsonUtility.GetJObjectFromObject(validate),
                           null,
                           DefaultErrorCodes.DefaultErrorCode,
                           $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            // Wait for deployment
            while (true)
            {
                Thread.Sleep(5000);
                var status = await client.Deployments.GetAsync(resourceGroup, deploymentName, new CancellationToken());

                var operations = await client.DeploymentOperations.ListAsync(resourceGroup, deploymentName, new DeploymentOperationsListParameters());

                var provisioningState = status.Deployment.Properties.ProvisioningState;

                if (provisioningState == "Accepted" || provisioningState == "Running")
                {
                    continue;
                }

                if (provisioningState == "Succeeded")
                {
                    break;
                }

                var operation       = operations.Operations.First(p => p.Properties.ProvisioningState == ProvisioningState.Failed);
                var operationFailed = await client.DeploymentOperations.GetAsync(resourceGroup, deploymentName, operation.OperationId);

                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(operationFailed), null, DefaultErrorCodes.DefaultErrorCode, operationFailed.Operation.Properties.StatusMessage));
            }

            SqlCredentials credentials = new SqlCredentials()
            {
                Server         = server,
                Username       = user,
                Password       = password,
                Authentication = SqlAuthentication.SQL,
                Database       = database
            };

            var connectionStringResponse = SqlUtility.GetConnectionString(credentials);

            return(new ActionResponse(ActionStatus.Success, JsonUtility.CreateJObjectWithValueFromObject(connectionStringResponse), true));
        }
        public void NewResourceGroupUsesDeploymentNameForDeploymentName()
        {
            string deploymentName = "abc123";
            Deployment deploymentFromGet = new Deployment();
            Deployment deploymentFromValidate = new Deployment();
            CreatePSResourceGroupParameters parameters = new CreatePSResourceGroupParameters()
            {
                ResourceGroupName = resourceGroupName,
                Location = resourceGroupLocation,
                DeploymentName = deploymentName,
                GalleryTemplateIdentity = "abc",
                StorageAccountName = storageAccountName,
                ConfirmAction = ConfirmAction
            };

            galleryTemplatesClientMock.Setup(g => g.GetGalleryTemplateFile(It.IsAny<string>())).Returns("http://path/file.html");

            deploymentsMock.Setup(f => f.ValidateAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentValidateResponse
                {
                    IsValid = true,
                    Error = new ResourceManagementErrorWithDetails()
                }))
                .Callback((string rg, string dn, Deployment d, CancellationToken c) => { deploymentFromValidate = d; });

            deploymentsMock.Setup(f => f.CreateOrUpdateAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsCreateResult
                {
                    RequestId = requestId
                }))
                .Callback((string name, string dName, Deployment bDeploy, CancellationToken token) => { deploymentFromGet = bDeploy; deploymentName = dName; });

            SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResourceExtended>() { new GenericResourceExtended() { Name = "website" } });

            var operationId = Guid.NewGuid().ToString();
            var operationQueue = new Queue<DeploymentOperation>();
            operationQueue.Enqueue(
                new DeploymentOperation()
                {
                    OperationId = operationId,
                    Properties = new DeploymentOperationProperties()
                    {
                        ProvisioningState = ProvisioningState.Accepted,
                        TargetResource = new TargetResource()
                        {
                            ResourceType = "Microsoft.Website",
                            ResourceName = resourceName
                        }
                    }
                }
            );
            operationQueue.Enqueue(
                new DeploymentOperation()
                {
                    OperationId = operationId,
                    Properties = new DeploymentOperationProperties()
                    {
                        ProvisioningState = ProvisioningState.Running,
                        TargetResource = new TargetResource()
                        {
                            ResourceType = "Microsoft.Website",
                            ResourceName = resourceName
                        }
                    }
                }
            );
            operationQueue.Enqueue(
                new DeploymentOperation()
                {
                    OperationId = operationId,
                    Properties = new DeploymentOperationProperties()
                    {
                        ProvisioningState = ProvisioningState.Succeeded,
                        TargetResource = new TargetResource()
                        {
                            ResourceType = "Microsoft.Website",
                            ResourceName = resourceName
                        }
                    }
                }
            );
            deploymentOperationsMock.SetupSequence(f => f.ListAsync(It.IsAny<string>(), It.IsAny<string>(), null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        operationQueue.Dequeue()
                    }
                }))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        operationQueue.Dequeue()
                    }
                }))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        operationQueue.Dequeue()
                    }
                }));

            var deploymentQueue = new Queue<DeploymentExtended>();
            deploymentQueue.Enqueue(new DeploymentExtended()
            {
                Name = deploymentName,
                Properties = new DeploymentPropertiesExtended()
                {
                    Mode = DeploymentMode.Incremental,
                    CorrelationId = "123",
                    ProvisioningState = ProvisioningState.Accepted
                }
            });
            deploymentQueue.Enqueue(new DeploymentExtended
            {
                Name = deploymentName,
                Properties = new DeploymentPropertiesExtended()
                {
                    Mode = DeploymentMode.Incremental,
                    CorrelationId = "123",
                    ProvisioningState = ProvisioningState.Running
                }
            });
            deploymentQueue.Enqueue(new DeploymentExtended
            {
                Name = deploymentName,
                Properties = new DeploymentPropertiesExtended()
                {
                    Mode = DeploymentMode.Incremental,
                    CorrelationId = "123",
                    ProvisioningState = ProvisioningState.Succeeded
                }
            });
            deploymentsMock.SetupSequence(f => f.GetAsync(It.IsAny<string>(), It.IsAny<string>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = deploymentQueue.Dequeue()
                }))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = deploymentQueue.Dequeue()
                }))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = deploymentQueue.Dequeue()
                }));

            PSResourceGroupDeployment result = resourcesClient.ExecuteDeployment(parameters);
            Assert.Equal(deploymentName, deploymentName);
            Assert.Equal(ProvisioningState.Succeeded, result.ProvisioningState);
            progressLoggerMock.Verify(
                f => f(string.Format("Resource {0} '{1}' provisioning status is {2}", "Microsoft.Website", resourceName, ProvisioningState.Accepted.ToLower())),
                Times.Once());
            progressLoggerMock.Verify(
                f => f(string.Format("Resource {0} '{1}' provisioning status is {2}", "Microsoft.Website", resourceName, ProvisioningState.Running.ToLower())),
                Times.Once());
            progressLoggerMock.Verify(
                f => f(string.Format("Resource {0} '{1}' provisioning status is {2}", "Microsoft.Website", resourceName, ProvisioningState.Succeeded.ToLower())),
                Times.Once());
        }
        public void ExtractsErrorMessageFromFailedDeploymentOperation()
        {
            Uri templateUri = new Uri("http://templateuri.microsoft.com");
            Deployment deploymentFromGet = new Deployment();
            Deployment deploymentFromValidate = new Deployment();
            CreatePSResourceGroupParameters parameters = new CreatePSResourceGroupParameters()
            {
                ResourceGroupName = resourceGroupName,
                Location = resourceGroupLocation,
                DeploymentName = deploymentName,
                TemplateFile = templateFile,
                StorageAccountName = storageAccountName,
                ConfirmAction = ConfirmAction
            };
            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = false
                }));

            resourceGroupMock.Setup(f => f.CreateOrUpdateAsync(
                parameters.ResourceGroupName,
                It.IsAny<ResourceGroup>(),
                new CancellationToken()))
                    .Returns(Task.Factory.StartNew(() => new ResourceGroupCreateOrUpdateResult
                    {
                        ResourceGroup = new ResourceGroupExtended() { Name = parameters.ResourceGroupName, Location = parameters.Location }
                    }));
            resourceGroupMock.Setup(f => f.GetAsync(resourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupGetResult
                {
                    ResourceGroup = new ResourceGroupExtended() { Location = resourceGroupLocation }
                }));
            deploymentsMock.Setup(f => f.CreateOrUpdateAsync(resourceGroupName, deploymentName, It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsCreateResult
                {
                    RequestId = requestId
                }))
                .Callback((string name, string dName, Deployment bDeploy, CancellationToken token) => { deploymentFromGet = bDeploy; });
            deploymentsMock.Setup(f => f.GetAsync(resourceGroupName, deploymentName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = new DeploymentExtended()
                    {
                        Name = deploymentName,
                        Properties = new DeploymentPropertiesExtended()
                        {
                            Mode = DeploymentMode.Incremental,
                            ProvisioningState = ProvisioningState.Succeeded
                        }
                    }
                }));
            deploymentsMock.Setup(f => f.ValidateAsync(resourceGroupName, It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentValidateResponse
                {
                    IsValid = true,
                    Error = new ResourceManagementErrorWithDetails()
                }))
                .Callback((string rg, string dn, Deployment d, CancellationToken c) => { deploymentFromValidate = d; });
            SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResourceExtended>() { new GenericResourceExtended() { Name = "website" } });
            deploymentOperationsMock.Setup(f => f.ListAsync(resourceGroupName, deploymentName, null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        new DeploymentOperation()
                        {
                            OperationId = Guid.NewGuid().ToString(),
                            Properties = new DeploymentOperationProperties()
                            {
                                ProvisioningState = ProvisioningState.Failed,
                                StatusMessage = JsonConvert.SerializeObject(new ResourceManagementError()
                                {
                                    Message = "A really bad error occured"
                                }),
                                TargetResource = new TargetResource()
                                {
                                    ResourceType = "Microsoft.Website",
                                    ResourceName = resourceName
                                }
                            }
                        }
                    }
                }));

            PSResourceGroup result = resourcesClient.CreatePSResourceGroup(parameters);

            deploymentsMock.Verify((f => f.CreateOrUpdateAsync(resourceGroupName, deploymentName, deploymentFromGet, new CancellationToken())), Times.Once());
            Assert.Equal(parameters.ResourceGroupName, result.ResourceGroupName);
            Assert.Equal(parameters.Location, result.Location);
            Assert.Equal(1, result.Resources.Count);

            Assert.Equal(DeploymentMode.Incremental, deploymentFromGet.Properties.Mode);
            Assert.NotNull(deploymentFromGet.Properties.Template);

            errorLoggerMock.Verify(
                f => f(string.Format("Resource {0} '{1}' failed with message '{2}'",
                        "Microsoft.Website",
                        resourceName,
                        "A really bad error occured")),
                Times.Once());
        }
        private async Task CreateTemplateDeploymentAsync(TokenCloudCredentials credential, string rgName, string templateContent, string parameterContent)
        {
            Deployment deployment = new Deployment();

            string deploymentname = rgName + "dp";
            deployment.Properties = new DeploymentProperties
            {
                Mode = DeploymentMode.Incremental,
                Template = templateContent,
                Parameters = parameterContent,
            };

            using (ResourceManagementClient templateDeploymentClient = new ResourceManagementClient(credential))
            {
                try
                {
                    DeploymentOperationsCreateResult dpResult =
                        await templateDeploymentClient.Deployments.CreateOrUpdateAsync(rgName, deploymentname, deployment);
                    ServiceEventSource.Current.Message("ArmClusterOperator: Deployment in RG {0}: {1} ({2})", rgName, dpResult.RequestId, dpResult.StatusCode);
                }
                catch (Exception e)
                {
                    ServiceEventSource.Current.Message(
                        "ArmClusterOperator: Failed deploying ARM template to create a cluster in RG {0}. {1}",
                        rgName,
                        e.Message);

                    throw;
                }
            }
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string azureToken      = request.DataStore.GetJson("AzureToken", "access_token");
            string subscription    = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            string resourceGroup   = request.DataStore.GetValue("SelectedResourceGroup");
            string doNotWaitString = request.DataStore.GetValue("Wait");

            bool   doNotWait      = !string.IsNullOrEmpty(doNotWaitString) && bool.Parse(doNotWaitString);
            string deploymentName = request.DataStore.GetValue("DeploymentName");

            // Read from file
            var armTemplatefilePath        = request.DataStore.GetValue("AzureArmFile");
            var armParamTemplateProperties = request.DataStore.GetJson("AzureArmParameters");

            if (deploymentName == null && !doNotWait)
            {
                deploymentName = request.DataStore.CurrentRoute;
            }

            var param = new AzureArmParameterGenerator();

            foreach (var prop in armParamTemplateProperties.Children())
            {
                string key   = prop.Path.Split('.').Last();
                string value = prop.First().ToString();

                param.AddStringParam(key, value);
            }

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, armTemplatefilePath)));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(
                           ActionStatus.Failure,
                           JsonUtility.GetJObjectFromObject(validate),
                           null,
                           DefaultErrorCodes.DefaultErrorCode,
                           $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (doNotWait)
            {
                return(new ActionResponse(ActionStatus.Success, deploymentItem));
            }

            return(await WaitForAction(client, resourceGroup, deploymentName));
        }
Exemple #19
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string idSubscription    = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            string nameResourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            string tokenAzure        = request.DataStore.GetJson("AzureToken", "access_token");

            string databaseConnectionString       = request.DataStore.GetValue("SqlConnectionString");
            string storageAccountConnectionString = request.DataStore.GetJson("SelectedStorageAccount", "StorageAccountConnectionString");
            string functionName = request.DataStore.GetValue("functionName");
            string functionStorageAccountName = request.DataStore.GetValue("functionStorageAccountName");

            string deploymentName = request.DataStore.GetValue("DeploymentName") ?? request.DataStore.CurrentRoute;

            var payload = new AzureArmParameterGenerator();

            var armParameters = request.DataStore.GetJson("AzureArmParameters");

            if (armParameters != null)
            {
                foreach (var prop in armParameters.Children())
                {
                    string key   = prop.Path.Split('.').Last();
                    string value = prop.First().ToString();

                    payload.AddStringParam(key, value);
                }
            }

            payload.AddStringParam("siteName", functionName);
            payload.AddStringParam("blobStorageConnectionString", storageAccountConnectionString);
            payload.AddStringParam("functionStorageAccountName", functionStorageAccountName);
            payload.AddStringParam("databaseConnectionString", databaseConnectionString);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, request.DataStore.GetValue("AzureArmFile"))));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(payload.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            ResourceManagementClient client = new ResourceManagementClient(new TokenCloudCredentials(idSubscription, tokenAzure));
            var validate = await client.Deployments.ValidateAsync(nameResourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null, DefaultErrorCodes.DefaultErrorCode,
                                          $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(nameResourceGroup, deploymentName, deployment, new CancellationToken());

            ActionResponse r = await WaitForAction(client, nameResourceGroup, deploymentName);

            request.DataStore.AddToDataStore("ArmOutput", r.DataStore.GetValue("ArmOutput"), DataStoreType.Public);
            return(r);
        }
        public void CreateDummyDeploymentProducesOperations()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };
            var dictionary = new Dictionary<string, object> {
                    {"string", new Dictionary<string, object>() {
                        {"value", "myvalue"},
                    }},
                    {"securestring", new Dictionary<string, object>() {
                        {"value", "myvalue"},
                    }},
                    {"int", new Dictionary<string, object>() {
                        {"value", 42},
                    }},
                    {"bool", new Dictionary<string, object>() {
                        {"value", true},
                    }}
                };
            var serializedDictionary = JsonConvert.SerializeObject(dictionary, new JsonSerializerSettings
            {
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeNameHandling = TypeNameHandling.None
            });

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);
                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(DummyTemplateUri)
                        },
                        Parameters = serializedDictionary,
                        Mode = DeploymentMode.Incremental,
                    }
                };

                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");
                string resourceName = TestUtilities.GenerateName("csmr");

                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });
                client.Deployments.CreateOrUpdate(groupName, deploymentName, parameters);

                // Wait until deployment completes
                TestUtilities.Wait(30000);
                var operations = client.DeploymentOperations.List(groupName, deploymentName, null);

                Assert.True(operations.Operations.Any());
                Assert.NotNull(operations.Operations[0].Id);
                Assert.NotNull(operations.Operations[0].OperationId);
                Assert.NotNull(operations.Operations[0].Properties);
            }
        }
        private Deployment WaitDeploymentStatus(
            string resourceGroup,
            string deploymentName,
            BasicDeployment basicDeployment,
            Action<string, string, BasicDeployment> job,
            params string[] status)
        {
            Deployment deployment = new Deployment();

            do
            {
                if (job != null)
                {
                    job(resourceGroup, deploymentName, basicDeployment);
                }

                deployment = ResourceManagementClient.Deployments.Get(resourceGroup, deploymentName).Deployment;
                Thread.Sleep(2000);

            } while (!status.Any(s => s.Equals(deployment.Properties.ProvisioningState, StringComparison.OrdinalIgnoreCase)));

            return deployment;
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            List <Task <ActionResponse> > task = new List <Task <ActionResponse> >();
            var    token         = request.DataStore.GetJson("AzureToken", "access_token");
            var    subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var    resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            string connString    = request.DataStore.GetValue("SqlConnectionString");
            string schema        = "dbo";

            string postDeploymentPipelineType = request.DataStore.GetValue("postDeploymentPipelineType");
            string pipelineFrequency          = request.DataStore.GetValue("pipelineFrequency");
            string pipelineInterval           = request.DataStore.GetValue("pipelineInterval");
            string pipelineType  = request.DataStore.GetValue("pipelineType");
            string pipelineStart = request.DataStore.GetValue("pipelineStart");
            string pipelineEnd   = request.DataStore.GetValue("pipelineEnd");

            bool historicalOnly = Convert.ToBoolean(request.DataStore.GetValue("historicalOnly"));

            string dataFactoryName = resourceGroup.Replace("_", string.Empty) + "SalesforceCopyFactory";

            if (!string.IsNullOrWhiteSpace(postDeploymentPipelineType))
            {
                pipelineFrequency = request.DataStore.GetValue("postDeploymentPipelineFrequency");
                pipelineType      = postDeploymentPipelineType;
                pipelineInterval  = request.DataStore.GetValue("postDeploymentPipelineInterval");
                pipelineStart     = DateTime.UtcNow.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
                pipelineEnd       = new DateTime(9999, 12, 31, 23, 59, 59).ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
            }

            if (string.IsNullOrWhiteSpace(pipelineStart))
            {
                pipelineStart = DateTime.UtcNow.AddYears(-3).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
            }

            if (string.IsNullOrWhiteSpace(pipelineEnd))
            {
                pipelineEnd = DateTime.UtcNow.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
            }

            var adfJsonData = request.DataStore.GetValue("ADFPipelineJsonData");

            var obj = JsonConvert.DeserializeObject(adfJsonData, typeof(DeserializedADFPayload)) as DeserializedADFPayload;

            obj = ReorderObjects(obj);

            for (int i = 0; i < obj.fields.Count(); i++)
            {
                var o = obj.fields[i];

                string deploymentName = string.Concat("ADFPipeline", pipelineType, o.Item1);

                var sqlCreds = SqlUtility.GetSqlCredentialsFromConnectionString(connString);
                var param    = new AzureArmParameterGenerator();
                param.AddStringParam("dataFactoryName", dataFactoryName);
                param.AddStringParam("targetSqlSchema", schema);
                param.AddStringParam("targetSqlTable", o.Item1.ToLowerInvariant());
                param.AddStringParam("targetSalesforceTable", o.Item1);
                param.AddStringParam("pipelineName", o.Item1 + "_CopyPipeline");
                param.AddStringParam("sqlWritableTypeName", o.Item1.ToLowerInvariant() + "type");
                param.AddStringParam("sqlWriterStoredProcedureName", "spMerge" + o.Item1.ToLowerInvariant());
                param.AddStringParam("pipelineStartDate", pipelineStart);
                param.AddStringParam("pipelineEndDate", pipelineEnd);
                param.AddStringParam("pipelineType", pipelineType);
                param.AddStringParam("sliceFrequency", pipelineFrequency);
                param.AddStringParam("sliceInterval", pipelineInterval);

                var armTemplate      = JsonUtility.GetJsonObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/ADF/pipeline.json")));
                var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

                armTemplate.Remove("parameters");
                armTemplate.Add("parameters", armParamTemplate["parameters"]);

                if (i >= 1)
                {
                    armTemplate = CreatePipelineDependency(pipelineType, obj, armTemplate, i - 1);
                }

                string tableFields = JsonConvert.SerializeObject(o.Item2);

                StringBuilder query;

                if (o.Item1 != "Opportunity" &&
                    o.Item1 != "Lead" &&
                    o.Item1 != "OpportunityLineItem" &&
                    pipelineType == "PreDeployment")
                {
                    query       = CreateQuery(o, tableFields, true, pipelineStart, pipelineEnd);
                    armTemplate = CreateOneTimePipeline(armTemplate);
                }
                else
                {
                    query = CreateQuery(o, tableFields, false);
                }

                if (historicalOnly && pipelineType == "PostDeployment")
                {
                    armTemplate = this.PausePipeline(armTemplate);
                }

                string stringTemplate = ReplaceTableFieldsAndQuery(tableFields, query, armTemplate);

                SubscriptionCloudCredentials creds  = new TokenCloudCredentials(subscription, token);
                ResourceManagementClient     client = new ResourceManagementClient(creds);

                var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
                {
                    Properties = new DeploymentPropertiesExtended()
                    {
                        Template   = stringTemplate,
                        Parameters = JsonUtility.GetEmptyJObject().ToString()
                    }
                };

                var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;
                if (!validate.IsValid)
                {
                    return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                              DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
                }

                task.Add(new Task <ActionResponse>(() =>
                {
                    var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;
                    var helper         = new DeploymentHelper();
                    return(helper.WaitForDeployment(resourceGroup, deploymentName, client));
                }));
            }

            foreach (var t in task)
            {
                t.Start();
            }

            Task.WaitAll(task.ToArray());

            foreach (var t in task)
            {
                if (t.Result.Status != ActionStatus.Success)
                {
                    return(new ActionResponse(ActionStatus.Failure, t.Result.ExceptionDetail.FriendlyErrorMessage));
                }
            }

            return(new ActionResponse(ActionStatus.Success, JsonUtility.GetJObjectFromStringValue(dataFactoryName)));
        }
Exemple #23
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var cognitiveServiceKey = request.DataStore.GetValue("CognitiveServiceKey");

            if (!string.IsNullOrEmpty(cognitiveServiceKey))
            {
                return(new ActionResponse(ActionStatus.Success));
            }

            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            var location      = request.DataStore.GetJson("SelectedLocation", "Name");

            var deploymentName       = request.DataStore.GetValue("DeploymentName");
            var cognitiveServiceName = request.DataStore.GetValue("CognitiveServiceName");
            var cognitiveServiceType = request.DataStore.GetValue("CognitiveServiceType");
            var skuName = request.DataStore.GetValue("CognitiveSkuName");

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("CognitiveServiceName", cognitiveServiceName);
            param.AddStringParam("CognitiveServiceType", cognitiveServiceType);

            if (cognitiveServiceType == "Bing.Search")
            {
                param.AddStringParam("Location", "global");
            }
            else
            {
                param.AddStringParam("Location", "West US");
            }

            param.AddStringParam("skuName", skuName);


            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);
            var registeration = await client.Providers.RegisterAsync("Microsoft.CognitiveServices");

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/AzureArm/CognitiveServices.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            while (true)
            {
                Thread.Sleep(5000);
                var status            = client.Deployments.GetAsync(resourceGroup, deploymentName, new CancellationToken()).Result;
                var operations        = client.DeploymentOperations.ListAsync(resourceGroup, deploymentName, new DeploymentOperationsListParameters(), new CancellationToken()).Result;
                var provisioningState = status.Deployment.Properties.ProvisioningState;

                if (provisioningState == "Accepted" || provisioningState == "Running")
                {
                    continue;
                }

                if (provisioningState == "Succeeded")
                {
                    return(new ActionResponse(ActionStatus.Success, operations));
                }

                var operation       = operations.Operations.First(p => p.Properties.ProvisioningState == ProvisioningState.Failed);
                var operationFailed = client.DeploymentOperations.GetAsync(resourceGroup, deploymentName, operation.OperationId, new CancellationToken()).Result;

                return(new ActionResponse(ActionStatus.Failure, operationFailed));
            }
        }
Exemple #24
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken    = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            var location      = request.DataStore.GetJson("SelectedLocation", "Name");

            var deploymentName = request.DataStore.GetValue("DeploymentName");
            var logicAppName   = request.DataStore.GetValue("LogicAppName");

            var apiKeyTopics   = request.DataStore.GetAllValues("AzureMLKey")[0];
            var apiKeyImages   = request.DataStore.GetAllValues("AzureMLKey")[1];;
            var apiKeyEntities = request.DataStore.GetAllValues("AzureMLKey")[2];;

            var apiUrlTopics   = request.DataStore.GetAllValues("AzureMLUrl")[0];
            var apiUrlImages   = request.DataStore.GetAllValues("AzureMLUrl")[1];
            var apiUrlEntities = request.DataStore.GetAllValues("AzureMLUrl")[2];

            var param = new AzureArmParameterGenerator();

            param.AddStringParam("resourcegroup", resourceGroup);
            param.AddStringParam("subscription", subscription);
            param.AddStringParam("logicappname", logicAppName);
            param.AddStringParam("apikeytopics", apiKeyTopics);
            param.AddStringParam("apikeyimages", apiKeyImages);
            param.AddStringParam("apikeyentities", apiKeyEntities);
            param.AddStringParam("apiurltopics", apiUrlTopics);
            param.AddStringParam("apiurlentities", apiUrlEntities);
            param.AddStringParam("apiurlimages", apiUrlImages);

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/AzureArm/AzureMLSchedulerLogicApp.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription, azureToken);

            Microsoft.Azure.Management.Resources.ResourceManagementClient client = new ResourceManagementClient(creds);


            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var validate = await client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken());

            // Log logic app
            request.Logger.LogResource(request.DataStore, logicAppName,
                                       DeployedResourceType.LogicApp, CreatedBy.BPST, DateTime.UtcNow.ToString("o"));

            return(new ActionResponse(ActionStatus.Success, deploymentItem));
        }
        private DeploymentExtended WaitDeploymentStatus(
            string resourceGroup,
            string deploymentName,
            Deployment basicDeployment,
            Action<string, string, Deployment> job,
            params string[] status)
        {
            DeploymentExtended deployment;
            int counter = 5000;

            do
            {
                WriteVerbose(string.Format("Checking deployment status in {0} seconds.", counter / 1000));
                TestMockSupport.Delay(counter);

                if (job != null)
                {
                    job(resourceGroup, deploymentName, basicDeployment);
                }

                deployment = ResourceManagementClient.Deployments.Get(resourceGroup, deploymentName).Deployment;
                counter = counter + 5000 > 60000 ? 60000 : counter + 5000;

            } while (!status.Any(s => s.Equals(deployment.Properties.ProvisioningState, StringComparison.OrdinalIgnoreCase)));

            return deployment;
        }
        private static void HandleDeploy(Options options, AuthenticationContext authContext, AuthenticationResult token, ResourceManagementClient resourceManagementClient)
        {
            if (!string.IsNullOrWhiteSpace(options.Deploy))
            {
                ResourceGroupExtended rg = GetResourceGroup(options, resourceManagementClient);
                //Fix location to displayname from template
                using (var subscriptionClient = new SubscriptionClient(new TokenCloudCredentials(token.AccessToken)))
                {
                    var a = subscriptionClient.Subscriptions.ListLocations(options.SubscriptionId);
                    rg.Location = a.Locations.Single(l => l.Name == rg.Location).DisplayName;
                }

                var graphtoken = authContext.AcquireToken("https://graph.windows.net/", options.ClientID, new Uri(options.RedirectUri), PromptBehavior.Auto);
                var graph = new ActiveDirectoryClient(new Uri("https://graph.windows.net/" + graphtoken.TenantId), () => Task.FromResult(graphtoken.AccessToken));
                var principal = graph.ServicePrincipals.Where(p => p.AppId == options.ApplicaitonId).ExecuteSingleAsync().GetAwaiter().GetResult();


                DeploymentExtended deploymentInfo = null;
                if (!resourceManagementClient.Deployments.CheckExistence(options.ResourceGroup, options.DeployName).Exists)
                {

                    var deployment = new Deployment
                    {
                        Properties = new DeploymentProperties
                        {
                            Mode = DeploymentMode.Incremental, //Dont Delete other resources
                            Template = File.ReadAllText(options.Deploy),
                            Parameters = new JObject(
                                new JProperty("siteName", CreateValue(options.SiteName)),
                                new JProperty("hostingPlanName", CreateValue(options.HostingPlanName)),
                                new JProperty("storageAccountType", CreateValue(options.StorageAccountType)),
                                new JProperty("siteLocation", CreateValue(rg.Location)),
                                new JProperty("sku", CreateValue(options.WebsitePlan)),
                                new JProperty("tenantId", CreateValue(token.TenantId)),
                                new JProperty("objectId", CreateValue(token.UserInfo.UniqueId)),
                                new JProperty("appOwnerTenantId", CreateValue(principal.AppOwnerTenantId.Value.ToString())),
                                new JProperty("appOwnerObjectId", CreateValue(principal.ObjectId))
                                ).ToString(),

                        }
                    };

                    var result = resourceManagementClient.Deployments.CreateOrUpdate(options.ResourceGroup, options.DeployName, deployment);
                    deploymentInfo = result.Deployment;
                }
                else
                {
                    var deploymentStatus = resourceManagementClient.Deployments.Get(options.ResourceGroup, options.DeployName);
                    deploymentInfo = deploymentStatus.Deployment;

                }

                while (!(deploymentInfo.Properties.ProvisioningState == "Succeeded" || deploymentInfo.Properties.ProvisioningState == "Failed"))
                {
                    var deploymentStatus = resourceManagementClient.Deployments.Get(options.ResourceGroup, options.DeployName);
                    deploymentInfo = deploymentStatus.Deployment;
                    Thread.Sleep(5000);
                }
                Console.WriteLine(deploymentInfo.Properties.Outputs);
                var outputs = JObject.Parse(deploymentInfo.Properties.Outputs);
                var storageAccountName = outputs["storageAccount"]["value"].ToString();
                var keyvaultName = outputs["keyvault"]["value"].ToString();

                using (var client = new KeyVaultManagementClient(new TokenCloudCredentials(options.SubscriptionId, token.AccessToken)))
                {
                    using (var storageClient = new StorageManagementClient(new TokenCloudCredentials(options.SubscriptionId, token.AccessToken)))
                    {
                        var keys = storageClient.StorageAccounts.ListKeys(options.ResourceGroup, storageAccountName);

                        var vaultInfo = client.Vaults.Get(options.ResourceGroup, keyvaultName);
                        //CHEATING (using powershell application id to get token on behhalf of user);
                        var vaultToken = authContext.AcquireToken("https://vault.azure.net", "1950a258-227b-4e31-a9cf-717495945fc2", new Uri("urn:ietf:wg:oauth:2.0:oob"));
                        var keyvaultClient = new KeyVaultClient((_, b, c) => Task.FromResult(vaultToken.AccessToken));

                        var secrets = keyvaultClient.GetSecretsAsync(vaultInfo.Vault.Properties.VaultUri).GetAwaiter().GetResult();
                        if (secrets.Value == null || !secrets.Value.Any(s => s.Id == vaultInfo.Vault.Properties.VaultUri + "secrets/storage"))
                        {
                            keyvaultClient.SetSecretAsync(vaultInfo.Vault.Properties.VaultUri, "storage", $"{storageAccountName}:{keys.StorageAccountKeys.Key1}").GetAwaiter().GetResult();
                            keyvaultClient.SetSecretAsync(vaultInfo.Vault.Properties.VaultUri, "storage", $"{storageAccountName}:{keys.StorageAccountKeys.Key2}").GetAwaiter().GetResult();


                            var secret = keyvaultClient.GetSecretVersionsAsync(vaultInfo.Vault.Properties.VaultUri, "storage").GetAwaiter().GetResult();
                        }
                    }



                }


            }
        }
        public void ValidateGoodDeploymentWithInlineTemplate()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);
                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");

                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        Template = File.ReadAllText("ScenarioTests\\good-website.js"),
                        Parameters =
                            @"{ 'siteName': {'value': 'mctest0101'},'hostingPlanName': {'value': 'mctest0101'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",
                        Mode = DeploymentMode.Incremental,
                    }
                };

                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });

                //Action
                DeploymentValidateResponse validationResult = client.Deployments.Validate(groupName, deploymentName, parameters);

                //Assert
                Assert.True(validationResult.IsValid);
                Assert.Null(validationResult.Error);
                Assert.NotNull(validationResult.Properties);
                Assert.NotNull(validationResult.Properties.Providers);
                Assert.Equal(1, validationResult.Properties.Providers.Count);
                Assert.Equal("Microsoft.Web", validationResult.Properties.Providers[0].Namespace);
            }
        }
        public void ValidateBadDeployment()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);

                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");
                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(BadTemplateUri),
                        },
                        Parameters =
                            @"{ 'siteName': {'value': 'mctest0101'},'hostingPlanName': {'value': 'mctest0101'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",
                        Mode = DeploymentMode.Incremental,
                    }
                };

                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });
                var result = client.Deployments.Validate(groupName, deploymentName, parameters);
                Assert.False(result.IsValid);
                Assert.Equal("InvalidTemplate", result.Error.Code);
            }
        }
        private void WriteDeploymentProgress(string resourceGroup, string deploymentName, Deployment deployment)
        {
            const string normalStatusFormat = "Resource {0} '{1}' provisioning status is {2}";
            const string failureStatusFormat = "Resource {0} '{1}' failed with message '{2}'";
            List<DeploymentOperation> newOperations;
            DeploymentOperationsListResult result;

            result = ResourceManagementClient.DeploymentOperations.List(resourceGroup, deploymentName, null);
            newOperations = GetNewOperations(operations, result.Operations);
            operations.AddRange(newOperations);

            while (!string.IsNullOrEmpty(result.NextLink))
            {
                result = ResourceManagementClient.DeploymentOperations.ListNext(result.NextLink);
                newOperations = GetNewOperations(operations, result.Operations);
                operations.AddRange(newOperations);
            }

            foreach (DeploymentOperation operation in newOperations)
            {
                string statusMessage;

                if (operation.Properties.ProvisioningState != ProvisioningState.Failed)
                {
                    if (operation.Properties.TargetResource != null)
                    {
                        statusMessage = string.Format(normalStatusFormat,
                        operation.Properties.TargetResource.ResourceType,
                        operation.Properties.TargetResource.ResourceName,
                        operation.Properties.ProvisioningState.ToLower());

                        WriteVerbose(statusMessage);
                    }
                }
                else
                {
                    string errorMessage = ParseErrorMessage(operation.Properties.StatusMessage);

                    if(operation.Properties.TargetResource != null)
                    {
                        statusMessage = string.Format(failureStatusFormat,
                        operation.Properties.TargetResource.ResourceType,
                        operation.Properties.TargetResource.ResourceName,
                        errorMessage);

                        WriteError(statusMessage);
                    }
                    else
                    {
                        WriteError(errorMessage);
                    }

                    List<string> detailedMessage = ParseDetailErrorMessage(operation.Properties.StatusMessage);

                    if (detailedMessage != null)
                    {
                        detailedMessage.ForEach(s => WriteError(s));
                    }
                }
            }
        }
        public void CreateLargeWebDeploymentTemplateWorks()
        {
            var handler = new RecordedDelegatingHandler();
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                string resourceName = TestUtilities.GenerateName("csmr");
                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");

                var client = GetResourceManagementClient(handler);
                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(GoodWebsiteTemplateUri),
                        },
                        Parameters =
                            "{ 'siteName': {'value': '" + resourceName + "'},'hostingPlanName': {'value': '" +
                            resourceName +
                            "'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",
                        Mode = DeploymentMode.Incremental,
                    }
                };

                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "South Central US" });
                client.Deployments.CreateOrUpdate(groupName, deploymentName, parameters);

                // Wait until deployment completes
                TestUtilities.Wait(30000);
                var operations = client.DeploymentOperations.List(groupName, deploymentName, null);

                Assert.True(operations.Operations.Any());
            }
        }
        private Deployment CreateBasicDeployment(ValidatePSResourceGroupDeploymentParameters parameters, DeploymentMode deploymentMode, string debugSetting)
        {
            Deployment deployment = new Deployment
            {
                Properties = new DeploymentProperties {
                    Mode = deploymentMode
                }
            };

            if(!string.IsNullOrEmpty(debugSetting))
            {
                deployment.Properties.DebugSetting = new DeploymentDebugSetting
                {
                    DeploymentDebugDetailLevel = debugSetting
                };
            }

            if (Uri.IsWellFormedUriString(parameters.TemplateFile, UriKind.Absolute))
            {
                deployment.Properties.TemplateLink = new TemplateLink
                {
                    Uri = new Uri(parameters.TemplateFile)
                };
            }
            else
            {
                deployment.Properties.Template = FileUtilities.DataStore.ReadFileAsText(parameters.TemplateFile);
            }

            if (Uri.IsWellFormedUriString(parameters.ParameterUri, UriKind.Absolute))
            {
                deployment.Properties.ParametersLink = new ParametersLink
                {
                    Uri = new Uri(parameters.ParameterUri)
                };
            }
            else
            {
                deployment.Properties.Parameters = GetDeploymentParameters(parameters.TemplateParameterObject);
            }

            return deployment;
        }
        public void CreateDeploymentAndValidateProperties()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.Created };

            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                var client = GetResourceManagementClient(handler);
                string resourceName = TestUtilities.GenerateName("csmr");

                var parameters = new Deployment
                {
                    Properties = new DeploymentProperties()
                    {
                        TemplateLink = new TemplateLink
                        {
                            Uri = new Uri(GoodWebsiteTemplateUri),
                        },
                        Parameters =
                            @"{ 'siteName': {'value': 'mctest0101'},'hostingPlanName': {'value': 'mctest0101'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",
                        Mode = DeploymentMode.Incremental,
                    }
                };
                string groupName = TestUtilities.GenerateName("csmrg");
                string deploymentName = TestUtilities.GenerateName("csmd");
                client.ResourceGroups.CreateOrUpdate(groupName, new ResourceGroup { Location = "West Europe" });
                var deploymentCreateResult = client.Deployments.CreateOrUpdate(groupName, deploymentName, parameters);

                Assert.NotNull(deploymentCreateResult.Deployment.Id);
                Assert.Equal(deploymentName, deploymentCreateResult.Deployment.Name);

                TestUtilities.Wait(1000);

                var deploymentListResult = client.Deployments.List(groupName, null);
                var deploymentGetResult = client.Deployments.Get(groupName, deploymentName);

                Assert.NotEmpty(deploymentListResult.Deployments);
                Assert.Equal(deploymentName, deploymentGetResult.Deployment.Name);
                Assert.Equal(deploymentName, deploymentListResult.Deployments[0].Name);
                Assert.Equal(GoodWebsiteTemplateUri, deploymentGetResult.Deployment.Properties.TemplateLink.Uri.AbsoluteUri);
                Assert.Equal(GoodWebsiteTemplateUri, deploymentListResult.Deployments[0].Properties.TemplateLink.Uri.AbsoluteUri);
                Assert.NotNull(deploymentGetResult.Deployment.Properties.ProvisioningState);
                Assert.NotNull(deploymentListResult.Deployments[0].Properties.ProvisioningState);
                Assert.NotNull(deploymentGetResult.Deployment.Properties.CorrelationId);
                Assert.NotNull(deploymentListResult.Deployments[0].Properties.CorrelationId);
                Assert.True(deploymentGetResult.Deployment.Properties.Parameters.Contains("mctest0101"));
                Assert.True(deploymentListResult.Deployments[0].Properties.Parameters.Contains("mctest0101"));
            }
        }
        private Deployment CreateBasicDeployment(ValidatePSResourceGroupDeploymentParameters parameters, DeploymentMode deploymentMode)
        {
            Deployment deployment = new Deployment
            {
                Properties = new DeploymentProperties {
                    Mode = deploymentMode,
                    Template = GetTemplate(parameters.TemplateFile, parameters.GalleryTemplateIdentity),
                    Parameters = GetDeploymentParameters(parameters.TemplateParameterObject)
                }
            };

            return deployment;
        }
Exemple #34
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var token         = request.DataStore.GetJson("AzureToken", "access_token");
            var subscription  = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            var resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");

            string sfUsername = request.DataStore.GetValue("SalesforceUser");
            string sfPassword = request.DataStore.GetValue("SalesforcePassword");
            string sfToken    = request.DataStore.GetValue("SalesforceToken");
            string sfUrl      = request.DataStore.GetValue("SalesforceUrl");

            string fullServerUrl = request.DataStore.GetValue("SalesforceBaseUrl");
            string connString    = request.DataStore.GetValue("SqlConnectionString");
            string emails        = request.DataStore.GetValue("EmailAddresses");

            string dataFactoryName = resourceGroup + "SalesforceCopyFactory";
            var    param           = new AzureArmParameterGenerator();
            var    sqlCreds        = SqlUtility.GetSqlCredentialsFromConnectionString(connString);

            param.AddStringParam("dataFactoryName", dataFactoryName);
            param.AddStringParam("sqlServerFullyQualifiedName", sqlCreds.Server);
            param.AddStringParam("sqlServerUsername", sqlCreds.Username);
            param.AddStringParam("targetDatabaseName", sqlCreds.Database);
            param.AddStringParam("salesforceUsername", sfUsername);
            param.AddStringParam("subscriptionId", subscription);
            param.AddStringParam("environmentUrl", sfUrl);
            param.AddParameter("salesforcePassword", "securestring", sfPassword);
            param.AddParameter("sqlServerPassword", "securestring", sqlCreds.Password);
            param.AddParameter("salesforceSecurityToken", "securestring", sfToken);

            var armTemplate      = JsonUtility.GetJsonObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/ADF/linkedServices.json")));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(param.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            if (string.IsNullOrEmpty(emails))
            {
                (armTemplate
                 .SelectToken("resources")[0]
                 .SelectToken("resources") as JArray)
                .RemoveAt(2);
            }
            else
            {
                var           addresses = emails.Split(',');
                List <string> adr       = new List <string>();

                foreach (var address in addresses)
                {
                    adr.Add(address);
                }

                var stringTemplate = armTemplate.ToString();

                stringTemplate = stringTemplate.Replace("\"EMAILS\"", JsonConvert.SerializeObject(adr));
                armTemplate    = JsonUtility.GetJObjectFromJsonString(stringTemplate);
            }

            SubscriptionCloudCredentials creds  = new TokenCloudCredentials(subscription, token);
            ResourceManagementClient     client = new ResourceManagementClient(creds);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            var factoryIdenity = new ResourceIdentity
            {
                ResourceProviderApiVersion = "2015-10-01",
                ResourceName = dataFactoryName,
                ResourceProviderNamespace = "Microsoft.DataFactory",
                ResourceType = "datafactories"
            };

            var factory = client.Resources.CheckExistence(resourceGroup, factoryIdenity);

            if (factory.Exists)
            {
                client.Resources.Delete(resourceGroup, factoryIdenity);
            }

            string deploymentName = "SalesforceCopyFactory-linkedServices";

            var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                          DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

            var helper = new DeploymentHelper();

            return(helper.WaitForDeployment(resourceGroup, deploymentName, client));
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string idSubscription    = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
            string nameResourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            string tokenAzure        = request.DataStore.GetJson("AzureToken", "access_token");

            string databaseConnectionString = request.DataStore.GetValue("SqlConnectionString");
            string dataFactoryName          = request.DataStore.GetValue("dataFactoryName");

            string deploymentName = request.DataStore.GetValue("DeploymentName") ?? request.DataStore.CurrentRoute;

            var payload       = new AzureArmParameterGenerator();
            var armParameters = request.DataStore.GetJson("AzureArmParameters");

            if (armParameters != null)
            {
                foreach (var prop in armParameters.Children())
                {
                    string key   = prop.Path.Split('.').Last();
                    string value = prop.First().ToString();

                    payload.AddStringParam(key, value);
                }
            }

            payload.AddStringParam("databaseConnectionString", databaseConnectionString);
            payload.AddStringParam("factoryName", dataFactoryName);
            payload.AddStringParam("startTime", DateTime.UtcNow.ToString());

            var armTemplate      = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, request.DataStore.GetValue("AzureArmFile"))));
            var armParamTemplate = JsonUtility.GetJObjectFromObject(payload.GetDynamicObject());

            armTemplate.Remove("parameters");
            armTemplate.Add("parameters", armParamTemplate["parameters"]);

            var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
            {
                Properties = new DeploymentPropertiesExtended()
                {
                    Template   = armTemplate.ToString(),
                    Parameters = JsonUtility.GetEmptyJObject().ToString()
                }
            };

            ResourceManagementClient client = new ResourceManagementClient(new TokenCloudCredentials(idSubscription, tokenAzure));
            var validate = await client.Deployments.ValidateAsync(nameResourceGroup, deploymentName, deployment, new CancellationToken());

            if (!validate.IsValid)
            {
                return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null, DefaultErrorCodes.DefaultErrorCode,
                                          $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
            }

            var deploymentItem = await client.Deployments.CreateOrUpdateAsync(nameResourceGroup, deploymentName, deployment, new CancellationToken());

            ActionResponse r = await WaitForAction(client, nameResourceGroup, deploymentName);

            request.DataStore.AddToDataStore("ArmOutput", r.DataStore.GetValue("ArmOutput"), DataStoreType.Public);

            if (!r.IsSuccess)
            {
                return(r);
            }

            // Active the function trigger
            AzureHttpClient httpClient = new AzureHttpClient(tokenAzure, idSubscription, nameResourceGroup);
            var             response   = await httpClient.ExecuteWithSubscriptionAndResourceGroupAsync(
                HttpMethod.Post,
                $"providers/Microsoft.DataFactory/factories/{dataFactoryName}/triggers/DefaultTrigger/start",
                "2017-09-01-preview",
                string.Empty);

            if (!response.IsSuccessStatusCode)
            {
                var error = await response.Content.ReadAsStringAsync();

                return(new ActionResponse(ActionStatus.Failure, error, null, DefaultErrorCodes.DefaultErrorCode, "Active trigger"));
            }

            return(r);
        }
        public void CreatesResourceGroupWithDeploymentFromTemplateParameterObject()
        {
            Uri templateUri = new Uri("http://templateuri.microsoft.com");
            Deployment deploymentFromGet = new Deployment();
            Deployment deploymentFromValidate = new Deployment();
            CreatePSResourceGroupParameters parameters = new CreatePSResourceGroupParameters()
            {
                ResourceGroupName = resourceGroupName,
                Location = resourceGroupLocation,
                DeploymentName = deploymentName,
                TemplateFile = templateFile,
                TemplateParameterObject = new Hashtable()
                {
                    { "string", "myvalue" },
                    { "securestring", "myvalue" },
                    { "int", 12 },
                    { "bool", true },
                },
                StorageAccountName = storageAccountName,
                ConfirmAction = ConfirmAction
            };
            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = false
                }));

            resourceGroupMock.Setup(f => f.CreateOrUpdateAsync(
                parameters.ResourceGroupName,
                It.IsAny<ResourceGroup>(),
                new CancellationToken()))
                    .Returns(Task.Factory.StartNew(() => new ResourceGroupCreateOrUpdateResult
                    {
                        ResourceGroup = new ResourceGroupExtended() { Name = parameters.ResourceGroupName, Location = parameters.Location }
                    }));
            resourceGroupMock.Setup(f => f.GetAsync(resourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupGetResult
                {
                    ResourceGroup = new ResourceGroupExtended() { Location = resourceGroupLocation }
                }));
            deploymentsMock.Setup(f => f.CreateOrUpdateAsync(resourceGroupName, deploymentName, It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsCreateResult
                {
                    RequestId = requestId
                }))
                .Callback((string name, string dName, Deployment bDeploy, CancellationToken token) => { deploymentFromGet = bDeploy; });
            deploymentsMock.Setup(f => f.GetAsync(resourceGroupName, deploymentName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = new DeploymentExtended()
                        {
                            Name = deploymentName,
                            Properties = new DeploymentPropertiesExtended()
                            {
                                Mode = DeploymentMode.Incremental,
                                ProvisioningState = ProvisioningState.Succeeded
                            },
                        }
                }));
            deploymentsMock.Setup(f => f.ValidateAsync(resourceGroupName, It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentValidateResponse
                {
                    IsValid = true,
                    Error = new ResourceManagementErrorWithDetails()
                }))
                .Callback((string rg, string dn, Deployment d, CancellationToken c) => { deploymentFromValidate = d; });
            SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResourceExtended>() { new GenericResourceExtended() { Name = "website" } });
            deploymentOperationsMock.Setup(f => f.ListAsync(resourceGroupName, deploymentName, null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        new DeploymentOperation()
                        {
                            OperationId = Guid.NewGuid().ToString(),
                            Properties = new DeploymentOperationProperties()
                            {
                                ProvisioningState = ProvisioningState.Succeeded,
                                TargetResource = new TargetResource()
                                {
                                    ResourceType = "Microsoft.Website",
                                    ResourceName = resourceName
                                }
                            }
                        }
                    }
                }));

            PSResourceGroup result = resourcesClient.CreatePSResourceGroup(parameters);

            deploymentsMock.Verify((f => f.CreateOrUpdateAsync(resourceGroupName, deploymentName, deploymentFromGet, new CancellationToken())), Times.Once());
            Assert.Equal(parameters.ResourceGroupName, result.ResourceGroupName);
            Assert.Equal(parameters.Location, result.Location);
            Assert.Equal(1, result.Resources.Count);

            Assert.Equal(DeploymentMode.Incremental, deploymentFromGet.Properties.Mode);
            Assert.NotNull(deploymentFromGet.Properties.Template);
            // Skip: Test produces different outputs since hashtable order is not guaranteed.
            //EqualsIgnoreWhitespace(File.ReadAllText(templateParameterFile), deploymentFromGet.Parameters);

            // Skip: Test produces different outputs since hashtable order is not guaranteed.
            //EqualsIgnoreWhitespace(File.ReadAllText(templateParameterFile), deploymentFromValidate.Parameters);

            progressLoggerMock.Verify(
                f => f(string.Format("Resource {0} '{1}' provisioning status is {2}",
                        "Microsoft.Website",
                        resourceName,
                        ProvisioningState.Succeeded.ToLower())),
                Times.Once());
        }
        public void DeploymentTestsValidateSuccess()
        {
            var handler = new RecordedDelegatingHandler() { StatusCodeToReturn = HttpStatusCode.OK };

            var client = GetResourceManagementClient(handler);

            var parameters = new Deployment
            {
                Properties = new DeploymentProperties()
                {
                    TemplateLink = new TemplateLink
                    {
                        Uri = new Uri("http://abc/def/template.json"),
                        ContentVersion = "1.0.0.0",
                    },
                    Mode = DeploymentMode.Incremental
                }
            };

            var result = client.Deployments.Validate("foo", "bar", parameters);

            JObject json = JObject.Parse(handler.Request);

            // Validate headers
            Assert.Equal("application/json; charset=utf-8", handler.ContentHeaders.GetValues("Content-Type").First());
            Assert.Equal(HttpMethod.Post, handler.Method);
            Assert.NotNull(handler.RequestHeaders.GetValues("Authorization"));

            // Validate result
            Assert.True(result.IsValid);
            Assert.Null(result.Error);
        }
        public void TestTemplateShowsSuccessMessage()
        {
            Uri templateUri = new Uri("http://templateuri.microsoft.com");
            Deployment deploymentFromValidate = new Deployment();
            ValidatePSResourceGroupDeploymentParameters parameters = new ValidatePSResourceGroupDeploymentParameters()
            {
                ResourceGroupName = resourceGroupName,
                TemplateFile = templateFile,
                StorageAccountName = storageAccountName,
            };
            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = true
                }));
            deploymentsMock.Setup(f => f.ValidateAsync(resourceGroupName, It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentValidateResponse
                {
                    IsValid = true,
                    Error = new ResourceManagementErrorWithDetails()
                    {
                        Code = "404",
                        Message = "Awesome error message",
                        Details = new List<ResourceManagementError>(new[] { new ResourceManagementError
                            {
                                Code = "SubError",
                                Message = "Sub error message"
                            }})
                    }
                }))
                .Callback((string rg, string dn, Deployment d, CancellationToken c) => { deploymentFromValidate = d; });

            IEnumerable<PSResourceManagerError> error = resourcesClient.ValidatePSResourceGroupDeployment(parameters, DeploymentMode.Incremental);
            Assert.Equal(0, error.Count());
            progressLoggerMock.Verify(f => f("Template is valid."), Times.Once());
        }
        public void NewResourceGroupFailsWithInvalidDeployment()
        {
            Uri templateUri = new Uri("http://templateuri.microsoft.com");
            Deployment deploymentFromGet = new Deployment();
            Deployment deploymentFromValidate = new Deployment();
            CreatePSResourceGroupParameters parameters = new CreatePSResourceGroupParameters()
            {
                ResourceGroupName = resourceGroupName,
                Location = resourceGroupLocation,
                DeploymentName = deploymentName,
                TemplateFile = templateFile,
                StorageAccountName = storageAccountName,
                ConfirmAction = ConfirmAction
            };
            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = false
                }));

            resourceGroupMock.Setup(f => f.CreateOrUpdateAsync(
                parameters.ResourceGroupName,
                It.IsAny<ResourceGroup>(),
                new CancellationToken()))
                    .Returns(Task.Factory.StartNew(() => new ResourceGroupCreateOrUpdateResult
                    {
                        ResourceGroup = new ResourceGroupExtended() { Name = parameters.ResourceGroupName, Location = parameters.Location }
                    }));
            resourceGroupMock.Setup(f => f.GetAsync(resourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupGetResult
                {
                    ResourceGroup = new ResourceGroupExtended() { Location = resourceGroupLocation }
                }));
            deploymentsMock.Setup(f => f.CreateOrUpdateAsync(resourceGroupName, deploymentName, It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsCreateResult
                {
                    RequestId = requestId
                }))
                .Callback((string name, string dName, Deployment bDeploy, CancellationToken token) => { deploymentFromGet = bDeploy; });
            deploymentsMock.Setup(f => f.GetAsync(resourceGroupName, deploymentName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentGetResult
                {
                    Deployment = new DeploymentExtended()
                        {
                            Name = deploymentName,
                            Properties = new DeploymentPropertiesExtended()
                            {
                                Mode = DeploymentMode.Incremental,
                                ProvisioningState = ProvisioningState.Succeeded
                            },
                        }
                }));
            deploymentsMock.Setup(f => f.ValidateAsync(resourceGroupName, It.IsAny<string>(), It.IsAny<Deployment>(), new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentValidateResponse
                {
                    Error = new ResourceManagementErrorWithDetails()
                        {
                            Code = "404",
                            Message = "Awesome error message",
                            Target = "Bad deployment"
                        }
                }))
                .Callback((string rg, string dn, Deployment d, CancellationToken c) => { deploymentFromValidate = d; });
            SetupListForResourceGroupAsync(parameters.ResourceGroupName, new List<GenericResourceExtended>() { new GenericResourceExtended() { Name = "website" } });
            deploymentOperationsMock.Setup(f => f.ListAsync(resourceGroupName, deploymentName, null, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new DeploymentOperationsListResult
                {
                    Operations = new List<DeploymentOperation>()
                    {
                        new DeploymentOperation()
                        {
                            OperationId = Guid.NewGuid().ToString(),
                            Properties = new DeploymentOperationProperties()
                            {
                                ProvisioningState = ProvisioningState.Succeeded,
                                TargetResource = new TargetResource()
                                {
                                    ResourceName = resourceName,
                                    ResourceType = "Microsoft.Website"
                                }
                            }
                        }
                    }
                }));

            Assert.Throws<ArgumentException>(() => resourcesClient.CreatePSResourceGroup(parameters));
        }
        public DeploymentExtended ProvisionDeploymentStatus(string resourceGroup, string deploymentName, Deployment deployment)
        {
            operations = new List<DeploymentOperation>();

            return WaitDeploymentStatus(
                resourceGroup,
                deploymentName,
                deployment,
                WriteDeploymentProgress,
                ProvisioningState.Canceled,
                ProvisioningState.Succeeded,
                ProvisioningState.Failed);
        }
        private Deployment CreateBasicDeployment(ValidatePSResourceGroupDeploymentParameters parameters, DeploymentMode deploymentMode)
        {
            Deployment deployment = new Deployment
            {
                Properties = new DeploymentProperties {
                    Mode = deploymentMode
                }
            };

            if (Uri.IsWellFormedUriString(parameters.TemplateFile, UriKind.Absolute))
            {
                deployment.Properties.TemplateLink = new TemplateLink
                {
                    Uri = new Uri(parameters.TemplateFile)
                };
            }
            else if (!string.IsNullOrEmpty(parameters.GalleryTemplateIdentity))
            {
                deployment.Properties.TemplateLink = new TemplateLink
                {
                    Uri = new Uri(GalleryTemplatesClient.GetGalleryTemplateFile(parameters.GalleryTemplateIdentity))
                };
            }
            else
            {
                deployment.Properties.Template = FileUtilities.DataStore.ReadFileAsText(parameters.TemplateFile);
            }

            if (Uri.IsWellFormedUriString(parameters.ParameterUri, UriKind.Absolute))
            {
                deployment.Properties.ParametersLink = new ParametersLink
                {
                    Uri = new Uri(parameters.ParameterUri)
                };
            }
            else
            {
                deployment.Properties.Parameters = GetDeploymentParameters(parameters.TemplateParameterObject);
            }

            return deployment;
        }
        private DeploymentExtended WaitDeploymentStatus(
            string resourceGroup,
            string deploymentName,
            Deployment basicDeployment,
            Action<string, string, Deployment> job,
            params string[] status)
        {
            DeploymentExtended deployment;

            do
            {
                if (job != null)
                {
                    job(resourceGroup, deploymentName, basicDeployment);
                }

                deployment = ResourceManagementClient.Deployments.Get(resourceGroup, deploymentName).Deployment;
                TestMockSupport.Delay(10000);

            } while (!status.Any(s => s.Equals(deployment.Properties.ProvisioningState, StringComparison.OrdinalIgnoreCase)));

            return deployment;
        }
        public void DeploymentTestsCreateValidateMessage()
        {
            var response = new HttpResponseMessage(HttpStatusCode.Created)
            {
                Content = new StringContent(@"{
                    'id': 'foo',
                    'name':'myrealease-3.14',
                    'properties':{
                        'provisioningState':'Succeeded',    
                        'timestamp':'2014-01-05T12:30:43.00Z',
                        'mode':'Incremental',
                        'template': { 'api-version' : '123' },
		                'templateLink': {
                           'uri': 'http://wa/template.json',
                           'contentVersion': '1.0.0.0',
                           'contentHash': {
                              'algorithm': 'sha256',
                              'value': 'yyz7xhhshfasf',
                           }
                        },
                        'parametersLink': { /* use either one of parameters or parametersLink */
                           'uri': 'http://wa/parameters.json',
                           'contentVersion': '1.0.0.0',
                           'contentHash': {
                              'algorithm': 'sha256',
                              'value': 'yyz7xhhshfasf',
                           }
                        },
                        'parameters': {
                            'key' : {
                                'type':'string',           
                                'value':'user'
                            }
		                },
                        'outputs': {
                            'key' : {
                                'type':'string',           
                                'value':'user'
                            }
                        }        
                    }
                }")
            };

            var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.Created };

            var client = GetResourceManagementClient(handler);
            var dictionary = new Dictionary<string, object> {
                    {"param1", "value1"},
                    {"param2", true},
                    {"param3", new Dictionary<string, object>() {
                        {"param3_1", 123},
                        {"param3_2", "value3_2"},
                    }}
                };
            var serializedDictionary = JsonConvert.SerializeObject(dictionary, new JsonSerializerSettings
            {
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeNameHandling = TypeNameHandling.None,
                Formatting = Formatting.Indented
            });
            var parameters = new Deployment
            {
                Properties = new DeploymentProperties()
                {
                    Template = "{'api-version':'123'}",
                    TemplateLink = new TemplateLink
                        {
                            Uri = new Uri("http://abc/def/template.json"),
                            ContentVersion = "1.0.0.0"
                        },
                    Parameters = serializedDictionary,
                    ParametersLink = new ParametersLink
                    {
                        Uri = new Uri("http://abc/def/template.json"),
                        ContentVersion = "1.0.0.0"
                    },
                    Mode = DeploymentMode.Incremental
                }
            };

            var result = client.Deployments.CreateOrUpdate("foo", "myrealease-3.14", parameters); 

            JObject json = JObject.Parse(handler.Request);

            // Validate headers 
            Assert.Equal("application/json; charset=utf-8", handler.ContentHeaders.GetValues("Content-Type").First());
            Assert.Equal(HttpMethod.Put, handler.Method);
            Assert.NotNull(handler.RequestHeaders.GetValues("Authorization"));

            // Validate payload
            Assert.Equal("Incremental", json["properties"]["mode"].Value<string>());
            Assert.Equal("http://abc/def/template.json", json["properties"]["templateLink"]["uri"].Value<string>());
            Assert.Equal("1.0.0.0", json["properties"]["templateLink"]["contentVersion"].Value<string>());
            Assert.Equal("1.0.0.0", json["properties"]["parametersLink"]["contentVersion"].Value<string>());
            Assert.Equal("value1", json["properties"]["parameters"]["param1"].Value<string>());
            Assert.Equal(true, json["properties"]["parameters"]["param2"].Value<bool>());
            Assert.Equal(123, json["properties"]["parameters"]["param3"]["param3_1"].Value<int>());
            Assert.Equal("value3_2", json["properties"]["parameters"]["param3"]["param3_2"].Value<string>());
            Assert.Equal(123, json["properties"]["template"]["api-version"].Value<int>());

            // Validate result
            Assert.Equal("foo", result.Deployment.Id);
            Assert.Equal("myrealease-3.14", result.Deployment.Name);
            Assert.Equal("Succeeded", result.Deployment.Properties.ProvisioningState);
            Assert.Equal(new DateTime(2014, 1, 5, 12, 30, 43), result.Deployment.Properties.Timestamp);
            Assert.Equal(DeploymentMode.Incremental, result.Deployment.Properties.Mode);
            Assert.Equal("http://wa/template.json", result.Deployment.Properties.TemplateLink.Uri.ToString());
            Assert.Equal("1.0.0.0", result.Deployment.Properties.TemplateLink.ContentVersion);
            Assert.True(result.Deployment.Properties.Parameters.Contains("\"type\": \"string\""));
            Assert.True(result.Deployment.Properties.Outputs.Contains("\"type\": \"string\""));
        }
        private TemplateValidationInfo CheckBasicDeploymentErrors(string resourceGroup, string deploymentName, Deployment deployment)
        {
            DeploymentValidateResponse validationResult = ResourceManagementClient.Deployments.Validate(
                resourceGroup,
                deploymentName,
                deployment);

            return new TemplateValidationInfo(validationResult);
        }
        public void DeploymentTestsValidateCheckPayload()
        {
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(@"{
                      'error': {
                        'code': 'InvalidTemplate',
                        'message': 'Deployment template validation failed.'
                      }
                    }")
            };
            var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.BadRequest };

            var client = GetResourceManagementClient(handler);
            var dictionary = new Dictionary<string, object> {
                    {"param1", "value1"},
                    {"param2", true},
                    {"param3", new Dictionary<string, object>() {
                        {"param3_1", 123},
                        {"param3_2", "value3_2"},
                    }}
                };
            var serializedDictionary = JsonConvert.SerializeObject(dictionary, new JsonSerializerSettings
            {
                TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeNameHandling = TypeNameHandling.None
            });
            var parameters = new Deployment
            {
                Properties = new DeploymentProperties()
                {
                    TemplateLink = new TemplateLink
                    {
                        Uri = new Uri("http://abc/def/template.json"),
                        ContentVersion = "1.0.0.0",
                    },
                    Parameters = serializedDictionary,
                    Mode = DeploymentMode.Incremental
                }
            };

            var result = client.Deployments.Validate("foo", "bar", parameters);

            JObject json = JObject.Parse(handler.Request);

            // Validate headers
            Assert.Equal("application/json; charset=utf-8", handler.ContentHeaders.GetValues("Content-Type").First());
            Assert.Equal(HttpMethod.Post, handler.Method);
            Assert.NotNull(handler.RequestHeaders.GetValues("Authorization"));

            // Validate payload
            Assert.Equal("Incremental", json["properties"]["mode"].Value<string>());
            Assert.Equal("http://abc/def/template.json", json["properties"]["templateLink"]["uri"].Value<string>());
            Assert.Equal("1.0.0.0", json["properties"]["templateLink"]["contentVersion"].Value<string>());
            Assert.Equal("value1", json["properties"]["parameters"]["param1"].Value<string>());
            Assert.Equal(true, json["properties"]["parameters"]["param2"].Value<bool>());
            Assert.Equal(123, json["properties"]["parameters"]["param3"]["param3_1"].Value<int>());
            Assert.Equal("value3_2", json["properties"]["parameters"]["param3"]["param3_2"].Value<string>());
        }
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            List <Task <ActionResponse> > task = new List <Task <ActionResponse> >();
            var    token         = request.DataStore.GetJson("AzureToken")["access_token"].ToString();
            var    subscription  = request.DataStore.GetJson("SelectedSubscription")["SubscriptionId"].ToString();
            var    resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
            string schema        = "dbo";

            string postDeploymentPipelineType = request.DataStore.GetValue("postDeploymentPipelineType");
            string pipelineFrequency          = request.DataStore.GetValue("pipelineFrequency");
            string pipelineInterval           = request.DataStore.GetValue("pipelineInterval");
            string pipelineType  = request.DataStore.GetValue("pipelineType");
            string pipelineStart = request.DataStore.GetValue("pipelineStart");
            string pipelineEnd   = request.DataStore.GetValue("pipelineEnd");
            string connString    = request.DataStore.GetValue("SqlConnectionString");

            string sfUsername = request.DataStore.GetValue("SalesforceUser");
            string sfPassword = request.DataStore.GetValue("SalesforcePassword");
            string sfToken    = request.DataStore.GetValue("SalesforceToken");

            if (!string.IsNullOrWhiteSpace(postDeploymentPipelineType))
            {
                pipelineFrequency = request.DataStore.GetValue("postDeploymentPipelineFrequency");
                pipelineType      = postDeploymentPipelineType;
                pipelineInterval  = request.DataStore.GetValue("postDeploymentPipelineInterval");
            }

            string adfJsonData = request.DataStore.GetValue("ADFPipelineJsonData");
            var    sqlCreds    = SqlUtility.GetSqlCredentialsFromConnectionString(connString);

            var obj = JsonConvert.DeserializeObject(adfJsonData, typeof(DeserializedADFPayload)) as DeserializedADFPayload;

            foreach (var o in obj.fields)
            {
                var deploymentName = string.Concat("ADFDataset", pipelineType, o.Item1);

                dynamic datasetParam = new AzureArmParameterGenerator();
                datasetParam.AddStringParam("dataFactoryName", resourceGroup + "SalesforceCopyFactory");
                datasetParam.AddStringParam("sqlServerName", sqlCreds.Server.Split('.')[0]);
                datasetParam.AddStringParam("sqlServerUsername", sqlCreds.Username);
                datasetParam.AddStringParam("targetDatabaseName", sqlCreds.Database);
                datasetParam.AddStringParam("targetSqlSchema", schema);
                datasetParam.AddStringParam("targetSqlTable", o.Item1);
                datasetParam.AddStringParam("salesforceUsername", sfUsername);
                datasetParam.AddStringParam("targetSalesforceTable", o.Item1);
                datasetParam.AddStringParam("pipelineName", o.Item1 + "CopyPipeline");
                datasetParam.AddStringParam("sqlWritableTypeName", o.Item1 + "Type");
                datasetParam.AddStringParam("sqlWriterStoredProcedureName", "spMerge" + o.Item1);
                datasetParam.AddStringParam("pipelineStartDate", pipelineStart);
                datasetParam.AddStringParam("pipelineEndDate", pipelineEnd);
                datasetParam.AddStringParam("sliceFrequency", pipelineFrequency);
                datasetParam.AddStringParam("sliceInterval", pipelineInterval);
                datasetParam.AddStringParam("pipelineType", pipelineType);
                datasetParam.AddParameter("salesforcePassword", "securestring", sfPassword);
                datasetParam.AddParameter("sqlServerPassword", "securestring", sqlCreds.Password);
                datasetParam.AddParameter("salesforceSecurityToken", "securestring", sfToken);

                var armTemplate      = JsonUtility.GetJsonObjectFromJsonString(System.IO.File.ReadAllText(Path.Combine(request.Info.App.AppFilePath, "Service/ADF/datasets.json")));
                var armParamTemplate = JsonUtility.GetJObjectFromObject(datasetParam.GetDynamicObject());

                armTemplate.Remove("parameters");
                armTemplate.Add("parameters", armParamTemplate["parameters"]);

                string        tableFields    = JsonConvert.SerializeObject(o.Item2);
                StringBuilder query          = CreateQuery(o, tableFields);
                string        stringTemplate = ReplaceTableFieldsAndQuery(tableFields, query, armTemplate);

                var creds  = new TokenCloudCredentials(subscription, token);
                var client = new ResourceManagementClient(creds);

                var deployment = new Microsoft.Azure.Management.Resources.Models.Deployment()
                {
                    Properties = new DeploymentPropertiesExtended()
                    {
                        Template   = stringTemplate,
                        Parameters = JsonUtility.GetEmptyJObject().ToString()
                    }
                };

                var validate = client.Deployments.ValidateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;
                if (!validate.IsValid)
                {
                    return(new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromObject(validate), null,
                                              DefaultErrorCodes.DefaultErrorCode, $"Azure:{validate.Error.Message} Details:{validate.Error.Details}"));
                }

                task.Add(new Task <ActionResponse>(() =>
                {
                    var deploymentItem = client.Deployments.CreateOrUpdateAsync(resourceGroup, deploymentName, deployment, new CancellationToken()).Result;

                    var helper = new DeploymentHelper();
                    return(helper.WaitForDeployment(resourceGroup, deploymentName, client));
                }));
            }

            foreach (var t in task)
            {
                t.Start();
            }

            Task.WaitAll(task.ToArray());

            foreach (var t in task)
            {
                if (t.Result.Status != ActionStatus.Success)
                {
                    return(new ActionResponse(ActionStatus.Failure, t.Result.ExceptionDetail.FriendlyErrorMessage));
                }
            }

            return(new ActionResponse(ActionStatus.Success));
        }
        public void DeploymentTestsValidateComplexFailure()
        {
            var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(@"{
                      'error': {
                        'code': 'InvalidTemplate',
                        'target': '',
                        'message': 'Deployment template validation failed: The template parameters hostingPlanName, siteMode, computeMode are not valid; they are not present.',
                        'details': [
                          {
                            'code': 'Error1',
                            'message': 'Deployment template validation failed.'
                          },
                          {
                            'code': 'Error2',
                            'message': 'Deployment template validation failed.'
                          } 
                        ]
                      }
                    }")
            };
            var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.BadRequest };

            var client = GetResourceManagementClient(handler);
            var parameters = new Deployment
            {
                Properties = new DeploymentProperties()
                {
                    TemplateLink = new TemplateLink
                    {
                        Uri = new Uri("http://abc/def/template.json"),
                        ContentVersion = "1.0.0.0",
                    },
                    Mode = DeploymentMode.Incremental
                }
            };

            var result = client.Deployments.Validate("foo", "bar", parameters);

            JObject json = JObject.Parse(handler.Request);

            // Validate result
            Assert.False(result.IsValid);
            Assert.Equal("InvalidTemplate", result.Error.Code);
            Assert.Equal("Deployment template validation failed: The template parameters hostingPlanName, siteMode, computeMode are not valid; they are not present.", result.Error.Message);
            Assert.Equal("", result.Error.Target);
            Assert.True(result.Error.Details.Contains("Error1"));
            Assert.True(result.Error.Details.Contains("Error2"));
        }