Skip to content

Commit 746443b

Browse files
committed
Apply the storage validation logic to IaaS.
1 parent 0e5c609 commit 746443b

File tree

7 files changed

+178
-147
lines changed

7 files changed

+178
-147
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement/Commands.ServiceManagement.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.Compute.12.3.1\lib\net40\Microsoft.WindowsAzure.Management.Compute.dll</HintPath>
134134
</Reference>
135135
<Reference Include="Microsoft.WindowsAzure.Management.Network, Version=7.0.0.0, Culture=neutral, processorArchitecture=MSIL">
136-
<SpecificVersion>False</SpecificVersion>
136+
<SpecificVersion>False</SpecificVersion>
137137
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.Network.7.0.4\lib\net40\Microsoft.WindowsAzure.Management.Network.dll</HintPath>
138138
</Reference>
139139
<Reference Include="Microsoft.WindowsAzure.Management.Storage">
@@ -175,6 +175,7 @@
175175
<Reference Include="System.Xml" />
176176
</ItemGroup>
177177
<ItemGroup>
178+
<Compile Include="Common\DiagnosticsHelper.cs" />
178179
<Compile Include="IaaS\Extensions\SqlServer\AzureVMSqlServerPublicAutoBackupSettings.cs" />
179180
<Compile Include="IaaS\Extensions\SqlServer\AzureVMSqlServerPublicKeyVaultCredentialSettings.cs" />
180181
<Compile Include="IaaS\Extensions\SqlServer\AzureVMSqlServerPrivateKeyVaultCredentialSettings.cs" />

src/ServiceManagement/Compute/Commands.ServiceManagement/Extensions/Diagnostics/BaseAzureServiceDiagnosticsExtension.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private void ValidateStorageAccountName()
7676

7777
if (string.IsNullOrEmpty(this.StorageAccountName))
7878
{
79-
throw new ArgumentException(Resources.PaaSDiagnosticsNullStorageAccountName);
79+
throw new ArgumentException(Resources.DiagnosticsExtensionNullStorageAccountName);
8080
}
8181
}
8282

@@ -87,7 +87,7 @@ private void ValidateStorageAccountKey()
8787

8888
if (string.IsNullOrEmpty(this.StorageAccountKey))
8989
{
90-
throw new ArgumentException(Resources.PaaSDiagnosticsNullStorageAccountKey);
90+
throw new ArgumentException(Resources.DiagnosticsExtensionNullStorageAccountKey);
9191
}
9292
}
9393

@@ -99,7 +99,7 @@ private void ValidateStorageAccountEndpoint()
9999

100100
if (string.IsNullOrEmpty(this.StorageAccountEndpoint))
101101
{
102-
throw new ArgumentNullException(Resources.PaaSDiagnosticsNullStorageAccountEndpoint);
102+
throw new ArgumentNullException(Resources.DiagnosticsExtensionNullStorageAccountEndpoint);
103103
}
104104
}
105105

@@ -111,14 +111,14 @@ protected override void ValidateConfiguration()
111111
// make sure it is the header
112112
if (!header.Trim().StartsWith("<?xml"))
113113
{
114-
throw new ArgumentException(Resources.PaaSDiagnosticsWrongHeader);
114+
throw new ArgumentException(Resources.DiagnosticsExtensionWrongHeader);
115115
}
116116
}
117117

