Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions src/corelib/Compute/v2_1/ComputeApiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -978,25 +978,100 @@ public virtual async Task<TPage> ListImageDetailsAsync<TPage>(Url url, Cancellat

#region Keypairs

/// <summary />
public virtual async Task<T> GetKeyPairAsync<T>(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
where T : IServiceResource
{
if (keypairName == null)
throw new ArgumentNullException("keypairName");

PreparedRequest request = await BuildGetKeyPairRequestAsync(keypairName, cancellationToken);

var result = await request.SendAsync().ReceiveJson<T>();
result.PropogateOwner(this);
return result;
}

/// <summary />
public virtual async Task<PreparedRequest> BuildGetKeyPairRequestAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

return endpoint
.AppendPathSegment($"os-keypairs/{keypairName}")
.Authenticate(AuthenticationProvider)
.SetMicroversion(this)
.PrepareGet(cancellationToken);
}

/// <summary />
public virtual async Task<T> CreateKeyPairAsync<T>(object keypair, CancellationToken cancellationToken = default(CancellationToken))
where T : IServiceResource
{
if(keypair == null)
throw new ArgumentNullException("keypair");

PreparedRequest request = await BuildCreateKeyPairRequestAsync(keypair, cancellationToken);
return await request.SendAsync().ReceiveJson<T>();
var result = await request.SendAsync().ReceiveJson<T>();
result.PropogateOwner(this);
return result;
}

/// <summary />
public virtual async Task<PreparedRequest> BuildCreateKeyPairRequestAsync(object keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

return endpoint
.AppendPathSegment("os-keypairs")
.Authenticate(AuthenticationProvider)
.SetMicroversion(this)
.PreparePostJson(keypairName, cancellationToken);
}

/// <summary />
public virtual async Task<T> ListKeyPairsAsync<T>(CancellationToken cancellationToken = default(CancellationToken))
where T : IEnumerable<IServiceResource>
{
PreparedRequest request = await BuildListKeyPairsRequestAsync(cancellationToken);
var result = await request.SendAsync().ReceiveJson<T>();
result.PropogateOwner(this);
return result;
}

/// <summary />
public virtual async Task<PreparedRequest> BuildCreateKeyPairRequestAsync(object keypair, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<PreparedRequest> BuildListKeyPairsRequestAsync(CancellationToken cancellationToken = default(CancellationToken))
{
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

return endpoint
.AppendPathSegment("os-keypairs")
.Authenticate(AuthenticationProvider)
.SetMicroversion(this)
.PreparePostJson(keypair, cancellationToken);
.PrepareGet(cancellationToken);
}

/// <summary />
public virtual Task DeleteKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
if (keypairName == null)
throw new ArgumentNullException("keypairName");

return BuildDeleteKeyPairRequestAsync(keypairName, cancellationToken).SendAsync();
}

/// <summary />
public virtual async Task<PreparedRequest> BuildDeleteKeyPairRequestAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
Url endpoint = await UrlBuilder.GetEndpoint(cancellationToken).ConfigureAwait(false);

return (PreparedRequest)endpoint
.AppendPathSegment($"os-keypairs/{keypairName}")
.Authenticate(AuthenticationProvider)
.SetMicroversion(this)
.PrepareDelete(cancellationToken)
.AllowHttpStatus(HttpStatusCode.NotFound);
}
#endregion

#region Security Groups
Expand Down
28 changes: 25 additions & 3 deletions src/corelib/Compute/v2_1/ComputeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,35 @@ public ComputeService(IAuthenticationProvider authenticationProvider, string reg

#region Keypairs

/// <inheritdoc cref="ComputeApiBuilder.GetKeyPairAsync{T}" />
public virtual Task<KeyPairDetails> GetKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
return _computeApi.GetKeyPairAsync<KeyPairDetails>(keypairName, cancellationToken);
}

