Skip to content

Vmss rdp #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2015
Merged
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 @@ -23,9 +23,15 @@

namespace Microsoft.Azure.Commands.Compute
{
using Microsoft.Azure.Management.Network.Models;

[Cmdlet(VerbsCommon.Get, ProfileNouns.RemoteDesktopFile)]
public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet
{
const string PublicIPAddressResource = "publicIPAddresses";
const string NetworkInterfaceResouce = "networkInterfaces";
const string LoadBalancerResouce = "loadBalancers";

[Parameter(
Mandatory = true,
Position = 0,
Expand All @@ -39,36 +45,88 @@ public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseC
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource name.")]
HelpMessage = "The resource name.",
ParameterSetName = "StandAloneVMDownload")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource name.",
ParameterSetName = "StandAloneVMLaunch")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The instance of the VM in virtual machine scaleset.",
ParameterSetName = "ScaleSetVMDownload")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The instance of the VM in virtual machine scaleset.",
ParameterSetName = "ScaleSetVMLaunch")]
[ValidateNotNullOrEmpty]
public string InstanceId { get; set; }

[Parameter(
Mandatory = true,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Path and name of the output RDP file.",
ParameterSetName = "StandAloneVMDownload")]
[Parameter(
Mandatory = false,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Path and name of the output RDP file.",
ParameterSetName = "StandAloneVMLaunch")]
[Parameter(
Mandatory = true,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Path and name of the output RDP file.",
ParameterSetName = "Download")]
ParameterSetName = "ScaleSetVMDownload")]
[Parameter(
Mandatory = false,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Path and name of the output RDP file.",
ParameterSetName = "Launch")]
ParameterSetName = "ScaleSetVMLaunch")]
[ValidateNotNullOrEmpty]
public string LocalPath { get; set; }

[Parameter(
Mandatory = true,
Position = 3,
HelpMessage = "Start a remote desktop session to the specified role instance.",
ParameterSetName = "Launch")]
HelpMessage = "Start a remote desktop session to the specified role instance.",
ParameterSetName = "StandAloneVMLaunch")]
[Parameter(
Mandatory = true,
Position = 3,
HelpMessage = "Start a remote desktop session to the specified role instance.",
ParameterSetName = "ScaleSetVMLaunch")]
public SwitchParameter Launch
{
get;
set;
}

[Parameter(
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine scaleset name.",
ParameterSetName = "ScaleSetVMDownload")]
[Parameter(
Mandatory = true,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine scaleset name.",
ParameterSetName = "ScaleSetVMLaunch")]
[ValidateNotNullOrEmpty]
public string VirtualMachineScaleSetName { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();
Expand All @@ -82,24 +140,46 @@ public override void ExecuteCmdlet()
string address = string.Empty;
int port = defaultPort;

// Get Azure VM
var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name);
NetworkInterface nic;

if (this.ParameterSetName.Contains("StandAloneVM"))
{
// Get standalone Azure VM
var vmResponse = this.VirtualMachineClient.Get(this.ResourceGroupName, this.Name);

var nicId = vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri;

// Get the NIC
var nicResourceGroupName = this.GetResourceGroupName(nicId);

// Get the NIC
var nicResourceGroupName =
this.GetResourceGroupName(vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri);
var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);

var nicName =
this.GetResourceName(
vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri, "networkInterfaces");
nic = this.NetworkClient.NetworkManagementClient.NetworkInterfaces.Get(
nicResourceGroupName,
nicName);
}
else
{
var vmResponse = this.VirtualMachineScaleSetVMClient.Get(this.ResourceGroupName, this.VirtualMachineScaleSetName, this.InstanceId);

var nicId = vmResponse.VirtualMachineScaleSetVM.NetworkProfile.NetworkInterfaces.First().ReferenceUri;

var nic =
this.NetworkClient.NetworkManagementClient.NetworkInterfaces.Get(nicResourceGroupName, nicName);
// Get the NIC
var nicResourceGroupName = this.GetResourceGroupName(nicId);

var nicName = this.GetResourceName(nicId, NetworkInterfaceResouce);

nic = this.NetworkClient.NetworkManagementClient.NetworkInterfaces.GetVirtualMachineScaleSetNetworkInterface(
nicResourceGroupName,
this.VirtualMachineScaleSetName,
this.InstanceId,
nicName);
}

if (nic.IpConfigurations.First().PublicIPAddress != null && !string.IsNullOrEmpty(nic.IpConfigurations.First().Id))
if (nic.IpConfigurations.First().PublicIPAddress != null && !string.IsNullOrEmpty(nic.IpConfigurations.First().PublicIPAddress.Id))
{
// Get PublicIPAddress resource if present
address = this.GetAddressFromPublicIPResource(nic.IpConfigurations.First().Id);
address = this.GetAddressFromPublicIPResource(nic.IpConfigurations.First().PublicIPAddress.Id);
}
else if (nic.IpConfigurations.First().LoadBalancerInboundNatRules.Any())
{
Expand All @@ -108,7 +188,7 @@ public override void ExecuteCmdlet()
// Get ipaddress and port from loadbalancer
foreach (var nicRuleRef in nic.IpConfigurations.First().LoadBalancerInboundNatRules)
{
var lbName = this.GetResourceName(nicRuleRef.Id, "loadBalancers");
var lbName = this.GetResourceName(nicRuleRef.Id, LoadBalancerResouce);
var lbResourceGroupName = this.GetResourceGroupName(nicRuleRef.Id);

var loadbalancer =
Expand Down Expand Up @@ -172,7 +252,7 @@ public override void ExecuteCmdlet()
WindowStyle = ProcessWindowStyle.Hidden
};

if (this.LocalPath == null)
if (string.IsNullOrEmpty(this.LocalPath))
{
string scriptGuid = Guid.NewGuid().ToString();

Expand Down Expand Up @@ -200,10 +280,10 @@ public override void ExecuteCmdlet()
private string GetAddressFromPublicIPResource(string resourceId)
{
string address = string.Empty;

// Get IpAddress from public IPAddress resource
var publicIPResourceGroupName = this.GetResourceGroupName(resourceId);
var publicIPName = this.GetResourceName(resourceId, "publicIPAddresses");
var publicIPName = this.GetResourceName(resourceId, PublicIPAddressResource);

var publicIp =
this.NetworkClient.NetworkManagementClient.PublicIPAddresses.Get(
Expand All @@ -223,14 +303,24 @@ private string GetAddressFromPublicIPResource(string resourceId)

return address;
}

private string GetResourceGroupName(string resourceId)
{
return resourceId.Split('/')[4];
}

private string GetResourceName(string resourceId, string resource)
{
return resourceId.Split('/')[8];
int resourceTypeLocation = resourceId.IndexOf(resource, StringComparison.OrdinalIgnoreCase);

var resourceName = resourceId.Substring(resourceTypeLocation + resource.Length + 1);
if (resourceName.Contains("/"))
{
int resourceNameEnd = resourceName.IndexOf("/");
resourceName = resourceName.Substring(0, resourceNameEnd);
}

return resourceName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ public IVirtualMachineOperations VirtualMachineClient
return ComputeClient.ComputeManagementClient.VirtualMachines;
}
}

public IVirtualMachineScaleSetVMOperations VirtualMachineScaleSetVMClient
{
get
{
return ComputeClient.ComputeManagementClient.VirtualMachineScaleSetVMs;
}
}
}
}