public override void ExecuteCmdlet()
        {
            CreatePSResourceParameters parameters = new CreatePSResourceParameters()
            {
                Name = Name,
                ResourceGroupName = ResourceGroupName,
                ResourceType = ResourceType,
                Location = Location,
                ParentResource = ParentResource,
                PropertyObject = PropertyObject,
                Force = Force.IsPresent,
                ConfirmAction = ConfirmAction,
                ApiVersion = ApiVersion,
            };

            WriteObject(ResourcesClient.CreatePSResource(parameters));
        }
        public void CreatesNewPSResourceGroupWithUserTemplate()
        {
            CreatePSResourceParameters expectedParameters = new CreatePSResourceParameters()
            {
                Name = resourceName,
                ParentResource = resourceParentName,
                ResourceType = resourceType,
                ResourceGroupName = resourceGroupName,
                PropertyObject = properties.ToHashtable()
            };
            CreatePSResourceParameters actualParameters = new CreatePSResourceParameters();
            PSResource expected = new PSResource()
            {
                Name = expectedParameters.Name,
                Location = resourceGroupLocation,
                ResourceGroupName = expectedParameters.ResourceGroupName,
                Properties = expectedParameters.PropertyObject,
                ResourceType = expectedParameters.ResourceType
            };
            resourcesClientMock.Setup(f => f.CreatePSResource(It.IsAny<CreatePSResourceParameters>()))
                .Returns(expected)
                .Callback((CreatePSResourceParameters 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 NewResourceWithAllParametersSucceeds()
        {
            CreatePSResourceParameters parameters = new CreatePSResourceParameters()
            {
                Location = "West US",
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                PropertyObject = new Hashtable(properties),
                ResourceGroupName = resourceGroupName,
                ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType,
                ConfirmAction = ConfirmAction
            };

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

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

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

            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.CreatePSResource(parameters);

            Assert.NotNull(result);
        }
        public void NewResourceWithIncorrectTypeThrowsException()
        {
            CreatePSResourceParameters parameters = new CreatePSResourceParameters()
            {
                Location = "West US",
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                PropertyObject = new Hashtable(properties),
                ResourceGroupName = resourceGroupName,
                ResourceType = "abc",
                ConfirmAction = ConfirmAction
            };

            Assert.Throws<ArgumentException>(() => resourcesClient.CreatePSResource(parameters));
        }
        public void NewResourceWithExistingResourceAsksForUserConfirmation()
        {
            CreatePSResourceParameters parameters = new CreatePSResourceParameters()
            {
                Location = "West US",
                Name = resourceIdentity.ResourceName,
                ParentResource = resourceIdentity.ParentResourcePath,
                PropertyObject = new Hashtable(properties),
                ResourceGroupName = resourceGroupName,
                ResourceType = resourceIdentity.ResourceProviderNamespace + "/" + resourceIdentity.ResourceType,
                ConfirmAction = RejectAction
            };

            RejectActionCounter = 0;

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

            resourceGroupMock.Setup(f => f.CheckExistenceAsync(resourceGroupName, It.IsAny<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
                    {
                        Resource = new Resource
                            {
                                Location = "West US",
                                Properties = serializedProperties,
                                ProvisioningState = ProvisioningState.Running
                            }
                    }));

            resourcesClient.CreatePSResource(parameters);
            Assert.Equal(1, RejectActionCounter);
        }
        /// <summary>
        /// Creates a new resource.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created resource</returns>
        public virtual PSResource CreatePSResource(CreatePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            if (ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
            {
                WriteVerbose(string.Format("Resource group \"{0}\" is found.", parameters.ResourceGroupName));
            }
            else
            {
                parameters.ConfirmAction(parameters.Force,
                                             Resources.ResourceGroupDoesntExistsAdd,
                                             Resources.AddingResourceGroup,
                                             parameters.Name,
                                             () => CreateResourceGroup(parameters.ResourceGroupName, parameters.Location));

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

            bool resourceExists = ResourceManagementClient.Resources.CheckExistence(parameters.ResourceGroupName, resourceIdentity).Exists;

            Action createOrUpdateResource = () =>
                {
                    WriteVerbose(string.Format("Creating resource \"{0}\" started.", parameters.Name));

                    ResourceCreateOrUpdateResult createOrUpdateResult = ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName, 
                        resourceIdentity,
                        new ResourceCreateOrUpdateParameters
                            {
                                ValidationMode = ResourceValidationMode.NameValidation,
                                Resource = new BasicResource
                                    {
                                        Location = parameters.Location,
                                        Properties = SerializeHashtable(parameters.PropertyObject, addValueLayer: false)
                                    }
                            });

                    if (createOrUpdateResult.Resource != null)
                    {
                        WriteVerbose(string.Format("Creating resource \"{0}\" complete.", parameters.Name));
                    }
                };
            
            if (resourceExists && !parameters.Force)
            {
                parameters.ConfirmAction(parameters.Force,
                                         Resources.ResourceAlreadyExists,
                                         Resources.NewResourceMessage,
                                         parameters.Name,
                                         createOrUpdateResource);
            }
            else
            {
                createOrUpdateResource();
            }
            
            ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);

            return getResult.Resource.ToPSResource(this);
        }
        /// <summary>
        /// Creates a new resource.
        /// </summary>
        /// <param name="parameters">The create parameters</param>
        /// <returns>The created resource</returns>
        public virtual PSResource CreatePSResource(CreatePSResourceParameters parameters)
        {
            ResourceIdentity resourceIdentity = parameters.ToResourceIdentity();

            if (ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
            {
                WriteVerbose(string.Format("Resource group \"{0}\" is found.", parameters.ResourceGroupName));
            }
            else
            {
                parameters.ConfirmAction(parameters.Force,
                                         Resources.ResourceGroupDoesntExistsAdd,
                                         Resources.AddingResourceGroup,
                                         parameters.Name,
                                         () => CreateResourceGroup(parameters.ResourceGroupName, parameters.Location));

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

            bool resourceExists = ResourceManagementClient.Resources.CheckExistence(parameters.ResourceGroupName, resourceIdentity).Exists;

            Action createOrUpdateResource = () =>
            {
                WriteVerbose(string.Format("Creating resource \"{0}\" started.", parameters.Name));

                ResourceCreateOrUpdateResult createOrUpdateResult = ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName,
                                                                                                                      resourceIdentity,
                                                                                                                      new ResourceCreateOrUpdateParameters
                {
                    ValidationMode = ResourceValidationMode.NameValidation,
                    Resource       = new BasicResource
                    {
                        Location   = parameters.Location,
                        Properties = SerializeHashtable(parameters.PropertyObject, addValueLayer: false)
                    }
                });

                if (createOrUpdateResult.Resource != null)
                {
                    WriteVerbose(string.Format("Creating resource \"{0}\" complete.", parameters.Name));
                }
            };

            if (resourceExists && !parameters.Force)
            {
                parameters.ConfirmAction(parameters.Force,
                                         Resources.ResourceAlreadyExists,
                                         Resources.NewResourceMessage,
                                         parameters.Name,
                                         createOrUpdateResource);
            }
            else
            {
                createOrUpdateResource();
            }

            ResourceGetResult getResult = ResourceManagementClient.Resources.Get(parameters.ResourceGroupName, resourceIdentity);

            return(getResult.Resource.ToPSResource(this));
        }