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

Commit 044aaf3

Browse files
committed
Add Snapshots api
1 parent 1268d33 commit 044aaf3

6 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Generic;
2+
using DigitalOcean.API.Clients;
3+
using DigitalOcean.API.Http;
4+
using DigitalOcean.API.Models.Responses;
5+
using NSubstitute;
6+
using RestSharp;
7+
using Xunit;
8+
9+
namespace DigitalOcean.API.Tests.Clients {
10+
public class SnapshotsClientTest {
11+
[Fact]
12+
public void CorrectRequestForGetAll() {
13+
var factory = Substitute.For<IConnection>();
14+
var client = new SnapshotsClient(factory);
15+
16+
client.GetAll();
17+
factory.Received().GetPaginated<Snapshot>("snapshots", null, "snapshots");
18+
}
19+
20+
[Fact]
21+
public void CorrectRequestForGetAllDroplet() {
22+
var factory = Substitute.For<IConnection>();
23+
var client = new SnapshotsClient(factory);
24+
25+
client.GetAllDroplet();
26+
factory.Received().GetPaginated<Snapshot>("snapshots?resource_type=droplet", null, "snapshots");
27+
}
28+
29+
[Fact]
30+
public void CorrectRequestForGetAllVolume() {
31+
var factory = Substitute.For<IConnection>();
32+
var client = new SnapshotsClient(factory);
33+
34+
client.GetAllVolume();
35+
factory.Received().GetPaginated<Snapshot>("snapshots?resource_type=volume", null, "snapshots");
36+
}
37+
38+
[Fact]
39+
public void CorrectRequestForGet() {
40+
var factory = Substitute.For<IConnection>();
41+
var client = new SnapshotsClient(factory);
42+
43+
client.Get("snapshot:abc123");
44+
45+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "snapshot:abc123");
46+
factory.Received().ExecuteRequest<Snapshot>("snapshots/{snapshot_id}", parameters, null, "snapshot");
47+
}
48+
49+
[Fact]
50+
public void CorrectRequestForDelete() {
51+
var factory = Substitute.For<IConnection>();
52+
var client = new SnapshotsClient(factory);
53+
54+
client.Delete("snapshot:abc123");
55+
56+
var parameters = Arg.Is<List<Parameter>>(list => (string)list[0].Value == "snapshot:abc123");
57+
factory.Received().ExecuteRaw("snapshots/{snapshot_id}", parameters, null, Method.DELETE);
58+
}
59+
60+
}
61+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using DigitalOcean.API.Models.Responses;
4+
5+
namespace DigitalOcean.API.Clients {
6+
public interface ISnapshotsClient {
7+
/// <summary>
8+
/// To list all of the snapshots available on your account.
9+
/// </summary>
10+
Task<IReadOnlyList<Snapshot>> GetAll();
11+
12+
/// <summary>
13+
/// To retrieve only snapshots based on Droplets.
14+
/// </summary>
15+
Task<IReadOnlyList<Snapshot>> GetAllDroplet();
16+
17+
/// <summary>
18+
/// To retrieve only snapshots based on volumes.
19+
/// </summary>
20+
Task<IReadOnlyList<Snapshot>> GetAllVolume();
21+
22+
/// <summary>
23+
/// To retrieve information about a snapshot,
24+
/// </summary>
25+
Task<Snapshot> Get(string snapshotId);
26+
27+
/// <summary>
28+
/// To delete a snapshot.
29+
/// </summary>
30+
Task Delete(string snapshotId);
31+
}
32+
}
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 System.Threading.Tasks;
3+
using DigitalOcean.API.Http;
4+
using DigitalOcean.API.Models.Responses;
5+
using RestSharp;
6+
7+
namespace DigitalOcean.API.Clients {
8+
public class SnapshotsClient : ISnapshotsClient {
9+
private readonly IConnection _connection;
10+
11+
public SnapshotsClient(IConnection connection) {
12+
_connection = connection;
13+
}
14+
15+
/// <summary>
16+
/// To list all of the snapshots available on your account.
17+
/// </summary>
18+
public Task<IReadOnlyList<Snapshot>> GetAll() {
19+
return _connection.GetPaginated<Snapshot>("snapshots", null, "snapshots");
20+
}
21+
22+
/// <summary>
23+
/// To retrieve only snapshots based on Droplets.
24+
/// </summary>
25+
public Task<IReadOnlyList<Snapshot>> GetAllDroplet() {
26+
return _connection.GetPaginated<Snapshot>("snapshots?resource_type=droplet", null, "snapshots");
27+
}
28+
29+
/// <summary>
30+
/// To retrieve only snapshots based on volumes.
31+
/// </summary>
32+
public Task<IReadOnlyList<Snapshot>> GetAllVolume() {
33+
return _connection.GetPaginated<Snapshot>("snapshots?resource_type=volume", null, "snapshots");
34+
}
35+
36+
/// <summary>
37+
/// To retrieve information about a snapshot,
38+
/// </summary>
39+
public Task<Snapshot> Get(string snapshotId) {
40+
var parameters = new List<Parameter> {
41+
new Parameter { Name = "snapshot_id", Value = snapshotId, Type = ParameterType.UrlSegment }
42+
};
43+
return _connection.ExecuteRequest<Snapshot>("snapshots/{snapshot_id}", parameters, null, "snapshot");
44+
}
45+
46+
/// <summary>
47+
/// To delete a snapshot.
48+
/// </summary>
49+
public Task Delete(string snapshotId) {
50+
var parameters = new List<Parameter> {
51+
new Parameter { Name = "snapshot_id", Value = snapshotId, Type = ParameterType.UrlSegment }
52+
};
53+
return _connection.ExecuteRaw("snapshots/{snapshot_id}", parameters, null, Method.DELETE);
54+
}
55+
}
56+
}

