Skip to content

Commit 3b0f70c

Browse files
committed
Add floating ip resource
1 parent 82d943e commit 3b0f70c

15 files changed

Lines changed: 644 additions & 2 deletions
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Extensions;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
using OpenStack.Serialization;
9+
10+
namespace OpenStack.Networking.v2.Layer3
11+
{
12+
/// <summary>
13+
/// An external IP address that you map to a port in an internal network
14+
/// </summary>
15+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "floatingip")]
16+
public class FloatingIP : IHaveExtraData, IServiceResource
17+
{
18+
/// <summary>
19+
/// The floating IP identifier.
20+
/// </summary>
21+
[JsonProperty("id")]
22+
public Identifier Id { get; set; }
23+
24+
/// <summary>
25+
/// The associated router.
26+
/// </summary>
27+
[JsonProperty("router_id")]
28+
public Identifier RouterId { get; set; }
29+
30+
/// <summary>
31+
/// The network associated with the floating IP.
32+
/// </summary>
33+
[JsonProperty("floating_network_id")]
34+
public Identifier NetworkId { get; set; }
35+
36+
/// <summary>
37+
/// The fixed IP address that is associated with the floating IP address.
38+
/// </summary>
39+
[JsonProperty("fixed_ip_address")]
40+
public string FixedIPAddress { get; set; }
41+
42+
/// <summary>
43+
/// The floating IP address.
44+
/// </summary>
45+
[JsonProperty("floating_ip_address")]
46+
public string FloatingIPAddress { get; set; }
47+
48+
/// <summary>
49+
/// The associated port.
50+
/// </summary>
51+
[JsonProperty("port_id")]
52+
public Identifier PortId { get; set; }
53+
54+
/// <summary>
55+
/// The status of the floating IP address.
56+
/// </summary>
57+
[JsonProperty("status")]
58+
public FloatingIPStatus Status { get; set; }
59+
60+
[JsonExtensionData]
61+
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
62+
63+
object IServiceResource.Owner { get; set; }
64+
65+
/// <inheritdoc cref="NetworkingApiBuilder.DeleteFloatingIPAsync"/>
66+
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
67+
public Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
68+
{
69+
var networking = this.GetOwnerOrThrow<NetworkingApiBuilder>();
70+
return networking.DeleteFloatingIPAsync(Id, cancellationToken);
71+
}
72+
73+
/// <summary>
74+
/// Associates the floating IP withe the specified port.
75+
/// </summary>
76+
/// <param name="portId">The port identifier.</param>
77+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
78+
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
79+
public async Task AssociateAsync(Identifier portId, CancellationToken cancellationToken = default(CancellationToken))
80+
{
81+
var networking = this.GetOwnerOrThrow<NetworkingApiBuilder>();
82+
var request = new FloatingIPUpdateDefinition {PortId = portId};
83+
var result = await networking.UpdateFloatingIPAsync<FloatingIP>(Id, request, cancellationToken).ConfigureAwait(false);
84+
result.CopyProperties(this);
85+
}
86+
87+
/// <summary>
88+
/// Frees the floating IP from any associations.
89+
/// </summary>
90+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
91+
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
92+
public async Task DisassociateAsync(CancellationToken cancellationToken = default(CancellationToken))
93+
{
94+
var networking = this.GetOwnerOrThrow<NetworkingApiBuilder>();
95+
var request = new FloatingIPUpdateDefinition {PortId = null};
96+
var result = await networking.UpdateFloatingIPAsync<FloatingIP>(Id, request, cancellationToken).ConfigureAwait(false);
97+
result.CopyProperties(this);
98+
}
99+
}
100+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Newtonsoft.Json;
2+
using OpenStack.Serialization;
3+
4+
namespace OpenStack.Networking.v2.Layer3
5+
{
6+
/// <summary>
7+
/// Defines a new floating IP instance.
8+
/// </summary>
9+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "floatingip")]
10+
public class FloatingIPCreateDefinition
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="FloatingIPCreateDefinition"/> class.
14+
/// </summary>
15+
/// <param name="networkId">The network identifier.</param>
16+
public FloatingIPCreateDefinition(Identifier networkId)
17+
{
18+
NetworkId = networkId;
19+
}
20+
21+
/// <inheritdoc cref="FloatingIP.NetworkId"/>
22+
[JsonProperty("floating_network_id")]
23+
public Identifier NetworkId { get; set; }
24+
25+
/// <inheritdoc cref="FloatingIP.FixedIPAddress"/>
26+
[JsonProperty("fixed_ip_address")]
27+
public string FixedIPAddress { get; set; }
28+
29+
/// <inheritdoc cref="FloatingIP.FloatingIPAddress"/>
30+
[JsonProperty("floating_ip_address")]
31+
public string FloatingIPAddress { get; set; }
32+
33+
/// <inheritdoc cref="FloatingIP.PortId"/>
34+
[JsonProperty("port_id")]
35+
public Identifier PortId { get; set; }
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using OpenStack.Synchronous.Extensions;
3+
4+
namespace OpenStack.Networking.v2.Layer3.Synchronous
5+
{
6+
/// <summary>
7+
/// Provides synchronous extention methods for a <see cref="FloatingIP"/> instance.
8+
/// </summary>
9+
public static class FloatingIPExtensions
10+
{
11+
/// <inheritdoc cref="FloatingIP.AssociateAsync" />
12+
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
13+
public static void Associate(this FloatingIP floatingIP, Identifier portId)
14+
{
15+
floatingIP.AssociateAsync(portId).ForceSynchronous();
16+
}
17+
18+
/// <inheritdoc cref="FloatingIP.DisassociateAsync" />
19+
/// <exception cref="InvalidOperationException">Thrown when a resource as not constructed by the SDK.</exception>
20+
public static void Disassociate(this FloatingIP floatingIP)
21+
{
22+
floatingIP.DisassociateAsync().ForceSynchronous();
23+
}
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
3+
namespace OpenStack.Networking.v2.Layer3
4+
{
5+
/// <summary>
6+
/// Optional filter and paging options when listing ports.
7+
/// </summary>
8+
public class FloatingIPListOptions : FilterOptions
9+
{
10+
/// <summary>
11+
/// Filter by status.
12+
/// </summary>
13+
public FloatingIPStatus Status { get; set; }
14+
15+
/// <summary />
16+
protected override IDictionary<string, object> BuildQueryString()
17+
{
18+
var queryString = new Dictionary<string, object>
19+
{
20+
["status"] = Status
21+
};
22+
23+
return queryString;
24+
}
25+
}
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OpenStack.Networking.v2.Layer3
2+
{
3+
/// <inheritdoc />
4+
public class FloatingIPStatus : FloatingIPStatus<FloatingIPStatus>
5+
{ }
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
using OpenStack.Serialization;
3+
4+
namespace OpenStack.Networking.v2.Layer3
5+
{
6+
/// <summary>
7+
/// Defines a set of fields to update on a floating IP.
8+
/// </summary>
9+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "floatingip")]
10+
public class FloatingIPUpdateDefinition
11+
{
12+
/// <summary>
13+
/// The new port to which the floating IP should be associated, or null to disassociate the floating IP.
14+
/// </summary>
15+
[JsonProperty("port_id")]
16+
public Identifier PortId { get; set; }
17+
}
18+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System.Collections.Generic;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using OpenStack.Networking.v2.Serialization;
5+
using OpenStack.Synchronous.Extensions;
6+
7+
namespace OpenStack.Networking.v2.Layer3
8+
{
9+
/// <summary>
10+
/// Exposes functionality from the Level3 networking extension
11+
/// </summary>
12+
/// <seealso href="http://developer.openstack.org/api-ref-networking-v2-ext.html#layer3-ext"/>
13+
public static class NetworkingService_Layer3_Extensions
14+
{
15+
#region Floating IPs
16+
/// <inheritdoc cref="NetworkingApiBuilder.GetFloatingIPAsync{T}" />
17+
public static Task<FloatingIP> GetFloatingIPAsync(this NetworkingService service, Identifier floatingIPId, CancellationToken cancellationToken = default(CancellationToken))
18+
{
19+
return service._networkingApiBuilder.GetFloatingIPAsync<FloatingIP>(floatingIPId, cancellationToken);
20+
}
21+
22+
/// <inheritdoc cref="NetworkingApiBuilder.CreateFloatingIPAsync{T}" />
23+
public static Task<FloatingIP> CreateFloatingIPAsync(this NetworkingService service, FloatingIPCreateDefinition floatingIP, CancellationToken cancellationToken = default(CancellationToken))
24+
{
25+
return service._networkingApiBuilder.CreateFloatingIPAsync<FloatingIP>(floatingIP, cancellationToken);
26+
}
27+
28+
/// <inheritdoc cref="NetworkingApiBuilder.UpdateFloatingIPAsync{T}" />
29+
public static Task<FloatingIP> UpdateFloatingIPAsync(this NetworkingService service, FloatingIPUpdateDefinition floatingIP, CancellationToken cancellationToken = default(CancellationToken))
30+
{
31+
return service._networkingApiBuilder.CreateFloatingIPAsync<FloatingIP>(floatingIP, cancellationToken);
32+
}
33+
34+
/// <inheritdoc cref="NetworkingApiBuilder.ListFloatingIPsAsync{T}" />
35+
public static async Task<IEnumerable<FloatingIP>> ListFloatingIPAsync(this NetworkingService service, FloatingIPListOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
36+
{
37+
return await service._networkingApiBuilder.ListFloatingIPsAsync<FloatingIPCollection>(options, cancellationToken).ConfigureAwait(false);
38+
}
39+
40+
/// <inheritdoc cref="NetworkingApiBuilder.DeleteFloatingIPAsync" />
41+
public static Task DeleteFloatingIPAsync(this NetworkingService service, Identifier floatingIPId, CancellationToken cancellationToken = default(CancellationToken))
42+
{
43+
return service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId, cancellationToken);
44+
}
45+
#endregion
46+
}
47+
}
48+
49+
namespace OpenStack.Networking.v2.Layer3.Synchronous
50+
{
51+
/// <summary>
52+
/// Exposes synchronous extension methods for the Level3 networking extension
53+
/// </summary>
54+
/// <seealso href="http://developer.openstack.org/api-ref-networking-v2-ext.html#layer3-ext"/>
55+
public static class NetworkingService_Layer3_Extensions
56+
{
57+
#region Floating IPs
58+
/// <inheritdoc cref="NetworkingApiBuilder.GetFloatingIPAsync{T}" />
59+
public static FloatingIP GetFloatingIP(this NetworkingService service, Identifier floatingIPId)
60+
{
61+
return service._networkingApiBuilder.GetFloatingIPAsync<FloatingIP>(floatingIPId).ForceSynchronous();
62+
}
63+
64+
/// <inheritdoc cref="NetworkingApiBuilder.CreateFloatingIPAsync{T}" />
65+
public static FloatingIP CreateFloatingIP(this NetworkingService service, FloatingIPCreateDefinition floatingIP)
66+
{
67+
return service._networkingApiBuilder.CreateFloatingIPAsync<FloatingIP>(floatingIP).ForceSynchronous();
68+
}
69+
70+
/// <inheritdoc cref="NetworkingApiBuilder.UpdateFloatingIPAsync{T}" />
71+
public static FloatingIP UpdateFloatingIP(this NetworkingService service, FloatingIPUpdateDefinition floatingIP)
72+
{
73+
return service._networkingApiBuilder.CreateFloatingIPAsync<FloatingIP>(floatingIP).ForceSynchronous();
74+
}
75+
76+
/// <inheritdoc cref="NetworkingApiBuilder.ListFloatingIPsAsync{T}" />
77+
public static IEnumerable<FloatingIP> ListFloatingIPs(this NetworkingService service, FloatingIPListOptions options = null)
78+
{
79+
return service._networkingApiBuilder.ListFloatingIPsAsync<FloatingIPCollection>(options).ForceSynchronous();
80+
}
81+
82+
/// <inheritdoc cref="NetworkingApiBuilder.DeleteFloatingIPAsync" />
83+
public static void DeleteFloatingIP(this NetworkingService service, Identifier floatingIPId)
84+
{
85+
service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId).ForceSynchronous();
86+
}
87+
#endregion
88+
}
89+
}

0 commit comments

Comments
 (0)