Skip to content

Commit c530041

Browse files
committed
added the ability to add or remove ports
1 parent 0ace3f1 commit c530041

File tree

7 files changed

+168
-13
lines changed

7 files changed

+168
-13
lines changed

Elastacloud.AzureManagement.Fluent/Clients/Interfaces/IVirtualMachineClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public interface IVirtualMachineClient
6262
/// Gets the virtual networks available in the current subscription
6363
/// </summary>
6464
List<VirtualNetworkSite> GetAvailableVirtualNetworks();
65-
// put a placeholder in here to do the same for availability sets
65+
// used to open the ports for a virtual machine deployment
66+
void OpenPorts(string cloudServiceName, string virtualMachineName, params InputEndpoint[] endpoints);
67+
// used to close the ports for virtual machine deployment
68+
void ClosePorts(string cloudSErviceName, string virtualMachineName, params InputEndpoint[] endpoints);
6669
}
6770
}

Elastacloud.AzureManagement.Fluent/Clients/LinuxVirtualMachineClient.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,41 @@ public List<VirtualNetworkSite> GetAvailableVirtualNetworks()
610610
return command.VirtualNetworks;
611611
}
612612

613+
public void OpenPorts(string cloudServiceName, string virtualMachineName, params InputEndpoint[] endpoints)
614+
{
615+
// 1. Get host details
616+
var host = GetHostDetails(cloudServiceName);
617+
var vmHost = host.First(hostVm => hostVm.RoleName == virtualMachineName);
618+
// 2. Update Xml by inserting objects
619+
var finalEndpoints = vmHost.Endpoints.Concat(endpoints).ToArray();
620+
// 3. Commit using UpdateRole
621+
var command = new UpdateVirtualMachinePortsCommand(cloudServiceName, virtualMachineName, finalEndpoints)
622+
{
623+
SubscriptionId = SubscriptionId,
624+
Certificate = ManagementCertificate
625+
};
626+
command.Execute();
627+
}
628+
629+
public void ClosePorts(string cloudServiceName, string virtualMachineName, params InputEndpoint[] endpoints)
630+
{
631+
// 1. Get host details
632+
var host = GetHostDetails(cloudServiceName);
633+
var vmHost = host.First(hostVm => hostVm.RoleName == virtualMachineName);
634+
// 2. Update Xml by inserting objects
635+
var query = from a in vmHost.Endpoints
636+
where !(from b in endpoints
637+
select b.Port).Contains(a.Port)
638+
select a;
639+
// 3. Commit using UpdateRole
640+
var command = new UpdateVirtualMachinePortsCommand(cloudServiceName, virtualMachineName, query.ToArray())
641+
{
642+
SubscriptionId = SubscriptionId,
643+
Certificate = ManagementCertificate
644+
};
645+
command.Execute();
646+
}
647+
613648
#endregion
614649
}
615650

