Skip to content

Commit a773686

Browse files
Zehua Jiangcarolynvs
authored andcommitted
Network.listSecurityGroupAsync
1 parent afc49aa commit a773686

7 files changed

Lines changed: 242 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using OpenStack.Networking.v2.Serialization;
77
using OpenStack.Serialization;
88
using OpenStack.Synchronous.Extensions;
9+
using Flurl.Extensions;
10+
using Flurl.Http;
911

1012
namespace OpenStack.Networking.v2.Layer3
1113
{
@@ -102,6 +104,18 @@ public static class NetworkingService_Layer3_Extensions
102104
return service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId, cancellationToken);
103105
}
104106
#endregion
107+
108+
#region Security Groups
109+
/// <inheritdoc cref="NetworkingApiBuilder.ListSecurityGroupAsync(CancellationToken)" />
110+
public static async Task<IEnumerable<SecurityGroup>> ListSecurityGroupAsync(this NetworkingService service, CancellationToken cancellationToken = default(CancellationToken))
111+
{
112+
return await service._networkingApiBuilder
113+
.ListSecurityGroupAsync(cancellationToken)
114+
.SendAsync()
115+
.ReceiveJson<NetSecurityGroupCollection>()
116+
.ConfigureAwait(false);
117+
}
118+
#endregion
105119
}
106120
}
107121

@@ -200,5 +214,14 @@ public static void DeleteFloatingIP(this NetworkingService service, Identifier f
200214
service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId).ForceSynchronous();
201215
}
202216
#endregion
217+
218+
#region Security Group
219+
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupAsync" />
220+
public static IEnumerable<SecurityGroup> ListSecurityGroup (this NetworkingService service)
221+
{
222+
return service._networkingApiBuilder.ListSecurityGroupAsync().SendAsync().ReceiveJson<NetSecurityGroupCollection>().ForceSynchronous();
223+
}
224+
#endregion
225+
203226
}
204227
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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;
7+
using OpenStack.Networking.v2;
8+
using OpenStack.Serialization;
9+
10+
namespace OpenStack.Networking.v2.Layer3
11+
{
12+
/// <summary>
13+
///Regpresents the security group of the <see cref="NetworkingService"/>
14+
/// </summary>
15+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "securitygroup")]
16+
public class SecurityGroup
17+
{
18+
/// <summary>
19+
///the security group description
20+
/// </summary>
21+
[JsonProperty("description")]
22+
public string Description;
23+
24+
/// <summary>
25+
///the UUID of security group
26+
/// </summary>
27+
[JsonProperty("id")]
28+
public Identifier Id;
29+
30+
/// <summary>
31+
/// the security group name
32+
/// </summary>
33+
[JsonProperty("name")]
34+
public string Name;
35+
36+
/// <summary>
37+
///A list of <see cref="SecurityGroup"/> objects.
38+
/// </summary>
39+
[JsonProperty("security_group_rules")]
40+
public IList<SecurityGroupRule> SecurityGroupRules;
41+
42+
/// <summary>
43+
///The UUId of tenant who owns the scurity group
44+
/// </summary>
45+
[JsonProperty("tenant_id")]
46+
public string TenantId;
47+
}
48+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
7+
namespace OpenStack.Networking.v2.Layer3
8+
{
9+
/// <summary>
10+
///
11+
/// </summary>
12+
public class SecurityGroupRule
13+
{
14+
/// <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.
18+
/// </summary>
19+
[JsonProperty("direction")]
20+
public string Direction;
21+
22+
/// <summary>
23+
///Must be IPv4 or IPv6, and addresses represented in CIDR must match the ingress or egress rules.
24+
/// </summary>
25+
[JsonProperty("ethertype")]
26+
public string Ethertype;
27+
28+
/// <summary>
29+
/// The UUID of the security group rule.
30+
/// </summary>
31+
[JsonProperty("id")]
32+
public Identifier Id;
33+
34+
/// <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.
38+
/// </summary>
39+
[JsonProperty("port_range_max")]
40+
public int PortRangeMax;
41+
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.
46+
/// </summary>
47+
[JsonProperty("port_range_min")]
48+
public int PortRangeMin;
49+
50+
/// <summary>
51+
///The protocol that is matched by the security group rule. Value is null, icmp, icmpv6, tcp, or udp.
52+
/// </summary>
53+
[JsonProperty("protocol")]
54+
public string Protocol;
55+
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.
59+
/// </summary>
60+
[JsonProperty("remote_group_id")]
61+
public string RemoteGroupId;
62+
63+
/// <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.
67+
/// </summary>
68+
[JsonProperty("remote_ip_prefix")]
69+
public string RemoteIpPrefix;
70+
71+
/// <summary>
72+
///The UUId of security group
73+
/// </summary>
74+
[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;
82+
}
83+
}

src/corelib/Networking/v2/NetworkingApiBuilder.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,26 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
667667
}
668668
#endregion
669669

