public void RemovesResourceGroup()
        {
            BasePSResourceParameters actualParameters = new BasePSResourceParameters();
            BasePSResourceParameters expectedParameters = new BasePSResourceParameters()
            {
                Name = resourceName,
                ResourceType = resourceType,
                ResourceGroupName = resourceGroupName,
                ParentResource = resourceParentName,
            };

            commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
            resourcesClientMock
                .Setup(f => f.DeleteResource(It.IsAny<BasePSResourceParameters>()))
                .Callback((BasePSResourceParameters p) =>
                    {
                        actualParameters = p;
                    });

            cmdlet.Name = resourceName;
            cmdlet.ResourceType = resourceType;
            cmdlet.ResourceGroupName = resourceGroupName;
            cmdlet.ParentResource = resourceParentName;
            cmdlet.PassThru = true;
            cmdlet.Force = true;

            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);

            commandRuntimeMock.Verify(f => f.WriteObject(true), Times.Once());
        }
        public override void ExecuteCmdlet()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = Name,
                ResourceGroupName = ResourceGroupName,
                ResourceType = ResourceType,
                ParentResource = ParentResource,
                ApiVersion = ApiVersion
            };

            List<PSResource> resourceList = ResourcesClient.FilterPSResources(parameters);
            if (resourceList != null)
            {
                if (resourceList.Count == 1 && Name != null)
                {
                    WriteObject(resourceList[0]);
                }
                else
                {
                    List<PSObject> output = new List<PSObject>();
                    resourceList.ForEach(r => output.Add(base.ConstructPSObject(
                        null,
                        "Name", r.Name,
                        "ResourceGroupName", r.ResourceGroupName,
                        "ResourceType", r.ResourceType,
                        "Location", r.Location,
                        "ParentResource", r.ParentResource)));

                    WriteObject(output, true);
                }
            }
        }
        /// <summary>
        /// Deletes a given resource
        /// </summary>
        /// <param name="parameters">The resource identification</param>
        public virtual void DeleteResource(BasePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            if (!ResourceManagementClient.Resources.CheckExistence(parameters.ResourceGroupName, resourceIdentity).Exists)
            {
                throw new ArgumentException(Resources.ResourceDoesntExists);
            }

            ResourceManagementClient.Resources.Delete(parameters.ResourceGroupName, resourceIdentity);
        }
        /// <summary>
        /// Get an existing resource or resources.
        /// </summary>
        /// <param name="parameters">The get parameters</param>
        /// <returns>List of resources</returns>
        public virtual List <PSResource> FilterPSResources(BasePSResourceParameters parameters)
        {
            List <PSResource> resources = new List <PSResource>();

            if (!string.IsNullOrEmpty(parameters.ResourceGroupName))
            {
                if (!ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
                {
                    throw new ArgumentException(Resources.ResourceGroupDoesntExists);
                }
            }

            if (!string.IsNullOrEmpty(parameters.Name))
            {
                ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

                ResourceGetResult getResult;

                try
                {
                    getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);
                }
                catch (CloudException)
                {
                    throw new ArgumentException(Resources.ResourceDoesntExists);
                }

                resources.Add(getResult.Resource.ToPSResource(this));
            }
            else
            {
                ResourceListResult listResult = ResourceManagementClient.Resources.List(new ResourceListParameters
                {
                    ResourceGroupName = parameters.ResourceGroupName,
                    ResourceType      = parameters.ResourceType
                });

                if (listResult.Resources != null)
                {
                    resources.AddRange(listResult.Resources.Select(r => r.ToPSResource(this)));
                }
            }
            return(resources);
        }
        public override void ExecuteCmdlet()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = Name,
                ResourceGroupName = ResourceGroupName,
                ResourceType = ResourceType,
                ParentResource = ParentResource,
                ApiVersion = ApiVersion
            };

            ConfirmAction(
                Force.IsPresent,
                string.Format(Resources.RemovingResource, Name),
                Resources.RemoveResourceMessage,
                Name,
                () => ResourcesClient.DeleteResource(parameters));

            if (PassThru)
            {
                WriteObject(true);
            }
        }
Ejemplo n.º 6
0
        public void GetResourceWithIncorrectTypeThrowsException()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                ResourceGroupName = resourceGroupName,
                ResourceType = "abc",
            };

            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = true
                }));

            Assert.Throws<ArgumentException>(() => resourcesClient.FilterPSResources(parameters));
        }