/// <inheritdoc cref="ComputeApiBuilder.CreateKeyPairAsync{T}" />
public virtual Task<KeyPair> CreateKeyPairAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<KeyPairResponse> CreateKeyPairAsync(KeyPairRequest request, CancellationToken cancellationToken = default(CancellationToken))
{
return _computeApi.CreateKeyPairAsync<KeyPairResponse>(request, cancellationToken);
}

/// <summary />
public virtual Task<KeyPair> ImportKeyPairAsync(KeyPairDefinition keypair, CancellationToken cancellationToken = default(CancellationToken))
{
var keyPair = new KeyPairDefinition(name);
return _computeApi.CreateKeyPairAsync<KeyPair>(keyPair, cancellationToken);
return _computeApi.CreateKeyPairAsync<KeyPair>(keypair, cancellationToken);
}

/// <inheritdoc cref="ComputeApiBuilder.ListKeyPairsAsync{T}" />
public virtual async Task<IEnumerable<KeyPair>> ListKeyPairsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return await _computeApi.ListKeyPairsAsync<KeyPairCollection>(cancellationToken);
}

/// <summary />
public virtual Task DeleteKeyPairAsync(string keypairName, CancellationToken cancellationToken = default(CancellationToken))
{
return _computeApi.DeleteKeyPairAsync(keypairName, cancellationToken);
}
#endregion

#region Security Groups
Expand Down
29 changes: 27 additions & 2 deletions src/corelib/Compute/v2_1/ComputeServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,36 @@ public static void DetachVolume(this ComputeService service, Identifier serverId
#endregion

#region KeyPairs
/// <inheritdoc cref="ComputeService.GetKeyPairAsync" />
public static KeyPairDetails GetKeyPair(this ComputeService service, string keypairName)
{
return service.GetKeyPairAsync(keypairName).ForceSynchronous();
}

/// <inheritdoc cref="ComputeService.CreateKeyPairAsync" />
public static KeyPair CreateKeyPair(this ComputeService service, string name)
public static KeyPairResponse CreateKeyPair(this ComputeService service, KeyPairRequest request)
{
return service.CreateKeyPairAsync(name).ForceSynchronous();
return service.CreateKeyPairAsync(request).ForceSynchronous();
}

/// <inheritdoc cref="ComputeService.ImportKeyPairAsync" />
public static KeyPair ImportKeyPair(this ComputeService service, KeyPairDefinition keyPair)
{
return service.ImportKeyPairAsync(keyPair).ForceSynchronous();
}

/// <inheritdoc cref="ComputeService.ListKeyPairsAsync" />
public static IEnumerable<KeyPair> ListKeyPairs(this ComputeService service)
{
return service.ListKeyPairsAsync().ForceSynchronous();
}

/// <inheritdoc cref="ComputeService.DeleteKeyPairAsync" />
public static void DeleteKeyPair(this ComputeService service, string keypairName)
{
service.DeleteKeyPairAsync(keypairName).ForceSynchronous();
}

#endregion

#region Security Groups
Expand Down
16 changes: 14 additions & 2 deletions src/corelib/Compute/v2_1/KeyPair.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenStack.Compute.v2_1.Serialization;
using OpenStack.Serialization;

namespace OpenStack.Compute.v2_1
{
/// <summary />
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "keypair")]
public class KeyPair : IHaveExtraData
[JsonConverter(typeof(KeyPairConverter))]
public class KeyPair : IHaveExtraData, IServiceResource
{
/// <summary />
[JsonProperty("name")]
Expand All @@ -24,5 +27,14 @@ public class KeyPair : IHaveExtraData
/// <summary />
[JsonExtensionData]
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();

object IServiceResource.Owner { get; set; }

/// <inheritdoc cref="ComputeApiBuilder.DeleteKeyPairAsync" />
public Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var compute = this.GetOwnerOrThrow<ComputeApiBuilder>();
return compute.DeleteKeyPairAsync(Name, cancellationToken);
}
}
}
7 changes: 4 additions & 3 deletions src/corelib/Compute/v2_1/KeyPairDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Newtonsoft.Json;
using OpenStack.Serialization;
using OpenStack.Compute.v2_1.Serialization;

