Ejemplo n.º 1
0
        /// <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,
                                         ProjectResources.ResourceGroupDoesntExistsAdd,
                                         ProjectResources.AddingResourceGroup,
                                         parameters.Name,
                                         () => CreateOrUpdateResourceGroup(parameters.ResourceGroupName, parameters.Location, null));

                if (!ResourceManagementClient.ResourceGroups.CheckExistence(parameters.ResourceGroupName).Exists)
                {
                    throw new ArgumentException(ProjectResources.ResourceGroupDoesntExists);
                }
                else
                {
                    WriteVerbose(string.Format("Created resource group '{0}' in location '{1}'", parameters.Name, parameters.Location));
                }
            }

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

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

                Dictionary <string, string> tagDictionary = TagsConversionHelper.CreateTagDictionary(parameters.Tag, validate: true);

                ResourceCreateOrUpdateResult createOrUpdateResult = ResourceManagementClient.Resources.CreateOrUpdate(parameters.ResourceGroupName,
                                                                                                                      resourceIdentity,
                                                                                                                      new BasicResource
                {
                    Location   = parameters.Location,
                    Properties = SerializeHashtable(parameters.PropertyObject, addValueLayer: false),
                    Tags       = tagDictionary
                });

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

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

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

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