public async Task CreateDeploymentsUsingJsonElement()
        {
            #region Snippet:Managing_Deployments_CreateADeploymentUsingJsonElement
            // First we need to get the deployment collection from the resource group
            DeploymentCollection deploymentCollection = resourceGroup.GetDeployments();
            // Use the same location as the resource group
            string deploymentName = "myDeployment";
            // Create a parameter object
            var parametersObject = new { storageAccountType = new { value = "Standard_GRS" } };
            //convert this object to JsonElement
            var parametersString = JsonSerializer.Serialize(parametersObject);
            var parameters       = JsonDocument.Parse(parametersString).RootElement;
            var input            = new DeploymentInput(new DeploymentProperties(DeploymentMode.Incremental)
            {
                TemplateLink = new TemplateLink()
                {
                    Uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json"
                },
                Parameters = parameters
            });
            DeploymentCreateOrUpdateOperation lro = await deploymentCollection.CreateOrUpdateAsync(true, deploymentName, input);

            Deployment deployment = lro.Value;
            #endregion Snippet:Managing_Deployments_CreateADeployment
        }
Example #2
0
        public async Task CreateDeployments()
        {
            #region Snippet:Managing_Deployments_CreateADeployment
            // First we need to get the deployment collection from the resource group
            DeploymentCollection deploymentCollection = resourceGroup.GetDeployments();
            // Use the same location as the resource group
            string deploymentName = "myDeployment";
            var    input          = new DeploymentInput(new DeploymentProperties(DeploymentMode.Incremental)
            {
                TemplateLink = new TemplateLink()
                {
                    Uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.storage/storage-account-create/azuredeploy.json"
                },
                Parameters = new JsonObject()
                {
                    { "storageAccountType", new JsonObject()
                      {
                          { "value", "Standard_GRS" }
                      } }
                }
            });
            DeploymentCreateOrUpdateAtScopeOperation lro = await deploymentCollection.CreateOrUpdateAsync(deploymentName, input);

            Deployment deployment = lro.Value;
            #endregion Snippet:Managing_Deployments_CreateADeployment
        }
Example #3
0
        public static JObject PrepareParameters(DeploymentInput deploymentInput, DeploymentOutput deploymentOutput, string path)
        {
            deploymentInput.AssertNotNull(nameof(deploymentInput));
            deploymentOutput.AssertNotNull(nameof(deploymentOutput));
            path.AssertNotEmpty(nameof(path));

            var parameterFileContents = TemplateHelper.GetJsonFileContents(path);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.AdminUserName][TemplateConstants.ValueSection] = new JValue(deploymentInput.VirtualMachineAdminUserName);
            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.AdminPassword][TemplateConstants.ValueSection] = new JValue(deploymentInput.VirtualMachineAdminUserNamePassword);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.UserName][TemplateConstants.ValueSection]     = new JValue(deploymentInput.VirtualMachineUserName);
            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.UserPassword][TemplateConstants.ValueSection] = new JValue(deploymentInput.VirtualMachineUserNamePassword);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.DomainNameLabel][TemplateConstants.ValueSection]    = new JValue(deploymentOutput.VirtualMachineDomainName);
            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.VirtualMachineName][TemplateConstants.ValueSection] = new JValue(deploymentOutput.VirtualMachineName);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.TagClassName][TemplateConstants.ValueSection] = new JValue(deploymentOutput.ClassName);
            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.TagClassId][TemplateConstants.ValueSection]   = new JValue(deploymentOutput.ClassId);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.TagStudentEmail][TemplateConstants.ValueSection] = new JValue(deploymentOutput.StudentEmailAddress);

            parameterFileContents[TemplateConstants.ParametersSection][TemplateConstants.TagStudentName][TemplateConstants.ValueSection] = new JValue(deploymentOutput.StudentName);

            return(parameterFileContents);
        }
        public virtual DeploymentValidateAtScopeOperation Validate(DeploymentInput parameters, bool waitForCompletion = true, CancellationToken cancellationToken = default)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _clientDiagnostics.CreateScope("Deployment.Validate");
            scope.Start();
            try
            {
                var response  = _deploymentsRestClient.ValidateAtScope(Id.Parent, Id.Name, parameters, cancellationToken);
                var operation = new DeploymentValidateAtScopeOperation(_clientDiagnostics, Pipeline, _deploymentsRestClient.CreateValidateAtScopeRequest(Id.Parent, Id.Name, parameters).Request, response);
                if (waitForCompletion)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        public async Task Get()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-3-");
            ResourceGroupData rgData = new ResourceGroupData(Location.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-G-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());
            Deployment      deployment     = (await rg.GetDeployments().CreateOrUpdateAsync(deployName, deploymentData)).Value;
            Deployment      getDeployment  = await rg.GetDeployments().GetAsync(deployName);

            AssertValidDeployment(deployment, getDeployment);
        }
        public async Task CreateOrUpdate()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-1-");
            ResourceGroupData rgData = new ResourceGroupData(Location.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-C-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());
            Deployment      deployment     = (await rg.GetDeployments().CreateOrUpdateAsync(deployName, deploymentData)).Value;

            Assert.AreEqual(deployName, deployment.Data.Name);
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await rg.GetDeployments().CreateOrUpdateAsync(null, deploymentData));
            Assert.ThrowsAsync <ArgumentNullException>(async() => _ = await rg.GetDeployments().CreateOrUpdateAsync(deployName, null));
        }
        public async Task CreateDeploymentsUsingString()
        {
            #region Snippet:Managing_Deployments_CreateADeploymentUsingString
            // First we need to get the deployment collection from the resource group
            DeploymentCollection deploymentCollection = resourceGroup.GetDeployments();
            // Use the same location as the resource group
            string deploymentName = "myDeployment";
            // Passing string to template and parameters
            var input = new DeploymentInput(new DeploymentProperties(DeploymentMode.Incremental)
            {
                Template   = File.ReadAllText("storage-template.json"),
                Parameters = File.ReadAllText("storage-parameters.json")
            });
            DeploymentCreateOrUpdateOperation lro = await deploymentCollection.CreateOrUpdateAsync(true, deploymentName, input);

            Deployment deployment = lro.Value;
            #endregion Snippet:Managing_Deployments_CreateADeployment
        }
