Skip to content

HPF PR: dev <- huangpf:dev #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Network, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.3.2.0-preview\lib\net45\Microsoft.Azure.Management.Network.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.3.3.0-preview\lib\net45\Microsoft.Azure.Management.Network.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Storage, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@
"scheduledTransferLogLevelFilter": "Error",
"scheduledTransferPeriod": "PT1M"
},
"Metrics": {
"resourceId": "dummy",
"MetricAggregation": [
{
"scheduledTransferPeriod": "PT1M"
}
]
},
"overallQuotaInMB": 4096
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<CrashDumpConfiguration processName="w3wp.exe" />
</CrashDumps>
<Logs scheduledTransferPeriod="PT3M" />
<Metrics resourceId="dummy">
<MetricAggregation scheduledTransferPeriod="PT1M"/>
</Metrics>
</DiagnosticMonitorConfiguration>
</WadCfg>
<StorageAccount>definedinconfigstorage</StorageAccount>
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<package id="Microsoft.Azure.Graph.RBAC" version="1.9.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Authorization" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Compute" version="11.3.0-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Network" version="3.2.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Network" version="3.3.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Storage" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.5896.19355-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.6.0-preview" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Network, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.3.2.0-preview\lib\net45\Microsoft.Azure.Management.Network.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Network.3.3.0-preview\lib\net45\Microsoft.Azure.Management.Network.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.Storage">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Xml;
using System.Xml.Linq;
Expand Down Expand Up @@ -46,12 +47,15 @@ public static class DiagnosticsHelper
private static string StorageAccountEndPointTag = "storageAccountEndPoint";

public static string DiagnosticsConfigurationElemStr = "DiagnosticsConfiguration";
public static string DiagnosticMonitorConfigurationElemStr = "DiagnosticMonitorConfiguration";
public static string PublicConfigElemStr = "PublicConfig";
public static string PrivateConfigElemStr = "PrivateConfig";
public static string StorageAccountElemStr = "StorageAccount";
public static string PrivConfNameAttr = "name";
public static string PrivConfKeyAttr = "key";
public static string PrivConfEndpointAttr = "endpoint";
public static string MetricsElemStr = "Metrics";
public static string MetricsResourceIdAttr = "resourceId";

public enum ConfigFileType
{
Expand All @@ -70,71 +74,58 @@ public static ConfigFileType GetConfigFileType(string configurationPath)
doc.Load(configurationPath);
return ConfigFileType.Xml;
}
catch
catch (XmlException)
{ }

try
{
JsonConvert.DeserializeObject(File.ReadAllText(configurationPath));
return ConfigFileType.Json;
}
catch
catch (JsonReaderException)
{ }
}

return ConfigFileType.Unknown;
}

public static Hashtable GetPublicDiagnosticsConfigurationFromFile(string configurationPath,
string storageAccountName)
string storageAccountName, string resourceId, Cmdlet cmdlet)
{
switch (GetConfigFileType(configurationPath))
{
case ConfigFileType.Xml:
return GetPublicConfigFromXmlFile(configurationPath, storageAccountName);
return GetPublicConfigFromXmlFile(configurationPath, storageAccountName, resourceId, cmdlet);
case ConfigFileType.Json:
return GetPublicConfigFromJsonFile(configurationPath, storageAccountName);
return GetPublicConfigFromJsonFile(configurationPath, storageAccountName, resourceId, cmdlet);
default:
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionInvalidConfigFileFormat);
}
}

private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, string storageAccountName)
private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, string storageAccountName, string resourceId, Cmdlet cmdlet)
{
var config = File.ReadAllText(configurationPath);

// find the <WadCfg> element and extract it
int wadCfgBeginIndex = config.IndexOf("<WadCfg>");
if (wadCfgBeginIndex == -1)
var doc = XDocument.Load(configurationPath);
var wadCfgElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == WadCfg);
var wadCfgBlobElement = doc.Descendants().FirstOrDefault(d => d.Name.LocalName == WadCfgBlob);
if (wadCfgElement == null && wadCfgBlobElement == null)
{
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionXmlConfigNoWadCfgStartTag);
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefinedInXml);
}

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

if (wadCfgEndIndex <= wadCfgBeginIndex)
{
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionXmlConfigWadCfgTagNotMatch);
}

string encodedConfiguration = Convert.ToBase64String(
Encoding.UTF8.GetBytes(
config.Substring(
wadCfgBeginIndex, wadCfgEndIndex + "</WadCfg>".Length - wadCfgBeginIndex).ToCharArray()));
string originalConfiguration = wadCfgElement != null ? wadCfgElement.ToString() : wadCfgBlobElement.ToString();
string encodedConfiguration = Convert.ToBase64String(Encoding.UTF8.GetBytes(wadCfgElement.ToString().ToCharArray()));