Elastacloud.AzureManagement.Fluent/Clients/WindowsVirtualMachineClient.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,15 @@ public List<VirtualNetworkSite> GetAvailableVirtualNetworks()
375375
command.Execute();
376376
return command.VirtualNetworks;
377377
}
378+
379+
public void OpenPorts(string cloudServiceName, string virtualMachineName, params InputEndpoint[] endpoints)
380+
{
381+
throw new NotImplementedException();
382+
}
383+
384+
public void ClosePorts(string cloudSErviceName, string virtualMachineName, params InputEndpoint[] endpoints)
385+
{
386+
throw new NotImplementedException();
387+
}
378388
}
379389
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/************************************************************************************************************
2+
* This software is distributed under a GNU Lesser License by Elastacloud Limited and it is free to *
3+
* modify and distribute providing the terms of the license are followed. From the root of the source the *
4+
* license can be found in /Resources/license.txt *
5+
* *
6+
* Web at: www.elastacloud.com *
7+
* Email: [email protected] *
8+
************************************************************************************************************/
9+
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Xml.Linq;
13+
using Elastacloud.AzureManagement.Fluent.Commands.Services;
14+
using Elastacloud.AzureManagement.Fluent.Helpers;
15+
using Elastacloud.AzureManagement.Fluent.Types;
16+
using Elastacloud.AzureManagement.Fluent.Types.VirtualMachines;
17+
using Elastacloud.AzureManagement.Fluent.VirtualMachines.Classes;
18+
using Deployment = Elastacloud.AzureManagement.Fluent.Types.VirtualMachines.Deployment;
19+
20+
namespace Elastacloud.AzureManagement.Fluent.Commands.VirtualMachines
21+
{
22+
/// <summary>
23+
/// Creates a deployment for a virtual machine and allows some preconfigured defaults from the image gallery
24+
/// </summary>
25+
internal class UpdateVirtualMachinePortsCommand : ServiceCommand
26+
{
27+
/// <summary>
28+
/// Used to construct the command to create a virtual machine deployment including the creation of a role
29+
/// </summary>
30+
/// https://management.core.windows.net/<subscription-id>/services/hostedservices/<cloudservice-name>/deployments/<deployment-name>/roles/<role-name>
31+
internal UpdateVirtualMachinePortsCommand(string cloudServiceName, string vmName, params InputEndpoint[] ports)
32+
{
33+
AdditionalHeaders["x-ms-version"] = "2012-03-01";
34+
OperationId = "hostedservices";
35+
ServiceType = "services";
36+
HttpVerb = HttpVerbPut;
37+
HttpCommand = String.Format("{0}/deployments/{0}/roles/{1}", (CloudServiceName = cloudServiceName), vmName);
38+
AllEndpoints = new InputEndpoints();
39+
foreach (var port in ports)
40+
{
41+
AllEndpoints.AddEndpoint(port);
42+
}
43+
}
44+
45+
public InputEndpoints AllEndpoints { private set; get; }
46+
/// <summary>
47+
/// The name of the cloud service to which the virtual machine will be created
48+
/// </summary>
49+
public string CloudServiceName { get; set; }
50+
/// <summary>
51+
/// Specifies the name of the role in the cloud service
52+
/// </summary>
53+
public string RoleName { get; set; }
54+
55+
56+
/// <summary>
57+
/// Creates a deployment payload for a predefined template
58+
/// </summary>
59+
/// <returns>A string xml representation</returns>
60+
protected override string CreatePayload()
61+
{
62+
var deployment = new NetworkConfigurationSet {InputEndpoints = AllEndpoints};
63+
var template = new PersistentVMRole {NetworkConfigurationSet = deployment, RoleName = RoleName};
64+
var document = new XDocument(template.GetXmlTree());
65+
return document.ToStringFullXmlDeclarationWithReplace();
66+
}
67+
68+
/// <summary>
69+
/// returns the name of the command
70+
/// </summary>
71+
public override string ToString()
72+
{
73+
return "UpdateVirtualMachinePortsCommand";
74+
}
75+
}
76+
}

Elastacloud.AzureManagement.Fluent/Elastacloud.AzureManagement.Fluent.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="Commands\Services\DeleteCloudServiceAndDeploymentCommand.cs" />
156156
<Compile Include="Commands\SqlAzure\DeleteSqlFirewallRuleCommand.cs" />
157157
<Compile Include="Commands\SqlAzure\ListFirewallRulesCommand.cs" />
158+
<Compile Include="Commands\Virtual Machines\UpdateVirtualMachinePortsCommand.cs" />
158159
<Compile Include="Commands\Virtual Machines\AddLinuxVirtualMachineToDeploymentCommand.cs" />
159160
<Compile Include="Commands\Virtual Machines\GetVirtualDisksCommand.cs" />
160161
<Compile Include="Commands\Virtual Machines\GetVirtualMachineContextCommand.cs" />

