Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 5add958

Browse files
authored
Merge pull request #31 from Nicholi/add_project_resources
Added Project Resources api calls
2 parents 3a4b7f2 + 08e7354 commit 5add958

8 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Collections.Generic;
2+
using DigitalOcean.API.Clients;
3+
using DigitalOcean.API.Http;
4+
using DigitalOcean.API.Models.Requests;
5+
using DigitalOcean.API.Models.Responses;
6+
using NSubstitute;
7+
using RestSharp;
8+
using Xunit;
9+
10+
namespace DigitalOcean.API.Tests.Clients {
11+
public class ProjectResourcesClientTest {
12+
[Fact]
13+
public void CorrectRequestForGetResources() {
14+
var factory = Substitute.For<IConnection>();
15+
var client = new ProjectResourcesClient(factory);
16+
17+
client.GetResources("project:abc123");
18+
19+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "project:abc123");
20+
factory.Received().GetPaginated<ProjectResource>("projects/{project_id}/resources", parameters, "resources");
21+
}
22+
23+
[Fact]
24+
public void CorrectRequestForGetDefaultResources() {
25+
var factory = Substitute.For<IConnection>();
26+
var client = new ProjectResourcesClient(factory);
27+
28+
client.GetDefaultResources();
29+
30+
factory.Received().GetPaginated<ProjectResource>("projects/default/resources", null, "resources");
31+
}
32+
33+
[Fact]
34+
public void CorrectRequestForAssignResources() {
35+
var factory = Substitute.For<IConnection>();
36+
var client = new ProjectResourcesClient(factory);
37+
38+
var body = new AssignResourceNames();
39+
client.AssignResources("project:abc123", body);
40+
41+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "project:abc123");
42+
factory.Received().ExecuteRequest<List<ProjectResource>>("projects/{project_id}/resources", parameters, body, "resources", Method.POST);
43+
}
44+
45+
[Fact]
46+
public void CorrectRequestForAssignDefaultResources() {
47+
var factory = Substitute.For<IConnection>();
48+
var client = new ProjectResourcesClient(factory);
49+
50+
var body = new AssignResourceNames();
51+
client.AssignDefaultResources(body);
52+
53+
factory.Received().ExecuteRequest<List<ProjectResource>>("projects/default/resources", null, body, "resources", Method.POST);
54+
}
55+
}
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DigitalOcean.API.Models.Requests;
4+
using DigitalOcean.API.Models.Responses;
5+
6+
namespace DigitalOcean.API.Clients {
7+
public interface IProjectResourcesClient {
8+
/// <summary>
9+
/// List all your resources in a project.
10+
/// </summary>
11+
Task<IReadOnlyList<ProjectResource>> GetResources(string projectId);
12+
13+
/// <summary>
14+
/// List all your resources in the default project.
15+
/// </summary>
16+
Task<IReadOnlyList<ProjectResource>> GetDefaultResources();
17+
18+
/// <summary>
19+
/// To assign resources to a project.
20+
/// </summary>
21+
Task<IReadOnlyList<ProjectResource>> AssignResources(string projectId, AssignResourceNames resources);
22+
23+
/// <summary>
24+
/// To assign resources to the default project.
25+
/// </summary>
26+
Task<IReadOnlyList<ProjectResource>> AssignDefaultResources(AssignResourceNames resources);
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DigitalOcean.API.Extensions;
4+
using DigitalOcean.API.Http;
5+
using DigitalOcean.API.Models.Requests;
6+
using DigitalOcean.API.Models.Responses;
7+
using RestSharp;
8+
9+
namespace DigitalOcean.API.Clients {
10+
public class ProjectResourcesClient : IProjectResourcesClient {
11+
private readonly IConnection _connection;
12+
13+
public ProjectResourcesClient(IConnection connection) {
14+
_connection = connection;
15+
}
16+
17+
/// <summary>
18+
/// List all your resources in a project.
19+
/// </summary>
20+
public Task<IReadOnlyList<ProjectResource>> GetResources(string projectId) {
21+
var parameters = new List<Parameter> {
22+
new Parameter { Name = "project_id", Value = projectId, Type = ParameterType.UrlSegment }
23+
};
24+
return _connection.GetPaginated<ProjectResource>("projects/{project_id}/resources", parameters, "resources");
25+
}
26+
27+
/// <summary>
28+
/// List all your resources in the default project.
29+
/// </summary>
30+
public Task<IReadOnlyList<ProjectResource>> GetDefaultResources() {
31+
return _connection.GetPaginated<ProjectResource>("projects/default/resources", null, "resources");
32+
}
33+
34+
/// <summary>
35+
/// To assign resources to a project.
36+
/// </summary>
37+
public Task<IReadOnlyList<ProjectResource>> AssignResources(string projectId, AssignResourceNames resources) {
38+
var parameters = new List<Parameter> {
39+
new Parameter { Name = "project_id", Value = projectId, Type = ParameterType.UrlSegment }
40+
};
41+
return _connection.ExecuteRequest<List<ProjectResource>>("projects/{project_id}/resources", parameters, resources, "resources", Method.POST)
42+
.ToReadOnlyListAsync();
43+
}
44+
45+
/// <summary>
46+
/// To assign resources to the default project.
47+
/// </summary>
48+
public Task<IReadOnlyList<ProjectResource>> AssignDefaultResources(AssignResourceNames resources) {
49+
return _connection.ExecuteRequest<List<ProjectResource>>("projects/default/resources", null, resources, "resources", Method.POST)
50+
.ToReadOnlyListAsync();
51+
}
52+
}
53+
}

DigitalOcean.API/DigitalOceanClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public DigitalOceanClient(string token) {
2323
ImageActions = new ImageActionsClient(_connection);
2424
Images = new ImagesClient(_connection);
2525
LoadBalancers = new LoadBalancerClient(_connection);
26+
ProjectResources = new ProjectResourcesClient(_connection);
2627
Keys = new KeysClient(_connection);
2728
Regions = new RegionsClient(_connection);
2829
Sizes = new SizesClient(_connection);
@@ -44,6 +45,7 @@ public IRateLimit Rates {
4445
public IImagesClient Images { get; private set; }
4546
public IKeysClient Keys { get; private set; }
4647
public ILoadBalancerClient LoadBalancers { get; private set; }
48+
public IProjectResourcesClient ProjectResources { get; private set; }
4749
public IRegionsClient Regions { get; private set; }
4850
public ISizesClient Sizes { get; private set; }
4951
public ITagsClient Tags { get; private set; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace DigitalOcean.API.Extensions {
5+
public static class ListExtensions {
6+
public static Task<IReadOnlyList<T>> ToReadOnlyListAsync<T>(this Task<List<T>> listTask) {
7+
return listTask.ContinueWith(t => t.Result as IReadOnlyList<T>);
8+
}
9+
}
10+
}

DigitalOcean.API/IDigitalOceanClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IDigitalOceanClient {
1212
IImagesClient Images { get; }
1313
IKeysClient Keys { get; }
1414
ILoadBalancerClient LoadBalancers { get;}
15+
IProjectResourcesClient ProjectResources { get; }
1516
IRegionsClient Regions { get; }
1617
ISizesClient Sizes { get; }
1718

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace DigitalOcean.API.Models.Requests {
5+
public class AssignResourceNames {
6+
/// <summary>
7+
/// A list of uniform resource names (URNs) to be added to a project.
8+
/// </summary>
9+
[JsonProperty("resources")]
10+
public List<string> Resources { get; set; }
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace DigitalOcean.API.Models.Responses {
4+
public class ProjectResource {
5+
/// <summary>
6+
/// The uniform resource name of the resource.
7+
/// </summary>
8+
public string Urn { get; set; }
9+
10+
/// <summary>
11+
/// A time value given in ISO8601 combined date and time format that represents when the project was created.
12+
/// </summary>
13+
public DateTime AssignedAt { get; set; }
14+
15+
/// <summary>
16+
/// The links object contains the self object, which contains the resource relationship.
17+
/// </summary>
18+
public SelfLink Links { get; set; }
19+
20+
/// <summary>
21+
/// The status of assigning and fetching the resources.
22+
/// </summary>
23+
public string Status { get; set; }
24+
25+
public class SelfLink {
26+
public string Self { get; set; }
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)