12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using Microsoft . Azure . Commands . Common . Authentication ;
16
15
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkModels ;
17
16
using Microsoft . WindowsAzure . Commands . Utilities . Common ;
18
17
using Newtonsoft . Json ;
19
18
using ProjectResources = Microsoft . Azure . Commands . ResourceManager . Cmdlets . Properties . Resources ;
19
+ using Microsoft . Azure . Management . Resources . Models ;
20
+ using System . Net ;
20
21
using System ;
21
22
using System . Collections ;
22
23
using System . Collections . Generic ;
25
26
using System . Management . Automation ;
26
27
using System . Diagnostics ;
27
28
using System . Security ;
28
- using Microsoft . WindowsAzure . Commands . Common ;
29
29
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
30
30
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Extensions ;
31
31
using Newtonsoft . Json . Linq ;
32
- using System . Threading ;
32
+ using Microsoft . Azure . Commands . ResourceManager . Cmdlets . Components ;
33
+ using Microsoft . Azure . Management . Resources ;
33
34
34
35
namespace Microsoft . Azure . Commands . ResourceManager . Cmdlets . Utilities
35
36
{
36
37
public static class TemplateUtility
37
38
{
38
- /// <summary>
39
- /// Gets the parameters for a given template file.
40
- /// </summary>
41
- /// <param name="templateFilePath">The gallery template path (local or remote)</param>
42
- /// <param name="templateParameterObject">Existing template parameter object</param>
43
- /// <param name="templateParameterFilePath">Path to the template parameter file if present</param>
44
- /// <param name="staticParameters">The existing PowerShell cmdlet parameters</param>
45
- /// <returns>The template parameters</returns>
46
- public static RuntimeDefinedParameterDictionary GetTemplateParametersFromFile ( string templateFilePath , Hashtable templateParameterObject , string templateParameterFilePath , string [ ] staticParameters )
39
+
40
+ public static string ExtractTemplateContent (
41
+ string templateFile ,
42
+ string templateUri ,
43
+ string templateSpecId ,
44
+ ITemplateSpecsClient templateSpecsClient ,
45
+ Hashtable templateObject
46
+ )
47
47
{
48
48
string templateContent = null ;
49
+ if ( templateObject != null )
50
+ {
51
+ templateContent = GetTemplateContentFromHashtable ( templateObject ) ;
52
+ }
53
+ else if ( ! string . IsNullOrEmpty ( templateFile ) || ! string . IsNullOrEmpty ( templateUri ) )
54
+ {
55
+ var file = ! string . IsNullOrEmpty ( templateFile ) ? templateFile : templateUri ;
56
+ templateContent = GetTemplateContentFromFile ( file ) ;
57
+ }
58
+ else if ( ! string . IsNullOrEmpty ( templateSpecId ) )
59
+ {
60
+ templateContent = GetTemplateContentFromTemplateSpec ( templateSpecId , templateSpecsClient ) ;
61
+ }
62
+
63
+ return templateContent ;
64
+ }
65
+
66
+ public static Hashtable ExtractTemplateParameterContent (
67
+ string templateParameterFile ,
68
+ string templateParameterUri
69
+ )
70
+ {
71
+ Hashtable templateParameterContent = null ;
72
+ if ( ! string . IsNullOrEmpty ( templateParameterFile ) || ! string . IsNullOrEmpty ( templateParameterUri ) )
73
+ {
74
+ var file = ! string . IsNullOrEmpty ( templateParameterFile ) ? templateParameterFile : templateParameterUri ;
75
+ templateParameterContent = GetTemplateParameterContentFromFile ( file ) ;
76
+ }
49
77
78
+ return templateParameterContent ;
79
+ }
80
+
81
+ private static string GetTemplateContentFromFile ( string templateFilePath )
82
+ {
83
+ string templateContent = null ;
50
84
if ( templateFilePath != null )
51
85
{
52
86
if ( Uri . IsWellFormedUriString ( templateFilePath , UriKind . Absolute ) )
@@ -59,27 +93,80 @@ public static RuntimeDefinedParameterDictionary GetTemplateParametersFromFile(st
59
93
}
60
94
}
61
95
62
- RuntimeDefinedParameterDictionary dynamicParameters = ParseTemplateAndExtractParameters ( templateContent , templateParameterObject , templateParameterFilePath , staticParameters ) ;
63
-
64
- return dynamicParameters ;
65
- }
66
-
67
- public static RuntimeDefinedParameterDictionary GetTemplateParametersFromFile ( object template , Hashtable templateParameterObject , string templateParameterFilePath , string [ ] staticParameters )
68
- {
69
- return ParseTemplateAndExtractParameters ( template . ToString ( ) , templateParameterObject , templateParameterFilePath , staticParameters ) ;
96
+ return templateContent ;
70
97
}
71
98
72
- public static RuntimeDefinedParameterDictionary GetTemplateParametersFromFile ( Hashtable templateObject , Hashtable templateParameterObject , string templateParameterFilePath , string [ ] staticParameters )
99
+ private static string GetTemplateContentFromHashtable ( Hashtable templateObject )
73
100
{
74
101
string templateContent = null ;
75
102
if ( templateObject != null )
76
103
{
77
104
templateContent = JsonConvert . SerializeObject ( templateObject ) ;
78
105
}
106
+
107
+ return templateContent ;
108
+ }
79
109
80
- RuntimeDefinedParameterDictionary dynamicParameters = ParseTemplateAndExtractParameters ( templateContent , templateParameterObject , templateParameterFilePath , staticParameters ) ;
110
+ private static string GetTemplateContentFromTemplateSpec ( string templateSpecId , ITemplateSpecsClient client )
111
+ {
112
+ ResourceIdentifier resourceIdentifier = new ResourceIdentifier ( templateSpecId ) ;
113
+ if ( ! resourceIdentifier . ResourceType . Equals ( "Microsoft.Resources/templateSpecs/versions" , StringComparison . OrdinalIgnoreCase ) )
114
+ {
115
+ throw new PSArgumentException ( "No version found in Resource ID" ) ;
116
+ }
81
117
82
- return dynamicParameters ;
118
+ if ( ! string . IsNullOrEmpty ( resourceIdentifier . Subscription ) &&
119
+ client . SubscriptionId != resourceIdentifier . Subscription )
120
+ {
121
+ // The template spec is in a different subscription than our default
122
+ // context. Force the client to use that subscription:
123
+ client . SubscriptionId = resourceIdentifier . Subscription ;
124
+ }
125
+ try
126
+ {
127
+ var templateSpecVersion = client . TemplateSpecVersions . Get (
128
+ ResourceIdUtility . GetResourceGroupName ( templateSpecId ) ,
129
+ ResourceIdUtility . GetResourceName ( templateSpecId ) . Split ( '/' ) [ 0 ] ,
130
+ resourceIdentifier . ResourceName ) ;
131
+
132
+ if ( ! ( templateSpecVersion . MainTemplate is JObject ) )
133
+ {
134
+ throw new InvalidOperationException ( "Unexpected type." ) ; // Sanity check
135
+ }
136
+ var templateObj = ( JObject ) templateSpecVersion . MainTemplate ;
137
+
138
+ return templateObj . ToString ( ) ;
139
+ }
140
+ catch ( TemplateSpecsErrorException e )
141
+ {
142
+ // If the templateSpec resourceID is pointing to a non existant resource
143
+ if ( ! e . Response . StatusCode . Equals ( HttpStatusCode . NotFound ) )
144
+ {
145
+ // Throw for any other error that is not due to a 404 for the template resource.
146
+ throw ;
147
+ }
148
+
149
+ return null ;
150
+ }
151
+ }
152
+
153
+ private static Hashtable GetTemplateParameterContentFromFile ( string templateParameterFilePath )
154
+ {
155
+ Hashtable templateParameterContent = null ;
156
+
157
+ if ( templateParameterFilePath != null )
158
+ {
159
+ if ( Uri . IsWellFormedUriString ( templateParameterFilePath , UriKind . Absolute ) )
160
+ {
161
+ templateParameterContent = new Hashtable ( ParseTemplateParameterContent ( templateParameterFilePath ) ) ;
162
+ }
163
+ else if ( FileUtilities . DataStore . FileExists ( templateParameterFilePath ) )
164
+ {
165
+ templateParameterContent = new Hashtable ( ParseTemplateParameterFileContents ( templateParameterFilePath ) ) ;
166
+ }
167
+ }
168
+
169
+ return templateParameterContent ;
83
170
}
84
171
85
172
public static Dictionary < string , TemplateParameterFileParameter > ParseTemplateParameterFileContents ( string templateParameterFilePath )
@@ -146,14 +233,14 @@ public static Dictionary<string, TemplateParameterFileParameter> ParseTemplatePa
146
233
return parameters ;
147
234
}
148
235
149
- private static RuntimeDefinedParameterDictionary ParseTemplateAndExtractParameters ( string templateContent , Hashtable templateParameterObject , string templateParameterFilePath , string [ ] staticParameters )
236
+ public static RuntimeDefinedParameterDictionary GetDynamicParameters ( string templateContent , Hashtable templateParameterObject , string [ ] staticParameters )
150
237
{
151
238
RuntimeDefinedParameterDictionary dynamicParameters = new RuntimeDefinedParameterDictionary ( ) ;
152
239
240
+ // If the template content is not null, parameters should be extracted into dynamic parameters:
153
241
if ( ! string . IsNullOrEmpty ( templateContent ) )
154
242
{
155
- TemplateFile templateFile = null ;
156
-
243
+ TemplateFile templateFile ;
157
244
try
158
245
{
159
246
templateFile = templateContent . FromJson < TemplateFile > ( ) ;
@@ -175,41 +262,34 @@ private static RuntimeDefinedParameterDictionary ParseTemplateAndExtractParamete
175
262
dynamicParameters . Add ( dynamicParameter . Name , dynamicParameter ) ;
176
263
}
177
264
}
265
+
178
266
if ( templateParameterObject != null )
179
267
{
180
- UpdateParametersWithObject ( staticParameters , dynamicParameters , templateParameterObject ) ;
181
- }
182
- if ( templateParameterFilePath != null && FileUtilities . DataStore . FileExists ( templateParameterFilePath ) )
183
- {
184
- var parametersFromFile = ParseTemplateParameterFileContents ( templateParameterFilePath ) ;
185
- UpdateParametersWithObject ( staticParameters , dynamicParameters , new Hashtable ( parametersFromFile ) ) ;
186
- }
187
- if ( templateParameterFilePath != null && Uri . IsWellFormedUriString ( templateParameterFilePath , UriKind . Absolute ) )
188
- {
189
- var parametersFromUri = ParseTemplateParameterContent ( GeneralUtilities . DownloadFile ( templateParameterFilePath ) ) ;
190
- UpdateParametersWithObject ( staticParameters , dynamicParameters , new Hashtable ( parametersFromUri ) ) ;
268
+ SetDynamicParametersPassedInTemplateParameterObject ( staticParameters , dynamicParameters , templateParameterObject ) ;
191
269
}
270
+
192
271
return dynamicParameters ;
193
272
}
194
273
195
- private static void UpdateParametersWithObject ( string [ ] staticParameters , RuntimeDefinedParameterDictionary dynamicParameters , Hashtable templateParameterObject )
274
+ /// <summary>
275
+ /// Sets the dynamic parameters that are defined in the passed in template parameter object, so that the user is not prompted on execution for a value.
276
+ /// </summary>
277
+ private static void SetDynamicParametersPassedInTemplateParameterObject ( string [ ] staticParameters , RuntimeDefinedParameterDictionary dynamicParameters , Hashtable templateParameterObject )
196
278
{
197
279
const string duplicatedParameterSuffix = "FromTemplate" ;
198
280
199
281
if ( templateParameterObject != null )
200
282
{
201
283
foreach ( string paramName in templateParameterObject . Keys )
202
284
{
285
+ // The template parameters that clash with static parameter names will receive a suffix on their respective dynamic parameter:
203
286
string dynamicParamName = staticParameters . Contains ( paramName , StringComparer . OrdinalIgnoreCase )
204
287
? paramName + duplicatedParameterSuffix
205
288
: paramName ;
206
289
207
290
if ( dynamicParameters . TryGetValue ( dynamicParamName , out RuntimeDefinedParameter dynamicParameter ) )
208
291
{
209
- dynamicParameter . Value = templateParameterObject [ paramName ] is TemplateParameterFileParameter TemplateFileParameter
210
- ? TemplateFileParameter . Value
211
- : templateParameterObject [ paramName ] ;
212
-
292
+ // Param exists in the template parameter object, so set it:
213
293
dynamicParameter . IsSet = true ;
214
294
( ( ParameterAttribute ) dynamicParameter . Attributes [ 0 ] ) . Mandatory = false ;
215
295
}
0 commit comments