|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using DigitalOcean.API.Http; |
| 5 | +using DigitalOcean.API.Models.Responses; |
| 6 | +using RestSharp; |
| 7 | + |
| 8 | +namespace DigitalOcean.API.Clients { |
| 9 | + public class VpcClient : IVpcClient { |
| 10 | + private readonly IConnection _connection; |
| 11 | + |
| 12 | + public VpcClient(IConnection connection) { |
| 13 | + _connection = connection; |
| 14 | + } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// To list all of the VPCs on your account. |
| 18 | + /// </summary> |
| 19 | + public Task<IReadOnlyList<Vpc>> GetAll() { |
| 20 | + return _connection.GetPaginated<Vpc>("vpcs", null, "vpcs"); |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// To create a VPC. |
| 25 | + /// </summary> |
| 26 | + public Task<Vpc> Create(Models.Requests.Vpc vpc) { |
| 27 | + return _connection.ExecuteRequest<Vpc>("vpcs", null, vpc, "vpc", Method.POST); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// To show information about an existing VPC. |
| 32 | + /// </summary> |
| 33 | + public Task<Vpc> Get(string vpcId) { |
| 34 | + var parameters = new List<Parameter> { |
| 35 | + new Parameter("id", vpcId, ParameterType.UrlSegment) |
| 36 | + }; |
| 37 | + return _connection.ExecuteRequest<Vpc>("vpcs/{id}", parameters, null, "vpc"); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// To delete a VPC. |
| 42 | + /// The default VPC for a region can not be deleted. |
| 43 | + /// Additionally, a VPC can only be deleted if it does not contain any member resources. |
| 44 | + /// Attempting to delete a region's default VPC or a VPC that still has members will result in an error response. |
| 45 | + /// </summary> |
| 46 | + public Task Delete(string vpcId) { |
| 47 | + var parameters = new List<Parameter> { |
| 48 | + new Parameter("id", vpcId, ParameterType.UrlSegment) |
| 49 | + }; |
| 50 | + return _connection.ExecuteRaw("vpcs/{id}", parameters, null, Method.DELETE); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// To update information about a VPC. |
| 55 | + /// Currently, only the name and description attributes can be updated. |
| 56 | + /// Excluding an attribute will reset it to its default. |
| 57 | + /// </summary> |
| 58 | + public Task<Vpc> Update(string vpcId, Models.Requests.UpdateVpc updateVpc) { |
| 59 | + var parameters = new List<Parameter> { |
| 60 | + new Parameter("id", vpcId, ParameterType.UrlSegment) |
| 61 | + }; |
| 62 | + return _connection.ExecuteRequest<Vpc>("vpcs/{id}", parameters, updateVpc, "vpc", Method.PUT); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// To update a subset of information about a VPC. |
| 67 | + /// Currently, only the name and description attributes can be updated. |
| 68 | + /// </summary> |
| 69 | + public Task<Vpc> PartialUpdate(string vpcId, Models.Requests.UpdateVpc updateVpc) { |
| 70 | + var parameters = new List<Parameter> { |
| 71 | + new Parameter("id", vpcId, ParameterType.UrlSegment) |
| 72 | + }; |
| 73 | + return _connection.ExecuteRequest<Vpc>("vpcs/{id}", parameters, updateVpc, "vpc", Method.PATCH); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// To list all of the resources that are members of a VPC. |
| 78 | + /// To only list resources of a specific type that are members of the VPC, included a resource_type query parameter. |
| 79 | + /// </summary> |
| 80 | + public Task<IReadOnlyList<VpcMember>> ListMembers(string vpcId, string resourceType = null) { |
| 81 | + var parameters = new List<Parameter> { |
| 82 | + new Parameter("id", vpcId, ParameterType.UrlSegment) |
| 83 | + }; |
| 84 | + if (!String.IsNullOrEmpty(resourceType)) { |
| 85 | + parameters.Add(new Parameter("resource_type", resourceType, ParameterType.QueryString)); |
| 86 | + } |
| 87 | + return _connection.GetPaginated<VpcMember>("vpcs/{id}/members", parameters, "members"); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments