Skip to content

Commit c8898d4

Browse files
unknownunknown
authored andcommitted
Update VMAccess extension to use Json configs
1 parent 7c9753b commit c8898d4

File tree

4 files changed

+120
-56
lines changed

4 files changed

+120
-56
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ExtensionTests/AzureVMAccessExtensionTests.cs

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.IO;
17-
using System.Threading;
18-
using System.Xml;
1915
using Microsoft.VisualStudio.TestTools.UnitTesting;
2016
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
2117
using Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions;
2218
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
2319
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ConfigDataInfo;
24-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
25-
using Microsoft.Azure.Common.Authentication;
20+
using Newtonsoft.Json.Linq;
21+
using System;
22+
using System.IO;
23+
using System.Xml;
2624

2725
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ExtensionTests
2826
{
@@ -32,16 +30,10 @@ public class AzureVMAccessExtensionTests: ServiceManagementTest
3230
private string serviceName;
3331
private string vmName;
3432
private const string referenceNamePrefix = "Reference";
35-
private string vmAccessUserName;
36-
private string vmAccessPassword;
37-
private string publicConfiguration;
38-
private string privateConfiguration;
39-
private string publicConfigPath;
40-
private string privateConfigPath;
41-
private string version = "1.0";
42-
string rdpPath = @".\AzureVM.rdp";
43-
string dns;
44-
int port;
33+
private readonly string vmAccessUserName = Utilities.GetUniqueShortName();
34+
private readonly string vmAccessPassword = Utilities.GetUniqueShortName("", 8);
35+
private const string version = "1.0";
36+
const string rdpPath = @".\AzureVM.rdp";
4537
private string referenceName;
4638

4739
[ClassInitialize]
@@ -57,7 +49,6 @@ public void TestIntialize()
5749
serviceName = Utilities.GetUniqueShortName(serviceNamePrefix);
5850
vmName = Utilities.GetUniqueShortName(vmNamePrefix);
5951
testStartTime = DateTime.Now;
60-
GetVmAccessConfiguration();
6152
referenceName = Utilities.GetUniqueShortName(referenceNamePrefix);
6253
}
6354

@@ -76,7 +67,6 @@ public static void ClassCleanUp()
7667
#region Test cases
7768

7869
[TestMethod(), TestCategory(Category.Scenario), TestProperty("Feature", "IAAS"), Priority(0), Owner("hylee"), Description("Test the cmdlet ((Get,Set)-AzureVMAccessExtension)")]
79-
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\package.csv", "package#csv", DataAccessMethod.Sequential)]
8070
public void CreateVMAccessExtensionTest()
8171
{
8272
try
@@ -109,8 +99,39 @@ public void CreateVMAccessExtensionTest()
10999
}
110100
}
111101

102+
[TestMethod(), TestCategory(Category.Scenario), TestProperty("Feature", "IAAS"), Priority(0), Owner("hylee"), Description("Test the cmdlet ((Get,Set)-AzureVMAccessExtension)")]
103+
public void CreateVMAccessExtensionTest2()
104+
{
105+
try
106+
{
107+
// Deploy a new IaaS VM with Extension using Add-AzureVMExtension
108+
Console.WriteLine("Create a new VM with VM access extension.");
109+
var vm = CreateIaaSVMObject(vmName);
110+
vm = vmPowershellCmdlets.SetAzureVMAccessExtension(
111+
vm, vmAccessUserName, vmAccessPassword, "2.*", null, false, true);
112+
113+
vmPowershellCmdlets.NewAzureVM(serviceName, new[] { vm }, locationName, true);
114+
Console.WriteLine("Created a new VM {0} with VM access extension. Service Name : {1}", vmName, serviceName);
115+
116+
ValidateVMAccessExtension(vmName, serviceName, true, false);
117+
118+
Utilities.GetAzureVMAndWaitForReady(serviceName, vmName, 30000, 300000);
119+
// Verify that the extension actually work
120+
VerifyRDPExtension(vmName, serviceName);
121+
122+
// Disbale extesnion
123+
DisableExtension(vmName, serviceName);
124+
ValidateVMAccessExtension(vmName, serviceName, false, false);
125+
pass = true;
126+
}
127+
catch (Exception e)
128+
{
129+
Console.WriteLine(e.ToString());
130+
throw;
131+
}
132+
}
133+
112134
[TestMethod(), TestCategory(Category.Scenario), TestProperty("Feature", "IAAS"), Priority(0), Owner("hylee"), Description("Test the cmdlet ((Get,Set,Remove)-AzureVMAccessExtension)")]
113-
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\package.csv", "package#csv", DataAccessMethod.Sequential)]
114135
public void UpdateVMAccessExtensionTest()
115136
{
116137
try
@@ -140,7 +161,6 @@ public void UpdateVMAccessExtensionTest()
140161
}
141162

