Skip to content

Commit

Permalink
Merge pull request #17 from Azure/build
Browse files Browse the repository at this point in the history
u
  • Loading branch information
huangpf committed Apr 23, 2015
2 parents 8686d1b + 6f60546 commit b3bc453
Show file tree
Hide file tree
Showing 13 changed files with 6,204 additions and 2,976 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Get-AzureLoadBalancerBackendAddressPoolConfig
* Get-AzureLoadBalancerFrontendIpConfig
* Get-AzureLoadBalancerInboundNatRuleConfig
* Get-AzureLoadBalancerProbeConfig*
* Get-AzureLoadBalancerProbeConfig
* Get-AzureLoadBalancerRuleConfig
* New-AzureLoadBalancer
* New-AzureLoadBalancerBackendAddressPoolConfig
Expand Down Expand Up @@ -60,6 +60,7 @@
* Remove-AzureNetworkSecurityRuleConfig
* Add-AzureNetworkSecurityRuleConfig
* Set-AzureNetworkSecurityRuleConfig
* Get-AzureRemoteDesktopFile
* Azure Storage
* Added cmdlets in AzureResourceManager Mode
* New-AzureStorageAccount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Compute.5.0.0-preview\lib\net40\Microsoft.Azure.Management.Compute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Network">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.2.0.0-preview\lib\net40\Microsoft.Azure.Management.Network.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -170,6 +173,8 @@
<Compile Include="Models\PSVirtualMachineExtension.cs" />
<Compile Include="Models\PSAvailabilitySet.cs" />
<Compile Include="Models\PSVirtualMachineInstanceView.cs" />
<Compile Include="RemoteDesktop\VirtualMachineRemoteDesktopBaseCmdlet.cs" />
<Compile Include="RemoteDesktop\GetAzureRemoteDesktopFileCommand.cs" />
<Compile Include="StorageAccount\StorageManagementClient.cs" />
<Compile Include="Models\SourceImageReferenceExtension.cs" />
<Compile Include="StorageAccount\GetAzureStorageAccount.cs" />
Expand Down Expand Up @@ -227,6 +232,10 @@
<Project>{c60342b1-47d3-4a0e-8081-9b97ce60b7af}</Project>
<Name>Commands.Profile</Name>
</ProjectReference>
<ProjectReference Include="..\..\Network\Commands.Network\Commands.Network.csproj">
<Project>{98cfd96b-a6bc-4f15-ae2c-603fc2b58981}</Project>
<Name>Commands.Network</Name>
</ProjectReference>
<ProjectReference Include="..\..\Resources\Commands.Resources\Commands.Resources.csproj">
<Project>{e1f5201d-6067-430e-b303-4e367652991b}</Project>
<Name>Commands.Resources</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static class ProfileNouns

public const string VirtualMachineUsage = "AzureVMUsage";

public const string RemoteDesktopFile = "AzureRemoteDesktopFile";
public const string SshPublicKey = "AzureVMSshPublicKeyConfig";
public const string AdditionalUnattendContent = "AzureVMAdditionalUnattendContentConfig";
public const string VaultCertificate = "AzureVMVaultCertificateConfig";
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@
<data name="VirtualMachineExtensionRemovalConfirmation" xml:space="preserve">
<value>This cmdlet will remove the specified virtual machine extension. Do you want to continue?</value>
</data>
<data name="VirtualMachineNotAssociatedWithPublicIPOrPublicLoadBalancer" xml:space="preserve">
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference a PublicIP or an InboundNatRule of a public load balancer. </value>
</data>
<data name="VirtualMachineNotAssociatedWithPublicLoadBalancer" xml:space="preserve">
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference an InboundNatRule of a public load balancer.</value>
</data>
<data name="VirtualMachineReferencesInternalNetworkInterface" xml:space="preserve">
<value>The RDP file cannot be generated because the network interface of the virtual machine does not reference a PublicIP or an InboungNatRule of the load balancer.</value>
</data>
<data name="VirtualMachineRemovalCaption" xml:space="preserve">
<value>Virtual machine removal operation</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Common;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Network;

