/// <summary>
        ///     Asynchronously perform Cmdlet processing.
        /// </summary>
        /// <param name="cancellationToken">
        ///     A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing.
        /// </param>
        /// <returns>
        ///     A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            if (!ShouldProcess(target: $"{Type} network domain '{Name}' in '{DatacenterId}'", action: "Create"))
            {
                return;
            }

            WriteVerbose(
                $"Create {Type} network domain '{Name}' in datacenter '{DatacenterId}'."
                );

            WriteVerbose("Initiating deployment of network domain...");

            CloudControlClient client = GetClient();
            Guid networkDomainId      = await client.CreateNetworkDomain(DatacenterId, Name, Description, Type, cancellationToken);

            WriteVerbose($"Deployment initiated for network domain '{networkDomainId}'.");

            NetworkDomain networkDomain = await client.GetNetworkDomain(networkDomainId, cancellationToken);

            if (networkDomain == null)
            {
                WriteError(
                    Errors.ResourceNotFoundById <NetworkDomain>(networkDomainId)
                    );

                return;
            }

            WriteObject(networkDomain);
        }
        /// <summary>
        ///     Update an existing network domain.
        /// </summary>
        /// <param name="networkDomain">
        ///     The network domain to update.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     The API response from CloudControl.
        /// </returns>
        public async Task <ApiResponseV2> EditNetworkDomain(NetworkDomain networkDomain, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (networkDomain == null)
            {
                throw new ArgumentNullException(nameof(networkDomain));
            }

            Guid organizationId = await GetOrganizationId();

            HttpRequest request = Requests.Network.EditNetworkDomain.WithTemplateParameter("organizationId", organizationId);

            HttpResponseMessage response = await
                                           _httpClient.PostAsJsonAsync(request,
                                                                       new EditNetworkDomain
            {
                Id          = networkDomain.Id,
                Name        = networkDomain.Name,
                Description = networkDomain.Description,
                Type        = networkDomain.Type
            },
                                                                       cancellationToken
                                                                       );

            using (response)
            {
                return(await response.ReadContentAsApiResponseV2());
            }
        }
예제 #3
0
        /// <summary>
        ///     Asynchronously perform Cmdlet processing.
        /// </summary>
        /// <param name="cancellationToken">
        ///     A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing.
        /// </param>
        /// <returns>
        ///     A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            CloudControlClient client = GetClient();

            switch (ParameterSetName)
            {
            case "By Datacenter":
            {
                Paging paging = GetPagingConfiguration();

                NetworkDomains networkDomains = await client.ListNetworkDomains(DatacenterId, paging, cancellationToken);

                WriteObject(networkDomains.Items,
                            enumerateCollection: true
                            );

                break;
            }

            case "By Id":
            {
                NetworkDomain networkDomain = await client.GetNetworkDomain(Id, cancellationToken);

                if (networkDomain == null)
                {
                    WriteError(
                        Errors.ResourceNotFoundById <NetworkDomain>(Id)
                        );
                }
                else
                {
                    WriteObject(networkDomain);
                }

                break;
            }

            case "By Name":
            {
                NetworkDomain networkDomain = await client.GetNetworkDomainByName(Name, DatacenterId);

                if (networkDomain == null)
                {
                    WriteError(
                        Errors.ResourceNotFoundByName <NetworkDomain>(Name,
                                                                      message: $"No network domain named '{Name}' was found in datacenter '{DatacenterId}'."
                                                                      )
                        );
                }
                else
                {
                    WriteObject(networkDomain);
                }

                break;
            }

            default:
            {
                ThrowTerminatingError(
                    Errors.UnrecognizedParameterSet(this)
                    );

                return;
            }
            }
        }
        /// <summary>
        ///     Asynchronously perform Cmdlet processing.
        /// </summary>
        /// <param name="cancellationToken">
        ///     A <see cref="CancellationToken"/> that can be used to cancel Cmdlet processing.
        /// </param>
        /// <returns>
        ///     A <see cref="Task"/> representing the asynchronous operation.
        /// </returns>
        protected override async Task ProcessRecordAsync(CancellationToken cancellationToken)
        {
            CloudControlClient client = GetClient();

            NetworkDomain networkDomain;

            switch (ParameterSetName)
            {
            case "From NetworkDomain":
            {
                networkDomain = NetworkDomain;

                break;
            }

            case "By Id":
            {
                networkDomain = await client.GetNetworkDomain(Id, cancellationToken);

                if (networkDomain == null)
                {
                    WriteError(
                        Errors.ResourceNotFoundById <NetworkDomain>(Id)
                        );

                    return;
                }

                break;
            }

            default:
            {
                ThrowTerminatingError(
                    Errors.UnrecognizedParameterSet(this)
                    );

                return;
            }
            }

            if (!ShouldProcess(target: $"network domain '{networkDomain.Id}' ('{networkDomain.Name}') in '{networkDomain.DatacenterId}'", action: "update"))
            {
                return;
            }

            ApiResponseV2 editResponse = await client.EditNetworkDomain(networkDomain, cancellationToken);

            if (!editResponse.IsSuccess())
            {
                WriteError(
                    Errors.CloudControlApi(client, editResponse)
                    );

                return;
            }

            NetworkDomain updatedNetworkDomain = await client.GetNetworkDomain(networkDomain.Id, cancellationToken);

            if (updatedNetworkDomain == null)
            {
                WriteError(
                    Errors.ResourceNotFoundById <NetworkDomain>(networkDomain.Id)
                    );

                return;
            }

            WriteObject(updatedNetworkDomain);
        }