/// <summary> /// Updates an existing resource. /// </summary> /// <param name="parameters">The update parameters</param> /// <returns>The updated resource</returns> public virtual PSResource UpdatePSResource(UpdatePSResourceParameters parameters) { ResourceIdentity resourceIdentity = parameters.ToResourceIdentity(); ResourceGetResult getResource; try { getResource = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity); } catch (CloudException) { throw new ArgumentException(Resources.ResourceDoesntExists); } string newProperty = SerializeHashtable(parameters.PropertyObject, addValueLayer: false); ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName, resourceIdentity, new ResourceCreateOrUpdateParameters { ValidationMode = ResourceValidationMode.NameValidation, Resource = new BasicResource { Location = getResource.Resource.Location, Properties = newProperty } }); ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity); return(getResult.Resource.ToPSResource(this)); }
public override void ExecuteCmdlet() { UpdatePSResourceParameters parameters = new UpdatePSResourceParameters() { Name = Name, ResourceGroupName = ResourceGroupName, ResourceType = ResourceType, ParentResource = ParentResource, PropertyObject = PropertyObject, ApiVersion = ApiVersion }; WriteObject(ResourcesClient.UpdatePSResource(parameters)); }
public void UpdatrePSResourceGroup() { UpdatePSResourceParameters expectedParameters = new UpdatePSResourceParameters() { Name = resourceName, ParentResource = resourceParentName, ResourceType = resourceType, ResourceGroupName = resourceGroupName, PropertyObject = properties.ToHashtable() }; UpdatePSResourceParameters actualParameters = new UpdatePSResourceParameters(); PSResource expected = new PSResource() { Name = expectedParameters.Name, Location = resourceGroupLocation, ResourceGroupName = expectedParameters.ResourceGroupName, Properties = expectedParameters.PropertyObject, ResourceType = expectedParameters.ResourceType }; resourcesClientMock.Setup(f => f.UpdatePSResource(It.IsAny<UpdatePSResourceParameters>())) .Returns(expected) .Callback((UpdatePSResourceParameters p) => { actualParameters = p; }); cmdlet.Name = expectedParameters.Name; cmdlet.ResourceGroupName = expectedParameters.ResourceGroupName; cmdlet.ResourceType = expectedParameters.ResourceType; cmdlet.ParentResource = expectedParameters.ParentResource; cmdlet.PropertyObject = expectedParameters.PropertyObject; cmdlet.ExecuteCmdlet(); Assert.Equal(expectedParameters.Name, actualParameters.Name); Assert.Equal(expectedParameters.ResourceGroupName, actualParameters.ResourceGroupName); Assert.Equal(expectedParameters.ResourceType, actualParameters.ResourceType); Assert.Equal(expectedParameters.ParentResource, actualParameters.ParentResource); Assert.Equal(expectedParameters.PropertyObject, actualParameters.PropertyObject); commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once()); }
public void SetResourceWithReplaceRewritesResource() { var originalProperties = new Dictionary<string, object> { {"name", "site1"}, {"siteMode", "Standard"}, {"computeMode", "Dedicated"}, {"list", new [] {1,2,3}}, {"misc", new Dictionary<string, object> { {"key1", "value1"}, {"key2", "value2"} }}}; var originalPropertiesSerialized = JsonConvert.SerializeObject(originalProperties, new JsonSerializerSettings { TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple, TypeNameHandling = TypeNameHandling.None }); var patchProperties = new Dictionary<string, object> { {"siteMode", "Dedicated"}, {"newMode", "NewValue"}, {"list", new [] {4,5,6}}, {"misc", new Dictionary<string, object> { {"key3", "value3"} }}}; UpdatePSResourceParameters parameters = new UpdatePSResourceParameters() { Name = resourceIdentity.ResourceName, ParentResource = resourceIdentity.ParentResourcePath, PropertyObject = new Hashtable(patchProperties), ResourceGroupName = resourceGroupName, ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType }; ResourceCreateOrUpdateParameters actual = new ResourceCreateOrUpdateParameters(); resourceOperationsMock.Setup(f => f.GetAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>())) .Returns(() => Task.Factory.StartNew(() => new ResourceGetResult { StatusCode = HttpStatusCode.OK, Resource = new Resource { Name = parameters.Name, Location = "West US", Properties = originalPropertiesSerialized, ProvisioningState = ProvisioningState.Running, } })); resourceOperationsMock.Setup(f => f.CreateOrUpdateAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<ResourceCreateOrUpdateParameters>(), It.IsAny<CancellationToken>())) .Returns(Task.Factory.StartNew(() => new ResourceCreateOrUpdateResult { RequestId = "123", StatusCode = HttpStatusCode.OK, Resource = new BasicResource { Location = "West US", Properties = originalPropertiesSerialized, ProvisioningState = ProvisioningState.Running } })) .Callback((string groupName, ResourceIdentity id, ResourceCreateOrUpdateParameters p, CancellationToken token) => actual = p); resourcesClient.UpdatePSResource(parameters); JToken actualJson = JToken.Parse(actual.Resource.Properties); Assert.Null(actualJson["name"]); Assert.Equal("Dedicated", actualJson["siteMode"].ToObject<string>()); Assert.Null(actualJson["computeMode"]); Assert.Equal("NewValue", actualJson["newMode"].ToObject<string>()); Assert.Equal("[4,5,6]", actualJson["list"].ToString(Formatting.None)); Assert.Null(actualJson["misc"]["key1"]); Assert.Null(actualJson["misc"]["key2"]); Assert.Equal("value3", actualJson["misc"]["key3"].ToObject<string>()); }
public void SetResourceWithAllParameters() { UpdatePSResourceParameters parameters = new UpdatePSResourceParameters() { Name = resourceIdentity.ResourceName, ParentResource = resourceIdentity.ParentResourcePath, PropertyObject = new Hashtable(properties), ResourceGroupName = resourceGroupName, ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType, }; resourceOperationsMock.Setup(f => f.GetAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>())) .Returns(() => Task.Factory.StartNew(() => new ResourceGetResult { StatusCode = HttpStatusCode.OK, Resource = new Resource { Name = parameters.Name, Location = "West US", Properties = serializedProperties, ProvisioningState = ProvisioningState.Running, } })); resourceOperationsMock.Setup(f => f.CreateOrUpdateAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<ResourceCreateOrUpdateParameters>(), It.IsAny<CancellationToken>())) .Returns(Task.Factory.StartNew(() => new ResourceCreateOrUpdateResult { RequestId = "123", StatusCode = HttpStatusCode.OK, Resource = new BasicResource { Location = "West US", Properties = serializedProperties, ProvisioningState = ProvisioningState.Running } })); PSResource result = resourcesClient.UpdatePSResource(parameters); Assert.NotNull(result); }
public void SetResourceWithIncorrectTypeThrowsException() { UpdatePSResourceParameters parameters = new UpdatePSResourceParameters() { Name = resourceIdentity.ResourceName, ParentResource = resourceIdentity.ParentResourcePath, PropertyObject = new Hashtable(properties), ResourceGroupName = resourceGroupName, ResourceType = "abc", }; Assert.Throws<ArgumentException>(() => resourcesClient.UpdatePSResource(parameters)); }
public void SetResourceWithoutExistingResourceThrowsException() { UpdatePSResourceParameters parameters = new UpdatePSResourceParameters() { Name = resourceIdentity.ResourceName, ParentResource = resourceIdentity.ParentResourcePath, PropertyObject = new Hashtable(properties), ResourceGroupName = resourceGroupName, ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType, }; resourceOperationsMock.Setup(f => f.GetAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>())) .Returns(() => { throw new CloudException("Resource does not exist."); }); Assert.Throws<ArgumentException>(() => resourcesClient.UpdatePSResource(parameters)); }
/// <summary> /// Updates an existing resource. /// </summary> /// <param name="parameters">The update parameters</param> /// <returns>The updated resource</returns> public virtual PSResource UpdatePSResource(UpdatePSResourceParameters parameters) { ResourceIdentity resourceIdentity = parameters.ToResourceIdentity(); ResourceGetResult getResource; try { getResource = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity); } catch (CloudException) { throw new ArgumentException(Resources.ResourceDoesntExists); } string newProperty = SerializeHashtable(parameters.PropertyObject, addValueLayer: false); ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName, resourceIdentity, new ResourceCreateOrUpdateParameters { ValidationMode = ResourceValidationMode.NameValidation, Resource = new BasicResource { Location = getResource.Resource.Location, Properties = newProperty } }); ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity); return getResult.Resource.ToPSResource(this); }