Skip to content

Commit 0c0b768

Browse files
committed
Compute.KeyPairs
Closes #534
1 parent 144ed3a commit 0c0b768

19 files changed

Lines changed: 537 additions & 29 deletions

src/corelib/Compute/v2_1/ComputeApiBuilder.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -978,25 +978,100 @@ public virtual async Task<TPage> ListImageDetailsAsync<TPage>(Url url, Cancellat
978978

979979
#region Keypairs
980980

981+
/// <summary />
982+
public virtual async Task<T> GetKeyPairAsync<T>(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
983+
where T : IServiceResource
984+
{
985+
if (keypairName == null)
986+
throw new ArgumentNullException("keypairName");
987+
988+
PreparedRequest request = await BuildGetKeyPairRequestAsync(keypairName, cancellationToken);
989+
990+
var result = await request.SendAsync().ReceiveJson<T>();
991+
result.PropogateOwner(this);
992+
return result;
993+
}
994+
995+
/// <summary />
996+
public virtual async Task<PreparedRequest> BuildGetKeyPairRequestAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
997+
{
998+
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);
999+
1000+
return endpoint
1001+
.AppendPathSegment($"os-keypairs/{keypairName}")
1002+
.Authenticate(AuthenticationProvider)
1003+
.SetMicroversion(this)
1004+
.PrepareGet(cancellationToken);
1005+
}
1006+
9811007
/// <summary />
9821008
public virtual async Task<T> CreateKeyPairAsync<T>(object keypair, CancellationToken cancellationToken = default(CancellationToken))
1009+
where T : IServiceResource
9831010
{
1011+
if(keypair == null)
1012+
throw new ArgumentNullException("keypair");
1013+
9841014
PreparedRequest request = await BuildCreateKeyPairRequestAsync(keypair, cancellationToken);
985-
return await request.SendAsync().ReceiveJson<T>();
1015+
var result = await request.SendAsync().ReceiveJson<T>();
1016+
result.PropogateOwner(this);
1017+
return result;
1018+
}
1019+
1020+
/// <summary />
1021+
public virtual async Task<PreparedRequest> BuildCreateKeyPairRequestAsync(object keypairName, CancellationToken cancellationToken = default(CancellationToken))
1022+
{
1023+
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);
1024+
1025+
return endpoint
1026+
.AppendPathSegment("os-keypairs")
1027+
.Authenticate(AuthenticationProvider)
1028+
.SetMicroversion(this)
1029+
.PreparePostJson(keypairName, cancellationToken);
1030+
}
1031+
1032+
/// <summary />
1033+
public virtual async Task<T> ListKeyPairsAsync<T>(CancellationToken cancellationToken = default(CancellationToken))
1034+
where T : IEnumerable<IServiceResource>
1035+
{
1036+
PreparedRequest request = await BuildListKeyPairsRequestAsync(cancellationToken);
1037+
var result = await request.SendAsync().ReceiveJson<T>();
1038+
result.PropogateOwner(this);
1039+
return result;
9861040
}
9871041

9881042
/// <summary />
989-
public virtual async Task<PreparedRequest> BuildCreateKeyPairRequestAsync(object keypair, CancellationToken cancellationToken = default(CancellationToken))
1043+
public virtual async Task<PreparedRequest> BuildListKeyPairsRequestAsync(CancellationToken cancellationToken = default(CancellationToken))
9901044
{
9911045
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);
9921046

9931047
return endpoint
9941048
.AppendPathSegment("os-keypairs")
9951049
.Authenticate(AuthenticationProvider)
9961050
.SetMicroversion(this)
997-
.PreparePostJson(keypair, cancellationToken);
1051+
.PrepareGet(cancellationToken);
1052+
}
1053+
1054+
/// <summary />
1055+
public virtual Task DeleteKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
1056+
{
1057+
if (keypairName == null)
1058+
throw new ArgumentNullException("keypairName");
1059+
1060+
return BuildDeleteKeyPairRequestAsync(keypairName, cancellationToken).SendAsync();
9981061
}
9991062