Elastacloud.AzureManagement.Fluent/Types/Virtual Machines/PersistentVMRole.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,31 @@ public static RoleInstanceStatus ConvertToRoleInstanceStatus(string status)
9898
public XElement GetXmlTree()
9999
{
100100
var namer = new RandomAccountName();
101-
var element = new XElement(Namespaces.NsWindowsAzure + "Role",
102-
new XElement(Namespaces.NsWindowsAzure + "RoleName", RoleName),
103-
new XElement(Namespaces.NsWindowsAzure + "RoleType", RoleType));
104-
var configurationSets = new XElement(Namespaces.NsWindowsAzure + "ConfigurationSets", OperatingSystemConfigurationSet.GetXmlTree(),
105-
NetworkConfigurationSet.GetXmlTree());
101+
XElement element = null;
102+
if (RoleName != null)
103+
{
104+
element = new XElement(Namespaces.NsWindowsAzure + "Role",
105+
new XElement(Namespaces.NsWindowsAzure + "RoleName", RoleName),
106+
new XElement(Namespaces.NsWindowsAzure + "RoleType", RoleType));
107+
}
108+
else
109+
{
110+
element = new XElement(Namespaces.NsWindowsAzure + "PersistentVMRole");
111+
}
112+
XElement configurationSets = new XElement(Namespaces.NsWindowsAzure + "ConfigurationSets",
113+
NetworkConfigurationSet.GetXmlTree());
114+
115+
if (OperatingSystemConfigurationSet != null)
116+
configurationSets.Add(OperatingSystemConfigurationSet.GetXmlTree());
106117
element.Add(configurationSets);
107-
element.Add(HardDisks.GetXmlTree());
108-
element.Add(new XElement(Namespaces.NsWindowsAzure + "Label", Convert.ToBase64String(Encoding.UTF8.GetBytes(RoleName))));
109-
element.Add(OSHardDisk.GetXmlTree());
118+
if (HardDisks != null)
119+
element.Add(HardDisks.GetXmlTree());
120+
if (RoleName != null)
121+
element.Add(new XElement(Namespaces.NsWindowsAzure + "Label", Convert.ToBase64String(Encoding.UTF8.GetBytes(RoleName))));
122+
if (OSHardDisk != null)
123+
element.Add(OSHardDisk.GetXmlTree());
124+
// TODO: Another hack the enum value is always present here so assume if we have no OS hard disk then we don't need a role size
125+
if (OSHardDisk != null)
110126
element.Add(new XElement(Namespaces.NsWindowsAzure + "RoleSize", RoleSize.ToString()));
111127
if (AvailabilityNameSet != null)
112128
element.Add(new XElement(Namespaces.NsWindowsAzure + "AvailabilitySetName", AvailabilityNameSet));

Elastacloud.FluentManagement.FSTest/VirtualMachines.fsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#r "D:\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.dll"
2-
#r "D:\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.Types.dll"
3-
#r "D:\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.Utils.dll"
1+
#r "C:\\Users\\Richard\\Downloads\\Lenovo backup\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.dll"
2+
#r "C:\\Users\\Richard\\Downloads\\Lenovo backup\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.Types.dll"
3+
#r "C:\\Users\\Richard\\Downloads\\Lenovo backup\\Projects\\Elastacloud\\fluent-management\\Elastacloud.AzureManagement.Fluent\\bin\\Release\\Elastacloud.AzureManagement.Fluent.Utils.dll"
44
#r "C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5\\System.Xml.Linq.dll"
55
#r @"..\packages\IPNetwork.1.3.1.0\lib\LukeSkywalker.IPNetwork.dll"
66
#r @"..\packages\FSharp.Data.2.0.14\lib\net40\FSharp.Data.dll"
@@ -28,19 +28,25 @@ let subscriptionId = "84bf11d2-7751-4ce7-b22d-ac44bf33cbe9"
2828
let settingCert fileName subscriptionId =
2929
let settings = PublishSettingsExtractor fileName
3030
settings.AddAllPublishSettingsCertificatesToPersonalMachineStore(subscriptionId).[0]
31-
let getFromBizsparkPlus = settingCert "D:\\Projects\\BizSpark Plus-7-8-2014-credentials.publishsettings"
31+
let getFromBizsparkPlus = settingCert "C:\\Users\\Richard\\Downloads\\Lenovo backup\\Projects\\bizspark.pubsettings"
3232
let vmClient = LinuxVirtualMachineClient(subscriptionId, getFromBizsparkPlus subscriptionId)
33+
let csName = "briskit1003"
34+
let vmName = "briskit"
3335

3436

3537
let storageClient = StorageClient(subscriptionId, getFromBizsparkPlus subscriptionId)
3638
let accounts = storageClient.GetStorageAccountList()
3739
let account = accounts |> Seq.filter (fun account -> account.Name = "azurecoder11")
3840

3941

42+
43+
44+
4045
let sshEndpoint = InputEndpoint(EndpointName = "ssh",
4146
LocalPort = 22,
4247
Port = Nullable(22),
4348
Protocol = Protocol.TCP)
49+
4450
let properties = new LinuxVirtualMachineProperties(
4551
VmSize = VmSize.Standard_D1,
4652
UserName = "azurecoder",
@@ -63,6 +69,14 @@ try
6369
"briskit1003") |> ignore
6470
with
6571
| :? ApplicationException as fmwe -> printfn "%s" fmwe.Message |> ignore
72+
73+
let port23 = InputEndpoint(EndpointName = "test 23", LocalPort = 23, Port = Nullable(23), Protocol = Protocol.TCP)
74+
let port24 = InputEndpoint(EndpointName = "test 24", LocalPort = 24, Port = Nullable(24), Protocol = Protocol.TCP)
75+
let port22 = InputEndpoint(EndpointName = "ssh", LocalPort = 22, Port = Nullable(22), Protocol = Protocol.TCP)
76+
let endpoints = [| port23; port24|]
77+
let endpointsClose = [| port22 |]
78+
vmClient.OpenPorts(csName, vmName, endpoints)
79+
vmClient.ClosePorts(csName, vmName, endpointsClose)
6680
let hosts = vmClient.GetHostDetails("briskit1003")
6781
vmClient.GetCurrentUbuntuImage()
6882

0 commit comments

Comments
 (0)