コード例 #1
0
        public async Task RemoveDropletsAsync(
            string firewallId,
            int[] dropletIds,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/firewalls/{firewallId}/droplets";
            var    data     = new {
                droplet_ids = dropletIds
            };

            //We have to get funky to send a body in a delete
            var uri = new Uri(UriUtility.Combine(_client.BaseAddress.ToString(), endpoint));
            HttpRequestMessage request = new HttpRequestMessage
            {
                Content    = EncodeContent(data),
                Method     = HttpMethod.Delete,
                RequestUri = uri
            };

            var result = await _client.SendAsync(request, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }
        }
コード例 #2
0
        public async Task <Volume[]> GetAllVolumesByNameAsync(
            string volumeName,
            string region = null,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/volumes?name={volumeName}";

            if (region != null)
            {
                endpoint += $"&region={region}";
            }

            var result = await _client.GetAsync(endpoint, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <VolumeList>(jsonResult);

            return(resultObj.Volumes);
        }
コード例 #3
0
        public async Task <Firewall> AddDropletsAsync(
            string firewallId,
            int[] dropletIds,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/firewalls/{firewallId}/droplets";
            var    data     = new {
                droplet_ids = dropletIds
            };

            var result = await _client.PostAsync(endpoint, EncodeContent(data), cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <Firewall>(jsonResult);

            return(resultObj);
        }
コード例 #4
0
        public async Task DeleteVolumeAsync(
            string volumeId,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/volumes/{volumeId}";

            var result = await _client.DeleteAsync(endpoint, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }
        }
コード例 #5
0
        public async Task <Region[]> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            string endpoint = $"/v2/regions";

            var result = await _client.GetAsync(endpoint, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <RegionList>(jsonResult);

            return(resultObj.Regions);
        }
コード例 #6
0
        public async Task <NodePool[]> ListNodePoolsAsync(
            string clusterId,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/kubernetes/clusters/{clusterId}/node_pools";

            var result = await _client.GetAsync(endpoint, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            return(JsonConvert.DeserializeObject <NodePoolListResult>(jsonResult).NodePools);
        }
コード例 #7
0
        public async Task <Volume> CreateVolumeAsync(
            BlockStorageVolCreate data,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/volumes";

            var result = await _client.PostAsync(endpoint, EncodeContent(data), cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <Volume>(jsonResult);

            return(resultObj);
        }
コード例 #8
0
        public async Task <DropletList> GetByTagAsync(
            string tag,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/droplets?tag_name={tag}";

            var result = await _client.GetAsync(endpoint, cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <DropletList>(jsonResult);

            return(resultObj);
        }
コード例 #9
0
        public async Task <DigitalOceanAction> DetachVolumeAsync(
            string volumeId,
            VolumeDetach data,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/volumes/{volumeId}/actions";

            var result = await _client.PostAsync(endpoint, EncodeContent(data), cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var resultObj = JsonConvert.DeserializeObject <DigitalOceanActionResult>(jsonResult);

            return(resultObj.Action);
        }
コード例 #10
0
        /// <summary>
        /// To add an additional node pool to a Kubernetes clusters, send a
        /// POST request to /v2/kubernetes/clusters/$K8S_CLUSTER_ID/node_pools with the following attributes.
        /// </summary>
        /// <param name="clusterId"></param>
        /// <param name="doDropletSize">The slug identifier for the type of Droplet to be used as workers in the node pool.</param>
        /// <param name="poolName">A human-readable name for the node pool.</param>
        /// <param name="nodeCount">The number of Droplet instances in the node pool.</param>
        /// <param name="tags">A flat array of tag names as strings to be applied to the node pool. All node pools will be
        /// automatically tagged "k8s," "k8s-worker," and "k8s:$K8S_CLUSTER_ID" in addition to any tags provided by the user.</param>
        /// <returns></returns>
        public async Task <NodePool> CreateNodePoolAsync(
            string clusterId,
            string doDropletSize,
            string poolName,
            int nodeCount,
            List <string> tags = null,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            string endpoint = $"/v2/kubernetes/clusters/{clusterId}/node_pools";

            var data = new CreateNodePoolInput()
            {
                Size  = doDropletSize,
                Name  = poolName,
                Count = nodeCount
            };

            if (tags != null)
            {
                data.Tags = tags;
            }

            var result = await _client.PostAsync(endpoint, EncodeContent(data), cancellationToken);

            string jsonResult = await result.Content.ReadAsStringAsync();

            if (!result.IsSuccessStatusCode)
            {
                ApiErrorResult error = JsonConvert.DeserializeObject <ApiErrorResult>(jsonResult);
                throw new DigitalOceanApiCallException(error);
            }

            var nodePool = JsonConvert.DeserializeObject <NodePoolResult>(jsonResult).NodePool;

            return(nodePool);
        }
コード例 #11
0
 public DigitalOceanApiCallException(ApiErrorResult error) : base(error.ToString())
 {
 }