namespace OpenStack.Compute.v2_1
{
/// <summary />
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "keypair")]
[JsonConverter(typeof(KeyPairConverter))]
public class KeyPairDefinition
{
/// <summary />
public KeyPairDefinition(string name)
public KeyPairDefinition(string name, string publicKey)
{
Name = name;
PublicKey = publicKey;
}

/// <summary />
Expand Down
17 changes: 17 additions & 0 deletions src/corelib/Compute/v2_1/KeyPairDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Newtonsoft.Json;

namespace OpenStack.Compute.v2_1
{
/// <summary />
public class KeyPairDetails : KeyPair
{
/// <summary />
[JsonProperty("id")]
public Identifier Id { get; set; }

/// <summary />
[JsonProperty("created_at")]
public DateTimeOffset Created { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/corelib/Compute/v2_1/KeyPairExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using OpenStack.Compute.v2_1;
using OpenStack.Synchronous.Extensions;

// ReSharper disable once CheckNamespace
namespace OpenStack.Synchronous
{
/// <summary />
public static class KeypairExtensions_v2_1
{
/// <inheritdoc cref="KeyPair.DeleteAsync" />
public static void Delete(this KeyPair keypair)
{
keypair.DeleteAsync().ForceSynchronous();
}
}
}
20 changes: 20 additions & 0 deletions src/corelib/Compute/v2_1/KeyPairRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Newtonsoft.Json;
using OpenStack.Compute.v2_1.Serialization;

namespace OpenStack.Compute.v2_1
{
/// <summary />
[JsonConverter(typeof(KeyPairConverter))]
public class KeyPairRequest
{
/// <summary />
public KeyPairRequest(string name)
{
Name = name;
}

/// <summary />
[JsonProperty("name")]
public string Name { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/corelib/Compute/v2_1/KeyPairResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace OpenStack.Compute.v2_1
{
/// <summary />
public class KeyPairResponse : KeyPair
{
/// <summary />
[JsonProperty("private_key")]
public string PrivateKey { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/corelib/Compute/v2_1/Serialization/KeyPairCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using OpenStack.Serialization;

namespace OpenStack.Compute.v2_1.Serialization
{
/// <summary>
/// Represents a collection of keypair resources of the <see cref="ComputeService"/>.
/// </summary>
/// <exclude />
public class KeyPairCollection<T> : ResourceCollection<T>
where T : IServiceResource
{
/// <summary>
/// The requested keypairs.
/// </summary>
[JsonProperty("keypairs")]
protected IList<T> KeyPairs => Items;
}

/// <inheritdoc cref="KeyPairCollection{T}" />
/// <exclude />
public class KeyPairCollection : KeyPairCollection<KeyPair>
{ }
}
49 changes: 49 additions & 0 deletions src/corelib/Compute/v2_1/Serialization/KeyPairConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using Newtonsoft.Json;
using OpenStack.Serialization;

namespace OpenStack.Compute.v2_1.Serialization
{
/// <summary>
/// Handles serialization for Compute KeyPairs, which don't follow the usual convention of not wrapping the object when contained in a list.
/// </summary>
public class KeyPairConverter : DefaultJsonConverter
{
private readonly string _name = "keypair";

/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// Wrap
writer.WriteStartObject();
writer.WritePropertyName(_name);

// Default serialization
base.WriteJson(writer, value, serializer);

writer.WriteEndObject();
}

/// <inheritdoc/>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Skip to the desired property
while (reader.Read())
{
if (reader.TokenType != JsonToken.PropertyName || reader.Value.ToString() != _name)
continue;

// Advance to the contained value
reader.Read();
break;
}

// Default Deserialization
object result = base.ReadJson(reader, objectType, existingValue, serializer);

while (reader.Read() && reader.TokenType != JsonToken.EndObject) { } // Advance to end of object

return result;
}
}
}
Loading