118118
var publicConfigElem = DiagnosticsHelper.GetPublicConfigElement(this.DiagnosticsConfigurationPath);
119119
if (publicConfigElem == null)
120120
{
121-
throw new ArgumentException(Resources.PaaSDiagnosticsNullPublicConfig);
121+
throw new ArgumentException(Resources.DiagnosticsExtensionNullPublicConfig);
122122
}
123123
publicConfigElem.SetAttributeValue("xmlns", XmlNamespace);
124124
PublicConfiguration = publicConfigElem.ToString();
@@ -140,7 +140,7 @@ protected override void ValidateConfiguration()
140140
}
141141
else if (!string.IsNullOrEmpty(node.InnerText) && string.Compare(node.InnerText, StorageAccountName, true) != 0)
142142
{
143-
throw new ArgumentException(Resources.PassDiagnosticsNoMatchStorageAccount);
143+
throw new ArgumentException(Resources.DiagnosticsExtensionNoMatchStorageAccount);
144144
}
145145
}
146146
else
@@ -157,7 +157,7 @@ protected override void ValidateConfiguration()
157157
if (!string.IsNullOrEmpty(privateConfigStorageAccountName)
158158
&& !string.Equals(StorageAccountName, privateConfigStorageAccountName, StringComparison.OrdinalIgnoreCase))
159159
{
160-
throw new ArgumentException(Resources.PassDiagnosticsNoMatchPrivateStorageAccount);
160+
throw new ArgumentException(Resources.DiagnosticsExtensionNoMatchPrivateStorageAccount);
161161
}
162162

163163
PrivateConfigurationXml = new XDocument(PrivateConfigurationXmlTemplate);

src/ServiceManagement/Compute/Commands.ServiceManagement/Extensions/Diagnostics/GetAzureServiceDiagnosticsExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System.Linq;
1616
using System.Management.Automation;
17+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Common;
1718
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
1819
using Microsoft.WindowsAzure.Management.Compute;
1920