1063+
/// <summary />
1064+
public virtual async Task<PreparedRequest> BuildDeleteKeyPairRequestAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
1065+
{
1066+
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);
1067+
1068+
return (PreparedRequest)endpoint
1069+
.AppendPathSegment($"os-keypairs/{keypairName}")
1070+
.Authenticate(AuthenticationProvider)
1071+
.SetMicroversion(this)
1072+
.PrepareDelete(cancellationToken)
1073+
.AllowHttpStatus(HttpStatusCode.NotFound);
1074+
}
10001075
#endregion
10011076

10021077
#region Security Groups

src/corelib/Compute/v2_1/ComputeService.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,35 @@ public ComputeService(IAuthenticationProvider authenticationProvider, string reg
321321

322322
#region Keypairs
323323

324+
/// <inheritdoc cref="ComputeApiBuilder.GetKeyPairAsync{T}" />
325+
public virtual Task<KeyPairDetails> GetKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
326+
{
327+
return _computeApi.GetKeyPairAsync<KeyPairDetails>(keypairName, cancellationToken);
328+
}
329+
324330
/// <inheritdoc cref="ComputeApiBuilder.CreateKeyPairAsync{T}" />
325-
public virtual Task<KeyPair> CreateKeyPairAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
331+
public virtual Task<KeyPairResponse> CreateKeyPairAsync(KeyPairRequest request, CancellationToken cancellationToken = default(CancellationToken))
332+
{
333+
return _computeApi.CreateKeyPairAsync<KeyPairResponse>(request, cancellationToken);
334+
}
335+
336+
/// <summary />
337+
public virtual Task<KeyPair> ImportKeyPairAsync(KeyPairDefinition keypair, CancellationToken cancellationToken = default(CancellationToken))
326338
{
327-
var keyPair = new KeyPairDefinition(name);
328-
return _computeApi.CreateKeyPairAsync<KeyPair>(keyPair, cancellationToken);
339+
return _computeApi.CreateKeyPairAsync<KeyPair>(keypair, cancellationToken);
329340
}
330341

342+
/// <inheritdoc cref="ComputeApiBuilder.ListKeyPairsAsync{T}" />
343+
public virtual async Task<IEnumerable<KeyPair>> ListKeyPairsAsync(CancellationToken cancellationToken = default(CancellationToken))
344+
{
345+
return await _computeApi.ListKeyPairsAsync<KeyPairCollection>(cancellationToken);
346+
}
347+
348+
/// <summary />
349+
public virtual Task DeleteKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
350+
{
351+
return _computeApi.DeleteKeyPairAsync(keypairName, cancellationToken);
352+
}
331353
#endregion
332354

333355
#region Security Groups

src/corelib/Compute/v2_1/ComputeServiceExtensions.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,36 @@ public static void DetachVolume(this ComputeService service, Identifier serverId
300300
#endregion
301301

302302
#region KeyPairs
303+
/// <inheritdoc cref="ComputeService.GetKeyPairAsync" />
304+
public static KeyPairDetails GetKeyPair(this ComputeService service, string keypairName)
305+
{
306+
return service.GetKeyPairAsync(keypairName).ForceSynchronous();
307+
}
308+
303309
/// <inheritdoc cref="ComputeService.CreateKeyPairAsync" />
304-
public static KeyPair CreateKeyPair(this ComputeService service, string name)
310+
public static KeyPairResponse CreateKeyPair(this ComputeService service, KeyPairRequest request)
305311
{
306-
return service.CreateKeyPairAsync(name).ForceSynchronous();
312+
return service.CreateKeyPairAsync(request).ForceSynchronous();
307313
}
314+
315+
/// <inheritdoc cref="ComputeService.ImportKeyPairAsync" />
316+
public static KeyPair ImportKeyPair(this ComputeService service, KeyPairDefinition keyPair)
317+
{
318+
return service.ImportKeyPairAsync(keyPair).ForceSynchronous();
319+
}
320+
321+
/// <inheritdoc cref="ComputeService.ListKeyPairsAsync" />
322+
public static IEnumerable<KeyPair> ListKeyPairs(this ComputeService service)
323+
{
324+
return service.ListKeyPairsAsync().ForceSynchronous();
325+
}
326+
327+
/// <inheritdoc cref="ComputeService.DeleteKeyPairAsync" />
328+
public static void DeleteKeyPair(this ComputeService service, string keypairName)
329+
{
330+
service.DeleteKeyPairAsync(keypairName).ForceSynchronous();
331+
}
332+
308333
#endregion
309334

310335
#region Security Groups

src/corelib/Compute/v2_1/KeyPair.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24
using Newtonsoft.Json;
35
using Newtonsoft.Json.Linq;
6+
using OpenStack.Compute.v2_1.Serialization;
47
using OpenStack.Serialization;
58

69
namespace OpenStack.Compute.v2_1
710
{
811
/// <summary />
9-
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "keypair")]
10-
public class KeyPair : IHaveExtraData
12+
[JsonConverter(typeof(KeyPairConverter))]
13+
public class KeyPair : IHaveExtraData, IServiceResource
1114
{
1215
/// <summary />
1316
[JsonProperty("name")]
@@ -24,5 +27,14 @@ public class KeyPair : IHaveExtraData
2427
/// <summary />
2528
[JsonExtensionData]
2629
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
30+
31+
object IServiceResource.Owner { get; set; }
32+
33+
/// <inheritdoc cref="ComputeApiBuilder.DeleteKeyPairAsync" />
34+
public Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
35+
{
36+
var compute = this.GetOwnerOrThrow<ComputeApiBuilder>();
37+
return compute.DeleteKeyPairAsync(Name, cancellationToken);
38+
}
2739
}
2840
}

