/// <summary>
        /// Validates the prerequisites.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="parameters">The parameters.</param>
        protected override void ValidatePrerequisites(AzureStackClient client, ProviderRegistrationCreateOrUpdateParameters parameters)
        {
            ArgumentValidator.ValidateNotNull("client", client);
            ArgumentValidator.ValidateNotNull("parameters", parameters);

            client.ResourceGroups.CreateOrUpdate(new ResourceGroupCreateOrUpdateParameters()
            {
                ResourceGroup = new ResourceGroupDefinition()
                {
                    Location = this.ArmLocation,
                    Name = this.ResourceGroup,
                }
            });

            var name = parameters.ProviderRegistration.Properties.Name;
            var location = parameters.ProviderRegistration.Properties.Location;

            if (client.ProviderRegistrations.List(this.ResourceGroup).ProviderRegistrations
                .Any(p =>
                    string.Equals(p.Properties.Manifest.Namespace, name, StringComparison.OrdinalIgnoreCase)
                    && string.Equals(p.Properties.Location, location, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSInvalidOperationException(Resources.ProviderRegistrationAlreadyExists.FormatArgs(name, location));
            }
        }
 /// <summary>
 /// Registers a resource provider manifest
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.IProviderRegistrationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. Resource group name
 /// </param>
 /// <param name='parameters'>
 /// Required. Resource provider manifest definition
 /// </param>
 /// <returns>
 /// Result of the create or update of resource provider manifest
 /// </returns>
 public static ProviderRegistrationCreateOrUpdateResult CreateOrUpdate(this IProviderRegistrationOperations operations, string resourceGroupName, ProviderRegistrationCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IProviderRegistrationOperations)s).CreateOrUpdateAsync(resourceGroupName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// Validates the prerequisites.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="parameters">The parameters.</param>
        protected virtual void ValidatePrerequisites(AzureStackClient client, ProviderRegistrationCreateOrUpdateParameters parameters)
        {
            ArgumentValidator.ValidateNotNull("client", client);
            ArgumentValidator.ValidateNotNull("parameters", parameters);

            if (!client.ResourceGroups.List().ResourceGroups.Any(r => string.Equals(r.Name, this.ResourceGroup, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSInvalidOperationException(Resources.ResourceGroupDoesNotExist.FormatArgs(this.ResourceGroup));
            }

            var name = parameters.ProviderRegistration.Properties.Name;
            var location = parameters.ProviderRegistration.Properties.Location;

            if (!client.ProviderRegistrations.List(this.ResourceGroup).ProviderRegistrations
                .Any(p =>
                    string.Equals(p.Properties.Manifest.Namespace, name, StringComparison.OrdinalIgnoreCase)
                    && string.Equals(p.Properties.Location, location, StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSInvalidOperationException(Resources.ProviderRegistrationDoesNotExist.FormatArgs(name, location));
            }
        }
        /// <summary>
        /// Executes the API call(s) against Azure Resource Management API(s).
        /// </summary>
        protected override object ExecuteCore()
        {
            using (var client = this.GetAzureStackClient(this.SubscriptionId))
            {
                var parameters = new ProviderRegistrationCreateOrUpdateParameters()
                {
                    ProviderRegistration = this.ProviderRegistration ?? new ProviderRegistrationModel()
                    {
                        Name = this.Name,
                        Location = this.ArmLocation,
                        Properties = new ProviderRegistrationDefinition()
                        {
                            Name = this.Name,
                            DisplayName = this.DisplayName,
                            Enabled = true,
                            Location = this.Location,
                            ManifestEndpoint = new ResourceProviderEndpoint()
                            {
                                EndpointUri = this.ManifestEndpoint.AbsoluteUri,
                                AuthenticationUsername = this.UserName,
                                AuthenticationPassword = this.Password,
                            }
                        }
                    }
                };

                this.WriteVerbose(Resources.AddingResourceProviderRegistration.FormatArgs(parameters.ProviderRegistration.Properties.Name));

                this.ValidatePrerequisites(client, parameters);

                return client.ProviderRegistrations
                    .CreateOrUpdate(this.ResourceGroup, parameters)
                    .ProviderRegistration;
            }
        }
 /// <summary>
 /// Registers a resource provider manifest
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.AzureStack.Management.IProviderRegistrationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. Resource group name
 /// </param>
 /// <param name='parameters'>
 /// Required. Resource provider manifest definition
 /// </param>
 /// <returns>
 /// Result of the create or update of resource provider manifest
 /// </returns>
 public static Task<ProviderRegistrationCreateOrUpdateResult> CreateOrUpdateAsync(this IProviderRegistrationOperations operations, string resourceGroupName, ProviderRegistrationCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, parameters, CancellationToken.None);
 }