Beispiel #1
0
        private async Task <Deployment> GetDeploymentPayload(DeployInputs inputs)
        {
            var basicDeployment = new Deployment();

            if (string.Equals(Constants.Repository.CustomTemplateFileName, inputs.templateUrl))
            {
                // it is private repo, we should pass over the content instead of a link to template
                string     token    = GetTokenFromHeader();
                Repository repo     = Repository.CreateRepositoryObj(inputs.repoUrl, Request.RequestUri.Host, token);
                JObject    template = await repo.DownloadTemplateAsync();

                PurgeCustomProperties(template);
                basicDeployment.Properties = new DeploymentProperties
                {
                    Parameters = inputs.parameters.ToString(),
                    Template   = (template).ToString()
                };
            }
            else
            {
                basicDeployment.Properties = new DeploymentProperties
                {
                    Parameters   = inputs.parameters.ToString(),
                    TemplateLink = new TemplateLink(new Uri(inputs.templateUrl))
                };
            }

            return(basicDeployment);
        }
Beispiel #2
0
        public async Task <HttpResponseMessage> Deploy(DeployInputs inputs)
        {
            CreateDeploymentResponse responseObj = new CreateDeploymentResponse();
            HttpResponseMessage      response    = null;

            try
            {
                using (var client = GetRMClient(inputs.subscriptionId))
                {
                    // For now we just default to East US for the resource group location.
                    var resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
                        inputs.resourceGroup.name,
                        new ResourceGroup { Location = inputs.resourceGroup.location });

                    var        templateParams  = inputs.parameters.ToString();
                    Deployment basicDeployment = await this.GetDeploymentPayload(inputs);

                    var deploymentResult = await client.Deployments.CreateOrUpdateAsync(
                        inputs.resourceGroup.name,
                        inputs.resourceGroup.name,
                        basicDeployment);

                    response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
                }
            }
            catch (CloudException ex)
            {
                responseObj.Error     = ex.ErrorMessage;
                responseObj.ErrorCode = ex.ErrorCode;
                response = Request.CreateResponse(HttpStatusCode.BadRequest, responseObj);
            }

            return(response);
        }
Beispiel #3
0
        private async Task <Deployment> GetDeploymentPayload(DeployInputs inputs)
        {
            var basicDeployment = new Deployment();

            // template usl is like https://github.com/DRediske/Contoso-Security-Demo/blob/PerfTest/azuredeploy.json
            // while Constants.Repository.CustomTemplateFileName is "azuredeploy.json"
            // should do end with comparison
            if (!string.IsNullOrWhiteSpace(inputs.templateUrl) && inputs.templateUrl.EndsWith(Constants.Repository.CustomTemplateFileName, StringComparison.OrdinalIgnoreCase))
            {
                // it is private repo, we should pass over the content instead of a link to template
                string     token    = GetTokenFromHeader();
                Repository repo     = Repository.CreateRepositoryObj(inputs.repoUrl, Request.RequestUri.Host, token);
                JObject    template = await repo.DownloadTemplateAsync();

                PurgeCustomProperties(template);
                basicDeployment.Properties = new DeploymentProperties
                {
                    Parameters = inputs.parameters.ToString(),
                    Template   = (template).ToString()
                };
            }
            else
            {
                basicDeployment.Properties = new DeploymentProperties
                {
                    Parameters   = inputs.parameters.ToString(),
                    TemplateLink = new TemplateLink(new Uri(inputs.templateUrl))
                };
            }

            return(basicDeployment);
        }
Beispiel #4
0
#pragma warning disable 4014
        public async Task <HttpResponseMessage> Preview(DeployInputs inputs)
        {
            JObject             responseObj = new JObject();
            List <string>       providers   = new List <string>(32);
            HttpResponseMessage response    = null;

            using (var client = GetRMClient(inputs.subscriptionId))
            {
                ResourceGroupCreateOrUpdateResult resourceResult = null;
                string tempRGName = Guid.NewGuid().ToString();

                try
                {
                    resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
                        tempRGName,
                        new ResourceGroup { Location = inputs.resourceGroup.location });

                    Deployment basicDeployment = await this.GetDeploymentPayload(inputs);

                    var deploymentResult = await client.Deployments.ValidateAsync(tempRGName, tempRGName, basicDeployment);

                    if (deploymentResult.StatusCode == HttpStatusCode.OK)
                    {
                        foreach (var p in deploymentResult.Properties.Providers)
                        {
                            if (sm_providerMap.ContainsKey(p.Namespace))
                            {
                                providers.Add(sm_providerMap[p.Namespace]);
                            }
                            else
                            {
                                providers.Add(p.Namespace);
                            }
                        }

                        responseObj["providers"] = JArray.FromObject(providers);
                        response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
                    }
                    else
                    {
                        responseObj["error"] = deploymentResult.Error.Message;
                        response             = Request.CreateResponse(deploymentResult.StatusCode, responseObj);
                    }
                }
                finally
                {
                    if (resourceResult != null &&
                        (resourceResult.StatusCode == HttpStatusCode.Created || resourceResult.StatusCode == HttpStatusCode.OK))
                    {
                        string token = GetTokenFromHeader();
                        Task.Run(() => { DeleteResourceGroup(inputs.subscriptionId, token, tempRGName); });
                    }
                }
            }

            return(response);
        }
Beispiel #5
0
        public async Task <HttpResponseMessage> Deploy(DeployInputs inputs)
        {
            CreateDeploymentResponse responseObj = new CreateDeploymentResponse();
            HttpResponseMessage      response    = null;


            var requestHost = HttpContext.Current.Request.UrlReferrer != null ? HttpContext.Current.Request.UrlReferrer.Host : null;

            if (requestHost != null)
            {
                // For Azure scenarios we do extra checks for cross domains
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME")))
                {
                    if (!string.Equals(requestHost, "deploy.azure.com", StringComparison.OrdinalIgnoreCase) &&
                        !string.Equals(requestHost, "deploy-staging.azure.com", StringComparison.OrdinalIgnoreCase))
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid request domain"));
                    }
                }
            }

            try
            {
                using (var client = GetRMClient(inputs.subscriptionId))
                {
                    // For now we just default to East US for the resource group location.
                    var resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
                        inputs.resourceGroup.name,
                        new ResourceGroup { Location = inputs.resourceGroup.location });

                    var        templateParams  = inputs.parameters.ToString();
                    Deployment basicDeployment = await this.GetDeploymentPayload(inputs);

                    var deploymentResult = await client.Deployments.CreateOrUpdateAsync(
                        inputs.resourceGroup.name,
                        inputs.resourceGroup.name,
                        basicDeployment);

                    response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
                }
            }
            catch (CloudException ex)
            {
                responseObj.Error     = ex.ErrorMessage;
                responseObj.ErrorCode = ex.ErrorCode;
                response = Request.CreateResponse(HttpStatusCode.BadRequest, responseObj);
            }

            return(response);
        }