Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -102,6 +104,18 @@ public static class NetworkingService_Layer3_Extensions
return service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId, cancellationToken);
}
#endregion

#region Security Groups
/// <inheritdoc cref="NetworkingApiBuilder.ListSecurityGroupAsync(CancellationToken)" />
public static async Task<IEnumerable<SecurityGroup>> ListSecurityGroupAsync(this NetworkingService service, CancellationToken cancellationToken = default(CancellationToken))
{
return await service._networkingApiBuilder
.ListSecurityGroupAsync(cancellationToken)
.SendAsync()
.ReceiveJson<NetSecurityGroupCollection>()
.ConfigureAwait(false);
}
#endregion
}
}

Expand Down Expand Up @@ -200,5 +214,14 @@ public static void DeleteFloatingIP(this NetworkingService service, Identifier f
service._networkingApiBuilder.DeleteFloatingIPAsync(floatingIPId).ForceSynchronous();
}
#endregion

#region Security Group
/// <inheritdoc cref="NetworkingService_Layer3_Extensions.ListSecurityGroupAsync" />
public static IEnumerable<SecurityGroup> ListSecurityGroup (this NetworkingService service)
{
return service._networkingApiBuilder.ListSecurityGroupAsync().SendAsync().ReceiveJson<NetSecurityGroupCollection>().ForceSynchronous();
}
#endregion

}
}
48 changes: 48 additions & 0 deletions src/corelib/Networking/v2/Layer3/SecurityGroup.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
///Regpresents the security group of the <see cref="NetworkingService"/>
/// </summary>
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "securitygroup")]
public class SecurityGroup
{
/// <summary>
///the security group description
/// </summary>
[JsonProperty("description")]
public string Description;

/// <summary>
///the UUID of security group
/// </summary>
[JsonProperty("id")]
public Identifier Id;

/// <summary>
/// the security group name
/// </summary>
[JsonProperty("name")]
public string Name;

/// <summary>
///A list of <see cref="SecurityGroup"/> objects.
/// </summary>
[JsonProperty("security_group_rules")]
public IList<SecurityGroupRule> SecurityGroupRules;

/// <summary>
///The UUId of tenant who owns the scurity group
/// </summary>
[JsonProperty("tenant_id")]
public string TenantId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TenantId should be removed.

In general, admin-only properties should not be exposed in the main namespaces. The idea being that if a user picks up the SDK, they only see functionality and data that they are able to work with. If an operator wants to add admin functionality to the SDK, it is implemented in an XXX.Operator namespace, so that users don't see it unless they explicitly import it. 😄

}
}
83 changes: 83 additions & 0 deletions src/corelib/Networking/v2/Layer3/SecurityGroupRule.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
///
/// </summary>
public class SecurityGroupRule
{
/// <summary>
///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.
/// </summary>
[JsonProperty("direction")]
public string Direction;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subclass of OpenStack.Serialization.StringEnumeration, see OpenStack.Compute.v2_1.AddressType for an example.

When a resource property has a fixed set of values, it helps to wrap that in a enum-like class so that user's don't have to go to the API documentation to know what the allowed values are, in this case "egress" and "ingress". The hierarchy of the StringEnumeration type enables the SDK to define well known values while still allowing for provider specific values to be defined later. For example Rackspace may have different values available in their SDK which builds on top of OpenStack.NET.


/// <summary>
///Must be IPv4 or IPv6, and addresses represented in CIDR must match the ingress or egress rules.
/// </summary>
[JsonProperty("ethertype")]
public string Ethertype;

/// <summary>
/// The UUID of the security group rule.
/// </summary>
[JsonProperty("id")]
public Identifier Id;

/// <summary>
///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.
/// </summary>
[JsonProperty("port_range_max")]
public int PortRangeMax;

///<summary>
///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.
/// </summary>
[JsonProperty("port_range_min")]
public int PortRangeMin;

/// <summary>
///The protocol that is matched by the security group rule. Value is null, icmp, icmpv6, tcp, or udp.
/// </summary>
[JsonProperty("protocol")]
public string Protocol;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subclass of StringEnumeration as well.


///<summary>
///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.
/// </summary>
[JsonProperty("remote_group_id")]
public string RemoteGroupId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an Identifier.


/// <summary>
///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.
/// </summary>
[JsonProperty("remote_ip_prefix")]
public string RemoteIpPrefix;

/// <summary>
///The UUId of security group
/// </summary>
[JsonProperty("security_group_id")]
public string SecurityGroupId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an Identifier as well.


/// <summary>
/// The UUID of the tenant who owns the security group rule. Only administrative users can specify a tenant UUID other than their own.
/// </summary>
[JsonProperty("tenant_id")]
public string TenantId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TenantId should be removed.

}
}
20 changes: 20 additions & 0 deletions src/corelib/Networking/v2/NetworkingApiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,26 @@ public NetworkingApiBuilder(IServiceType serviceType, IAuthenticationProvider au
}
#endregion

#region SecurityGroup
/// <summary>
/// Lists all networks security groups associated with the account.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>
/// A collection of network resources associated with the account.
/// </returns>
public async Task<PreparedRequest> 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
/// <summary>
/// Shows details for a server group.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Represents a collection of security groups resources returned by the <see cref="NetworkingService"/>.
/// <para>Intended for custom implementations and stubbing responses in unit tests.</para>
/// </summary>
[JsonConverterWithConstructor(typeof(RootWrapperConverter), "security_groups")]
public class NetSecurityGroupCollection : List<SecurityGroup>
{

/// <summary>
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
/// </summary>
public NetSecurityGroupCollection()
{

}

/// <summary>
///Initializes a new instance of the<see cref="SecurityGroup"/> class.
/// </summary>
/// <param name="securityGroups"></param>
public NetSecurityGroupCollection(IEnumerable<SecurityGroup> securityGroups) : base(securityGroups)
{

}

}
}
3 changes: 3 additions & 0 deletions src/corelib/OpenStack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@
<Compile Include="Images\v2\Serialization\ImageStatus.cs" />
<Compile Include="Networking\IPVersion.cs" />
<Compile Include="Networking\NamespaceDoc.cs" />
<Compile Include="Networking\v2\Layer3\SecurityGroup.cs" />
<Compile Include="Networking\v2\Layer3\SecurityGroupRule.cs" />
<Compile Include="Networking\v2\AllowedAddress.cs" />
<Compile Include="Networking\v2\AllocationPool.cs" />
<Compile Include="Networking\v2\PortListOptions.cs" />
Expand Down Expand Up @@ -234,6 +236,7 @@
<Compile Include="Networking\v2\Serialization\NetworkDefinitionCollection.cs" />
<Compile Include="Networking\v2\Serialization\PortCollection.cs" />
<Compile Include="Networking\v2\Serialization\PortDefinitionCollection.cs" />
<Compile Include="Networking\v2\Serialization\NetSecurityGroupCollection.cs" />
<Compile Include="Networking\v2\Serialization\SubnetCollection.cs" />
<Compile Include="Networking\v2\Serialization\SubnetDefinitionCollection.cs" />
<Compile Include="Networking\v2\Subnet.cs" />
Expand Down
28 changes: 28 additions & 0 deletions src/testing/unit/Networking/v2/Layer3/Layer3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecurityGroupRule> rules = new List<SecurityGroupRule> { 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

}
}