Example #8
0
        public async Task Delete()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-4-");
            ResourceGroupData rgData = new ResourceGroupData(AzureLocation.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-D-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());
            Deployment      deployment     = (await rg.GetDeployments().CreateOrUpdateAsync(WaitUntil.Completed, deployName, deploymentData)).Value;
            await deployment.DeleteAsync(WaitUntil.Completed);

            var ex = Assert.ThrowsAsync <RequestFailedException>(async() => await deployment.GetAsync());

            Assert.AreEqual(404, ex.Status);
        }
Example #9
0
        public async Task Get()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-2-");
            ResourceGroupData rgData = new ResourceGroupData(AzureLocation.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());
            Deployment      deployment     = (await rg.GetDeployments().CreateOrUpdateAsync(WaitUntil.Completed, deployName, deploymentData)).Value;

            await foreach (var tempDeploymentOperation in deployment.GetDeploymentOperationsAsync())
            {
                DeploymentOperation getDeploymentOperation = await deployment.GetDeploymentOperationAsync(tempDeploymentOperation.OperationId);

                AssertValidDeploymentOperation(tempDeploymentOperation, getDeploymentOperation);
            }
        }
Example #10
0
        public async Task List()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-1-");
            ResourceGroupData rgData = new ResourceGroupData(AzureLocation.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());
            Deployment      deployment     = (await rg.GetDeployments().CreateOrUpdateAsync(WaitUntil.Completed, deployName, deploymentData)).Value;
            int             count          = 0;

            await foreach (var tempDeploymentOperation in deployment.GetDeploymentOperationsAsync())
            {
                count++;
            }
            Assert.AreEqual(count, 2); //One deployment contains two operations: Create and EvaluteDeploymentOutput
        }
        public async Task List()
        {
            Subscription subscription = await Client.GetDefaultSubscriptionAsync();

            string            rgName = Recording.GenerateAssetName("testRg-2-");
            ResourceGroupData rgData = new ResourceGroupData(Location.WestUS2);
            var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(rgName, rgData);

            ResourceGroup   rg             = lro.Value;
            string          deployName     = Recording.GenerateAssetName("deployEx-L-");
            DeploymentInput deploymentData = CreateDeploymentData(CreateDeploymentProperties());

            _ = await rg.GetDeployments().CreateOrUpdateAsync(deployName, deploymentData);

            int count = 0;

            await foreach (var tempDeployment in rg.GetDeployments().GetAllAsync())
            {
                count++;
            }
            Assert.AreEqual(count, 1);
        }
