Skip to content

Commit 5f5ab84

Browse files
committed
Support listing ports by the device owner
1 parent bdb7f4c commit 5f5ab84

7 files changed

Lines changed: 75 additions & 16 deletions

File tree

src/corelib/Networking/v2/NetworkingApiBuilder.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,32 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
266266
/// <summary>
267267
/// Lists all ports associated with the account.
268268
/// </summary>
269+
/// <param name="queryString">Options for filtering.</param>
269270
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
270271
/// <returns>
271272
/// A collection of port resources associated with the account.
272273
/// </returns>
273-
public virtual async Task<PreparedRequest> ListPortsAsync(CancellationToken cancellationToken = default(CancellationToken))
274+
public virtual async Task<T> ListPortsAsync<T>(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
275+
where T : IEnumerable<IServiceResource>
274276
{
275-
Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
277+
return await BuildListPortsRequest(queryString, cancellationToken)
278+
.SendAsync()
279+
.ReceiveJson<T>()
280+
.PropogateOwnerToChildren(this).ConfigureAwait(false);
281+
}
276282

277-
return endpoint
278-
.AppendPathSegment("ports")
279-
.Authenticate(AuthenticationProvider)
280-
.PrepareGet(cancellationToken);
283+
/// <summary>
284+
/// Builds the <see cref="ListPortsAsync{T}"/> request.
285+
/// </summary>
286+
/// <param name="queryString">Options for filtering.</param>
287+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
288+
public virtual async Task<PreparedRequest> BuildListPortsRequest(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
289+
{
290+
PreparedRequest request = await Endpoint.PrepareGetResourceRequest("ports", cancellationToken).ConfigureAwait(false);
291+
292+
request.Url.SetQueryParams(queryString?.Build());
293+
294+
return request;
281295
}
282296

283297
/// <summary>

src/corelib/Networking/v2/NetworkingService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,10 @@ public NetworkingService(IAuthenticationProvider authenticationProvider, string
141141

142142
#region Ports
143143

144-
/// <inheritdoc cref="NetworkingApiBuilder.ListPortsAsync" />
145-
public async Task<IEnumerable<Port>> ListPortsAsync(CancellationToken cancellationToken = default(CancellationToken))
144+
/// <inheritdoc cref="NetworkingApiBuilder.ListPortsAsync{T}" />
145+
public async Task<IEnumerable<Port>> ListPortsAsync(PortListOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
146146
{
147-
return await _networkingApiBuilder
148-
.ListPortsAsync(cancellationToken)
149-
.SendAsync()
150-
.ReceiveJson<PortCollection>();
147+
return await _networkingApiBuilder.ListPortsAsync<PortCollection>(options, cancellationToken);
151148
}
152149

153150
/// <inheritdoc cref="NetworkingApiBuilder.CreatePortAsync" />

src/corelib/Networking/v2/NetworkingServiceExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ public static void DeleteSubnet(this NetworkingService networkingService, Identi
169169
/// <returns>
170170
/// A collection of port resources associated with the account.
171171
/// </returns>
172-
public static IEnumerable<Port> ListPorts(this NetworkingService networkingService)
172+
public static IEnumerable<Port> ListPorts(this NetworkingService networkingService, PortListOptions options = null)
173173
{
174-
return networkingService.ListPortsAsync().ForceSynchronous();
174+
return networkingService.ListPortsAsync(options).ForceSynchronous();
175175
}
176176

177177
/// <summary>

src/corelib/Networking/v2/Port.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Newtonsoft.Json;
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using OpenStack.Serialization;
25

36
namespace OpenStack.Networking.v2
47
{
@@ -9,7 +12,7 @@ namespace OpenStack.Networking.v2
912
/// Virtual (or logical) switch ports on a given network.
1013
/// </para>
1114
/// <threadsafety static="true" instance="false"/>
12-
public class Port : PortCreateDefinition
15+
public class Port : PortCreateDefinition, IHaveExtraData, IServiceResource
1316
{
1417
/// <summary>
1518
/// The ID of the port.
@@ -34,5 +37,10 @@ public class Port : PortCreateDefinition
3437
/// </summary>
3538
[JsonProperty("port_security_enabled")]
3639
public bool IsPortSecurityEnabled { get; set; }
40+
41+
[JsonExtensionData]
42+
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
43+
44+
object IServiceResource.Owner { get; set; }
3745
}
3846
}
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
4+
{
5+
/// <summary>
6+
/// Optional filter and paging options when listing ports.
7+
/// </summary>
8+
public class PortListOptions : FilterOptions
9+
{
10+
/// <summary>
11+
/// Filter by the associated device identifier
12+
/// </summary>
13+
public Identifier DeviceId { get; set; }
14+
15+
/// <summary />
16+
protected override IDictionary<string, object> BuildQueryString()
17+
{
18+
var queryString = new Dictionary<string, object>
19+
{
20+
["device_id"] = DeviceId
21+
};
22+
23+
return queryString;
24+
}
25+
}
26+
}

src/corelib/OpenStack.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<Compile Include="Networking\NamespaceDoc.cs" />
197197
<Compile Include="Networking\v2\AllowedAddress.cs" />
198198
<Compile Include="Networking\v2\AllocationPool.cs" />
199+
<Compile Include="Networking\v2\PortListOptions.cs" />
199200
<Compile Include="Networking\v2\HostRoute.cs" />
200201
<Compile Include="Networking\v2\IPAddressAssociation.cs" />
201202
<Compile Include="Networking\v2\Layer3\ExternalGateway.cs" />

src/testing/unit/Networking/v2/PortTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public void ListPorts()
3434
}
3535
}
3636

37+
[Fact]
38+
public void FilterPorts()
39+
{
40+
using (var httpTest = new HttpTest())
41+
{
42+
httpTest.RespondWithJson(new PortCollection());
43+
44+
_networkingService.ListPorts(new PortListOptions {DeviceId = "123"});
45+
46+
httpTest.ShouldHaveCalled("*/ports?device_id=123");
47+
}
48+
}
49+
3750
[Fact]
3851
public void CreatePort()
3952
{

0 commit comments

Comments
 (0)