示例#1
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;
        }
示例#2
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;
        }
示例#3
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;
        }