DigitalOcean.API/DigitalOceanClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public DigitalOceanClient(string token) {
2929
Keys = new KeysClient(_connection);
3030
Regions = new RegionsClient(_connection);
3131
Sizes = new SizesClient(_connection);
32+
Snapshots = new SnapshotsClient(_connection);
3233
Tags = new TagsClient(_connection);
3334
}
3435

@@ -52,6 +53,7 @@ public IRateLimit Rates {
5253
public IProjectResourcesClient ProjectResources { get; private set; }
5354
public IRegionsClient Regions { get; private set; }
5455
public ISizesClient Sizes { get; private set; }
56+
public ISnapshotsClient Snapshots { get; private set; }
5557
public ITagsClient Tags { get; private set; }
5658

5759
#endregion

DigitalOcean.API/IDigitalOceanClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IDigitalOceanClient {
1717
IProjectResourcesClient ProjectResources { get; }
1818
IRegionsClient Regions { get; }
1919
ISizesClient Sizes { get; }
20+
ISnapshotsClient Snapshots { get; }
2021

2122
IRateLimit Rates { get; }
2223
ITagsClient Tags { get; }
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace DigitalOcean.API.Models.Responses {
5+
/// <summary>
6+
/// Snapshots are saved instances of a Droplet or a block storage volume, which is reflected in the resource_type attribute.
7+
/// In order to avoid problems with compressing filesystems, each defines a min_disk_size attribute which is the minimum size of the
8+
/// Droplet or volume disk when creating a new resource from the saved snapshot.
9+
/// </summary>
10+
public class Snapshot {
11+
/// <summary>
12+
/// The unique identifier for the snapshot.
13+
/// </summary>
14+
public string Id { get; set; }
15+
16+
/// <summary>
17+
/// A human-readable name for the snapshot.
18+
/// </summary>
19+
public string Name { get; set; }
20+
21+
/// <summary>
22+
/// A time value given in ISO8601 combined date and time format that represents when the snapshot was created.
23+
/// </summary>
24+
public DateTime CreatedAt { get; set; }
25+
26+
/// <summary>
27+
/// An array of the regions that the image is available in. The regions are represented by their identifying slug values.
28+
/// </summary>
29+
public List<string> Regions { get; set; }
30+
31+
/// <summary>
32+
/// A unique identifier for the resource that the action is associated with.
33+
/// </summary>
34+
public string ResourceId { get; set; }
35+
36+
/// <summary>
37+
/// The type of resource that the action is associated with.
38+
/// </summary>
39+
public string ResourceType { get; set; }
40+
41+
/// <summary>
42+
/// The minimum size in GB required for a volume or Droplet to use this snapshot.
43+
/// </summary>
44+
public int MinDiskSize { get; set; }
45+
46+
/// <summary>
47+
/// The billable size of the snapshot in gigabytes.
48+
/// </summary>
49+
public double SizeGigabytes { get; set; }
50+
51+
/// <summary>
52+
/// An array of Tags the snapshot has been tagged with
53+
/// </summary>
54+
public List<string> Tags { get; set; }
55+
}
56+
}

0 commit comments

Comments
 (0)