namespace Microsoft.Azure.Commands.Compute
{
[Cmdlet(VerbsCommon.Get, ProfileNouns.RemoteDesktopFile)]
public class GetAzureRemoteDesktopFileCommand : VirtualMachineRemoteDesktopBaseCmdlet
{
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ValidateNotNullOrEmpty]
public override string ResourceGroupName { get; set; }

[Alias("ResourceName", "VMName")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource name.")]
[ValidateNotNullOrEmpty]
public override string Name { get; set; }

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

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

const string fullAddressPrefix = "full address:s:";
const string promptCredentials = "prompt for credentials:i:1";
const int defaultPort = 3389;

string address = string.Empty;
int port = defaultPort;

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

// Get the NIC
var nicResourceGroupName =
this.GetResourceGroupName(vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri);

var nicName =
this.GetResourceName(
vmResponse.VirtualMachine.NetworkProfile.NetworkInterfaces.First().ReferenceUri, "networkInterfaces");

var nicResponse =
this.NetworkClient.NetworkResourceProviderClient.NetworkInterfaces.Get(nicResourceGroupName, nicName);

if (nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress != null && !string.IsNullOrEmpty(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id))
{
// Get PublicIPAddress resource if present
address = this.GetAddressFromPublicIPResource(nicResponse.NetworkInterface.IpConfigurations.First().PublicIpAddress.Id);
}
else if (nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules.Any())
{
address = string.Empty;

// Get ipaddress and port from loadbalancer
foreach (var nicRuleRef in nicResponse.NetworkInterface.IpConfigurations.First().LoadBalancerInboundNatRules)
{
var lbName = this.GetResourceName(nicRuleRef.Id, "loadBalancers");
var lbResourceGroupName = this.GetResourceGroupName(nicRuleRef.Id);

var loadbalancer =
this.NetworkClient.NetworkResourceProviderClient.LoadBalancers.Get(lbResourceGroupName, lbName).LoadBalancer;

// Iterate over the InboundNatRules where Backendport = 3389
var inboundRule =
loadbalancer.InboundNatRules.Where(
rule =>
rule.BackendPort == defaultPort
&& string.Equals(
rule.Id,
nicRuleRef.Id,
StringComparison.OrdinalIgnoreCase));

if (inboundRule.Any())
{
port = inboundRule.First().FrontendPort;

// Get the corresponding frontendIPConfig -> publicIPAddress
var frontendIPConfig =
loadbalancer.FrontendIpConfigurations.First(
frontend =>
string.Equals(
inboundRule.First().FrontendIPConfiguration.Id,
frontend.Id,
StringComparison.OrdinalIgnoreCase));

if (frontendIPConfig.PublicIpAddress != null)
{
address = this.GetAddressFromPublicIPResource(frontendIPConfig.PublicIpAddress.Id);
break;
}
}
}

if (string.IsNullOrEmpty(address))
{
throw new ArgumentException(Properties.Resources.VirtualMachineNotAssociatedWithPublicLoadBalancer);
}
}
else
{
throw new ArgumentException(Properties.Resources.VirtualMachineNotAssociatedWithPublicIPOrPublicLoadBalancer);
}

// Write to file
using (var file = new StreamWriter(this.LocalPath))
{
file.WriteLine(fullAddressPrefix + address + ":" + port);
file.WriteLine(promptCredentials);
}
}

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 publicIpResponse =
this.NetworkClient.NetworkResourceProviderClient.PublicIpAddresses.Get(
publicIPResourceGroupName,
publicIPName);


// Use the FQDN if present
if (publicIpResponse.PublicIpAddress.DnsSettings != null
&& !string.IsNullOrEmpty(publicIpResponse.PublicIpAddress.DnsSettings.Fqdn))
{
address = publicIpResponse.PublicIpAddress.DnsSettings.Fqdn;
}
else
{
address = publicIpResponse.PublicIpAddress.IpAddress;
}

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

private string GetResourceName(string resourceId, string resource)
{
return resourceId.Split('/')[8];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Network;

namespace Microsoft.Azure.Commands.Compute
{
public class VirtualMachineRemoteDesktopBaseCmdlet : VirtualMachineBaseCmdlet
{

private NetworkClient networkClient;

public NetworkClient NetworkClient
{
get
{
if (networkClient == null)
{
networkClient = new NetworkClient(Profile)
{
VerboseLogger = WriteVerboseWithTimestamp,
ErrorLogger = WriteErrorWithTimestamp,
WarningLogger = WriteWarningWithTimestamp
};
}
return networkClient;
}

set { networkClient = value; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ public override void ExecuteCmdlet()

if (!this.Primary.IsPresent)
{
networkProfile.NetworkInterfaces.Add(
new NetworkInterfaceReference

networkProfile.NetworkInterfaces.Add(new NetworkInterfaceReference
{
ReferenceUri = this.Id,
});

if (networkProfile.NetworkInterfaces.Count > 1)
{
// run through the entire list of networkInterfaces and if Primary is not set, set them to false
foreach (var nic in networkProfile.NetworkInterfaces)
{
nic.Primary = nic.Primary ?? false;
}
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<package id="Microsoft.Azure.Graph.RBAC" version="1.7.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="0.18.2-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Compute" version="5.0.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Network" version="2.0.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.18.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Storage" version="2.4.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
Loading

0 comments on commit b3bc453

Please sign in to comment.