src/corelib/Compute/v2_1/KeyPairDefinition.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using Newtonsoft.Json;
2-
using OpenStack.Serialization;
2+
using OpenStack.Compute.v2_1.Serialization;
33

44
namespace OpenStack.Compute.v2_1
55
{
66
/// <summary />
7-
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "keypair")]
7+
[JsonConverter(typeof(KeyPairConverter))]
88
public class KeyPairDefinition
99
{
1010
/// <summary />
11-
public KeyPairDefinition(string name)
11+
public KeyPairDefinition(string name, string publicKey)
1212
{
1313
Name = name;
14+
PublicKey = publicKey;
1415
}
1516

1617
/// <summary />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace OpenStack.Compute.v2_1
5+
{
6+
/// <summary />
7+
public class KeyPairDetails : KeyPair
8+
{
9+
/// <summary />
10+
[JsonProperty("id")]
11+
public Identifier Id { get; set; }
12+
13+
/// <summary />
14+
[JsonProperty("created_at")]
15+
public DateTimeOffset Created { get; set; }
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using OpenStack.Compute.v2_1;
2+
using OpenStack.Synchronous.Extensions;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace OpenStack.Synchronous
6+
{
7+
/// <summary />
8+
public static class KeypairExtensions_v2_1
9+
{
10+
/// <inheritdoc cref="KeyPair.DeleteAsync" />
11+
public static void Delete(this KeyPair keypair)
12+
{
13+
keypair.DeleteAsync().ForceSynchronous();
14+
}
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
using OpenStack.Compute.v2_1.Serialization;
3+
4+
namespace OpenStack.Compute.v2_1
5+
{
6+
/// <summary />
7+
[JsonConverter(typeof(KeyPairConverter))]
8+
public class KeyPairRequest
9+
{
10+
/// <summary />
11+
public KeyPairRequest(string name)
12+
{
13+
Name = name;
14+
}
15+
16+
/// <summary />
17+
[JsonProperty("name")]
18+
public string Name { get; set; }
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Newtonsoft.Json;
2+
3+
namespace OpenStack.Compute.v2_1
4+
{
5+
/// <summary />
6+
public class KeyPairResponse : KeyPair
7+
{
8+
/// <summary />
9+
[JsonProperty("private_key")]
10+
public string PrivateKey { get; set; }
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using OpenStack.Serialization;
4+
5+
namespace OpenStack.Compute.v2_1.Serialization
6+
{
7+
/// <summary>
8+
/// Represents a collection of keypair resources of the <see cref="ComputeService"/>.
9+
/// </summary>
10+
/// <exclude />
11+
public class KeyPairCollection<T> : ResourceCollection<T>
12+
where T : IServiceResource
13+
{
14+
/// <summary>
15+
/// The requested keypairs.
16+
/// </summary>
17+
[JsonProperty("keypairs")]
18+
protected IList<T> KeyPairs => Items;
19+
}
20+
21+
/// <inheritdoc cref="KeyPairCollection{T}" />
22+
/// <exclude />
23+
public class KeyPairCollection : KeyPairCollection<KeyPair>
24+
{ }
25+
}

0 commit comments

Comments
 (0)