Example #12
0
        /// <summary>
        /// Creates a deployment at provided target scope and returns deployment succeeded/failed message.
        /// </summary>
        /// <param name="deploymentCollectionProvider">deployment collection provider</param>
        /// <param name="armClient">arm client</param>
        /// <param name="template">template used in deployment</param>
        /// <param name="parameterFilePath">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>
        /// <returns>deployment succeeded/failed message</returns>
        public static async Task <string> CreateDeployment(
            IDeploymentCollectionProvider deploymentCollectionProvider,
            ArmClient armClient,
            string template,
            string parameterFilePath,
            string id,
            string scope,
            string location)
        {
            if ((scope == LanguageConstants.TargetScopeTypeSubscription ||
                 scope == LanguageConstants.TargetScopeTypeManagementGroup) &&
                string.IsNullOrWhiteSpace(location))
            {
                return(LangServerResources.MissingLocationDeploymentFailedMessage);
            }

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

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

            if (deploymentCollection is not null)
            {
                JsonElement parameters;

                try
                {
                    parameters = GetParameters(parameterFilePath);
                }
                catch (Exception e)
                {
                    return(e.Message);
                }

                var deploymentProperties = new DeploymentProperties(DeploymentMode.Incremental)
                {
                    Template   = JsonDocument.Parse(template).RootElement,
                    Parameters = parameters
                };
                var input = new DeploymentInput(deploymentProperties)
                {
                    Location = location,
                };

                string deployment = "bicep_deployment_" + DateTime.UtcNow.ToString("yyyyMMddHHmmss");

                try
                {
                    var deploymentCreateOrUpdateOperation = await deploymentCollection.CreateOrUpdateAsync(waitForCompletion : true, deployment, input);

                    return(GetDeploymentResultMessage(deploymentCreateOrUpdateOperation));
                }
                catch (Exception e)
                {
                    return(string.Format(LangServerResources.DeploymentFailedWithExceptionMessage, e.Message));
                }
            }

            return(LangServerResources.DeploymentFailedMessage);
        }
Example #13
0
        public async virtual Task <DeploymentCreateOrUpdateAtScopeOperation> CreateOrUpdateAsync(string deploymentName, DeploymentInput parameters, bool waitForCompletion = true, CancellationToken cancellationToken = default)
        {
            if (deploymentName == null)
            {
                throw new ArgumentNullException(nameof(deploymentName));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _clientDiagnostics.CreateScope("DeploymentCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response = await _deploymentsRestClient.CreateOrUpdateAtScopeAsync(Id, deploymentName, parameters, cancellationToken).ConfigureAwait(false);

                var operation = new DeploymentCreateOrUpdateAtScopeOperation(Parent, _clientDiagnostics, Pipeline, _deploymentsRestClient.CreateCreateOrUpdateAtScopeRequest(Id, deploymentName, parameters).Request, response);
                if (waitForCompletion)
                {
                    await operation.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Example #14
0
        public virtual ArmOperation <DeploymentValidateResult> Validate(bool waitForCompletion, DeploymentInput parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(parameters, nameof(parameters));

            using var scope = _deploymentClientDiagnostics.CreateScope("Deployment.Validate");
            scope.Start();
            try
            {
                var response  = _deploymentRestClient.ValidateAtScope(Id.Parent, Id.Name, parameters, cancellationToken);
                var operation = new ResourcesArmOperation <DeploymentValidateResult>(new DeploymentValidateResultOperationSource(), _deploymentClientDiagnostics, Pipeline, _deploymentRestClient.CreateValidateAtScopeRequest(Id.Parent, Id.Name, parameters).Request, response, OperationFinalStateVia.Location);
                if (waitForCompletion)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        public virtual DeploymentCreateOrUpdateOperation CreateOrUpdate(bool waitForCompletion, string deploymentName, DeploymentInput parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(deploymentName, nameof(deploymentName));
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            using var scope = _deploymentClientDiagnostics.CreateScope("DeploymentCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response  = _deploymentRestClient.CreateOrUpdateAtScope(Id, deploymentName, parameters, cancellationToken);
                var operation = new DeploymentCreateOrUpdateOperation(ArmClient, _deploymentClientDiagnostics, Pipeline, _deploymentRestClient.CreateCreateOrUpdateAtScopeRequest(Id, deploymentName, parameters).Request, response);
                if (waitForCompletion)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Example #16
0
        public virtual ArmOperation <Deployment> CreateOrUpdate(WaitUntil waitUntil, string deploymentName, DeploymentInput parameters, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNullOrEmpty(deploymentName, nameof(deploymentName));
            Argument.AssertNotNull(parameters, nameof(parameters));

            using var scope = _deploymentClientDiagnostics.CreateScope("DeploymentCollection.CreateOrUpdate");
            scope.Start();
            try
            {
                var response  = _deploymentRestClient.CreateOrUpdateAtScope(Id, deploymentName, parameters, cancellationToken);
                var operation = new ResourcesArmOperation <Deployment>(new DeploymentOperationSource(Client), _deploymentClientDiagnostics, Pipeline, _deploymentRestClient.CreateCreateOrUpdateAtScopeRequest(Id, deploymentName, parameters).Request, response, OperationFinalStateVia.Location);
                if (waitUntil == WaitUntil.Completed)
                {
                    operation.WaitForCompletion(cancellationToken);
                }
                return(operation);
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }