Skip to content

Commit 9c2fd72

Browse files
author
Hovsep
committed
Merge pull request Azure#1962 from yantang-msft/AutoFillMetricsResourceId
Auto fill metrics resource id
2 parents 896f005 + d4d4237 commit 9c2fd72

File tree

18 files changed

+7957
-6059
lines changed

18 files changed

+7957
-6059
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/ConfigFiles/DiagnosticsExtensionConfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686
"scheduledTransferLogLevelFilter": "Error",
8787
"scheduledTransferPeriod": "PT1M"
8888
},
89+
"Metrics": {
90+
"resourceId": "dummy",
91+
"MetricAggregation": [
92+
{
93+
"scheduledTransferPeriod": "PT1M"
94+
}
95+
]
96+
},
8997
"overallQuotaInMB": 4096
9098
}
9199
}

src/ResourceManager/Compute/Commands.Compute.Test/ConfigFiles/DiagnosticsExtensionConfig.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
<CrashDumpConfiguration processName="w3wp.exe" />
2727
</CrashDumps>
2828
<Logs scheduledTransferPeriod="PT3M" />
29+
<Metrics resourceId="dummy">
30+
<MetricAggregation scheduledTransferPeriod="PT1M"/>
31+
</Metrics>
2932
</DiagnosticMonitorConfiguration>
3033
</WadCfg>
3134
<StorageAccount>definedinconfigstorage</StorageAccount>

src/ResourceManager/Compute/Commands.Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests/TestDiagnosticsExtensionBasic.json

Lines changed: 2535 additions & 1648 deletions
Large diffs are not rendered by default.

src/ResourceManager/Compute/Commands.Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests/TestDiagnosticsExtensionSepcifyStorageAccountName.json

Lines changed: 2093 additions & 1439 deletions
Large diffs are not rendered by default.

src/ResourceManager/Compute/Commands.Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests/TestDiagnosticsExtensionSupportJsonConfig.json

Lines changed: 1410 additions & 1278 deletions
Large diffs are not rendered by default.

src/ResourceManager/Compute/Commands.Compute/Common/DiagnosticsHelper.cs

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.IO;
1818
using System.Linq;
19+
using System.Management.Automation;
1920
using System.Text;
2021
using System.Xml;
2122
using System.Xml.Linq;
@@ -46,12 +47,15 @@ public static class DiagnosticsHelper
4647
private static string StorageAccountEndPointTag = "storageAccountEndPoint";
4748

4849
public static string DiagnosticsConfigurationElemStr = "DiagnosticsConfiguration";
50+
public static string DiagnosticMonitorConfigurationElemStr = "DiagnosticMonitorConfiguration";
4951
public static string PublicConfigElemStr = "PublicConfig";
5052
public static string PrivateConfigElemStr = "PrivateConfig";
5153
public static string StorageAccountElemStr = "StorageAccount";
5254
public static string PrivConfNameAttr = "name";
5355
public static string PrivConfKeyAttr = "key";
5456
public static string PrivConfEndpointAttr = "endpoint";
57+
public static string MetricsElemStr = "Metrics";
58+
public static string MetricsResourceIdAttr = "resourceId";
5559

5660
public enum ConfigFileType
5761
{
@@ -70,71 +74,58 @@ public static ConfigFileType GetConfigFileType(string configurationPath)
7074
doc.Load(configurationPath);
7175
return ConfigFileType.Xml;
7276
}
73-
catch
77+
catch (XmlException)
7478
{ }
7579

7680
try
7781
{
7882
JsonConvert.DeserializeObject(File.ReadAllText(configurationPath));
7983
return ConfigFileType.Json;
8084
}
81-
catch
85+
catch (JsonReaderException)
8286
{ }
8387
}
8488

8589
return ConfigFileType.Unknown;
8690
}
8791

8892
public static Hashtable GetPublicDiagnosticsConfigurationFromFile(string configurationPath,
89-
string storageAccountName)
93+
string storageAccountName, string resourceId, Cmdlet cmdlet)
9094
{
9195
switch (GetConfigFileType(configurationPath))
9296
{
9397
case ConfigFileType.Xml:
94-
return GetPublicConfigFromXmlFile(configurationPath, storageAccountName);
98+
return GetPublicConfigFromXmlFile(configurationPath, storageAccountName, resourceId, cmdlet);
9599
case ConfigFileType.Json:
96-
return GetPublicConfigFromJsonFile(configurationPath, storageAccountName);
100+
return GetPublicConfigFromJsonFile(configurationPath, storageAccountName, resourceId, cmdlet);
97101
default:
98102
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionInvalidConfigFileFormat);
99103
}
100104
}
101105