Ejemplo n.º 7
0
        public void GetResourceWithSomeParametersReturnsList()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                ResourceGroupName = resourceGroupName,
            };

            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = true
                }));

            resourceOperationsMock.Setup(f => f.ListAsync(It.IsAny<ResourceListParameters>(), It.IsAny<CancellationToken>()))
                .Returns(() => Task.Factory.StartNew(() => new ResourceListResult
                {
                    StatusCode = HttpStatusCode.OK,
                    Resources = new List<Resource>(new []
                        {
                            new Resource
                            {
                                Name = "foo",
                                Properties = null,
                                ProvisioningState = ProvisioningState.Running,
                                Location = "West US"
                            },
                            new Resource
                            {
                                Name = "bar",
                                Properties = null,
                                ProvisioningState = ProvisioningState.Running,
                                Location = "West US"
                            }
                        })
                    
                }));


            List<PSResource> result = resourcesClient.FilterPSResources(parameters);

            Assert.NotNull(result);
            Assert.Equal(2, result.Count);
            Assert.False(result.Any(r => r.Properties != null));
        }
Ejemplo n.º 8
0
        public void GetResourceWithAllParametersReturnsOneItem()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                ResourceGroupName = resourceGroupName,
                ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType,
            };

            resourceGroupMock.Setup(f => f.CheckExistenceAsync(parameters.ResourceGroupName, new CancellationToken()))
                .Returns(Task.Factory.StartNew(() => new ResourceGroupExistsResult
                {
                    Exists = true
                }));

            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,
                                Properties = serializedProperties,
                                ProvisioningState = ProvisioningState.Running,
                                Location = "West US",
                            }
                    }));

            
            List<PSResource> result = resourcesClient.FilterPSResources(parameters);

            Assert.NotNull(result);
            Assert.Equal(1, result.Count);
            Assert.Equal(4, result[0].Properties.Count);
            Assert.Equal(2, ((Dictionary<string, object>)result[0].Properties["misc"]).Count);
        }
Ejemplo n.º 9
0
        public void RemoveResourceWithAllParametersSucceeds()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                ResourceGroupName = resourceGroupName,
                ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType,
            };

            resourceOperationsMock.Setup(f => f.CheckExistenceAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>()))
                .Returns(() => Task.Factory.StartNew(() => new ResourceExistsResult
                {
                    Exists = true
                }
            ));

            resourceOperationsMock.Setup(f => f.DeleteAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>()))
                .Returns(Task.Factory.StartNew(() => new OperationResponse
                {
                    RequestId = "123",
                    StatusCode = HttpStatusCode.OK
                }));

            resourcesClient.DeleteResource(parameters);
        }
Ejemplo n.º 10
0
        public void RemoveResourceWithIncorrectTypeThrowsException()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                ResourceGroupName = resourceGroupName,
                ResourceType = "abc",
            };

            Assert.Throws<ArgumentException>(() => resourcesClient.DeleteResource(parameters));
        }
Ejemplo n.º 11
0
        public void RemoveResourceWithoutExistingResourceThrowsException()
        {
            BasePSResourceParameters parameters = new BasePSResourceParameters()
            {
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                ResourceGroupName = resourceGroupName,
                ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType,
            };

            resourceOperationsMock.Setup(f => f.CheckExistenceAsync(resourceGroupName, It.IsAny<ResourceIdentity>(), It.IsAny<CancellationToken>()))
                .Returns(() => Task.Factory.StartNew(() => new ResourceExistsResult
                            {
                                Exists = false
                            }
                    ));

            Assert.Throws<ArgumentException>(() => resourcesClient.DeleteResource(parameters));
        }
        /// <summary>
        /// Deletes a given resource
        /// </summary>
        /// <param name="parameters">The resource identification</param>
        public virtual void DeleteResource(BasePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            if (!ResourceManagementClient.Resources.CheckExistence(parameters.ResourceGroupName, resourceIdentity).Exists)
            {
                throw new ArgumentException(Resources.ResourceDoesntExists);
            }

            ResourceManagementClient.Resources.Delete(parameters.ResourceGroupName, resourceIdentity);
        }
        /// <summary>
        /// Get an existing resource or resources.
        /// </summary>
        /// <param name="parameters">The get parameters</param>
        /// <returns>List of resources</returns>
        public virtual List<PSResource> FilterPSResources(BasePSResourceParameters parameters)
        {
            List<PSResource> resources = new List<PSResource>();

            if (!string.IsNullOrEmpty(parameters.ResourceGroupName))
            {
                if (!ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
                {
                    throw new ArgumentException(Resources.ResourceGroupDoesntExists);
                }
            }

            if (!string.IsNullOrEmpty(parameters.Name))
            {
                ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

                ResourceGetResult getResult;

                try
                {
                    getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);
                }
                catch (CloudException)
                {
                    throw new ArgumentException(Resources.ResourceDoesntExists);
                }

                resources.Add(getResult.Resource.ToPSResource(this));
            }
            else
            {
                ResourceListResult listResult = ResourceManagementClient.Resources.List(new ResourceListParameters
                    {
                        ResourceGroupName = parameters.ResourceGroupName,
                        ResourceType = parameters.ResourceType
                    });

                if (listResult.Resources != null)
                {
                    resources.AddRange(listResult.Resources.Select(r => r.ToPSResource(this)));
                }
            }
            return resources;
        }