/// <summary>
        ///     Create a new server.
        /// </summary>
        /// <param name="deploymentConfiguration">
        ///     The configuration that the new server will be deployed with.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     The Id of the new server.
        /// </returns>
        public async Task <Guid> CreateServer(ServerDeploymentConfiguration deploymentConfiguration, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (deploymentConfiguration == null)
            {
                throw new ArgumentNullException(nameof(deploymentConfiguration));
            }

            Guid organizationId = await GetOrganizationId();

            HttpRequest createServer = Requests.Server.CreateServer.WithTemplateParameter("organizationId", organizationId);

            using (HttpResponseMessage response = await _httpClient.PostAsJsonAsync(createServer, deploymentConfiguration, cancellationToken))
            {
                ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2();

                if (apiResponse.ResponseCode != ApiResponseCodeV2.InProgress)
                {
                    throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode);
                }

                string serverId = apiResponse.InfoMessages.GetByName("serverId");
                if (String.IsNullOrWhiteSpace(serverId))
                {
                    throw new CloudControlException("Received an unexpected response from the CloudControl API (missing 'serverId' message).");
                }

                return(new Guid(serverId));
            }
        }
        /// <summary>
        ///     Retrieve a specific server by Id.
        /// </summary>
        /// <param name="serverId">
        ///     The Id of the server to retrieve.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     A <see cref="Server"/> representing the server, or <c>null</c> if no server was found with the specified Id.
        /// </returns>
        public async Task <Server> GetServer(Guid serverId, CancellationToken cancellationToken = default(CancellationToken))
        {
            Guid organizationId = await GetOrganizationId();

            HttpRequest getServer = Requests.Server.GetServerById
                                    .WithTemplateParameters(new
            {
                organizationId,
                serverId
            });

            using (HttpResponseMessage response = await _httpClient.GetAsync(getServer, cancellationToken))
            {
                if (!response.IsSuccessStatusCode)
                {
                    ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2();

                    if (apiResponse.ResponseCode == ApiResponseCodeV2.ResourceNotFound)
                    {
                        return(null);
                    }

                    throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode);
                }

                return(await response.ReadContentAsAsync <Server>());
            }
        }
        /// <summary>
        ///     Retrieve a list of servers.
        /// </summary>
        /// <param name="query">
        ///     A <see cref="ServerQuery"/> that determines which servers will be retrieved.
        /// </param>
        /// <param name="paging">
        ///     An optional <see cref="Paging"/> configuration for the results.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     A <see cref="Servers"/> representing the page of results.
        /// </returns>
        public async Task <Servers> ListServers(ServerQuery query, Paging paging = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            Guid organizationId = await GetOrganizationId();

            HttpRequest listServers = Requests.Server.ListServers
                                      .WithTemplateParameters(new
            {
                organizationId,
                networkDomainId = query.NetworkDomainId
            })
                                      .WithPaging(paging);

            using (HttpResponseMessage response = await _httpClient.GetAsync(listServers, cancellationToken))
            {
                if (!response.IsSuccessStatusCode)
                {
                    throw await CloudControlException.FromApiV2Response(response);
                }

                return(await response.ReadContentAsAsync <Servers>());
            }
        }
