12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
15
16
using System . Linq ;
16
17
using System . Management . Automation ;
17
18
using Microsoft . WindowsAzure . Commands . Common . Storage ;
18
19
using Microsoft . WindowsAzure . Commands . ServiceManagement . Common ;
19
20
using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
20
- using Microsoft . WindowsAzure . Management . Storage ;
21
+ using Microsoft . WindowsAzure . Commands . ServiceManagement . Properties ;
21
22
22
23
namespace Microsoft . WindowsAzure . Commands . ServiceManagement . IaaS . Extensions
23
24
{
@@ -31,7 +32,6 @@ public class SetAzureVMDiagnosticsExtensionCommand : VirtualMachineDiagnosticsEx
31
32
{
32
33
private string publicConfiguration ;
33
34
private string privateConfiguration ;
34
- private string storageKey ;
35
35
protected const string SetExtParamSetName = "SetDiagnosticsExtension" ;
36
36
protected const string SetExtRefParamSetName = "SetDiagnosticsWithReferenceExtension" ;
37
37
@@ -57,81 +57,90 @@ public string DiagnosticsConfigurationPath
57
57
[ Parameter ( ParameterSetName = SetExtParamSetName ,
58
58
Position = 1 ,
59
59
ValueFromPipelineByPropertyName = true ,
60
- Mandatory = true ,
61
- HelpMessage = "The storage connection context" ) ]
60
+ HelpMessage = "The storage account name" ) ]
62
61
[ Parameter ( ParameterSetName = SetExtRefParamSetName ,
63
62
Position = 1 ,
64
63
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 ,
66
106
HelpMessage = "The storage connection context" ) ]
67
- [ ValidateNotNullOrEmpty ]
68
107
public AzureStorageContext StorageContext
69
108
{
70
109
get ;
71
110
set ;
72
111
}
73
-
74
112
75
113
[ Parameter (
76
114
ParameterSetName = SetExtParamSetName ,
77
- Position = 2 ,
115
+ Position = 5 ,
78
116
ValueFromPipelineByPropertyName = false ,
79
117
HelpMessage = "WAD Version" ) ]
80
118
[ Parameter (
81
119
ParameterSetName = SetExtRefParamSetName ,
82
- Position = 2 ,
120
+ Position = 5 ,
83
121
ValueFromPipelineByPropertyName = false ,
84
122
HelpMessage = "WAD Version" ) ]
85
123
public override string Version { get ; set ; }
86
124
87
125
[ Parameter (
88
126
ParameterSetName = SetExtParamSetName ,
89
- Position = 3 ,
127
+ Position = 6 ,
90
128
ValueFromPipelineByPropertyName = true ,
91
129
HelpMessage = "To Set the Extension State to 'Disable'." ) ]
92
130
[ Parameter (
93
131
ParameterSetName = SetExtRefParamSetName ,
94
- Position = 3 ,
132
+ Position = 6 ,
95
133
ValueFromPipelineByPropertyName = true ,
96
134
HelpMessage = "To Set the Extension State to 'Disable'." ) ]
97
135
public override SwitchParameter Disable { get ; set ; }
98
136
99
137
[ Parameter (
100
138
ParameterSetName = SetExtRefParamSetName ,
101
- Position = 4 ,
139
+ Position = 7 ,
102
140
ValueFromPipelineByPropertyName = true ,
103
141
HelpMessage = "To specify the reference name." ) ]
104
142
public override string ReferenceName { get ; set ; }
105
143
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
-
135
144
public override string PublicConfiguration
136
145
{
137
146
get
@@ -152,15 +161,14 @@ public override string PrivateConfiguration
152
161
{
153
162
if ( string . IsNullOrEmpty ( this . privateConfiguration ) )
154
163
{
155
- this . privateConfiguration = DiagnosticsHelper . GetJsonSerializedPrivateDiagnosticsConfiguration ( this . StorageAccountName , this . StorageKey ,
156
- this . Endpoint ) ;
164
+ this . privateConfiguration = DiagnosticsHelper . GetJsonSerializedPrivateDiagnosticsConfiguration ( this . StorageAccountName , this . StorageAccountKey ,
165
+ this . StorageAccountEndpoint ) ;
157
166
}
158
167
159
168
return this . privateConfiguration ;
160
169
}
161
170
}
162
171
163
-
164
172
internal void ExecuteCommand ( )
165
173
{
166
174
ValidateParameters ( ) ;
@@ -170,10 +178,11 @@ internal void ExecuteCommand()
170
178
}
171
179
172
180
protected override void ValidateParameters ( )
173
- {
181
+ {
174
182
base . ValidateParameters ( ) ;
175
183
ExtensionName = DiagnosticsExtensionType ;
176
184
Publisher = DiagnosticsExtensionNamespace ;
185
+ Version = Version ?? DefaultVersion ;
177
186
178
187
// If the user didn't specify an extension reference name and the input VM already has a diagnostics extension,
179
188
// reuse its reference name
@@ -185,26 +194,44 @@ protected override void ValidateParameters()
185
194
ReferenceName = diagnosticsExtension . ReferenceName ;
186
195
}
187
196
}
197
+
198
+ ValidateStorageAccountName ( ) ;
199
+ ValidateStorageAccountKey ( ) ;
200
+ ValidateStorageAccountEndpoint ( ) ;
188
201
}
189
202
190
- protected string GetStorageKey ( )
203
+ private void ValidateStorageAccountName ( )
191
204
{
192
- string storageKey = string . Empty ;
205
+ this . StorageAccountName = this . StorageAccountName ??
206
+ DiagnosticsHelper . InitializeStorageAccountName ( this . StorageContext , this . DiagnosticsConfigurationPath ) ;
193
207
194
- if ( ! string . IsNullOrEmpty ( StorageAccountName ) )
208
+ if ( string . IsNullOrEmpty ( this . StorageAccountName ) )
195
209
{
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 ) ;
205
211
}
212
+ }
206
213
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
+ }
208
235
}
209
236
210
237
protected override void ProcessRecord ( )
0 commit comments