Skip to content

Commit c57db8b

Browse files
committed
Add router resource
1 parent b56bf4a commit c57db8b

21 files changed

Lines changed: 965 additions & 20 deletions

src/corelib/Compute/v2_1/ComputeService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ public ComputeService(IAuthenticationProvider authenticationProvider, string reg
217217
return _computeApi.AssociateFloatingIPAsync(serverId, request, cancellationToken);
218218
}
219219

220+
/// <inheritdoc cref="ComputeApi.DisassociateFloatingIPAsync" />
221+
public virtual Task DisassociateFloatingIPAsync(Identifier serverId, string floatingIPAddress, CancellationToken cancellationToken = default(CancellationToken))
222+
{
223+
return _computeApi.DisassociateFloatingIPAsync(serverId, floatingIPAddress, cancellationToken);
224+
}
225+
220226
/// <inheritdoc cref="ComputeApi.ListServerActionSummariesAsync{T}" />
221227
public virtual async Task<IEnumerable<ServerActionSummary>> ListServerActionSummariesAsync(Identifier serverId, CancellationToken cancellationToken = default(CancellationToken))
222228
{

src/corelib/Compute/v2_1/ComputeServiceExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ public static void CancelResizeServer(this ComputeService service, Identifier se
181181
service.CancelResizeServerAsync(serverId).ForceSynchronous();
182182
}
183183

184+
/// <inheritdoc cref="ComputeService.AssociateFloatingIPAddressAsync" />
185+
public static void AssociateFloatingIPAddressAsync(this ComputeService service, Identifier serverId, AssociateFloatingIPRequest request)
186+
{
187+
service.AssociateFloatingIPAddressAsync(serverId, request).ForceSynchronous();
188+
}
189+
190+
/// <inheritdoc cref="ComputeService.DisassociateFloatingIPAsync" />
191+
public static void DisassociateFloatingIPAsync(this ComputeService service, Identifier serverId, string floatingIPAddress)
192+
{
193+
service.DisassociateFloatingIPAsync(serverId, floatingIPAddress).ForceSynchronous();
194+
}
195+
184196
/// <inheritdoc cref="ComputeService.ListServerActionSummariesAsync" />
185197
public static IEnumerable<ServerActionSummary> ListServerActions(this ComputeService service, Identifier serverId)
186198
{

src/corelib/Compute/v2_1/Serialization/ComputeApi.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,16 +766,36 @@ protected ComputeApi(IServiceType serviceType, IAuthenticationProvider authentic
766766
}
767767

768768
/// <summary>
769-
/// Associates a floating IP address to the server instance.
769+
/// Associates a floating IP address to the server.
770770
/// </summary>
771771
/// <param name="serverId">The server identifier.</param>
772772
/// <param name="request">The request.</param>
773773
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
774+
/// <exception cref="ArgumentNullException"></exception>
774775
public virtual Task AssociateFloatingIPAsync(string serverId, object request, CancellationToken cancellationToken = default(CancellationToken))
775776
{
776777
return BuildServerActionRequest(serverId, request, cancellationToken).SendAsync();
777778
}
778779

780+
/// <summary>
781+
/// Disassociate a floating IP address from a server.
782+
/// </summary>
783+
/// <param name="serverId">The server identifier.</param>
784+
/// <param name="floatingIPAddress">The floating IP address to remove.</param>
785+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
786+
/// <exception cref="ArgumentNullException"></exception>
787+
public virtual Task DisassociateFloatingIPAsync(string serverId, string floatingIPAddress, CancellationToken cancellationToken = default(CancellationToken))
788+
{
789+
var request = new
790+
{
791+
removeFloatingIp = new
792+
{
793+
address = floatingIPAddress
794+
}
795+
};
796+
return BuildServerActionRequest(serverId, request, cancellationToken).SendAsync();
797+
}
798+
779799
/// <summary>
780800
/// Builds a server action request, where the server operation is specified in the request body.
781801
/// </summary>

src/corelib/Compute/v2_1/Server.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Extensions;
4+
using System.Linq;
45
using System.Runtime.Serialization;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -242,10 +243,33 @@ public string AdminPassword
242243

243244
/// <inheritdoc cref="ComputeApi.AssociateFloatingIPAsync" />
244245
/// <exception cref="InvalidOperationException">When this instance was not constructed by the <see cref="ComputeService"/>, as it is missing the appropriate internal state to execute service calls.</exception>
245-
public virtual Task AssociateFloatingIPAsync(AssociateFloatingIPRequest request, CancellationToken cancellationToken = default(CancellationToken))
246+
public virtual async Task AssociateFloatingIPAsync(AssociateFloatingIPRequest request, CancellationToken cancellationToken = default(CancellationToken))
246247
{
247248
var compute = this.GetOwnerOrThrow<ComputeApi>();
248-
return compute.AssociateFloatingIPAsync(Id, request, cancellationToken);
249+
await compute.AssociateFloatingIPAsync(Id, request, cancellationToken);
250+
251+
Addresses = await compute.ListServerAddressesAsync<ServerAddressCollection>(Id, cancellationToken);
252+
}
253+
254+
/// <inheritdoc cref="ComputeApi.DisassociateFloatingIPAsync" />
255+
/// <exception cref="InvalidOperationException">When this instance was not constructed by the <see cref="ComputeService"/>, as it is missing the appropriate internal state to execute service calls.</exception>
256+
public virtual async Task DisassociateFloatingIPAsync(string floatingIPAddress, CancellationToken cancellationToken = default(CancellationToken))
257+
{
258+
var compute = this.GetOwnerOrThrow<ComputeApi>();
259+
await compute.DisassociateFloatingIPAsync(Id, floatingIPAddress, cancellationToken);
260+
261+
// Remove the address from the current instance immediately
262+
foreach (KeyValuePair<string, IList<ServerAddress>> group in Addresses)
263+
{
264+
foreach (ServerAddress address in group.Value)
265+
{
266+
if (address.Type == AddressType.Floating && address.IP == floatingIPAddress)
267+
{
268+
Addresses[group.Key].Remove(address);
269+
return;
270+
}
271+
}
272+
}
249273
}
250274

251275
/// <summary />

src/corelib/Compute/v2_1/ServerExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ public static void AssociateFloatingIP(this Server server, AssociateFloatingIPRe
191191
server.AssociateFloatingIPAsync(request).ForceSynchronous();
192192
}
193193

194+
/// <inheritdoc cref="Server.DisassociateFloatingIPAsync"/>
195+
public static void DisassociateFloatingIP(this Server server, string floatingIPAddress)
196+
{
197+
server.DisassociateFloatingIPAsync(floatingIPAddress).ForceSynchronous();
198+
}
199+
194200
/// <inheritdoc cref="ServerReference.ListActionSummariesAsync"/>
195201
public static IEnumerable<ServerActionSummary> ListActionSummaries(this ServerReference server)
196202
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace OpenStack.Networking.v2.Layer3
5+
{
6+
/// <summary>
7+
/// Defines a connection from a router to an external network.
8+
/// </summary>
9+
public class ExternalGateway
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="ExternalGateway"/> class.
13+
/// </summary>
14+
public ExternalGateway()
15+
{
16+
ExternalFixedIPs = new List<IPAddressAssociation>();
17+
}
18+
19+
/// <summary>
20+
/// The external network identifier.
21+
/// </summary>
22+
[JsonProperty("network_id")]
23+
public Identifier ExternalNetworkId { get; set; }
24+
25+
/// <summary>
26+
/// Specifies if source NAT is enabled
27+
/// </summary>
28+
[JsonProperty("enable_snat")]
29+
public bool IsSourceNATEnabled { get; set; }
30+
31+
/// <summary>
32+
/// External fixed IP addresses assigned to the router.
33+
/// </summary>
34+
[JsonProperty("external_fixed_ips")]
35+
public IList<IPAddressAssociation> ExternalFixedIPs { get; set; }
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace OpenStack.Networking.v2.Layer3
5+
{
6+
/// <summary>
7+
/// Defines the set of fields that can be updated on an external gateway resource.
8+
/// </summary>
9+
public class ExternalGatewayDefinition
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="ExternalGateway"/> class.
13+
/// </summary>
14+
public ExternalGatewayDefinition(Identifier externalNetworkId)
15+
{
16+
ExternalNetworkId = externalNetworkId;
17+
ExternalFixedIPs = new List<IPAddressAssociation>();
18+
}
19+
20+
/// <summary>
21+
/// The external network identifier.
22+
/// </summary>
23+
[JsonProperty("network_id")]
24+
public Identifier ExternalNetworkId { get; set; }
25+
26+
/// <summary>
27+
/// Specifies if source NAT is enabled
28+
/// </summary>
29+
[JsonProperty("enable_snat")]
30+
public bool? IsSourceNATEnabled { get; set; }
31+
32+
/// <summary>
33+
/// External fixed IP addresses assigned to the router.
34+
/// </summary>
35+
[JsonProperty("external_fixed_ips")]
36+
public IList<IPAddressAssociation> ExternalFixedIPs { get; set; }
37+
}
38+
}

src/corelib/Networking/v2/Layer3/FloatingIP.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class FloatingIP : IHaveExtraData, IServiceResource
7979
public async Task AssociateAsync(Identifier portId, CancellationToken cancellationToken = default(CancellationToken))
8080
{
8181
var networking = this.GetOwnerOrThrow<NetworkingApiBuilder>();
82-
var request = new FloatingIPUpdateDefinition {PortId = portId};
82+
var request = new FloatingIPUpdateDefinition { PortId = portId };
8383
var result = await networking.UpdateFloatingIPAsync<FloatingIP>(Id, request, cancellationToken).ConfigureAwait(false);
8484
result.CopyProperties(this);
8585
}
@@ -92,7 +92,7 @@ public class FloatingIP : IHaveExtraData, IServiceResource
9292
public async Task DisassociateAsync(CancellationToken cancellationToken = default(CancellationToken))
9393
{
9494
var networking = this.GetOwnerOrThrow<NetworkingApiBuilder>();
95-
var request = new FloatingIPUpdateDefinition {PortId = null};
95+
var request = new FloatingIPUpdateDefinition { PortId = null };
9696
var result = await networking.UpdateFloatingIPAsync<FloatingIP>(Id, request, cancellationToken).ConfigureAwait(false);
9797
result.CopyProperties(this);
9898
}

src/corelib/Networking/v2/Layer3/FloatingIPExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
21
using OpenStack.Synchronous.Extensions;
32

3+
// ReSharper disable once CheckNamespace
44
namespace OpenStack.Networking.v2.Layer3.Synchronous
55
{
66
/// <summary>
@@ -9,14 +9,12 @@ namespace OpenStack.Networking.v2.Layer3.Synchronous
99
public static class FloatingIPExtensions
1010
{
1111
/// <inheritdoc cref="FloatingIP.AssociateAsync" />
12-
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
1312
public static void Associate(this FloatingIP floatingIP, Identifier portId)
1413
{
1514
floatingIP.AssociateAsync(portId).ForceSynchronous();
1615
}
1716

1817
/// <inheritdoc cref="FloatingIP.DisassociateAsync" />
19-
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
2018
public static void Disassociate(this FloatingIP floatingIP)
2119
{
2220
floatingIP.DisassociateAsync().ForceSynchronous();

0 commit comments

Comments
 (0)