public void WhatIfAtSubscriptionScope_BlankTemplate_ReturnsNoChange() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new DeploymentWhatIf { Location = "westus", Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = BlankTemplate, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.ResourceIdOnly) } }; // Act. WhatIfOperationResult result = client.Deployments .WhatIfAtSubscriptionScope(NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.Empty(result.Changes); } }
public void WhatIf_BlankTemplate_ReturnsNoChange() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new DeploymentWhatIf { Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = BlankTemplate, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.FullResourcePayloads) } }; string resourceGroupName = NewResourceGroupName(); string deploymentName = NewDeploymentName(); client.ResourceGroups.CreateOrUpdate(resourceGroupName, ResourceGroup); // Act. WhatIfOperationResult result = client.Deployments .WhatIf(resourceGroupName, deploymentName, deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.Empty(result.Changes); } }
public void WhatIf_CreateResources_ReturnsCreateChanges() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new DeploymentWhatIf { Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = ResourceGroupTemplate, Parameters = ResourceGroupTemplateParameters, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.ResourceIdOnly) } }; string resourceGroupName = NewResourceGroupName(); string deploymentName = NewDeploymentName(); client.ResourceGroups.CreateOrUpdate(resourceGroupName, ResourceGroup); // Act. WhatIfOperationResult result = client.Deployments .WhatIf(resourceGroupName, deploymentName, deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); result.Changes.ForEach(change => Assert.Equal(ChangeType.Create, change.ChangeType)); } }
private void CheckResourceWhatIf(PredictTemplateOrchestrationInput input, WhatIfOperationResult result, Dictionary <string, JsonElement> asset, Resource resource) { if (asset.TryGetValue(resource.Name, out JsonElement r)) { if (input.ResultFormat == WhatIfResultFormat.ResourceIdOnly) { result.Changes.Add(new WhatIfChange() { ChangeType = ChangeType.Deploy }); } else { // TODO: support WhatIfResultFormat.FullResourcePayloads result.Changes.Add(new WhatIfChange() { ChangeType = ChangeType.Modify }); } asset.Remove(resource.Name); } else { result.Changes.Add(new WhatIfChange() { ChangeType = ChangeType.Create, ResourceId = resource.ResourceId }); } }
public void WhatIfAtTenantScope_FullResourcePayloadMode_ReturnsChangesWithPayloads() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new ScopedDeploymentWhatIf { Location = "westus", Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = TenantTemplate, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.FullResourcePayloads) } }; // Act. WhatIfOperationResult result = client.Deployments .WhatIfAtTenantScope(NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); result.Changes.ForEach(change => { Assert.NotNull(change.ResourceId); Assert.NotEmpty(change.ResourceId); Assert.True(change.Before != null || change.After != null); }); } }
public void WhatIfAtSubscriptionScope_ReceivingResponse_DeserializesResult() { // Arrange. var deploymentWhatIf = new DeploymentWhatIf(new DeploymentWhatIfProperties()); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ 'status': 'Succeeded', 'properties': { 'changes': [ { 'resourceId': '/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/myResourceGroup', 'changeType': 'Create', 'after': { 'apiVersion': '2019-05-01', 'id': '/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/myResourceGroup', 'type': 'Microsoft.Resources/resourceGroups', 'name': 'myResourceGroup', 'location': 'eastus', } } ] } }") }; var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.OK }; using (ResourceManagementClient client = CreateResourceManagementClient(handler)) { // Act. WhatIfOperationResult result = client.Deployments.WhatIfAtSubscriptionScope("test-subscription-deploy", deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.Equal(1, result.Changes.Count); WhatIfChange change = result.Changes[0]; Assert.Equal(ChangeType.Create, change.ChangeType); Assert.Null(change.Before); Assert.Null(change.Delta); Assert.NotNull(change.After); Assert.Equal("myResourceGroup", JToken.FromObject(change.After)["name"]); } }
public void WhatIfAtManagementGroupScope_ReceivingResponse_DeserializesResult() { // Arrange. var deploymentWhatIf = new ScopedDeploymentWhatIf("westus", new DeploymentWhatIfProperties()); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ 'status': 'Succeeded', 'properties': { 'changes': [ { 'resourceId': '/providers/Microsoft.Management/managementGroups/myManagementGroup/providers/Microsoft.Authorization/policyDefinitions/testDef', 'changeType': 'Create', 'after': { 'apiVersion': '2018-03-01', 'id': '/providers/Microsoft.Management/managementGroups/myManagementGroup/providers/Microsoft.Authorization/policyDefinitions/testDef', 'type': 'Microsoft.Authorization/policyDefinitions', 'name': 'testDef' } } ] } }") }; var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.OK }; using (ResourceManagementClient client = CreateResourceManagementClient(handler)) { // Act. WhatIfOperationResult result = client.Deployments.WhatIfAtManagementGroupScope("test-mg", "test-mg-deploy", deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.Equal(1, result.Changes.Count); WhatIfChange change = result.Changes[0]; Assert.Equal(ChangeType.Create, change.ChangeType); Assert.Null(change.Before); Assert.Null(change.Delta); Assert.NotNull(change.After); Assert.Equal("testDef", JToken.FromObject(change.After)["name"]); } }
public void WhatIfAtSubscriptionScope_CreateResources_ReturnsCreateChanges() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new DeploymentWhatIf { Location = "westus2", Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = SubscriptionTemplate, Parameters = JObject.Parse("{ 'storageAccountName': {'value': 'whatifnetsdktest1'}}"), WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.ResourceIdOnly) } }; // Use resource group name from the template. client.ResourceGroups.CreateOrUpdate("SDK-test", ResourceGroup); // Act. WhatIfOperationResult result = client.Deployments .WhatIfAtSubscriptionScope(NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); result.Changes.ForEach(change => { if (change.ResourceId.EndsWith("SDK-test")) { Assert.Equal(ChangeType.Ignore, change.ChangeType); } else { Assert.True(change.ChangeType == ChangeType.Deploy || change.ChangeType == ChangeType.Create); } }); } }
public async Task WhatIfAtResourceGroup() { SubscriptionResource subscription = await Client.GetDefaultSubscriptionAsync(); string rgName = Recording.GenerateAssetName("testRg-5-"); ResourceGroupData rgData = new ResourceGroupData(AzureLocation.WestUS2); var lro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, rgData); ResourceGroupResource rg = lro.Value; ResourceIdentifier deploymentResourceIdentifier = ArmDeploymentResource.CreateResourceIdentifier(rg.Id, "testDeploymentWhatIf"); ArmDeploymentResource deployment = Client.GetArmDeploymentResource(deploymentResourceIdentifier); var deploymentWhatIf = new ArmDeploymentWhatIfContent(new ArmDeploymentWhatIfProperties(ArmDeploymentMode.Incremental) { Template = CreateDeploymentPropertiesUsingString().Template, Parameters = CreateDeploymentPropertiesUsingJsonElement().Parameters }); WhatIfOperationResult whatIfOperationResult = (await deployment.WhatIfAsync(WaitUntil.Completed, deploymentWhatIf)).Value; Assert.AreEqual(whatIfOperationResult.Status, "Succeeded"); Assert.AreEqual(whatIfOperationResult.Changes.Count, 1); Assert.AreEqual(whatIfOperationResult.Changes[0].ChangeType, WhatIfChangeType.Create); }
public void WhatIfAtManagementGroupScope_ResourceIdOnlyMode_ReturnsChangesWithResourceIdsOnly() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deploymentWhatIf = new ScopedDeploymentWhatIf { Location = "westus", Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = ManagementGroupTemplate, Parameters = ManagementGroupTemplateParameters, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.ResourceIdOnly) } }; // Act. WhatIfOperationResult result = client.Deployments .WhatIfAtManagementGroupScope("tag-mg-sdk", NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); result.Changes.ForEach(change => { Assert.NotNull(change.ResourceId); Assert.NotEmpty(change.ResourceId); Assert.True(change.ChangeType == ChangeType.Deploy || change.ChangeType == ChangeType.Create); Assert.Null(change.Before); Assert.Null(change.After); Assert.Null(change.Delta); }); } }
public void WhatIfAtSubscriptionScope_ModifyResources_ReturnsModifyChanges() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deployment = new Deployment { Location = "westus2", Properties = new DeploymentProperties { Mode = DeploymentMode.Incremental, Template = SubscriptionTemplate, Parameters = JObject.Parse("{ 'storageAccountName': {'value': 'whatifnetsdktest1'}}"), } }; // Change "northeurope" to "westeurope". JObject newTemplate = JObject.Parse(SubscriptionTemplate); newTemplate["resources"][0]["properties"]["policyRule"]["if"]["equals"] = "westeurope"; var deploymentWhatIf = new DeploymentWhatIf { Location = "westus2", Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = newTemplate, Parameters = JObject.Parse("{ 'storageAccountName': {'value': 'whatifnetsdktest1'}}"), WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.FullResourcePayloads) } }; client.ResourceGroups.CreateOrUpdate("SDK-test", ResourceGroup); client.Deployments.CreateOrUpdateAtSubscriptionScope(NewDeploymentName(), deployment); // Act. WhatIfOperationResult result = client.Deployments .WhatIfAtSubscriptionScope(NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); WhatIfChange policyChange = result.Changes.FirstOrDefault(change => change.ResourceId.EndsWith("Microsoft.Authorization/policyDefinitions/policy2")); Assert.NotNull(policyChange); Assert.True(policyChange.ChangeType == ChangeType.Deploy || policyChange.ChangeType == ChangeType.Modify); Assert.NotNull(policyChange.Delta); Assert.NotEmpty(policyChange.Delta); WhatIfPropertyChange policyRuleChange = policyChange.Delta .FirstOrDefault(propertyChange => propertyChange.Path.Equals("properties.policyRule.if.equals")); Assert.NotNull(policyRuleChange); Assert.Equal(PropertyChangeType.Modify, policyRuleChange.PropertyChangeType); Assert.Equal("northeurope", policyRuleChange.Before); Assert.Equal("westeurope", policyRuleChange.After); } }
public void WhatIf_ModifyResources_ReturnsModifyChanges() { using (MockContext context = MockContext.Start(this.GetType())) { // Arrange. ResourceManagementClient client = this.GetResourceManagementClient(context); var deployment = new Deployment { Properties = new DeploymentProperties { Mode = DeploymentMode.Incremental, Template = ResourceGroupTemplate, Parameters = ResourceGroupTemplateParameters, } }; // Modify account type: Standard_LRS => Standard_GRS. JObject newTemplate = JObject.Parse(ResourceGroupTemplate); newTemplate["resources"][0]["properties"]["accountType"] = "Standard_GRS"; var deploymentWhatIf = new DeploymentWhatIf { Properties = new DeploymentWhatIfProperties { Mode = DeploymentMode.Incremental, Template = newTemplate, Parameters = ResourceGroupTemplateParameters, WhatIfSettings = new DeploymentWhatIfSettings(WhatIfResultFormat.FullResourcePayloads) } }; string resourceGroupName = NewResourceGroupName(); client.ResourceGroups.CreateOrUpdate(resourceGroupName, ResourceGroup); client.Deployments.CreateOrUpdate(resourceGroupName, NewDeploymentName(), deployment); // Act. WhatIfOperationResult result = client.Deployments .WhatIf(resourceGroupName, NewDeploymentName(), deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.NotEmpty(result.Changes); WhatIfChange storageAccountChange = result.Changes.FirstOrDefault(change => change.ResourceId.EndsWith("Microsoft.Storage/storageAccounts/tianotest102")); Assert.NotNull(storageAccountChange); Assert.Equal(ChangeType.Modify, storageAccountChange.ChangeType); Assert.NotNull(storageAccountChange.Delta); Assert.NotEmpty(storageAccountChange.Delta); WhatIfPropertyChange accountTypeChange = storageAccountChange.Delta .FirstOrDefault(propertyChange => propertyChange.Path.Equals("properties.accountType")); Assert.NotNull(accountTypeChange); Assert.Equal("Standard_LRS", accountTypeChange.Before); Assert.Equal("Standard_GRS", accountTypeChange.After); } }
public void WhatIf_ReceivingResponse_DeserializesResult() { // Arrange. var deploymentWhatIf = new DeploymentWhatIf(new DeploymentWhatIfProperties()); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(@"{ 'status': 'Succeeded', 'properties': { 'changes': [ { 'resourceId': '/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myExistingIdentity', 'changeType': 'Modify', 'before': { 'apiVersion': '2018-11-30', 'id': '/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myExistingIdentity', 'type': 'Microsoft.ManagedIdentity/userAssignedIdentities', 'name': 'myExistingIdentity', 'location': 'westus2' }, 'after': { 'apiVersion': '2018-11-30', 'id': '/subscriptions/00000000-0000-0000-0000-000000000001/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myExistingIdentity', 'type': 'Microsoft.ManagedIdentity/userAssignedIdentities', 'name': 'myExistingIdentity', 'location': 'westus2', 'tags': { 'myNewTag': 'my tag value' } }, 'delta': [ { 'path': 'tags.myNewTag', 'propertyChangeType': 'Create', 'after': 'my tag value' } ] } ] } }") }; var handler = new RecordedDelegatingHandler(response) { StatusCodeToReturn = HttpStatusCode.OK }; using (ResourceManagementClient client = CreateResourceManagementClient(handler)) { // Act. WhatIfOperationResult result = client.Deployments.WhatIf("test-rg", "test-deploy", deploymentWhatIf); // Assert. Assert.Equal("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.Equal(1, result.Changes.Count); WhatIfChange change = result.Changes[0]; Assert.Equal(ChangeType.Modify, change.ChangeType); Assert.NotNull(change.Before); Assert.Equal("myExistingIdentity", JToken.FromObject(change.Before)["name"]); Assert.NotNull(change.After); Assert.Equal("myExistingIdentity", JToken.FromObject(change.After)["name"]); Assert.NotNull(change.Delta); Assert.Equal(1, change.Delta.Count); Assert.Equal("tags.myNewTag", change.Delta[0].Path); Assert.Equal(PropertyChangeType.Create, change.Delta[0].PropertyChangeType); Assert.Equal("my tag value", change.Delta[0].After); } }
public PSWhatIfOperationResult(WhatIfOperationResult whatIfOperationResult) { this.whatIfOperationResult = whatIfOperationResult; this.changes = new Lazy <IList <PSWhatIfChange> >(() => whatIfOperationResult.Changes?.Select(c => new PSWhatIfChange(c)).ToList()); }
public WhatIfOperationResult WhatIf(PredictTemplateOrchestrationInput input) { var result = new WhatIfOperationResult(); //var (Result, Message, Deployment) = ParseDeployment(new DeploymentOrchestrationInput() //{ // CorrelationId = input.CorrelationId, // Parameters = input.Parameters, // ResourceGroup = input.ResourceGroupName, // SubscriptionId = input.SubscriptionId, // TemplateContent = input.Template, // TenantId = input.TenantId //}); //if (!Result) //{ // result.Status = "failed"; // result.Error = new ErrorResponse() { Code = "400", Message = Message }; //} //DeploymentContext deploymentContext = new DeploymentContext() //{ // CorrelationId = input.CorrelationId, // Mode = input.Mode, // ResourceGroup = input.ResourceGroupName, // SubscriptionId = input.SubscriptionId, // TenantId = input.TenantId, // Parameters = input.Parameters //}; //string queryScope = $"/{infrastructure.BuiltinPathSegment.Subscription}/{input.SubscriptionId}"; //if (input.ScopeType == ScopeType.ResourceGroup) // queryScope += $"/{infrastructure.BuiltinPathSegment.ResourceGroup}/{input.ResourceGroupName}"; //var str = this.infrastructure.List(deploymentContext, queryScope, Deployment.Template.ApiProfile, string.Empty, "resources"); ////https://docs.microsoft.com/en-us/rest/api/resources/resources/listbyresourcegroup#resourcelistresult //using var doc = JsonDocument.Parse(str.Content); //Dictionary<string, JsonElement> asset = new Dictionary<string, JsonElement>(); //doc.RootElement.TryGetProperty("values", out JsonElement values); //foreach (var r in values.EnumerateArray()) //{ // if (!r.TryGetProperty("id", out JsonElement id)) // break; // asset.Add(id.GetString(), r); //} //foreach (var r in Deployment.Template.Resources.Values) //{ // CheckResourceWhatIf(input, result, asset, r); //} //if (input.Mode == DeploymentMode.Complete) //{ // foreach (var item in asset) // { // result.Changes.Add(new WhatIfChange() // { // ChangeType = ChangeType.Delete, // ResourceId = item.Key // }); // } //} //else //{ // foreach (var item in asset) // { // result.Changes.Add(new WhatIfChange() // { // ChangeType = ChangeType.Ignore, // ResourceId = item.Key // }); // } //} //result.Status = "succeeded"; return(result); }