@@ -82,7 +83,7 @@ where ExtensionManager.CheckNameSpaceType(extension, ProviderNamespace, Extensio
8283
ProviderNameSpace = extension.ProviderNamespace,
8384
Id = extension.Id,
8485
Role = role,
85-
StorageAccountName = GetPublicConfigValue(extension, StorageAccountElemStr),
86+
StorageAccountName = GetPublicConfigValue(extension, DiagnosticsHelper.StorageAccountElemStr),
8687
WadCfg = GetPublicConfigValue(extension, WadCfgElemStr),
8788
PublicConfiguration = GetPublicConfigValue(extension, "PublicConfig"),
8889
Version = extension.Version

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/Diagnostics/SetAzureVMDiagnosticsExtension.cs

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

15+
using System;
1516
using System.Linq;
1617
using System.Management.Automation;
1718
using Microsoft.WindowsAzure.Commands.Common.Storage;
1819
using Microsoft.WindowsAzure.Commands.ServiceManagement.Common;
1920
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
20-
using Microsoft.WindowsAzure.Management.Storage;
21+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
2122

2223
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
2324
{
@@ -31,7 +32,6 @@ public class SetAzureVMDiagnosticsExtensionCommand : VirtualMachineDiagnosticsEx
3132
{
3233
private string publicConfiguration;
3334
private string privateConfiguration;
34-
private string storageKey;
3535
protected const string SetExtParamSetName = "SetDiagnosticsExtension";
3636
protected const string SetExtRefParamSetName = "SetDiagnosticsWithReferenceExtension";
3737

@@ -57,81 +57,90 @@ public string DiagnosticsConfigurationPath
5757
[Parameter(ParameterSetName = SetExtParamSetName,
5858
Position = 1,
5959
ValueFromPipelineByPropertyName = true,
60-
Mandatory = true,
61-
HelpMessage = "The storage connection context")]
60+
HelpMessage = "The storage account name")]
6261
[Parameter(ParameterSetName = SetExtRefParamSetName,
6362
Position = 1,
6463
ValueFromPipelineByPropertyName = true,
65-
Mandatory = true,
64+
HelpMessage = "The storage account name")]
65+
public string StorageAccountName
66+
{
67+
get;
68+
set;
69+
}
70+
71+
[Parameter(ParameterSetName = SetExtParamSetName,
72+
Position = 2,
73+
ValueFromPipelineByPropertyName = true,
74+
HelpMessage = "The storage account key")]
75+
[Parameter(ParameterSetName = SetExtRefParamSetName,
76+
Position = 2,
77+
ValueFromPipelineByPropertyName = true,
78+
HelpMessage = "The storage account key")]
79+
public string StorageAccountKey
80+
{
81+
get;
82+
set;
83+
}
84+
85+
[Parameter(ParameterSetName = SetExtParamSetName,
86+
Position = 3,
87+
ValueFromPipelineByPropertyName = true,
88+
HelpMessage = "The storage account endpoint")]
89+
[Parameter(ParameterSetName = SetExtRefParamSetName,
90+
Position = 3,
91+
ValueFromPipelineByPropertyName = true,
92+
HelpMessage = "The storage account endpoint")]
93+
public string StorageAccountEndpoint
94+
{
95+
get;
96+
set;
97+
}
98+
99+
[Parameter(ParameterSetName = SetExtParamSetName,
100+
Position = 4,
101+
ValueFromPipelineByPropertyName = true,
102+
HelpMessage = "The storage connection context")]
103+
[Parameter(ParameterSetName = SetExtRefParamSetName,
104+
Position = 4,
105+
ValueFromPipelineByPropertyName = true,
66106
HelpMessage = "The storage connection context")]
67-
[ValidateNotNullOrEmpty]
68107
public AzureStorageContext StorageContext
69108
{
70109
get;
71110
set;
72111
}
73-
74112

75113
[Parameter(
76114
ParameterSetName = SetExtParamSetName,
77-
Position = 2,
115+
Position = 5,
78116
ValueFromPipelineByPropertyName = false,
79117
HelpMessage = "WAD Version")]
80118
[Parameter(
81119
ParameterSetName = SetExtRefParamSetName,
82-
Position = 2,
120+
Position = 5,
83121
ValueFromPipelineByPropertyName = false,
84122
HelpMessage = "WAD Version")]
85123
public override string Version { get; set; }
86124

87125
[Parameter(
88126
ParameterSetName = SetExtParamSetName,
89-
Position = 3,
127+
Position = 6,
90128
ValueFromPipelineByPropertyName = true,
91129
HelpMessage = "To Set the Extension State to 'Disable'.")]
92130
[Parameter(
93131
ParameterSetName = SetExtRefParamSetName,
94-
Position = 3,
132+
Position = 6,
95133
ValueFromPipelineByPropertyName = true,
96134
HelpMessage = "To Set the Extension State to 'Disable'.")]
97135
public override SwitchParameter Disable { get; set; }
98136

99137
[Parameter(
100138
ParameterSetName = SetExtRefParamSetName,
101-
Position = 4,
139+
Position = 7,
102140
ValueFromPipelineByPropertyName = true,
103141
HelpMessage = "To specify the reference name.")]
104142
public override string ReferenceName { get; set; }
105143

106-
public string StorageAccountName
107-
{
108-
get
109-
{
110-
return this.StorageContext.StorageAccountName;
111-
}
112-
}
113-
114-
public string Endpoint
115-
{
116-
get
117-
{
118-
return "https://" + this.StorageContext.EndPointSuffix;
119-
}
120-
}
121-
122-
public string StorageKey
123-
{
124-
get
125-
{
126-
if (string.IsNullOrEmpty(this.storageKey))
127-
{
128-
this.storageKey = GetStorageKey();
129-
}
130-
131-
return this.storageKey;
132-
}
133-
}
134-
135144
public override string PublicConfiguration
136145
{
137146
get
@@ -152,15 +161,14 @@ public override string PrivateConfiguration
152161
{
153162
if (string.IsNullOrEmpty(this.privateConfiguration))
154163
{
155-
this.privateConfiguration = DiagnosticsHelper.GetJsonSerializedPrivateDiagnosticsConfiguration(this.StorageAccountName, this.StorageKey,
156-
this.Endpoint);
164+
this.privateConfiguration = DiagnosticsHelper.GetJsonSerializedPrivateDiagnosticsConfiguration(this.StorageAccountName, this.StorageAccountKey,
165+
this.StorageAccountEndpoint);
157166
}
158167

159168
return this.privateConfiguration;
160169
}
161170
}
162171

163-
164172
internal void ExecuteCommand()
165173
{
166174
ValidateParameters();
@@ -170,10 +178,11 @@ internal void ExecuteCommand()
170178
}
171179

172180
protected override void ValidateParameters()
173-
{
181+
{
174182
base.ValidateParameters();
175183
ExtensionName = DiagnosticsExtensionType;
176184
Publisher = DiagnosticsExtensionNamespace;
185+
Version = Version ?? DefaultVersion;
177186

178187
// If the user didn't specify an extension reference name and the input VM already has a diagnostics extension,
179188
// reuse its reference name
@@ -185,26 +194,44 @@ protected override void ValidateParameters()
185194
ReferenceName = diagnosticsExtension.ReferenceName;
186195
}
187196
}
197+
198+
ValidateStorageAccountName();
199+
ValidateStorageAccountKey();
200+
ValidateStorageAccountEndpoint();
188201
}
189202

