Skip to content

Commit c2a6a12

Browse files
authored
[AzureRG Deploy] Parsing the override parameter based on the type (#3820)
1 parent 76f54a3 commit c2a6a12

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

Tasks/AzureResourceGroupDeployment/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"loc.messages.CreatedRG": "Resource Group created successfully.",
4646
"loc.messages.CreatingTemplateDeployment": "Creating deployment parameters.",
4747
"loc.messages.TemplateParsingFailed": "Ensure the Template file is valid. Task failed while parsing with following error: %s",
48-
"loc.messages.ParametersFileFetchFailed": "Failed to download the parameters file. Error: %s",
48+
"loc.messages.FileFetchFailed": "Failed to download the file. URL: '%s'. Error: %s",
4949
"loc.messages.ParametersFileParsingFailed": "Ensure the Parameters file is valid. Task failed while parsing with following error: %s",
5050
"loc.messages.StartingDeployment": "Starting Deployment.",
5151
"loc.messages.CreateTemplateDeploymentSucceeded": "Successfully deployed the template.",
@@ -160,5 +160,6 @@
160160
"loc.messages.TimeoutWhileWaiting": "Timed out while waiting",
161161
"loc.messages.InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
162162
"loc.messages.EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
163-
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
163+
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
164+
"loc.messages.ErrorWhileParsingParameter": "There was an error while overriding '%s' parameter because of '%s'."
164165
}

Tasks/AzureResourceGroupDeployment/operations/ResourceGroup.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,39 @@ export class ResourceGroup {
146146
return depName;
147147
}
148148

149-
private updateOverrideParameters(parameters: Object): Object {
149+
private castToType(value: string, type: string) {
150+
switch (type) {
151+
case "int":
152+
return parseInt(value);
153+
case "object":
154+
return JSON.parse(value);
155+
case "secureObject":
156+
return JSON.parse(value);
157+
case "array":
158+
return JSON.parse(value);
159+
case "bool":
160+
return value === "true";
161+
default:
162+
// Sending as string
163+
break;
164+
}
165+
return value;
166+
}
167+
168+
private updateOverrideParameters(template: Object, parameters: Object): Object {
150169
tl.debug("Overriding Parameters..");
151170

152-
var override = parameterParser(this.taskParameters.overrideParameters);
153-
for (var key in override) {
171+
var overrideParameters = parameterParser(this.taskParameters.overrideParameters);
172+
for (var key in overrideParameters) {
154173
tl.debug("Overriding key: " + key);
155-
parameters[key] = override[key];
156-
}
174+
try {
175+
overrideParameters[key]["value"] = this.castToType(overrideParameters[key]["value"], template["parameters"][key]["type"]);
176+
} catch (error) {
177+
tl.debug(tl.loc("ErrorWhileParsingParameter", key, error.toString()));
178+
}
179+
parameters[key] = overrideParameters[key];
157180

181+
}
158182
return parameters;
159183
}
160184

@@ -171,17 +195,17 @@ export class ResourceGroup {
171195
});
172196
}
173197