102-
private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, string storageAccountName)
106+
private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, string storageAccountName, string resourceId, Cmdlet cmdlet)
103107
{
104-
var config = File.ReadAllText(configurationPath);
105-
106-
// find the <WadCfg> element and extract it
107-
int wadCfgBeginIndex = config.IndexOf("<WadCfg>");
108-
if (wadCfgBeginIndex == -1)
108+
var doc = XDocument.Load(configurationPath);
109+
var wadCfgElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == WadCfg);
110+
var wadCfgBlobElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == WadCfgBlob);
111+
if (wadCfgElement == null && wadCfgBlobElement == null)
109112
{
110-
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionXmlConfigNoWadCfgStartTag);
113+
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefinedInXml);
111114
}
112115

113-
int wadCfgEndIndex = config.IndexOf("</WadCfg>");
114-
if (wadCfgEndIndex == -1)
116+
if (wadCfgElement != null)
115117
{
116-
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionXmlConfigNoWadCfgEndTag);
118+
AutoFillMetricsConfig(wadCfgElement, resourceId, cmdlet);
117119
}
118120

119-
if (wadCfgEndIndex <= wadCfgBeginIndex)
120-
{
121-
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionXmlConfigWadCfgTagNotMatch);
122-
}
123-
124-
string encodedConfiguration = Convert.ToBase64String(
125-
Encoding.UTF8.GetBytes(
126-
config.Substring(
127-
wadCfgBeginIndex, wadCfgEndIndex + "</WadCfg>".Length - wadCfgBeginIndex).ToCharArray()));
121+
string originalConfiguration = wadCfgElement != null ? wadCfgElement.ToString() : wadCfgBlobElement.ToString();
122+
string encodedConfiguration = Convert.ToBase64String(Encoding.UTF8.GetBytes(wadCfgElement.ToString().ToCharArray()));
128123

129124
// Now extract the local resource directory element
130-
XmlDocument doc = new XmlDocument();
131-
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
132-
ns.AddNamespace("ns", XmlNamespace);
133-
doc.LoadXml(config);
134-
var node = doc.SelectSingleNode("//ns:LocalResourceDirectory", ns);
135-
string localDirectory = (node != null && node.Attributes != null) ? node.Attributes[Path].Value : null;
136-
string localDirectoryExpand = (node != null && node.Attributes != null)
137-
? node.Attributes["expandEnvironment"].Value
125+
var node = doc.Descendants().FirstOrDefault(e => e.Name.LocalName == "LocalResourceDirectory");
126+
string localDirectory = (node != null && node.Attribute(Path) != null) ? node.Attribute(Path).Value : null;
127+
string localDirectoryExpand = (node != null && node.Attribute("expandEnvironment") != null)
128+
? node.Attribute("expandEnvironment").Value
138129
: null;
139130
if (localDirectoryExpand == "0")
140131
{
@@ -159,7 +150,39 @@ private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, st
159150
return hashTable;
160151
}
161152

162-
private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, string storageAccountName)
153+
private static void AutoFillMetricsConfig(XElement wadCfgElement, string resourceId, Cmdlet cmdlet)
154+
{
155+
if (string.IsNullOrEmpty(resourceId))
156+
{
157+
return;
158+
}
159+
160+
var configurationElem = wadCfgElement.Elements().FirstOrDefault(d => d.Name.LocalName == DiagnosticMonitorConfigurationElemStr);
161+
if (configurationElem == null)
162+
{
163+
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionDiagnosticMonitorConfigurationElementNotDefined);
164+
}
165+
166+
var metricsElement = configurationElem.Elements().FirstOrDefault(d => d.Name.LocalName == MetricsElemStr);
167+
if (metricsElement == null)
168+
{
169+
XNamespace ns = XmlNamespace;
170+
metricsElement = new XElement(ns + MetricsElemStr,
171+
new XAttribute(MetricsResourceIdAttr, resourceId));
172+
configurationElem.Add(metricsElement);
173+
}
174+
else
175+
{
176+
var resourceIdAttr = metricsElement.Attribute(MetricsResourceIdAttr);
177+
if (resourceIdAttr != null && !resourceIdAttr.Value.Equals(resourceId))
178+
{
179+
cmdlet.WriteWarning(Properties.Resources.DiagnosticsExtensionMetricsResourceIdNotMatch);
180+
}
181+
metricsElement.SetAttributeValue(MetricsResourceIdAttr, resourceId);
182+
}
183+
}
184+
185+
private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, string storageAccountName, string resourceId, Cmdlet cmdlet)
163186
{
164187
var publicConfig = GetPublicConfigJObjectFromJsonFile(configurationPath);
165188
var properties = publicConfig.Properties().Select(p => p.Name);
@@ -170,9 +193,11 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
170193
var hashTable = new Hashtable();
171194
hashTable.Add(StorageAccount, storageAccountName);
172195

173-
if (wadCfgProperty != null)
196+
if (wadCfgProperty != null && publicConfig[wadCfgProperty] is JObject)
174197
{
175-
hashTable.Add(wadCfgProperty, publicConfig[wadCfgProperty]);
198+
var wadCfgObject = (JObject)publicConfig[wadCfgProperty];
199+
AutoFillMetricsConfig(wadCfgObject, resourceId, cmdlet);
200+
hashTable.Add(wadCfgProperty, wadCfgObject);
176201
}
177202
else if (wadCfgBlobProperty != null)
178203
{
@@ -184,12 +209,43 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
184209
}
185210
else
186211
{
187-
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefined);
212+
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefinedInJson);
188213
}
189214