142163
[TestMethod(), TestCategory(Category.Scenario), TestProperty("Feature", "IAAS"), Priority(0), Owner("hylee"), Description("Test the cmdlet ((Get,Set)-AzureVMAccessExtension)")]
143-
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\package.csv", "package#csv", DataAccessMethod.Sequential)]
144164
public void AddRoleVMAccessExtensionTest()
145165
{
146166
try
@@ -167,7 +187,6 @@ public void AddRoleVMAccessExtensionTest()
167187
}
168188

169189
[TestMethod(), TestCategory(Category.Scenario), TestProperty("Feature", "IAAS"), Priority(0), Owner("hylee"), Description("Test the cmdlet ((Get,Set)-AzureVMAccessExtension)")]
170-
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Resources\\package.csv", "package#csv", DataAccessMethod.Sequential)]
171190
public void UpdateRoleVMAccessExtensionTest()
172191
{
173192
try
@@ -201,20 +220,6 @@ public void UpdateRoleVMAccessExtensionTest()
201220

202221
#region Helper Methods
203222

204-
private void GetVmAccessConfiguration()
205-
{
206-
privateConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "PrivateConfig.xml");
207-
publicConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "PublicConfig.xml");
208-
privateConfiguration = FileUtilities.DataStore.ReadFileAsText(privateConfigPath);
209-
publicConfiguration = FileUtilities.DataStore.ReadFileAsText(publicConfigPath);
210-
211-
XmlDocument doc = new XmlDocument();
212-
doc.LoadXml(publicConfiguration);
213-
vmAccessUserName = doc.GetElementsByTagName("UserName")[0].InnerText;
214-
doc.LoadXml(privateConfiguration);
215-
vmAccessPassword = doc.GetElementsByTagName("Password")[0].InnerText;
216-
}
217-
218223
private PersistentVM CreateIaaSVMObject(string vmName)
219224
{
220225
//Create an IaaS VM with a static CA.
@@ -233,7 +238,7 @@ private VirtualMachineAccessExtensionContext GetAzureVMAccessExtesnion(string vm
233238
return vmExtension[0];
234239
}
235240

236-
private void ValidateVMAccessExtension(string vmName, string serviceName, bool enabled)
241+
private void ValidateVMAccessExtension(string vmName, string serviceName, bool enabled, bool isXml = true)
237242
{
238243
var vmAccessExtension = GetAzureVMAccessExtesnion(vmName,serviceName);
239244
Utilities.PrintContext(vmAccessExtension);
@@ -243,9 +248,19 @@ private void ValidateVMAccessExtension(string vmName, string serviceName, bool e
243248
Assert.AreEqual(vmAccessUserName, vmAccessExtension.UserName, "Incorrect User name");
244249
Assert.AreEqual("Enable", vmAccessExtension.State, "State is not Enable");
245250
Assert.IsTrue(vmAccessExtension.Enabled, "Enabled is not true");
246-
XmlDocument doc = new XmlDocument();
247-
doc.LoadXml(vmAccessExtension.PublicConfiguration);
248-
Assert.AreEqual(vmAccessUserName, doc.GetElementsByTagName("UserName")[0].InnerText,"Incorrect User name in public configuration");
251+
if (isXml)
252+
{
253+
XmlDocument doc = new XmlDocument();
254+
doc.LoadXml(vmAccessExtension.PublicConfiguration);
255+
Assert.AreEqual(vmAccessUserName, doc.GetElementsByTagName("UserName")[0].InnerText,
256+
"Incorrect User name in public configuration");
257+
}
258+
else
259+
{
260+
var jsonObject = JObject.Parse(vmAccessExtension.PublicConfiguration);
261+
Assert.AreEqual(vmAccessUserName, jsonObject["UserName"].Value<string>(),
262+
"Incorrect User name in public configuration");
263+
}
249264
}
250265
else
251266
{
@@ -280,8 +295,8 @@ private void VerifyRDPExtension(string vmName, string serviceName)
280295
{
281296
string firstLine = stream.ReadLine();
282297
var dnsAndport = Utilities.FindSubstring(firstLine, ':', 2).Split(new char[] { ':' });
283-
dns = dnsAndport[0];
284-
port = int.Parse(dnsAndport[1]);
298+
var dns = dnsAndport[0];
299+
var port = int.Parse(dnsAndport[1]);
285300
}
286301
Console.WriteLine("Azure VM RDP file downloaded.");
287302
}
@@ -290,7 +305,7 @@ private void DisableExtension(string vmName, string serviceName)
290305
{
291306
var vm = GetAzureVM(vmName, serviceName);
292307
Console.WriteLine("Disabling the VM Access extesnion for the vm {0}",vmName);
293-
vm = vmPowershellCmdlets.SetAzureVMAccessExtension(vm, disable:true, forceUpdate:true);
308+
vm = vmPowershellCmdlets.SetAzureVMAccessExtension(vm, disable:true, version:"2.0", forceUpdate:true);
294309
vmPowershellCmdlets.UpdateAzureVM(vmName, serviceName, vm);
295310
Console.WriteLine("Disabled VM Access extesnion for the vm {0}", vmName);
296311
}

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/Common/VirtualMachineExtensionCmdletBase.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Common.Authentication;
16+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
17+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
18+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
19+
using Newtonsoft.Json.Linq;
1520
using System;
1621
using System.Collections.Generic;
1722
using System.Linq;
1823
using System.Management.Automation;
1924
using System.Xml.Linq;
20-
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
21-
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
22-
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
23-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
24-
using Microsoft.Azure.Common.Authentication;
2525

2626
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
2727
{
@@ -117,6 +117,15 @@ protected bool IsLegacyExtension(string name, string publisher, string version)
117117
&& eq(r.Version, version));
118118
}
119119

120+
protected bool IsXmlExtension(string version)
121+
{
122+
if (string.IsNullOrEmpty(version))
123+
{
124+
return true;
125+
}
126+
return version.StartsWith("1");
127+
}
128+
120129
protected ResourceExtensionReferenceList ResourceExtensionReferences
121130
{
122131
get
@@ -345,5 +354,15 @@ protected static string GetConfigValue(string xmlText, string element)
345354

346355
return result.FirstOrDefault();
347356
}
357+
358+
protected static string GetJsonConfigValue(string jsonText, string element)
359+
{
360+
if (string.IsNullOrEmpty(jsonText))
361+
{
362+
return null;
363+
}
364+
var jsonObject = JObject.Parse(jsonText);
365+
return jsonObject[element].Value<string>();
366+
}
348367
}
349368
}

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/VMAccess/SetAzureVMAccessExtension.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Management.Automation;
1615
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
16+
using System.Management.Automation;
1717