174-
private downloadParametersFile(url): Promise<string> {
198+
private downloadFile(url): Promise<string> {
175199
return new Promise<string>((resolve, reject) => {
176200
httpObj.get("GET", url, {}, (error, result, contents) => {
177201
if (error) {
178-
return reject(tl.loc("ParametersFileFetchFailed", error));
202+
return reject(tl.loc("FileFetchFailed", url, error));
179203
}
180204
if (result.statusCode === 200)
181205
resolve(contents);
182206
else {
183207
var errorMessage = result.statusCode.toString() + ": " + result.statusMessage;
184-
return reject(tl.loc("ParametersFileFetchFailed", errorMessage));
208+
return reject(tl.loc("FileFetchFailed", url, errorMessage));
185209
}
186210
});
187211
});
@@ -214,7 +238,7 @@ export class ResourceGroup {
214238
}
215239

216240
if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
217-
parameters = this.updateOverrideParameters(parameters);
241+
parameters = this.updateOverrideParameters(template, parameters);
218242
}
219243

220244
var deployment = new Deployment({
@@ -235,7 +259,7 @@ export class ResourceGroup {
235259

236260
if (utils.isNonEmpty(this.taskParameters.csmParametersFileLink)) {
237261
if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
238-
var contents = await this.downloadParametersFile(this.taskParameters.csmParametersFileLink)
262+
var contents = await this.downloadFile(this.taskParameters.csmParametersFileLink);
239263
parameters = JSON.parse(contents).parameters;
240264
}
241265
else {
@@ -246,7 +270,17 @@ export class ResourceGroup {
246270
}
247271

248272
if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
249-
parameters = this.updateOverrideParameters(parameters);
273+
tl.debug("Downloading CSM Template File.. " + this.taskParameters.csmFileLink);
274+
var templateFile = await this.downloadFile(this.taskParameters.csmFileLink);
275+
var template;
276+
try {
277+
var template = JSON.parse(templateFile);
278+
tl.debug("Loaded CSM File");
279+
}
280+
catch (error) {
281+
throw (tl.loc("TemplateParsingFailed", utils.getError(error.message)));
282+
}
283+
parameters = this.updateOverrideParameters(template, parameters);
250284
deployment.properties["parameters"] = parameters;
251285
}
252286

Tasks/AzureResourceGroupDeployment/task.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"version": {
1515
"Major": 2,
1616
"Minor": 0,
17-
"Patch": 4
17+
"Patch": 5
1818
},
1919
"demands": [],
2020
"minimumAgentVersion": "2.0.0",
@@ -254,7 +254,7 @@
254254
"CreatedRG": "Resource Group created successfully.",
255255
"CreatingTemplateDeployment": "Creating deployment parameters.",
256256
"TemplateParsingFailed": "Ensure the Template file is valid. Task failed while parsing with following error: %s",
257-
"ParametersFileFetchFailed": "Failed to download the parameters file. Error: %s",
257+
"FileFetchFailed": "Failed to download the file. URL: '%s'. Error: %s",
258258
"ParametersFileParsingFailed": "Ensure the Parameters file is valid. Task failed while parsing with following error: %s",
259259
"StartingDeployment": "Starting Deployment.",
260260
"CreateTemplateDeploymentSucceeded": "Successfully deployed the template.",
@@ -369,6 +369,7 @@
369369
"TimeoutWhileWaiting": "Timed out while waiting",
370370
"InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
371371
"EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
372-
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
372+
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
373+
"ErrorWhileParsingParameter": "There was an error while overriding '%s' parameter because of '%s'."
373374
}
374375
}

Tasks/AzureResourceGroupDeployment/task.loc.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"version": {
1515
"Major": 2,
1616
"Minor": 0,
17-
"Patch": 4
17+
"Patch": 5
1818
},
1919
"demands": [],
2020
"minimumAgentVersion": "2.0.0",
@@ -254,7 +254,7 @@
254254
"CreatedRG": "ms-resource:loc.messages.CreatedRG",
255255
"CreatingTemplateDeployment": "ms-resource:loc.messages.CreatingTemplateDeployment",
256256
"TemplateParsingFailed": "ms-resource:loc.messages.TemplateParsingFailed",
257-
"ParametersFileFetchFailed": "ms-resource:loc.messages.ParametersFileFetchFailed",
257+
"FileFetchFailed": "ms-resource:loc.messages.FileFetchFailed",
258258
"ParametersFileParsingFailed": "ms-resource:loc.messages.ParametersFileParsingFailed",
259259
"StartingDeployment": "ms-resource:loc.messages.StartingDeployment",
260260
"CreateTemplateDeploymentSucceeded": "ms-resource:loc.messages.CreateTemplateDeploymentSucceeded",
@@ -369,6 +369,7 @@
369369
"TimeoutWhileWaiting": "ms-resource:loc.messages.TimeoutWhileWaiting",
370370
"InvalidTemplateLocation": "ms-resource:loc.messages.InvalidTemplateLocation",
371371
"EncodingNotSupported": "ms-resource:loc.messages.EncodingNotSupported",
372-
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding"
372+
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding",
373+
"ErrorWhileParsingParameter": "ms-resource:loc.messages.ErrorWhileParsingParameter"
373374
}
374375
}

0 commit comments

Comments
 (0)