670+
#region SecurityGroup
671+
/// <summary>
672+
/// Lists all networks security groups associated with the account.
673+
/// </summary>
674+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
675+
/// <returns>
676+
/// A collection of network resources associated with the account.
677+
/// </returns>
678+
public async Task<PreparedRequest> ListSecurityGroupAsync(CancellationToken cancellationToken = default(CancellationToken))
679+
{
680+
Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
681+
682+
return endpoint
683+
.AppendPathSegments("security-groups")
684+
.Authenticate(AuthenticationProvider)
685+
.PrepareGet(cancellationToken);
686+
}
687+
688+
#endregion
689+
670690
#region Floating IPs
671691
/// <summary>
672692
/// Shows details for a server group.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using OpenStack.Serialization;
7+
using OpenStack.Networking.v2.Layer3;
8+
9+
namespace OpenStack.Networking.v2.Serialization
10+
{
11+
/// <summary>
12+
/// Represents a collection of security groups resources returned by the <see cref="NetworkingService"/>.
13+
/// <para>Intended for custom implementations and stubbing responses in unit tests.</para>
14+
/// </summary>
15+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_groups")]
16+
public class NetSecurityGroupCollection : List<SecurityGroup>
17+
{
18+
19+
/// <summary>
20+
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
21+
/// </summary>
22+
public NetSecurityGroupCollection()
23+
{
24+
25+
}
26+
27+
/// <summary>
28+
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
29+
/// </summary>
30+
/// <param name="securityGroups"></param>
31+
public NetSecurityGroupCollection(IEnumerable<SecurityGroup> securityGroups) : base(securityGroups)
32+
{
33+
34+
}
35+
36+
}
37+
}

src/corelib/OpenStack.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@
194194
<Compile Include="Images\v2\Serialization\ImageStatus.cs" />
195195
<Compile Include="Networking\IPVersion.cs" />
196196
<Compile Include="Networking\NamespaceDoc.cs" />
197+
<Compile Include="Networking\v2\Layer3\SecurityGroup.cs" />
198+
<Compile Include="Networking\v2\Layer3\SecurityGroupRule.cs" />
197199
<Compile Include="Networking\v2\AllowedAddress.cs" />
198200
<Compile Include="Networking\v2\AllocationPool.cs" />
199201
<Compile Include="Networking\v2\PortListOptions.cs" />
@@ -234,6 +236,7 @@
234236
<Compile Include="Networking\v2\Serialization\NetworkDefinitionCollection.cs" />
235237
<Compile Include="Networking\v2\Serialization\PortCollection.cs" />
236238
<Compile Include="Networking\v2\Serialization\PortDefinitionCollection.cs" />
239+
<Compile Include="Networking\v2\Serialization\NetSecurityGroupCollection.cs" />
237240
<Compile Include="Networking\v2\Serialization\SubnetCollection.cs" />
238241
<Compile Include="Networking\v2\Serialization\SubnetDefinitionCollection.cs" />
239242
<Compile Include="Networking\v2\Subnet.cs" />

src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,33 @@ public void DeleteFloatingIP(HttpStatusCode responseCode)
313313
}
314314
#endregion
315315

316+
#region Security Groups
317+
[Fact]
318+
public void ListSecurityGroupAsync()
319+
{
320+
using (var httpTest = new HttpTest())
321+
{
322+
Identifier securityGroupId = Guid.NewGuid();
323+
Identifier securityGroupRuleId = Guid.NewGuid();
324+
SecurityGroupRule rule = new SecurityGroupRule { Id = securityGroupRuleId };
325+
List<SecurityGroupRule> rules = new List<SecurityGroupRule> { rule };
326+
327+
httpTest.RespondWithJson(new NetSecurityGroupCollection
328+
{
329+
new SecurityGroup { Id = securityGroupId, SecurityGroupRules = rules }
330+
});
331+
332+
var results = _networking.ListSecurityGroup();
333+
334+
httpTest.ShouldHaveCalled("*/security-groups");
335+
Assert.Equal(1, results.Count());
336+
var result = results.First();
337+
var resultRule = result.SecurityGroupRules.First();
338+
Assert.Equal(securityGroupId, result.Id);
339+
Assert.Equal(rule.Id, resultRule.Id);
340+
}
341+
}
342+
#endregion
343+
316344
}
317345
}

0 commit comments

Comments
 (0)