예제 #4
0
        /// <summary>
        ///     Create a new VLAN.
        /// </summary>
        /// <param name="name">
        ///     The name of the new VLAN.
        /// </param>
        /// <param name="description">
        ///     The description (if any) for the new VLAN.
        /// </param>
        /// <param name="networkDomainId">
        ///     The Id of the network domain in which the VLAN will be created.
        /// </param>
        /// <param name="privateIPv4BaseAddress">
        ///     The base address of the VLAN's private IPv4 network.
        /// </param>
        /// <param name="privateIPv4PrefixSize">
        ///     The optional size, in bits, of the VLAN's private IPv4 network prefix.
        ///
        ///		Default is 24 (i.e. a class C network).
        /// </param>
        /// <param name="gatewayAddressing">
        ///     The gateway addressing style to use for the new VLAN.
        ///
        ///		Default is <see cref="VlanGatewayAddressing.Low"/>.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     The Id of the new VLAN.
        /// </returns>
        public async Task <Guid> CreateVlan(string name, string description, Guid networkDomainId, string privateIPv4BaseAddress, int privateIPv4PrefixSize = 24, VlanGatewayAddressing gatewayAddressing = VlanGatewayAddressing.Low, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (String.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Must supply a valid name.", nameof(name));
            }

            if (description == null)
            {
                description = "";
            }

            if (networkDomainId == Guid.Empty)
            {
                throw new ArgumentException("Must supply a valid network domain Id.", nameof(networkDomainId));
            }

            Guid organizationId = await GetOrganizationId();

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

            HttpResponseMessage response = await
                                           _httpClient.PostAsJsonAsync(request,
                                                                       new CreateVlan
            {
                Name                   = name,
                Description            = description,
                NetworkDomainId        = networkDomainId,
                PrivateIPv4BaseAddress = privateIPv4BaseAddress,
                PrivateIPv4PrefixSize  = privateIPv4PrefixSize,
                GatewayAddressing      = gatewayAddressing
            },
                                                                       cancellationToken
                                                                       );

            using (response)
            {
                ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2();

                if (apiResponse.ResponseCode != ApiResponseCodeV2.InProgress)
                {
                    throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode);
                }

                string vlanId = apiResponse.InfoMessages.GetByName("vlanId");
                if (String.IsNullOrWhiteSpace(vlanId))
                {
                    throw new CloudControlException("Received an unexpected response from the CloudControl API (missing 'vlanId' message).");
                }

                return(new Guid(vlanId));
            }
        }
        /// <summary>
        ///     Create a new network domain.
        /// </summary>
        /// <param name="datacenterId">
        ///     The Id of the target datacenter (e.g. AU10, NA9).
        /// </param>
        /// <param name="name">
        ///     The name of the new network domain.
        /// </param>
        /// <param name="description">
        ///     The description (if any) for the new network domain.
        /// </param>
        /// <param name="type">
        ///     The network domain type.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     The Id of the new network domain.
        /// </returns>
        public async Task <Guid> CreateNetworkDomain(string datacenterId, string name, string description, NetworkDomainType type, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (String.IsNullOrWhiteSpace(datacenterId))
            {
                throw new ArgumentException("Must supply a valid datacenter Id.", nameof(datacenterId));
            }

            if (String.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Must supply a valid name.", nameof(name));
            }

            if (description == null)
            {
                description = "";
            }

            Guid organizationId = await GetOrganizationId();

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

            HttpResponseMessage response = await
                                           _httpClient.PostAsJsonAsync(request,
                                                                       new CreateNetworkDomain
            {
                Name         = name,
                Description  = description,
                Type         = type,
                DatacenterId = datacenterId
            },
                                                                       cancellationToken
                                                                       );

            using (response)
            {
                ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2();

                if (apiResponse.ResponseCode != ApiResponseCodeV2.InProgress)
                {
                    throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode);
                }

                string networkDomainId = apiResponse.InfoMessages.GetByName("networkDomainId");
                if (String.IsNullOrWhiteSpace(networkDomainId))
                {
                    throw new CloudControlException("Received an unexpected response from the CloudControl API (missing 'networkDomainId' message).");
                }

                return(new Guid(networkDomainId));
            }
        }
        /// <summary>
        ///     Create a new NAT rule.
        /// </summary>
        /// <param name="networkDomainId">
        ///     The Id of the network domain in which the NAT rule will be created.
        /// </param>
        /// <param name="internalIPAddress">
        ///     The internal IPv4 address targeted by the NAT rule.
        /// </param>
        /// <param name="externalIPAddress">
        ///     The external IPv4 address targeted by the NAT rule.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     The Id of the new NAT rule.
        /// </returns>
        public async Task <Guid> CreateNatRule(Guid networkDomainId, string internalIPAddress, string externalIPAddress, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (networkDomainId == Guid.Empty)
            {
                throw new ArgumentException("Must supply a valid network domain Id.", nameof(networkDomainId));
            }

            if (String.IsNullOrWhiteSpace(internalIPAddress))
            {
                throw new ArgumentException("Must supply a internal IP address.", nameof(internalIPAddress));
            }

            if (String.IsNullOrWhiteSpace(externalIPAddress))
            {
                throw new ArgumentException("Must supply a internal IP address.", nameof(externalIPAddress));
            }

            Guid organizationId = await GetOrganizationId();

            HttpRequest createNatRule = Requests.Network.CreateNatRule.WithTemplateParameter("organizationId", organizationId);

            HttpResponseMessage response = await
                                           _httpClient.PostAsJsonAsync(createNatRule,
                                                                       new CreateNatRule
            {
                NetworkDomainId   = networkDomainId,
                InternalIPAddress = internalIPAddress,
                ExternalIPAddress = externalIPAddress
            },
                                                                       cancellationToken
                                                                       );

            using (response)
            {
                ApiResponseV2 apiResponse = await response.ReadContentAsApiResponseV2();

                if (apiResponse.ResponseCode != ApiResponseCodeV2.InProgress)
                {
                    throw CloudControlException.FromApiV2Response(apiResponse, response.StatusCode);
                }

                string natRuleId = apiResponse.InfoMessages.GetByName("natRuleId");
                if (String.IsNullOrWhiteSpace(natRuleId))
                {
                    throw new CloudControlException("Received an unexpected response from the CloudControl API (missing 'natRuleId' message).");
                }

                return(new Guid(natRuleId));
            }
        }
        /// <summary>
        ///     Retrieve a list of NAT rules in the specified network domain.
        /// </summary>
        /// <param name="networkDomainId">
        ///     The Id of the target network domain.
        /// </param>
        /// <param name="paging">
        ///     An optional <see cref="Paging"/> configuration for the results.
        /// </param>
        /// <param name="cancellationToken">
        ///     An optional cancellation token that can be used to cancel the request.
        /// </param>
        /// <returns>
        ///     A <see cref="NatRules"/> representing the page of results.
        /// </returns>
        public async Task <NatRules> ListNatRules(Guid networkDomainId, Paging paging = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Guid organizationId = await GetOrganizationId();

            HttpRequest listNatRules = Requests.Network.ListNatRules
                                       .WithTemplateParameters(new
            {
                organizationId,
                networkDomainId
            })
                                       .WithPaging(paging);

            using (HttpResponseMessage response = await _httpClient.GetAsync(listNatRules, cancellationToken))
            {
                if (!response.IsSuccessStatusCode)
                {
                    throw await CloudControlException.FromApiV2Response(response);
                }

                return(await response.ReadContentAsAsync <NatRules>());
            }
        }