1818
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
1919
{
@@ -119,12 +119,21 @@ protected override void ValidateParameters()
119119
this.PublicConfiguration = GetLegacyConfiguration();
120120
this.Version = VMAccessAgentLegacyVersion;
121121
}
122-
else
122+
else if (IsXmlExtension(this.Version))
123123
{
124124
this.ReferenceName = string.IsNullOrEmpty(this.ReferenceName) ? ExtensionDefaultName : this.ReferenceName;
125125
this.PublicConfiguration = GetPublicConfiguration();
126126
this.PrivateConfiguration = GetPrivateConfiguration();
127-
this.Version = ExtensionDefaultVersion;
127+
if (string.IsNullOrEmpty(this.Version))
128+
{
129+
this.Version = ExtensionDefaultVersion;
130+
}
131+
}
132+
else
133+
{
134+
this.ReferenceName = string.IsNullOrEmpty(this.ReferenceName) ? ExtensionDefaultName : this.ReferenceName;
135+
this.PublicConfiguration = GetJsonPublicConfiguration();
136+
this.PrivateConfiguration = GetJsonPrivateConfiguration();
128137
}
129138
}
130139

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/VMAccess/VirtualMachineAccessExtensionCmdletBase.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Linq;
16-
using System.Xml.Linq;
1715
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
1816
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
17+
using Newtonsoft.Json;
18+
using System.Collections;
19+
using System.Linq;
20+
using System.Xml.Linq;
1921

