public async Task WhatIfAtSubscriptionScope_ModifyResources_ReturnsModifyChanges() { // Arrange. var deployment = new Deployment( new DeploymentProperties(DeploymentMode.Incremental) { Template = SubscriptionTemplate, Parameters = "{ 'storageAccountName': {'value': 'whatifnetsdktest1'}}".Replace("'", "\"") }) { Location = "westus2" }; var deploymentWhatIf = new DeploymentWhatIf( new DeploymentWhatIfProperties(DeploymentMode.Incremental) { Template = SubscriptionTemplateWestEurope, Parameters = "{ 'storageAccountName': {'value': 'whatifnetsdktest1'}}".Replace("'", "\""), WhatIfSettings = new DeploymentWhatIfSettings() { ResultFormat = WhatIfResultFormat.FullResourcePayloads } } ) { Location = "westus2" }; var resourcegroup = (await ResourceGroupsOperations.CreateOrUpdateAsync("SDK-test", ResourceGroup)).Value; var deploy = await DeploymentsOperations.StartCreateOrUpdateAtSubscriptionScopeAsync(NewDeploymentName(), deployment); await WaitForCompletionAsync(deploy); // Act. var rawResult = await DeploymentsOperations.StartWhatIfAtSubscriptionScopeAsync(NewDeploymentName(), deploymentWhatIf); var result = (await WaitForCompletionAsync(rawResult)).Value; // Assert. Assert.AreEqual("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.IsNotEmpty(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.IsNotEmpty(policyChange.Delta); WhatIfPropertyChange policyRuleChange = policyChange.Delta .FirstOrDefault(propertyChange => propertyChange.Path.Equals("properties.policyRule.if.equals")); Assert.NotNull(policyRuleChange); Assert.AreEqual(PropertyChangeType.Modify, policyRuleChange.PropertyChangeType); Assert.AreEqual("northeurope", policyRuleChange.Before); Assert.AreEqual("westeurope", policyRuleChange.After); }
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 async Task WhatIfAtSubscriptionScope_ReceivingResponse_DeserializesResult() { // Arrange. var deploymentWhatIf = new DeploymentWhatIf(new DeploymentWhatIfProperties(DeploymentMode.Incremental)); var mockResponse = new MockResponse((int)HttpStatusCode.OK); var content = @"{ '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' } } ] } }".Replace("'", "\""); mockResponse.SetContent(content); var mockTransport = new MockTransport(mockResponse); var client = GetResourceManagementClient(mockTransport); // Act. var raw = await client.Deployments.StartWhatIfAtSubscriptionScopeAsync("test-subscription-deploy", deploymentWhatIf); var result = (await WaitForCompletionAsync(raw)).Value; // Assert. Assert.AreEqual("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.AreEqual(1, result.Changes.Count); WhatIfChange change = result.Changes[0]; Assert.AreEqual(ChangeType.Create, change.ChangeType); Assert.Null(change.Before); Assert.IsEmpty(change.Delta); Assert.NotNull(change.After); Assert.AreEqual("myResourceGroup", JsonDocument.Parse(JsonSerializer.Serialize(change.After)).RootElement.GetProperty("name").GetString()); }
public PSWhatIfChange(WhatIfChange whatIfChange) { this.whatIfChange = whatIfChange; (string scope, string relativeResourceId) = ResourceIdUtility.SplitResourceId(whatIfChange.ResourceId); this.Scope = scope; this.RelativeResourceId = relativeResourceId; this.apiVersion = new Lazy <string>(() => this.Before?["apiVersion"]?.Value <string>() ?? this.After?["apiVersion"]?.Value <string>()); this.before = new Lazy <JToken>(() => whatIfChange.Before.ToJToken()); this.after = new Lazy <JToken>(() => whatIfChange.After.ToJToken()); this.delta = new Lazy <IList <PSWhatIfPropertyChange> >(() => whatIfChange.Delta?.Select(pc => new PSWhatIfPropertyChange(pc)).ToList()); }
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 async Task WhatIf_ModifyResources_ReturnsModifyChanges() { // Arrange. JsonElement jsonParameter = JsonSerializer.Deserialize <JsonElement>(ResourceGroupTemplateParameters); if (!jsonParameter.TryGetProperty("parameters", out JsonElement parameter)) { parameter = jsonParameter; } var deployment = new Deployment(new DeploymentProperties(DeploymentMode.Incremental) { Template = ResourceGroupTemplate, ParametersJson = parameter }); // Modify account type: Standard_LRS => Standard_GRS. var deploymentWhatIf = new DeploymentWhatIf( new DeploymentWhatIfProperties(DeploymentMode.Incremental) { Template = ResourceGroupTemplateGRS, ParametersJson = parameter, WhatIfSettings = new DeploymentWhatIfSettings() { ResultFormat = WhatIfResultFormat.FullResourcePayloads } } ); string resourceGroupName = NewResourceGroupName(); var resourcegroup = (await ResourceGroupsOperations.CreateOrUpdateAsync(resourceGroupName, ResourceGroup)).Value; var deploy = await DeploymentsOperations.StartCreateOrUpdateAsync(resourceGroupName, NewDeploymentName(), deployment); await WaitForCompletionAsync(deploy); // Act. var rawResult = await DeploymentsOperations.StartWhatIfAsync(resourceGroupName, NewDeploymentName(), deploymentWhatIf); var result = (await WaitForCompletionAsync(rawResult)).Value; // Assert. Assert.AreEqual("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.IsNotEmpty(result.Changes); WhatIfChange storageAccountChange = result.Changes.FirstOrDefault(change => change.ResourceId.EndsWith("Microsoft.Storage/storageAccounts/ramokaSATestAnother1")); Assert.NotNull(storageAccountChange); Assert.AreEqual(ChangeType.Modify, storageAccountChange.ChangeType); Assert.NotNull(storageAccountChange.Delta); Assert.IsNotEmpty(storageAccountChange.Delta); WhatIfPropertyChange accountTypeChange = storageAccountChange.Delta .FirstOrDefault(propertyChange => propertyChange.Path.Equals("properties.accountType")); Assert.NotNull(accountTypeChange); Assert.AreEqual("Standard_LRS", accountTypeChange.Before); Assert.AreEqual("Standard_GRS", accountTypeChange.After); }
public async Task WhatIf_ReceivingResponse_DeserializesResult() { // Arrange. var deploymentWhatIf = new DeploymentWhatIf(new DeploymentWhatIfProperties(DeploymentMode.Incremental)); var content = @"{ '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' } ] } ] } }".Replace("'", "\""); var mockResponse = new MockResponse((int)HttpStatusCode.OK); mockResponse.SetContent(content); var mockTransport = new MockTransport(mockResponse); var client = GetResourceManagementClient(mockTransport); // Act. var raw = await client.Deployments.StartWhatIfAsync("test-rg", "test-deploy", deploymentWhatIf); var result = (await WaitForCompletionAsync(raw)).Value; // Assert. Assert.AreEqual("Succeeded", result.Status); Assert.NotNull(result.Changes); Assert.AreEqual(1, result.Changes.Count); WhatIfChange change = result.Changes[0]; Assert.AreEqual(ChangeType.Modify, change.ChangeType); Assert.NotNull(change.Before); Assert.AreEqual("myExistingIdentity", JsonDocument.Parse(JsonSerializer.Serialize(change.Before)).RootElement.GetProperty("name").GetString()); Assert.NotNull(change.After); Assert.AreEqual("myExistingIdentity", JsonDocument.Parse(JsonSerializer.Serialize(change.After)).RootElement.GetProperty("name").GetString()); Assert.NotNull(change.Delta); Assert.AreEqual(1, change.Delta.Count); Assert.AreEqual("tags.myNewTag", change.Delta[0].Path); Assert.AreEqual(PropertyChangeType.Create, change.Delta[0].PropertyChangeType); Assert.AreEqual("my tag value", change.Delta[0].After); }