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

Commit 683421e

Browse files
committed
Merge remote-tracking branch 'upstream/master' into add_cdn_endpoints
2 parents a00afa3 + 370f0f4 commit 683421e

10 files changed

Lines changed: 195 additions & 4 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/DigitalOcean.API.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5-
<Version>2.2.0</Version>
5+
<Version>2.3.0</Version>
66
<Description>.NET wrapper of the DigitalOcean API</Description>
77
<Copyright>2019</Copyright>
88
<PackageLicenseUrl>https://github.com/trmcnvn/DigitalOcean.API/blob/master/LICENSE.md</PackageLicenseUrl>
@@ -11,7 +11,7 @@
1111
<RepositoryUrl>https://github.com/trmcnvn/DigitalOcean.API</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<PackageTags>DigitalOcean API</PackageTags>
14-
<PackageReleaseNotes>- Add monitoring property to Droplet (@Nicholi)</PackageReleaseNotes>
14+
<PackageReleaseNotes>Added Project Resources API Support (@Nicholi)</PackageReleaseNotes>
1515
</PropertyGroup>
1616
<ItemGroup>
1717
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />

DigitalOcean.API/DigitalOcean.API.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
33
<metadata>
44
<id>DigitalOcean.API</id>
5-
<version>2.2.0</version>
5+
<version>2.3.0</version>
66
<authors>DigitalOcean.API</authors>
77
<owners>DigitalOcean.API</owners>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>
99
<licenseUrl>https://github.com/trmcnvn/DigitalOcean.API/blob/master/LICENSE.md</licenseUrl>
1010
<projectUrl>https://github.com/trmcnvn/DigitalOcean.API</projectUrl>
1111
<iconUrl>http://i.imgur.com/llqIpX6.png</iconUrl>
1212
<description>.NET wrapper of the DigitalOcean API</description>
13-
<releaseNotes>- Add monitoring property to Droplet (@Nicholi)</releaseNotes>
13+
<releaseNotes>Added Project Resources API Support (@Nicholi)</releaseNotes>
1414
<copyright>2019</copyright>
1515
<tags>DigitalOcean API</tags>
1616
<repository type="git" url="https://github.com/trmcnvn/DigitalOcean.API" />

DigitalOcean.API/DigitalOceanClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public DigitalOceanClient(string token) {
2525
ImageActions = new ImageActionsClient(_connection);
2626
Images = new ImagesClient(_connection);
2727
LoadBalancers = new LoadBalancerClient(_connection);
28+
ProjectResources = new ProjectResourcesClient(_connection);
2829
Keys = new KeysClient(_connection);
2930
Regions = new RegionsClient(_connection);
3031
Sizes = new SizesClient(_connection);
@@ -48,6 +49,7 @@ public IRateLimit Rates {
4849
public IImagesClient Images { get; private set; }
4950
public IKeysClient Keys { get; private set; }
5051
public ILoadBalancerClient LoadBalancers { get; private set; }
52+
public IProjectResourcesClient ProjectResources { get; private set; }
5153
public IRegionsClient Regions { get; private set; }
5254
public ISizesClient Sizes { get; private set; }
5355
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
@@ -14,6 +14,7 @@ public interface IDigitalOceanClient {
1414
IImagesClient Images { get; }
1515
IKeysClient Keys { get; }
1616
ILoadBalancerClient LoadBalancers { get;}
17+
IProjectResourcesClient ProjectResources { get; }
1718
IRegionsClient Regions { get; }
1819
ISizesClient Sizes { get; }
1920

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)