Skip to content

Commit eec28e8

Browse files
committed
Merge pull request #619 from carolynvs/layer3-security-groups
Layer 3 Security Groups
2 parents afc49aa + 7ce4d0b commit eec28e8

File tree

16 files changed

+496
-25
lines changed

16 files changed

+496
-25
lines changed
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
1-
using OpenStack.Serialization;
2-
31
namespace OpenStack.Compute.v2_1
42
{
53
/// <summary>
64
/// Internet Protocols.
75
/// </summary>
8-
public class IPProtocol : StringEnumeration
9-
{
10-
/// <summary />
11-
protected IPProtocol(string displayName)
12-
: base(displayName)
13-
{ }
14-
15-
/// <summary>
16-
/// ICMP
17-
/// </summary>
18-
public static readonly IPProtocol ICMP = new IPProtocol("icmp");
19-
20-
/// <summary>
21-
/// TCP
22-
/// </summary>
23-
public static readonly IPProtocol TCP = new IPProtocol("tcp");
24-
25-
/// <summary>
26-
/// UDP
27-
/// </summary>
28-
public static readonly IPProtocol UDP = new IPProtocol("udp");
29-
}
6+
public class IPProtocol : Networking.v2.Serialization.IPProtocol<IPProtocol>
7+
{ }
308
}
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 IPProtocol : IPProtocol<IPProtocol>
7+
{ }
8+
}

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

Lines changed: 30 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,20 @@ 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.ListSecurityGroupsAsync{T}" />
110+
public static async Task<IEnumerable<SecurityGroup>> ListSecurityGroupsAsync(this NetworkingService service, SecurityGroupListOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
111+
{
112+
return await service._networkingApiBuilder.ListSecurityGroupsAsync<SecurityGroupCollection>(options, cancellationToken).ConfigureAwait(false);
113+
}
114+
115+
/// <inheritdoc cref="NetworkingApiBuilder.ListSecurityGroupRulesAsync{T}" />
116+
public static async Task<IEnumerable<SecurityGroupRule>> ListSecurityGroupRulesAsync(this NetworkingService service, SecurityGroupRuleListOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
117+
{
118+
return await service._networkingApiBuilder.ListSecurityGroupRulesAsync<SecurityGroupRuleCollection>(options, cancellationToken).ConfigureAwait(false);
119+
}
120+
#endregion
105121
}
106122
}
107123

@@ -200,5 +216,19 @@ public static void DeleteFloatingIP(this NetworkingService service, Identifier f
200216
service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId).ForceSynchronous();
201217
}
202218
#endregion
219+
220+
#region Security Groups
221+
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupsAsync" />
222+
public static IEnumerable<SecurityGroup> ListSecurityGroups(this NetworkingService service, SecurityGroupListOptions options = null)
223+
{
224+
return service.ListSecurityGroupsAsync(options).ForceSynchronous();
225+
}
226+
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupRulesAsync" />
227+
public static IEnumerable<SecurityGroupRule> ListSecurityGroupRules(this NetworkingService service, SecurityGroupRuleListOptions options = null)
228+
{
229+
return service.ListSecurityGroupRulesAsync(options).ForceSynchronous();
230+
}
231+
#endregion
232+
203233
}
204234
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using OpenStack.Serialization;
5+
6+
namespace OpenStack.Networking.v2.Layer3
7+
{
8+
/// <summary>
9+
/// Represents the security group of the <see cref="NetworkingService"/>
10+
/// </summary>
11+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_group")]
12+
public class SecurityGroup : IHaveExtraData, IServiceResource
13+
{
14+
/// <summary>
15+
/// The security group description
16+
/// </summary>
17+
[JsonProperty("description")]
18+
public string Description;
19+
20+
/// <summary>
21+
/// The UUID of security group
22+
/// </summary>
23+
[JsonProperty("id")]
24+
public Identifier Id;
25+
26+
/// <summary>
27+
/// The security group name
28+
/// </summary>
29+
[JsonProperty("name")]
30+
public string Name;
31+
32+
/// <summary>
33+
/// A list of <see cref="SecurityGroup"/> objects.
34+
/// </summary>
35+
[JsonProperty("security_group_rules")]
36+
public IList<SecurityGroupRule> SecurityGroupRules;
37+
38+
[JsonExtensionData]
39+
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
40+
41+
object IServiceResource.Owner { get; set; }
42+
}
43+
}
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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
4+
using OpenStack.Serialization;
5+
6+
namespace OpenStack.Networking.v2.Layer3
7+
{
8+
/// <summary>
9+
///
10+
/// </summary>
11+
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_group_rule")]
12+
public class SecurityGroupRule : IHaveExtraData, IServiceResource
13+
{
14+
/// <summary>
15+
/// ingress 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 TrafficDirection Direction;
21+
22+
/// <summary>
23+
/// The internet protocol version. Addresses represented in CIDR must match the ingress or egress rules.
24+
/// </summary>
25+
[JsonProperty("ethertype")]
26+
public IPVersion 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 minimum port number in the range that is matched by the security group rule.
36+
/// If the protocol is TCP or UDP, this value must be less than or equal to the port_range_max attribute value.
37+
/// If the protocol is ICMP, this value must be an ICMP type.
38+
/// </summary>
39+
[JsonProperty("port_range_min")]
40+
public int MinPort { get; set; }
41+
42+
/// <summary>
43+
/// The maximum port number in the range that is matched by the security group rule.
44+
/// The port_range_min attribute constrains the port_range_max attribute.
45+
/// If the protocol is ICMP, this value must be an ICMP type.
46+
/// </summary>
47+
[JsonProperty("port_range_max")]
48+
public int MaxPort { get; set; }
49+
50+
/// <summary>
51+
/// The protocol that is matched by the security group rule.
52+
/// </summary>
53+
[JsonProperty("protocol")]
54+
public IPProtocol 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 Identifier RemoteGroupId;
62+
63+
/// <summary>
64+
/// The remote IP prefix or CIDR 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 RemoteCIDR;
70+
71+
/// <summary>
72+
/// The UUId of security group
73+
/// </summary>
74+
[JsonProperty("security_group_id")]
75+
public Identifier SecurityGroupId;
76+
77+
[JsonExtensionData]
78+
IDictionary<string, JToken> IHaveExtraData.Data { get; set; } = new Dictionary<string, JToken>();
79+
80+
object IServiceResource.Owner { get; set; }
81+
}
82+
}
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 group rules.
7+
/// </summary>
8+
public class SecurityGroupRuleListOptions : FilterOptions
9+
{
10+
/// <summary>
11+
/// Filter by the group name.
12+
/// </summary>
13+
public TrafficDirection Direction { get; set; }
14+
15+
/// <summary />
16+
protected override IDictionary<string, object> BuildQueryString()
17+
{
18+
var queryString = new Dictionary<string, object>
19+
{
20+
["direction"] = Direction
21+
};
22+
23+
return queryString;
24+
}
25+
}
26+
}

