public async virtual Task <ArmDeploymentPropertiesExtended> DeployLocalTemplateAsync <TParameters>(
        string templateName,
        TParameters parameters,
        ResourceIdentifier scope,
        CancellationToken cancellationToken)
    {
        var armClient = new ArmClient(_credential);
        var rgClient  = armClient.GetResourceGroupResource(scope);

        var template = File.ReadAllText(Path.Combine(Assembly.GetExecutingAssembly().Location, "..", templateName + ".json"));
        var props    = new ArmDeploymentProperties(ArmDeploymentMode.Incremental)
        {
            Template   = BinaryData.FromString(template),
            Parameters = BinaryData.FromObjectAsJson(parameters),
        };

        _logger.LogInformation("Beginning deployment of {}.json", templateName);
        var deploymentOperation = await rgClient.GetArmDeployments().CreateOrUpdateAsync(WaitUntil.Completed, templateName, new ArmDeploymentContent(props), cancellationToken);

        var deployment = await deploymentOperation.WaitForCompletionAsync(cancellationToken);

        var result = deployment.Value.Data.Properties;

        if (result.ProvisioningState != ResourcesProvisioningState.Succeeded)
        {
            throw new Exception();
        }
        return(result);
    }
Beispiel #2
0
        protected static ArmDeploymentProperties CreateDeploymentPropertiesUsingJsonElement()
        {
            ArmDeploymentProperties tmpDeploymentProperties = new ArmDeploymentProperties(ArmDeploymentMode.Incremental);

            tmpDeploymentProperties.TemplateLink     = new ArmDeploymentTemplateLink();
            tmpDeploymentProperties.TemplateLink.Uri = new Uri("https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json");
            var parametersObject = new { storageAccountType = new { value = "Standard_GRS" } };
            //convert this object to JsonElement
            var parametersString = JsonSerializer.Serialize(parametersObject);
            var parameters       = JsonDocument.Parse(parametersString).RootElement;

            tmpDeploymentProperties.Parameters = BinaryData.FromString(parameters.GetRawText());
            return(tmpDeploymentProperties);
        }
Beispiel #3
0
        protected static ArmDeploymentProperties CreateDeploymentProperties()
        {
            ArmDeploymentProperties tmpDeploymentProperties = new ArmDeploymentProperties(ArmDeploymentMode.Incremental);

            tmpDeploymentProperties.TemplateLink     = new ArmDeploymentTemplateLink();
            tmpDeploymentProperties.TemplateLink.Uri = new Uri("https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json");
            tmpDeploymentProperties.Parameters       = BinaryData.FromObjectAsJson(new JsonObject()
            {
                { "storageAccountType", new JsonObject()
                  {
                      { "value", "Standard_GRS" }
                  } }
            });
            return(tmpDeploymentProperties);
        }
Beispiel #4
0
        protected static ArmDeploymentProperties CreateDeploymentPropertiesUsingString()
        {
            ArmDeploymentProperties tmpDeploymentProperties = new ArmDeploymentProperties(ArmDeploymentMode.Incremental);

            tmpDeploymentProperties.Template = BinaryData.FromString(File.ReadAllText(Path.Combine(
                                                                                          Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                                                                          "Scenario",
                                                                                          "DeploymentTemplates",
                                                                                          $"storage-template.json")));
            tmpDeploymentProperties.Parameters = BinaryData.FromString(File.ReadAllText(Path.Combine(
                                                                                            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                                                                                            "Scenario",
                                                                                            "DeploymentTemplates",
                                                                                            $"storage-parameters.json")));
            return(tmpDeploymentProperties);
        }
Beispiel #5
0
        protected static ArmDeploymentProperties CreateDeploymentPropertiesAtSub()
        {
            ArmDeploymentProperties tmpDeploymentProperties = new ArmDeploymentProperties(ArmDeploymentMode.Incremental);

            tmpDeploymentProperties.TemplateLink     = new ArmDeploymentTemplateLink();
            tmpDeploymentProperties.TemplateLink.Uri = new Uri("https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/emptyrg.json");
            tmpDeploymentProperties.Parameters       = BinaryData.FromObjectAsJson(new JsonObject()
            {
                { "rgName", new JsonObject()
                  {
                      { "value", "testDeployAtSub" }
                  } },
                { "rgLocation", new JsonObject()
                  {
                      { "value", $"{AzureLocation.CentralUS}" }
                  } },
            });
            return(tmpDeploymentProperties);
        }
Beispiel #6
0
 protected static ArmDeploymentContent CreateDeploymentData(ArmDeploymentProperties deploymentProperties, AzureLocation location) => new ArmDeploymentContent(deploymentProperties)
 {
     Location = location
 };
