diff --git a/src/corelib/Networking/v2/Layer3/NetworkingService_Layer3_Extensions.cs b/src/corelib/Networking/v2/Layer3/NetworkingService_Layer3_Extensions.cs index 4cff952a9..0ae60f16d 100644 --- a/src/corelib/Networking/v2/Layer3/NetworkingService_Layer3_Extensions.cs +++ b/src/corelib/Networking/v2/Layer3/NetworkingService_Layer3_Extensions.cs @@ -6,6 +6,8 @@ using OpenStack.Networking.v2.Serialization; using OpenStack.Serialization; using OpenStack.Synchronous.Extensions; +using Flurl.Extensions; +using Flurl.Http; namespace OpenStack.Networking.v2.Layer3 { @@ -102,6 +104,18 @@ public static class NetworkingService_Layer3_Extensions return service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId, cancellationToken); } #endregion + + #region Security Groups + /// + public static async Task> ListSecurityGroupAsync(this NetworkingService service, CancellationToken cancellationToken = default(CancellationToken)) + { + return await service._networkingApiBuilder + .ListSecurityGroupAsync(cancellationToken) + .SendAsync() + .ReceiveJson() + .ConfigureAwait(false); + } + #endregion } } @@ -200,5 +214,14 @@ public static void DeleteFloatingIP(this NetworkingService service, Identifier f service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId).ForceSynchronous(); } #endregion + + #region Security Group + /// + public static IEnumerable ListSecurityGroup (this NetworkingService service) + { + return service._networkingApiBuilder.ListSecurityGroupAsync().SendAsync().ReceiveJson().ForceSynchronous(); + } + #endregion + } } diff --git a/src/corelib/Networking/v2/Layer3/SecurityGroup.cs b/src/corelib/Networking/v2/Layer3/SecurityGroup.cs new file mode 100644 index 000000000..bb42aeb66 --- /dev/null +++ b/src/corelib/Networking/v2/Layer3/SecurityGroup.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +using OpenStack.Networking.v2; +using OpenStack.Serialization; + +namespace OpenStack.Networking.v2.Layer3 +{ + /// + ///Regpresents the security group of the + /// + [JsonConverterWithConstructor(typeof(RootWrapperConverter), "securitygroup")] + public class SecurityGroup + { + /// + ///the security group description + /// + [JsonProperty("description")] + public string Description; + + /// + ///the UUID of security group + /// + [JsonProperty("id")] + public Identifier Id; + + /// + /// the security group name + /// + [JsonProperty("name")] + public string Name; + + /// + ///A list of objects. + /// + [JsonProperty("security_group_rules")] + public IList SecurityGroupRules; + + /// + ///The UUId of tenant who owns the scurity group + /// + [JsonProperty("tenant_id")] + public string TenantId; + } +} diff --git a/src/corelib/Networking/v2/Layer3/SecurityGroupRule.cs b/src/corelib/Networking/v2/Layer3/SecurityGroupRule.cs new file mode 100644 index 000000000..d2a1f5baa --- /dev/null +++ b/src/corelib/Networking/v2/Layer3/SecurityGroupRule.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; +namespace OpenStack.Networking.v2.Layer3 +{ + /// + /// + /// + public class SecurityGroupRule + { + /// + ///ngress or egress: the direction in which the security group rule is applied. + ///For a compute instance, an ingress security group rule is applied to incoming (ingress) traffic for that instance. + ///An egress rule is applied to traffic leaving the instance. + /// + [JsonProperty("direction")] + public string Direction; + + /// + ///Must be IPv4 or IPv6, and addresses represented in CIDR must match the ingress or egress rules. + /// + [JsonProperty("ethertype")] + public string Ethertype; + + /// + /// The UUID of the security group rule. + /// + [JsonProperty("id")] + public Identifier Id; + + /// + ///The maximum port number in the range that is matched by the security group rule. + ///The port_range_min attribute constrains the port_range_max attribute. + ///If the protocol is ICMP, this value must be an ICMP type. + /// + [JsonProperty("port_range_max")] + public int PortRangeMax; + + /// + ///The minimum port number in the range that is matched by the security group rule. + ///If the protocol is TCP or UDP, this value must be less than or equal to the port_range_max attribute value. + ///If the protocol is ICMP, this value must be an ICMP type. + /// + [JsonProperty("port_range_min")] + public int PortRangeMin; + + /// + ///The protocol that is matched by the security group rule. Value is null, icmp, icmpv6, tcp, or udp. + /// + [JsonProperty("protocol")] + public string Protocol; + + /// + ///The remote group UUID to associate with this security group rule. + ///You can specify either the remote_group_id or remote_ip_prefix attribute in the request body. + /// + [JsonProperty("remote_group_id")] + public string RemoteGroupId; + + /// + ///The remote IP prefix to associate with this security group rule. + ///You can specify either the remote_group_id or remote_ip_prefix attribute in the request body. + ///This attribute value matches the IP prefix as the source IP address of the IP packet. + /// + [JsonProperty("remote_ip_prefix")] + public string RemoteIpPrefix; + + /// + ///The UUId of security group + /// + [JsonProperty("security_group_id")] + public string SecurityGroupId; + + /// + /// The UUID of the tenant who owns the security group rule. Only administrative users can specify a tenant UUID other than their own. + /// + [JsonProperty("tenant_id")] + public string TenantId; + } +} diff --git a/src/corelib/Networking/v2/NetworkingApiBuilder.cs b/src/corelib/Networking/v2/NetworkingApiBuilder.cs index cc86c88d0..1610c7ca0 100644 --- a/src/corelib/Networking/v2/NetworkingApiBuilder.cs +++ b/src/corelib/Networking/v2/NetworkingApiBuilder.cs @@ -667,6 +667,26 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au } #endregion + #region SecurityGroup + /// + /// Lists all networks security groups associated with the account. + /// + /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. + /// + /// A collection of network resources associated with the account. + /// + public async Task ListSecurityGroupAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + Url endpoint = await Endpoint.GetEndpoint(cancellationToken).ConfigureAwait(false); + + return endpoint + .AppendPathSegments("security-groups") + .Authenticate(AuthenticationProvider) + .PrepareGet(cancellationToken); + } + + #endregion + #region Floating IPs /// /// Shows details for a server group. diff --git a/src/corelib/Networking/v2/Serialization/NetSecurityGroupCollection.cs b/src/corelib/Networking/v2/Serialization/NetSecurityGroupCollection.cs new file mode 100644 index 000000000..8243a6d10 --- /dev/null +++ b/src/corelib/Networking/v2/Serialization/NetSecurityGroupCollection.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenStack.Serialization; +using OpenStack.Networking.v2.Layer3; + +namespace OpenStack.Networking.v2.Serialization +{ + /// + /// Represents a collection of security groups resources returned by the . + /// Intended for custom implementations and stubbing responses in unit tests. + /// + [JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_groups")] + public class NetSecurityGroupCollection : List + { + + /// + ///Initializes a new instance of the class. + /// + public NetSecurityGroupCollection() + { + + } + + /// + ///Initializes a new instance of the class. + /// + /// + public NetSecurityGroupCollection(IEnumerable securityGroups) : base(securityGroups) + { + + } + + } +} diff --git a/src/corelib/OpenStack.csproj b/src/corelib/OpenStack.csproj index 620fd589b..6078ced5a 100644 --- a/src/corelib/OpenStack.csproj +++ b/src/corelib/OpenStack.csproj @@ -194,6 +194,8 @@ + + @@ -234,6 +236,7 @@ + diff --git a/src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs b/src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs index fa8141f17..31d91179b 100644 --- a/src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs +++ b/src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs @@ -313,5 +313,33 @@ public void DeleteFloatingIP(HttpStatusCode responseCode) } #endregion + #region Security Groups + [Fact] + public void ListSecurityGroupAsync() + { + using (var httpTest = new HttpTest()) + { + Identifier securityGroupId = Guid.NewGuid(); + Identifier securityGroupRuleId = Guid.NewGuid(); + SecurityGroupRule rule = new SecurityGroupRule { Id = securityGroupRuleId }; + List rules = new List { rule }; + + httpTest.RespondWithJson(new NetSecurityGroupCollection + { + new SecurityGroup { Id = securityGroupId, SecurityGroupRules = rules } + }); + + var results = _networking.ListSecurityGroup(); + + httpTest.ShouldHaveCalled("*/security-groups"); + Assert.Equal(1, results.Count()); + var result = results.First(); + var resultRule = result.SecurityGroupRules.First(); + Assert.Equal(securityGroupId, result.Id); + Assert.Equal(rule.Id, resultRule.Id); + } + } + #endregion + } }