Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 182b8bd

Browse files
authored
Using the FQDN of a GCE instance instead of the IP address. (#815)
* Creating the FQDN of an instance. * Adding FQDN to the item. * Adding tests for the new method. * Removing unnecessary UI change.
1 parent 564cd16 commit 182b8bd

File tree

6 files changed

+93
-6
lines changed

6 files changed

+93
-6
lines changed

GoogleCloudExtension/GoogleCloudExtension.DataSources.UnitTests/GoogleCloudExtension.DataSources.UnitTests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
<HintPath>..\packages\Google.Apis.CloudResourceManager.v1.1.29.1.993\lib\net45\Google.Apis.CloudResourceManager.v1.dll</HintPath>
6262
<Private>True</Private>
6363
</Reference>
64+
<Reference Include="Google.Apis.Compute.v1, Version=1.29.1.981, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Google.Apis.Compute.v1.1.29.1.981\lib\net45\Google.Apis.Compute.v1.dll</HintPath>
66+
<Private>True</Private>
67+
</Reference>
6468
<Reference Include="Google.Apis.Core, Version=1.29.1.0, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab, processorArchitecture=MSIL">
6569
<HintPath>..\packages\Google.Apis.Core.1.29.1\lib\net45\Google.Apis.Core.dll</HintPath>
6670
<Private>True</Private>
@@ -97,6 +101,7 @@
97101
</Otherwise>
98102
</Choose>
99103
<ItemGroup>
104+
<Compile Include="InstanceExtensionsUnitTests.cs" />
100105
<Compile Include="ResourceManagerDataSourceUnitTests.cs" />
101106
<Compile Include="DataSourceUnitTestsBase.cs" />
102107
<Compile Include="PubSubDataSourceUnitTests.cs" />
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2017 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Google.Apis.Compute.v1.Data;
16+
using Microsoft.VisualStudio.TestTools.UnitTesting;
17+
using System.Collections.Generic;
18+
19+
namespace GoogleCloudExtension.DataSources.UnitTests
20+
{
21+
/// <summary>
22+
/// Tests for the <seealso cref="InstanceExtensions"/> class.
23+
/// </summary>
24+
[TestClass]
25+
public class InstanceExtensionsUnitTests
26+
{
27+
private static readonly Instance s_sampleInstance = new Instance
28+
{
29+
NetworkInterfaces = new List<NetworkInterface>
30+
{
31+
new NetworkInterface
32+
{
33+
AccessConfigs = new List<AccessConfig>
34+
{
35+
new AccessConfig { NatIP = "1.2.3.4" }
36+
}
37+
}
38+
}
39+
};
40+
41+
private const string ExpectedSampleInstanceFQDN = "4.3.2.1.bc.googleusercontent.com";
42+
43+
/// <summary>
44+
/// This test ensures that we get the right FQDN from a chosen instance.
45+
/// </summary>
46+
[TestMethod]
47+
public void GetFQDNTest()
48+
{
49+
var actual = s_sampleInstance.GetFullyQualifiedDomainName();
50+
Assert.AreEqual(ExpectedSampleInstanceFQDN, actual);
51+
}
52+
53+
/// <summary>
54+
/// This test ensure that we get an empty string from an instnace that doesn't have an IP.
55+
/// </summary>
56+
[TestMethod]
57+
public void GetNullFQDNTest()
58+
{
59+
Instance noPublicIpInstance = new Instance();
60+
var actual = noPublicIpInstance.GetFullyQualifiedDomainName();
61+
Assert.AreEqual("", actual);
62+
}
63+
}
64+
}

GoogleCloudExtension/GoogleCloudExtension.DataSources.UnitTests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<package id="Google.Apis" version="1.29.1" targetFramework="net452" />
55
<package id="Google.Apis.Auth" version="1.29.1" targetFramework="net452" />
66
<package id="Google.Apis.CloudResourceManager.v1" version="1.29.1.993" targetFramework="net452" />
7+
<package id="Google.Apis.Compute.v1" version="1.29.1.981" targetFramework="net452" />
78
<package id="Google.Apis.Core" version="1.29.1" targetFramework="net452" />
89
<package id="Google.Apis.Pubsub.v1" version="1.29.1.971" targetFramework="net452" />
910
<package id="Moq" version="4.7.127" targetFramework="net452" />

GoogleCloudExtension/GoogleCloudExtension.DataSources/InstanceExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,23 @@ public static string GetInternalIpAddress(this Instance instance) =>
139139
public static string GetPublicIpAddress(this Instance instance) =>
140140
instance.NetworkInterfaces?.FirstOrDefault()?.AccessConfigs?.FirstOrDefault()?.NatIP;
141141

142+
/// <summary>
143+
/// Returns the FQDN for an instance in GCE.
144+
/// </summary>
145+
/// <param name="instance">The instance to inspect.</param>
146+
/// <returns></returns>
147+
public static string GetFullyQualifiedDomainName(this Instance instance)
148+
{
149+
var ipAddress = instance.GetPublicIpAddress();
150+
if (ipAddress == null)
151+
{
152+
return "";
153+
}
154+
155+
var reverseIp = string.Join(".", ipAddress.Split('.').Reverse());
156+
return $"{reverseIp}.bc.googleusercontent.com";
157+
}
158+
142159
/// <summary>
143160
/// Returns a string with all of the tags for the instance.
144161
/// </summary>

GoogleCloudExtension/GoogleCloudExtension/Resources.Designer.cs

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GoogleCloudExtension/GoogleCloudExtension/TerminalServer/TerminalServerManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void WriteRdpFile(string path, Instance instance, WindowsInstanceC
5959
using (var writer = new StreamWriter(path))
6060
{
6161
// The IP (or name) of the VM to connect to.
62-
writer.WriteLine($"full address:s:{instance.GetPublicIpAddress()}");
62+
writer.WriteLine($"full address:s:{instance.GetFullyQualifiedDomainName()}");
6363

6464
// The user name to use.
6565
writer.WriteLine($"username:s:{credentials.User}");

0 commit comments

Comments
 (0)