src/corelib/Networking/v2/NetworkingApiBuilder.cs

Lines changed: 75 additions & 1 deletion
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>
@@ -667,6 +667,80 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
667667
}
668668
#endregion
669669

670+
#region SecurityGroup
671+
/// <summary>
672+
/// Lists all network security groups associated with the account.
673+
/// </summary>
674+
/// <param name="queryString">Options for filtering.</param>
675+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
676+
/// <returns>
677+
/// A collection of network security group resources associated with the account.
678+
/// </returns>
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))
694+
{
695+
Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
696+
697+
var request = endpoint
698+
.AppendPathSegments("security-groups")
699+
.Authenticate(AuthenticationProvider)
700+
.PrepareGet(cancellationToken);
701+
702+
request.Url.SetQueryParams(queryString?.Build());
703+
704+
return request;
705+
}
706+
707+
/// <summary>
708+
/// Lists all network security group rules associated with the account.
709+
/// </summary>
710+
/// <param name="queryString">Options for filtering.</param>
711+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
712+
/// <returns>
713+
/// A collection of network security group rule resources associated with the account.
714+
/// </returns>
715+
public async Task<T> ListSecurityGroupRulesAsync<T>(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
716+
where T : IEnumerable<IServiceResource>
717+
{
718+
return await BuildListSecurityGroupRulesRequest(queryString, cancellationToken)
719+
.SendAsync()
720+
.ReceiveJson<T>()
721+
.PropogateOwnerToChildren(this).ConfigureAwait(false);
722+
}
723+
724+
/// <summary>
725+
/// Builds a <see cref="ListSecurityGroupRulesAsync{T}"/> request.
726+
/// </summary>
727+
/// <param name="queryString">Options for filtering.</param>
728+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
729+
public async Task<PreparedRequest> BuildListSecurityGroupRulesRequest(IQueryStringBuilder queryString, CancellationToken cancellationToken = default(CancellationToken))
730+
{
731+
Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false);
732+
733+
var request = endpoint
734+
.AppendPathSegments("security-group-rules")
735+
.Authenticate(AuthenticationProvider)
736+
.PrepareGet(cancellationToken);
737+
738+
request.Url.SetQueryParams(queryString?.Build());
739+
740+
return request;
741+
}
742+
#endregion
743+
670744
#region Floating IPs
671745
/// <summary>
672746
/// Shows details for a server group.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using OpenStack.Serialization;
2+
3+
namespace OpenStack.Networking.v2.Serialization
4+
{
5+
/// <summary>
6+
/// Internet Protocols.
7+
/// </summary>
8+
/// <exclude />
9+
public class IPProtocol<T> : StringEnumeration
10+
where T : IPProtocol<T>, new()
11+
{
12+
/// <summary>
13+
/// ICMP
14+
/// </summary>
15+
public static readonly T ICMP = new T {DisplayName = "icmp"};
16+
17+
/// <summary>
18+
/// TCP
19+
/// </summary>
20+
public static readonly T TCP = new T {DisplayName = "tcp"};
21+
22+
/// <summary>
23+
/// UDP
24+
/// </summary>
25+
public static readonly T UDP = new T {DisplayName = "udp"};
26+
}
27+
}

0 commit comments

Comments
 (0)