16
16
using System . Collections ;
17
17
using System . IO ;
18
18
using System . Linq ;
19
+ using System . Management . Automation ;
19
20
using System . Text ;
20
21
using System . Xml ;
21
22
using System . Xml . Linq ;
@@ -46,12 +47,15 @@ public static class DiagnosticsHelper
46
47
private static string StorageAccountEndPointTag = "storageAccountEndPoint" ;
47
48
48
49
public static string DiagnosticsConfigurationElemStr = "DiagnosticsConfiguration" ;
50
+ public static string DiagnosticMonitorConfigurationElemStr = "DiagnosticMonitorConfiguration" ;
49
51
public static string PublicConfigElemStr = "PublicConfig" ;
50
52
public static string PrivateConfigElemStr = "PrivateConfig" ;
51
53
public static string StorageAccountElemStr = "StorageAccount" ;
52
54
public static string PrivConfNameAttr = "name" ;
53
55
public static string PrivConfKeyAttr = "key" ;
54
56
public static string PrivConfEndpointAttr = "endpoint" ;
57
+ public static string MetricsElemStr = "Metrics" ;
58
+ public static string MetricsResourceIdAttr = "resourceId" ;
55
59
56
60
public enum ConfigFileType
57
61
{
@@ -70,71 +74,58 @@ public static ConfigFileType GetConfigFileType(string configurationPath)
70
74
doc . Load ( configurationPath ) ;
71
75
return ConfigFileType . Xml ;
72
76
}
73
- catch
77
+ catch ( XmlException )
74
78
{ }
75
79
76
80
try
77
81
{
78
82
JsonConvert . DeserializeObject ( File . ReadAllText ( configurationPath ) ) ;
79
83
return ConfigFileType . Json ;
80
84
}
81
- catch
85
+ catch ( JsonReaderException )
82
86
{ }
83
87
}
84
88
85
89
return ConfigFileType . Unknown ;
86
90
}
87
91
88
92
public static Hashtable GetPublicDiagnosticsConfigurationFromFile ( string configurationPath ,
89
- string storageAccountName )
93
+ string storageAccountName , string resourceId , Cmdlet cmdlet )
90
94
{
91
95
switch ( GetConfigFileType ( configurationPath ) )
92
96
{
93
97
case ConfigFileType . Xml :
94
- return GetPublicConfigFromXmlFile ( configurationPath , storageAccountName ) ;
98
+ return GetPublicConfigFromXmlFile ( configurationPath , storageAccountName , resourceId , cmdlet ) ;
95
99
case ConfigFileType . Json :
96
- return GetPublicConfigFromJsonFile ( configurationPath , storageAccountName ) ;
100
+ return GetPublicConfigFromJsonFile ( configurationPath , storageAccountName , resourceId , cmdlet ) ;
97
101
default :
98
102
throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionInvalidConfigFileFormat ) ;
99
103
}
100
104
}
101
105
102
- private static Hashtable GetPublicConfigFromXmlFile ( string configurationPath , string storageAccountName )
106
+ private static Hashtable GetPublicConfigFromXmlFile ( string configurationPath , string storageAccountName , string resourceId , Cmdlet cmdlet )
103
107
{
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 )
109
112
{
110
- throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionXmlConfigNoWadCfgStartTag ) ;
113
+ throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionIaaSConfigElementNotDefinedInXml ) ;
111
114
}
112
115
113
- int wadCfgEndIndex = config . IndexOf ( "</WadCfg>" ) ;
114
- if ( wadCfgEndIndex == - 1 )
116
+ if ( wadCfgElement != null )
115
117
{
116
- throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionXmlConfigNoWadCfgEndTag ) ;
118
+ AutoFillMetricsConfig ( wadCfgElement , resourceId , cmdlet ) ;
117
119
}
118
120
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 ( ) ) ) ;
128
123
129
124
// 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
138
129
: null ;
139
130
if ( localDirectoryExpand == "0" )
140
131
{
@@ -159,7 +150,39 @@ private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, st
159
150
return hashTable ;
160
151
}
161
152
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 )
163
186
{
164
187
var publicConfig = GetPublicConfigJObjectFromJsonFile ( configurationPath ) ;
165
188
var properties = publicConfig . Properties ( ) . Select ( p => p . Name ) ;
@@ -170,9 +193,11 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
170
193
var hashTable = new Hashtable ( ) ;
171
194
hashTable . Add ( StorageAccount , storageAccountName ) ;
172
195
173
- if ( wadCfgProperty != null )
196
+ if ( wadCfgProperty != null && publicConfig [ wadCfgProperty ] is JObject )
174
197
{
175
- hashTable . Add ( wadCfgProperty , publicConfig [ wadCfgProperty ] ) ;
198
+ var wadCfgObject = ( JObject ) publicConfig [ wadCfgProperty ] ;
199
+ AutoFillMetricsConfig ( wadCfgObject , resourceId , cmdlet ) ;
200
+ hashTable . Add ( wadCfgProperty , wadCfgObject ) ;
176
201
}
177
202
else if ( wadCfgBlobProperty != null )
178
203
{
@@ -184,12 +209,43 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
184
209
}
185
210
else
186
211
{
187
- throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionIaaSConfigElementNotDefined ) ;
212
+ throw new ArgumentException ( Properties . Resources . DiagnosticsExtensionIaaSConfigElementNotDefinedInJson ) ;
188
213
}
189
214
190
215
return hashTable ;
191
216
}
192
217
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
+
193
249
public static Hashtable GetPrivateDiagnosticsConfiguration ( string storageAccountName ,
194
250
string storageKey , string endpoint )
195
251
{
@@ -201,7 +257,7 @@ public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccount
201
257
return privateConfig ;
202
258
}
203
259
204
- public static XElement GetPublicConfigXElementFromXmlFile ( string configurationPath )
260
+ private static XElement GetPublicConfigXElementFromXmlFile ( string configurationPath )
205
261
{
206
262
XElement publicConfig = null ;
207
263
@@ -224,7 +280,7 @@ public static XElement GetPublicConfigXElementFromXmlFile(string configurationPa
224
280
return publicConfig ;
225
281
}
226
282
227
- public static JObject GetPublicConfigJObjectFromJsonFile ( string configurationPath )
283
+ private static JObject GetPublicConfigJObjectFromJsonFile ( string configurationPath )
228
284
{
229
285
var config = JsonConvert . DeserializeObject < JObject > ( File . ReadAllText ( configurationPath ) ) ;
230
286
var properties = config . Properties ( ) . Select ( p => p . Name ) ;
0 commit comments