// Now extract the local resource directory element
XmlDocument doc = new XmlDocument();
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("ns", XmlNamespace);
doc.LoadXml(config);
var node = doc.SelectSingleNode("//ns:LocalResourceDirectory", ns);
string localDirectory = (node != null && node.Attributes != null) ? node.Attributes[Path].Value : null;
string localDirectoryExpand = (node != null && node.Attributes != null)
? node.Attributes["expandEnvironment"].Value
var node = doc.Descendants().FirstOrDefault(e => e.Name.LocalName == "LocalResourceDirectory");
string localDirectory = (node != null && node.Attribute(Path) != null) ? node.Attribute(Path).Value : null;
string localDirectoryExpand = (node != null && node.Attribute("expandEnvironment") != null)
? node.Attribute("expandEnvironment").Value
: null;
if (localDirectoryExpand == "0")
{
Expand All @@ -159,7 +150,39 @@ private static Hashtable GetPublicConfigFromXmlFile(string configurationPath, st
return hashTable;
}

private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, string storageAccountName)
private static void AutoFillMetricsConfig(XElement wadCfgElement, string resourceId, Cmdlet cmdlet)
{
if (string.IsNullOrEmpty(resourceId))
{
return;
}

var configurationElem = wadCfgElement.Elements().FirstOrDefault(d => d.Name.LocalName == DiagnosticMonitorConfigurationElemStr);
if (configurationElem == null)
{
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionDiagnosticMonitorConfigurationElementNotDefined);
}

var metricsElement = configurationElem.Elements().FirstOrDefault(d => d.Name.LocalName == MetricsElemStr);
if (metricsElement == null)
{
XNamespace ns = XmlNamespace;
metricsElement = new XElement(ns + MetricsElemStr,
new XAttribute(MetricsResourceIdAttr, resourceId));
configurationElem.Add(metricsElement);
}
else
{
var resourceIdAttr = metricsElement.Attribute(MetricsResourceIdAttr);
if (resourceIdAttr != null && !resourceIdAttr.Value.Equals(resourceId))
{
cmdlet.WriteWarning(Properties.Resources.DiagnosticsExtensionMetricsResourceIdNotMatch);
}
metricsElement.SetAttributeValue(MetricsResourceIdAttr, resourceId);
}
}

private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, string storageAccountName, string resourceId, Cmdlet cmdlet)
{
var publicConfig = GetPublicConfigJObjectFromJsonFile(configurationPath);
var properties = publicConfig.Properties().Select(p => p.Name);
Expand All @@ -170,9 +193,11 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
var hashTable = new Hashtable();
hashTable.Add(StorageAccount, storageAccountName);

if (wadCfgProperty != null)
if (wadCfgProperty != null && publicConfig[wadCfgProperty] is JObject)
{
hashTable.Add(wadCfgProperty, publicConfig[wadCfgProperty]);
var wadCfgObject = (JObject)publicConfig[wadCfgProperty];
AutoFillMetricsConfig(wadCfgObject, resourceId, cmdlet);
hashTable.Add(wadCfgProperty, wadCfgObject);
}
else if (wadCfgBlobProperty != null)
{
Expand All @@ -184,12 +209,43 @@ private static Hashtable GetPublicConfigFromJsonFile(string configurationPath, s
}
else
{
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefined);
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionIaaSConfigElementNotDefinedInJson);
}

return hashTable;
}

private static void AutoFillMetricsConfig(JObject wadCfgObject, string resourceId, Cmdlet cmdlet)
{
if (string.IsNullOrEmpty(resourceId))
{
return;
}

var configObject = wadCfgObject[DiagnosticMonitorConfigurationElemStr] as JObject;
if (configObject == null)
{
throw new ArgumentException(Properties.Resources.DiagnosticsExtensionDiagnosticMonitorConfigurationElementNotDefined);
}

var metricsObject = configObject[MetricsElemStr] as JObject;
if (metricsObject == null)
{
configObject.Add(new JProperty(MetricsElemStr,
new JObject(
new JProperty(MetricsResourceIdAttr, resourceId))));
}
else
{
var resourceIdValue = metricsObject[MetricsResourceIdAttr] as JValue;
if (resourceIdValue != null && !resourceIdValue.Value.Equals(resourceId))
{
cmdlet.WriteWarning(Properties.Resources.DiagnosticsExtensionMetricsResourceIdNotMatch);
}
metricsObject[MetricsResourceIdAttr] = resourceId;
}
}