190-
protected string GetStorageKey()
203+
private void ValidateStorageAccountName()
191204
{
192-
string storageKey = string.Empty;
205+
this.StorageAccountName = this.StorageAccountName ??
206+
DiagnosticsHelper.InitializeStorageAccountName(this.StorageContext, this.DiagnosticsConfigurationPath);
193207

194-
if (!string.IsNullOrEmpty(StorageAccountName))
208+
if (string.IsNullOrEmpty(this.StorageAccountName))
195209
{
196-
var storageAccount = this.StorageClient.StorageAccounts.Get(StorageAccountName);
197-
if (storageAccount != null)
198-
{
199-
var keys = this.StorageClient.StorageAccounts.GetKeys(StorageAccountName);
200-
if (keys != null)
201-
{
202-
storageKey = !string.IsNullOrEmpty(keys.PrimaryKey) ? keys.PrimaryKey : keys.SecondaryKey;
203-
}
204-
}
210+
throw new ArgumentException(Resources.DiagnosticsExtensionNullStorageAccountName);
205211
}
212+
}
206213

207-
return storageKey;
214+
private void ValidateStorageAccountKey()
215+
{
216+
this.StorageAccountKey = this.StorageAccountKey ??
217+
DiagnosticsHelper.InitializeStorageAccountKey(this.StorageClient, this.StorageAccountName, this.DiagnosticsConfigurationPath);
218+
219+
if (string.IsNullOrEmpty(this.StorageAccountKey))
220+
{
221+
throw new ArgumentException(Resources.DiagnosticsExtensionNullStorageAccountKey);
222+
}
223+
}
224+
225+
private void ValidateStorageAccountEndpoint()
226+
{
227+
this.StorageAccountEndpoint = this.StorageAccountEndpoint ??
228+
DiagnosticsHelper.InitializeStorageAccountEndpoint(this.StorageAccountName, this.StorageAccountKey, this.StorageClient,
229+
this.StorageContext, this.DiagnosticsConfigurationPath, this.DefaultContext);
230+
231+
if (string.IsNullOrEmpty(this.StorageAccountEndpoint))
232+
{
233+
throw new ArgumentNullException(Resources.DiagnosticsExtensionNullStorageAccountEndpoint);
234+
}
208235
}
209236

210237
protected override void ProcessRecord()

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/Diagnostics/VirtualMachineDiagnosticsExtensionCmdletBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class VirtualMachineDiagnosticsExtensionCmdletBase : VirtualMachineExtens
1919
protected const string DiagnosticsExtensionNamespace = "Microsoft.Azure.Diagnostics";
2020
protected const string DiagnosticsExtensionType = "IaaSDiagnostics";
2121
protected const string VirtualMachineDiagnosticsExtensionNoun = "AzureVMDiagnosticsExtension";
22+
protected const string DefaultVersion = "1.*";
23+
protected const string XmlNamespace = "http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration";
2224

2325
public VirtualMachineDiagnosticsExtensionCmdletBase()
2426
: base()

0 commit comments

Comments
 (0)