2022
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
2123
{
@@ -94,6 +96,14 @@ protected string GetPublicConfiguration()
9496
return config.ToString();
9597
}
9698

99+
protected string GetJsonPublicConfiguration()
100+
{
101+
Hashtable publicSettings = new Hashtable();
102+
publicSettings.Add(UserNameElem, UserName ?? "");
103+
104+
return JsonConvert.SerializeObject(publicSettings);
105+
}
106+
97107
protected string GetPrivateConfiguration()
98108
{
99109
XDocument config = new XDocument(
@@ -105,6 +115,13 @@ protected string GetPrivateConfiguration()
105115

106116
return config.ToString();
107117
}
118+
protected string GetJsonPrivateConfiguration()
119+
{
120+
Hashtable privateSettings = new Hashtable();
121+
privateSettings.Add(PasswordElem, Password ?? "");
122+
123+
return JsonConvert.SerializeObject(privateSettings);
124+
}
108125

109126
protected void GetVMAccessExtensionValues(ResourceExtensionReference extensionRef)
110127
{
@@ -117,7 +134,7 @@ protected void GetVMAccessExtensionValues(ResourceExtensionReference extensionRe
117134
else
118135
{
119136
Disable = string.Equals(extensionRef.State, ReferenceDisableStateStr);
120-
GetVMAccessExtensionValues(extensionRef.ResourceExtensionParameterValues);
137+
GetVMAccessExtensionValues(extensionRef.ResourceExtensionParameterValues, IsXmlExtension(extensionRef.Version));
121138
}
122139
}
123140
else
@@ -126,7 +143,7 @@ protected void GetVMAccessExtensionValues(ResourceExtensionReference extensionRe
126143
}
127144
}
128145

129-
private void GetVMAccessExtensionValues(ResourceExtensionParameterValueList paramVals)
146+
private void GetVMAccessExtensionValues(ResourceExtensionParameterValueList paramVals, bool isXml)
130147
{
131148
if (paramVals != null && paramVals.Any())
132149
{
@@ -135,7 +152,9 @@ private void GetVMAccessExtensionValues(ResourceExtensionParameterValueList para
135152
if (publicParamVal != null && !string.IsNullOrEmpty(publicParamVal.Value))
136153
{
137154
this.PublicConfiguration = publicParamVal.Value;
138-
this.UserName = GetConfigValue(this.PublicConfiguration, UserNameElem);
155+
this.UserName = (isXml)
156+
? GetConfigValue(this.PublicConfiguration, UserNameElem)
157+
: GetJsonConfigValue(this.PublicConfiguration, UserNameElem);
139158
}
140159

141160
var privateParamVal = paramVals.FirstOrDefault(
@@ -146,7 +165,9 @@ private void GetVMAccessExtensionValues(ResourceExtensionParameterValueList para
146165
if (privateParamVal != null)
147166
{
148167
this.PrivateConfiguration = privateParamVal.SecureValue.ConvertToUnsecureString();
149-
this.Password = GetConfigValue(this.PrivateConfiguration, PasswordElem);
168+
this.Password = (isXml)
169+
? GetConfigValue(this.PrivateConfiguration, PasswordElem)
170+
: GetJsonConfigValue(this.PrivateConfiguration, PasswordElem);
150171
}
151172
}
152173
}

0 commit comments

Comments
 (0)