190215
return hashTable;
191216
}
192217

218+
private static void AutoFillMetricsConfig(JObject wadCfgObject, string resourceId, Cmdlet cmdlet)
219+
{
220+
if (string.IsNullOrEmpty(resourceId))
221+
{
222+
return;
223+
}
224+
225+
var configObject = wadCfgObject[DiagnosticMonitorConfigurationElemStr] as JObject;
226+
if (configObject == null)
227+
{
228+
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionDiagnosticMonitorConfigurationElementNotDefined);
229+
}
230+
231+
var metricsObject = configObject[MetricsElemStr] as JObject;
232+
if (metricsObject == null)
233+
{
234+
configObject.Add(new JProperty(MetricsElemStr,
235+
new JObject(
236+
new JProperty(MetricsResourceIdAttr, resourceId))));
237+
}
238+
else
239+
{
240+
var resourceIdValue = metricsObject[MetricsResourceIdAttr] as JValue;
241+
if (resourceIdValue != null && !resourceIdValue.Value.Equals(resourceId))
242+
{
243+
cmdlet.WriteWarning(Properties.Resources.DiagnosticsExtensionMetricsResourceIdNotMatch);
244+
}
245+
metricsObject[MetricsResourceIdAttr] = resourceId;
246+
}
247+
}
248+
193249
public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccountName,
194250
string storageKey, string endpoint)
195251
{
@@ -201,7 +257,7 @@ public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccount
201257
return privateConfig;
202258
}
203259

204-
public static XElement GetPublicConfigXElementFromXmlFile(string configurationPath)
260+
private static XElement GetPublicConfigXElementFromXmlFile(string configurationPath)
205261
{
206262
XElement publicConfig = null;
207263

@@ -224,7 +280,7 @@ public static XElement GetPublicConfigXElementFromXmlFile(string configurationPa
224280
return publicConfig;
225281
}
226282

227-
public static JObject GetPublicConfigJObjectFromJsonFile(string configurationPath)
283+
private static JObject GetPublicConfigJObjectFromJsonFile(string configurationPath)
228284
{
229285
var config = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(configurationPath));
230286
var properties = config.Properties().Select(p => p.Name);

src/ResourceManager/Compute/Commands.Compute/Extension/Diagnostics/SetAzureRmVMDiagnosticsExtension.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
using Microsoft.Azure.Commands.Common.Authentication.Models;
2121
using Microsoft.Azure.Commands.Compute.Common;
2222
using Microsoft.Azure.Commands.Compute.Models;
23-
using Microsoft.Azure.ServiceManagemenet.Common;
24-
using Microsoft.Azure.ServiceManagemenet.Common.Models;
23+
using Microsoft.Azure.Management.Compute;
2524
using Microsoft.Azure.Management.Compute.Models;
2625
using Microsoft.Azure.Management.Storage;
2726
using Microsoft.WindowsAzure.Commands.Common.Storage;
@@ -179,9 +178,10 @@ private Hashtable PublicConfiguration
179178
{
180179
if (this.publicConfiguration == null)
181180
{
181+
var vm = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
182182
this.publicConfiguration =
183183
DiagnosticsHelper.GetPublicDiagnosticsConfigurationFromFile(this.DiagnosticsConfigurationPath,
184-
this.StorageAccountName);
184+
this.StorageAccountName, vm.Id, cmdlet: this);
185185
}
186186

187187
return this.publicConfiguration;

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.Designer.cs

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

0 commit comments

Comments
 (0)