Skip to content

Commit c1b5a2d

Browse files
committed
Standardize Networking.ListSecurityGroups
* Enable filtering by name * Expose a builder and executor on the API * Implement IHaveExtraData and IServiceResource
1 parent d9ba138 commit c1b5a2d

File tree

10 files changed

+149
-92
lines changed

10 files changed

+149
-92
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,10 @@ public static class NetworkingService_Layer3_Extensions
106106
#endregion
107107

108108
#region Security Groups
109-
/// <inheritdoc cref="NetworkingApiBuilder.ListSecurityGroupAsync(CancellationToken)" />
110-
public static async Task<IEnumerable<SecurityGroup>> ListSecurityGroupAsync(this NetworkingService service, CancellationToken cancellationToken = default(CancellationToken))
109+
/// <inheritdoc cref="NetworkingApiBuilder.ListSecurityGroupsAsync{T}" />
110+
public static async Task<IEnumerable<SecurityGroup>> ListSecurityGroupsAsync(this NetworkingService service, SecurityGroupListOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
111111
{
112-
return await service._networkingApiBuilder
113-
.ListSecurityGroupAsync(cancellationToken)
114-
.SendAsync()
115-
.ReceiveJson<NetSecurityGroupCollection>()
116-
.ConfigureAwait(false);
112+
return await service._networkingApiBuilder.ListSecurityGroupsAsync<SecurityGroupCollection>(options, cancellationToken).ConfigureAwait(false);
117113
}
118114
#endregion
119115
}
@@ -215,11 +211,11 @@ public static void DeleteFloatingIP(this NetworkingService service, Identifier f
215211
}
216212
#endregion
217213

218-
#region Security Group
219-
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupAsync" />
220-
public static IEnumerable<SecurityGroup> ListSecurityGroup (this NetworkingService service)
214+
#region Security Groups
215+
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupsAsync" />
216+
public static IEnumerable<SecurityGroup> ListSecurityGroups(this NetworkingService service, SecurityGroupListOptions options = null)
221217
{
222-
return service._networkingApiBuilder.ListSecurityGroupAsync().SendAsync().ReceiveJson<NetSecurityGroupCollection>().ForceSynchronous();
218+
return service.ListSecurityGroupsAsync(options).ForceSynchronous();
223219
}
224220
#endregion
225221

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
62
using Newtonsoft.Json;
7-
using OpenStack.Networking.v2;
3+
using Newtonsoft.Json.Linq;
84
using OpenStack.Serialization;
95

106
namespace OpenStack.Networking.v2.Layer3
117
{
128
/// <summary>
13-
///Regpresents the security group of the <see cref="NetworkingService"/>
9+
/// Represents the security group of the <see cref="NetworkingService"/>
1410
/// </summary>
15-
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "securitygroup")]
16-
public class SecurityGroup
11+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_group")]
12+
public class SecurityGroup : IHaveExtraData, IServiceResource
1713
{
1814
/// <summary>
19-
///the security group description
15+
/// The security group description
2016
/// </summary>
2117
[JsonProperty("description")]
2218
public string Description;
2319

2420
/// <summary>
25-
///the UUID of security group
21+
/// The UUID of security group
2622
/// </summary>
2723
[JsonProperty("id")]
2824
public Identifier Id;
2925

3026
/// <summary>
31-
/// the security group name
27+
/// The security group name
3228
/// </summary>
3329
[JsonProperty("name")]
3430
public string Name;
3531

3632
/// <summary>
37-
///A list of <see cref="SecurityGroup"/> objects.
33+
/// A list of <see cref="SecurityGroup"/> objects.
3834
/// </summary>
3935
[JsonProperty("security_group_rules")]
4036
public IList<SecurityGroupRule> SecurityGroupRules;
4137

42-
/// <summary>
43-
///The UUId of tenant who owns the scurity group
44-
/// </summary>
45-
[JsonProperty("tenant_id")]
46-
public string TenantId;
38+
[JsonExtensionData]
39+
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
40+
41+
object IServiceResource.Owner { get; set; }
4742
}
4843
}
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 security groups.
7+
/// </summary>
8+
public class SecurityGroupListOptions : FilterOptions
9+
{
10+
/// <summary>
11+
/// Filter by the group name.
12+
/// </summary>
13+
public string Name { get; set; }
14+
15+
/// <summary />
16+
protected override IDictionary<string, object> BuildQueryString()
17+
{
18+
var queryString = new Dictionary<string, object>
19+
{
20+
["name"] = Name
21+
};
22+
23+
return queryString;
24+
}
25+
}
26+
}
Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
2+
using OpenStack.Serialization;
3+
74
namespace OpenStack.Networking.v2.Layer3
85
{
96
/// <summary>
107
///
118
/// </summary>
9+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_group_rule")]
1210
public class SecurityGroupRule
1311
{
1412
/// <summary>
15-
///ngress or egress: the direction in which the security group rule is applied.
16-
///For a compute instance, an ingress security group rule is applied to incoming (ingress) traffic for that instance.
17-
///An egress rule is applied to traffic leaving the instance.
13+
/// ingress or egress: the direction in which the security group rule is applied.
14+
/// For a compute instance, an ingress security group rule is applied to incoming (ingress) traffic for that instance.
15+
/// An egress rule is applied to traffic leaving the instance.
1816
/// </summary>
1917
[JsonProperty("direction")]
20-
public string Direction;
18+
public TrafficDirection Direction;
2119

2220
/// <summary>
23-
///Must be IPv4 or IPv6, and addresses represented in CIDR must match the ingress or egress rules.
21+
/// The internet protocol version. Addresses represented in CIDR must match the ingress or egress rules.
2422
/// </summary>
2523
[JsonProperty("ethertype")]
26-
public string Ethertype;
24+
public IPVersion Ethertype;
2725

2826
/// <summary>
2927
/// The UUID of the security group rule.
@@ -32,52 +30,46 @@ public class SecurityGroupRule
3230
public Identifier Id;
3331

3432
/// <summary>
35-
///The maximum port number in the range that is matched by the security group rule.
36-
///The port_range_min attribute constrains the port_range_max attribute.
37-
///If the protocol is ICMP, this value must be an ICMP type.
33+
/// The minimum port number in the range that is matched by the security group rule.
34+
/// If the protocol is TCP or UDP, this value must be less than or equal to the port_range_max attribute value.
35+
/// If the protocol is ICMP, this value must be an ICMP type.
3836
/// </summary>
39-
[JsonProperty("port_range_max")]
40-
public int PortRangeMax;
37+
[JsonProperty("port_range_min")]
38+
public int MinPort { get; set; }
4139

42-
///<summary>
43-
///The minimum port number in the range that is matched by the security group rule.
44-
///If the protocol is TCP or UDP, this value must be less than or equal to the port_range_max attribute value.
45-
///If the protocol is ICMP, this value must be an ICMP type.
40+
/// <summary>
41+
/// The maximum port number in the range that is matched by the security group rule.
42+
/// The port_range_min attribute constrains the port_range_max attribute.
43+
/// If the protocol is ICMP, this value must be an ICMP type.
4644
/// </summary>
47-
[JsonProperty("port_range_min")]
48-
public int PortRangeMin;
45+
[JsonProperty("port_range_max")]
46+
public int MaxPort { get; set; }
4947

5048
/// <summary>
51-
///The protocol that is matched by the security group rule. Value is null, icmp, icmpv6, tcp, or udp.
49+
/// The protocol that is matched by the security group rule.
5250
/// </summary>
5351
[JsonProperty("protocol")]
54-
public string Protocol;
52+
public IPProtocol Protocol;
5553

56-
///<summary>
57-
///The remote group UUID to associate with this security group rule.
58-
///You can specify either the remote_group_id or remote_ip_prefix attribute in the request body.
54+
/// <summary>
55+
/// The remote group UUID to associate with this security group rule.
56+
/// You can specify either the remote_group_id or remote_ip_prefix attribute in the request body.
5957
/// </summary>
6058
[JsonProperty("remote_group_id")]
61-
public string RemoteGroupId;
59+
public Identifier RemoteGroupId;
6260

6361
/// <summary>
64-
///The remote IP prefix to associate with this security group rule.
65-
///You can specify either the remote_group_id or remote_ip_prefix attribute in the request body.
66-
///This attribute value matches the IP prefix as the source IP address of the IP packet.
62+
/// The remote IP prefix or CIDR to associate with this security group rule.
63+
/// You can specify either the remote_group_id or remote_ip_prefix attribute in the request body.
64+
/// This attribute value matches the IP prefix as the source IP address of the IP packet.
6765
/// </summary>
6866
[JsonProperty("remote_ip_prefix")]
69-
public string RemoteIpPrefix;
67+
public string RemoteCIDR;
7068

7169
/// <summary>
72-
///The UUId of security group
70+
/// The UUId of security group
7371
/// </summary>
7472
[JsonProperty("security_group_id")]
75-
public string SecurityGroupId;
76-
77-
/// <summary>
78-
/// The UUID of the tenant who owns the security group rule. Only administrative users can specify a tenant UUID other than their own.
79-
/// </summary>
80-
[JsonProperty("tenant_id")]
81-
public string TenantId;
73+
public Identifier SecurityGroupId;
8274
}
8375
}