Beispiel #7
0
 protected static ArmDeploymentContent CreateDeploymentData(ArmDeploymentProperties deploymentProperties) => new ArmDeploymentContent(deploymentProperties);
Beispiel #8
0
        /// <summary>
        /// Starts a deployment at provided target scope and returns <see cref="BicepDeploymentStartResponse"/>.
        /// </summary>
        /// <param name="deploymentCollectionProvider">deployment collection provider</param>
        /// <param name="armClient">arm client</param>
        /// <param name="documentPath">path to bicep file used in deployment</param>
        /// <param name="template">template used in deployment</param>
        /// <param name="parametersFilePath">path to parameter file used in deployment</param>
        /// <param name="id">id string to create the ResourceIdentifier from</param>
        /// <param name="scope">target scope</param>
        /// <param name="location">location to store the deployment data</param>
        /// <param name="deploymentId">deployment id</param>
        /// <param name="parametersFileName">parameters file name</param>
        /// <param name="parametersFileUpdateOption"><see cref="ParametersFileUpdateOption"/>update, create or overwrite parameters file</param>
        /// <param name="updatedDeploymentParameters">parameters that were updated during deployment flow</param>
        /// <param name="portalUrl">azure management portal URL</param>
        /// <param name="deploymentName">deployment name</param>
        /// <param name="deploymentOperationsCache">deployment operations cache that needs to be updated</param>
        /// <returns><see cref="BicepDeploymentStartResponse"/></returns>
        public static async Task <BicepDeploymentStartResponse> StartDeploymentAsync(
            IDeploymentCollectionProvider deploymentCollectionProvider,
            ArmClient armClient,
            string documentPath,
            string template,
            string parametersFilePath,
            string id,
            string scope,
            string location,
            string deploymentId,
            string parametersFileName,
            ParametersFileUpdateOption parametersFileUpdateOption,
            List <BicepUpdatedDeploymentParameter> updatedDeploymentParameters,
            string portalUrl,
            string deploymentName,
            IDeploymentOperationsCache deploymentOperationsCache)
        {
            if ((scope == LanguageConstants.TargetScopeTypeSubscription ||
                 scope == LanguageConstants.TargetScopeTypeManagementGroup) &&
                string.IsNullOrWhiteSpace(location))
            {
                return(new BicepDeploymentStartResponse(false, string.Format(LangServerResources.MissingLocationDeploymentFailedMessage, documentPath), null));
            }

            ArmDeploymentCollection?deploymentCollection;
            var resourceIdentifier = new ResourceIdentifier(id);

            try
            {
                deploymentCollection = deploymentCollectionProvider.GetDeploymentCollection(armClient, resourceIdentifier, scope);
            }
            catch (Exception e)
            {
                return(new BicepDeploymentStartResponse(false, string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath, e.Message), null));
            }

            if (deploymentCollection is not null)
            {
                JsonElement parameters;

                try
                {
                    var updatedParametersFileContents = DeploymentParametersHelper.GetUpdatedParametersFileContents(documentPath, parametersFileName, parametersFilePath, parametersFileUpdateOption, updatedDeploymentParameters);
                    parameters = JsonElementFactory.CreateElement(updatedParametersFileContents);
                }
                catch (Exception e)
                {
                    return(new BicepDeploymentStartResponse(false, e.Message, null));
                }

                var deploymentProperties = new ArmDeploymentProperties(ArmDeploymentMode.Incremental)
                {
                    Template   = new BinaryData(JsonDocument.Parse(template).RootElement),
                    Parameters = new BinaryData(parameters)
                };
                var armDeploymentContent = new ArmDeploymentContent(deploymentProperties)
                {
                    Location = location,
                };

                try
                {
                    var deploymentOperation = await deploymentCollection.CreateOrUpdateAsync(WaitUntil.Started, deploymentName, armDeploymentContent);

                    if (deploymentOperation is null)
                    {
                        return(new BicepDeploymentStartResponse(false, string.Format(LangServerResources.DeploymentFailedMessage, documentPath), null));
                    }

                    deploymentOperationsCache.CacheDeploymentOperation(deploymentId, deploymentOperation);

                    var linkToDeploymentInAzurePortal = GetLinkToDeploymentInAzurePortal(portalUrl, id, deploymentName);

                    return(new BicepDeploymentStartResponse(
                               true,
                               string.Format(LangServerResources.DeploymentStartedMessage, documentPath),
                               string.Format(LangServerResources.ViewDeploymentInPortalMessage, linkToDeploymentInAzurePortal)));
                }
                catch (Exception e)
                {
                    return(new BicepDeploymentStartResponse(false, string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, documentPath, e.Message), null));
                }
            }

            return(new BicepDeploymentStartResponse(false, string.Format(LangServerResources.DeploymentFailedMessage, documentPath), null));
        }