public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccountName,
string storageKey, string endpoint)
{
Expand All @@ -201,7 +257,7 @@ public static Hashtable GetPrivateDiagnosticsConfiguration(string storageAccount
return privateConfig;
}

public static XElement GetPublicConfigXElementFromXmlFile(string configurationPath)
private static XElement GetPublicConfigXElementFromXmlFile(string configurationPath)
{
XElement publicConfig = null;

Expand All @@ -224,7 +280,7 @@ public static XElement GetPublicConfigXElementFromXmlFile(string configurationPa
return publicConfig;
}

public static JObject GetPublicConfigJObjectFromJsonFile(string configurationPath)
private static JObject GetPublicConfigJObjectFromJsonFile(string configurationPath)
{
var config = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(configurationPath));
var properties = config.Properties().Select(p => p.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static class AEMExtensionConstants
public static Dictionary<string, string> AEMExtensionDefaultName = new Dictionary<string, string>() { { OSTypeWindows, "AzureCATExtensionHandler" }, { OSTypeLinux, "AzureEnhancedMonitorForLinux" } };
public static Dictionary<string, string> AEMExtensionPublisher = new Dictionary<string, string>() { { OSTypeWindows, "Microsoft.AzureCAT.AzureEnhancedMonitoring" }, { OSTypeLinux, "Microsoft.OSTCExtensions" } };
public static Dictionary<string, string> AEMExtensionType = new Dictionary<string, string>() { { OSTypeWindows, "AzureCATExtensionHandler" }, { OSTypeLinux, "AzureEnhancedMonitorForLinux" } };
public static Dictionary<string, string> AEMExtensionVersion = new Dictionary<string, string>() { { OSTypeWindows, "2.2" }, { OSTypeLinux, "3.0" } };
public static Dictionary<string, Version> AEMExtensionVersion = new Dictionary<string, Version>() { { OSTypeWindows, new Version(2, 2) }, { OSTypeLinux, new Version(3,0) } };

public static Dictionary<string, string> WADExtensionDefaultName = new Dictionary<string, string>() { { OSTypeWindows, "IaaSDiagnostics" }, { OSTypeLinux, "LinuxDiagnostic" } };
public static Dictionary<string, string> WADExtensionPublisher = new Dictionary<string, string>() { { OSTypeWindows, "Microsoft.Azure.Diagnostics" }, { OSTypeLinux, "Microsoft.OSTCExtensions" } };
public static Dictionary<string, string> WADExtensionType = new Dictionary<string, string>() { { OSTypeWindows, "IaaSDiagnostics" }, { OSTypeLinux, "LinuxDiagnostic" } };
public static Dictionary<string, string> WADExtensionVersion = new Dictionary<string, string>() { { OSTypeWindows, "1.5" }, { OSTypeLinux, "2.2" } };
public static Dictionary<string, Version> WADExtensionVersion = new Dictionary<string, Version>() { { OSTypeWindows, new Version(1,5) }, { OSTypeLinux, new Version(2,2) } };

public const string OSTypeWindows = "Windows";
public const string OSTypeLinux = "Linux";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,44 @@ internal VirtualMachineExtension GetExtension(VirtualMachine vm, string type, st
return null;
}

internal Version GetExtensionVersion(VirtualMachine vm, VirtualMachineInstanceView vmStatus, string osType, string type, string publisher)
{
Version version = new Version();
if (AEMExtensionConstants.AEMExtensionPublisher[osType].Equals(publisher, StringComparison.InvariantCultureIgnoreCase)
&& AEMExtensionConstants.AEMExtensionType[osType].Equals(type, StringComparison.InvariantCultureIgnoreCase))
{
version = AEMExtensionConstants.AEMExtensionVersion[osType];
}
else if (AEMExtensionConstants.WADExtensionPublisher[osType].Equals(publisher, StringComparison.InvariantCultureIgnoreCase)
&& AEMExtensionConstants.WADExtensionType[osType].Equals(type, StringComparison.InvariantCultureIgnoreCase))
{
version = AEMExtensionConstants.WADExtensionVersion[osType];
}

if (vm.Resources != null && vmStatus.Extensions != null)
{
var extension = vm.Resources.FirstOrDefault(ext =>
ext.VirtualMachineExtensionType.Equals(type)
&& ext.Publisher.Equals(publisher));

if (extension != null)
{
var extensionStatus = vmStatus.Extensions.FirstOrDefault(ext => ext.Name.Equals(extension.Name));

if (extensionStatus != null)
{
string strExtVersion = extensionStatus.TypeHandlerVersion;
Version extVersion;
if (Version.TryParse(strExtVersion, out extVersion))
{
version = extVersion;
}
}
}
}
return version;
}

internal VirtualMachineExtensionInstanceView GetExtension(VirtualMachine vm, VirtualMachineInstanceView vmStatus, string type, string publisher)
{
var ext = this.GetExtension(vm, type, publisher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ public override void ExecuteCmdlet()
});
}

private void WriteVerbose(string message, params object[] args)
{
base.WriteVerbose(String.Format(message, args));
}

private void WriteError(string message, params object[] args)
{
base.WriteError(new ErrorRecord(new Exception(String.Format(message, args)), "Error", ErrorCategory.NotSpecified, null));
Expand Down
Loading