src/corelib/Networking/v2/NetworkingApiBuilder.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
384384
}
385385
#endregion
386386

387-
#region Level 3 Extension
387+
#region Layer 3 Extension
388388

389389
#region Routers
390390
/// <summary>
@@ -669,20 +669,39 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
669669

670670
#region SecurityGroup
671671
/// <summary>
672-
/// Lists all networks security groups associated with the account.
672+
/// Lists all network security groups associated with the account.
673673
/// </summary>
674+
/// <param name="queryString">Options for filtering.</param>
674675
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
675676
/// <returns>
676-
/// A collection of network resources associated with the account.
677+
/// A collection of network security group resources associated with the account.
677678
/// </returns>
678-
public async Task<PreparedRequest> ListSecurityGroupAsync(CancellationToken cancellationToken = default(CancellationToken))
679+
public async Task<T> ListSecurityGroupsAsync<T>(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
680+
where T : IEnumerable<IServiceResource>
681+
{
682+
return await BuildListSecurityGroupsRequest(queryString, cancellationToken)
683+
.SendAsync()
684+
.ReceiveJson<T>()
685+
.PropogateOwnerToChildren(this).ConfigureAwait(false);
686+
}
687+
688+
/// <summary>
689+
/// Builds a <see cref="ListSecurityGroupsAsync{T}"/> request.
690+
/// </summary>
691+
/// <param name="queryString">Options for filtering.</param>
692+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
693+
public async Task<PreparedRequest> BuildListSecurityGroupsRequest(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
679694
{
680695
Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
681696

682-
return endpoint
697+
var request = endpoint
683698
.AppendPathSegments("security-groups")
684699
.Authenticate(AuthenticationProvider)
685700
.PrepareGet(cancellationToken);
701+
702+
request.Url.SetQueryParams(queryString?.Build());
703+
704+
return request;
686705
}
687706

688707
#endregion

src/corelib/Networking/v2/Serialization/NetSecurityGroupCollection.cs renamed to src/corelib/Networking/v2/Serialization/SecurityGroupCollection.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Collections.Generic;
62
using OpenStack.Serialization;
73
using OpenStack.Networking.v2.Layer3;
84

@@ -13,25 +9,24 @@ namespace OpenStack.Networking.v2.Serialization
139
/// <para>Intended for custom implementations and stubbing responses in unit tests.</para>
1410
/// </summary>
1511
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_groups")]
16-
public class NetSecurityGroupCollection : List<SecurityGroup>
12+
public class SecurityGroupCollection : List<SecurityGroup>
1713
{
18-
1914
/// <summary>
20-
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
15+
/// Initializes a new instance of the <see cref="SecurityGroupCollection"/> class.
2116
/// </summary>
22-
public NetSecurityGroupCollection()
17+
public SecurityGroupCollection()
2318
{
2419

2520
}
2621

2722
/// <summary>
28-
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
23+
/// Initializes a new instance of the<see cref="SecurityGroupCollection"/> class.
2924
/// </summary>
30-
/// <param name="securityGroups"></param>
31-
public NetSecurityGroupCollection(IEnumerable<SecurityGroup> securityGroups) : base(securityGroups)
25+
/// <param name="items">items</param>
26+
public SecurityGroupCollection(IEnumerable<SecurityGroup> items)
27+
: base(items)
3228
{
3329

3430
}
35-
3631
}
3732
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using OpenStack.Serialization;
2+
3+
namespace OpenStack.Networking.v2.Serialization
4+
{
5+
/// <summary>
6+
/// Direction of network traffic.
7+
/// </summary>
8+
/// <exclude />
9+
public class TrafficDirection<T> : StringEnumeration
10+
where T : TrafficDirection<T>, new()
11+
{
12+
/// <summary>
13+
/// Incoming network traffic (ingress).
14+
/// </summary>
15+
public static readonly T Incoming = new T { DisplayName = "ingress" };
16+
17+
/// <summary>
18+
/// Outgoing network traffic (egress).
19+
/// </summary>
20+
public static readonly T Outgoing = new T { DisplayName = "egress" };
21+
}
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using OpenStack.Networking.v2.Serialization;
2+
3+
namespace OpenStack.Networking.v2
4+
{
5+
/// <inheritdoc />
6+
public class TrafficDirection : TrafficDirection<TrafficDirection>
7+
{ }
8+
}

src/corelib/OpenStack.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
<Compile Include="Networking\IPVersion.cs" />
196196
<Compile Include="Networking\NamespaceDoc.cs" />
197197
<Compile Include="Networking\v2\IPProtocol.cs" />
198+
<Compile Include="Networking\v2\Layer3\SecurityGroupListOptions.cs" />
198199
<Compile Include="Networking\v2\Layer3\SecurityGroup.cs" />
199200
<Compile Include="Networking\v2\Layer3\SecurityGroupRule.cs" />
200201
<Compile Include="Networking\v2\AllowedAddress.cs" />
@@ -238,12 +239,14 @@
238239
<Compile Include="Networking\v2\Serialization\NetworkDefinitionCollection.cs" />
239240
<Compile Include="Networking\v2\Serialization\PortCollection.cs" />
240241
<Compile Include="Networking\v2\Serialization\PortDefinitionCollection.cs" />
241-
<Compile Include="Networking\v2\Serialization\NetSecurityGroupCollection.cs" />
242+
<Compile Include="Networking\v2\Serialization\SecurityGroupCollection.cs" />
242243
<Compile Include="Networking\v2\Serialization\SubnetCollection.cs" />
243244
<Compile Include="Networking\v2\Serialization\SubnetDefinitionCollection.cs" />
245+
<Compile Include="Networking\v2\Serialization\TrafficDirection.cs" />
244246
<Compile Include="Networking\v2\Subnet.cs" />
245247
<Compile Include="Networking\v2\SubnetCreateDefinition.cs" />
246248
<Compile Include="Networking\v2\SubnetUpdateDefinition.cs" />
249+
<Compile Include="Networking\v2\TrafficDirection.cs" />
247250
<Compile Include="OpenStackNet.cs" />
248251
<Compile Include="ContentDeliveryNetworks\v1\Flavor.cs" />
249252
<Compile Include="ContentDeliveryNetworks\v1\FlavorCollection.cs" />

0 commit comments

Comments
 (0)