Skip to content

Commit

Permalink
[AzureRG Deploy] Parsing the override parameter based on the type (#3820
Browse files Browse the repository at this point in the history
)
  • Loading branch information
thesattiraju authored Mar 21, 2017
1 parent 76f54a3 commit c2a6a12
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"loc.messages.CreatedRG": "Resource Group created successfully.",
"loc.messages.CreatingTemplateDeployment": "Creating deployment parameters.",
"loc.messages.TemplateParsingFailed": "Ensure the Template file is valid. Task failed while parsing with following error: %s",
"loc.messages.ParametersFileFetchFailed": "Failed to download the parameters file. Error: %s",
"loc.messages.FileFetchFailed": "Failed to download the file. URL: '%s'. Error: %s",
"loc.messages.ParametersFileParsingFailed": "Ensure the Parameters file is valid. Task failed while parsing with following error: %s",
"loc.messages.StartingDeployment": "Starting Deployment.",
"loc.messages.CreateTemplateDeploymentSucceeded": "Successfully deployed the template.",
Expand Down Expand Up @@ -160,5 +160,6 @@
"loc.messages.TimeoutWhileWaiting": "Timed out while waiting",
"loc.messages.InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
"loc.messages.EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
"loc.messages.CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
"loc.messages.ErrorWhileParsingParameter": "There was an error while overriding '%s' parameter because of '%s'."
}
56 changes: 45 additions & 11 deletions Tasks/AzureResourceGroupDeployment/operations/ResourceGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,39 @@ export class ResourceGroup {
return depName;
}

private updateOverrideParameters(parameters: Object): Object {
private castToType(value: string, type: string) {
switch (type) {
case "int":
return parseInt(value);
case "object":
return JSON.parse(value);
case "secureObject":
return JSON.parse(value);
case "array":
return JSON.parse(value);
case "bool":
return value === "true";
default:
// Sending as string
break;
}
return value;
}

private updateOverrideParameters(template: Object, parameters: Object): Object {
tl.debug("Overriding Parameters..");

var override = parameterParser(this.taskParameters.overrideParameters);
for (var key in override) {
var overrideParameters = parameterParser(this.taskParameters.overrideParameters);
for (var key in overrideParameters) {
tl.debug("Overriding key: " + key);
parameters[key] = override[key];
}
try {
overrideParameters[key]["value"] = this.castToType(overrideParameters[key]["value"], template["parameters"][key]["type"]);
} catch (error) {
tl.debug(tl.loc("ErrorWhileParsingParameter", key, error.toString()));
}
parameters[key] = overrideParameters[key];

}
return parameters;
}

Expand All @@ -171,17 +195,17 @@ export class ResourceGroup {
});
}

private downloadParametersFile(url): Promise<string> {
private downloadFile(url): Promise<string> {
return new Promise<string>((resolve, reject) => {
httpObj.get("GET", url, {}, (error, result, contents) => {
if (error) {
return reject(tl.loc("ParametersFileFetchFailed", error));
return reject(tl.loc("FileFetchFailed", url, error));
}
if (result.statusCode === 200)
resolve(contents);
else {
var errorMessage = result.statusCode.toString() + ": " + result.statusMessage;
return reject(tl.loc("ParametersFileFetchFailed", errorMessage));
return reject(tl.loc("FileFetchFailed", url, errorMessage));
}
});
});
Expand Down Expand Up @@ -214,7 +238,7 @@ export class ResourceGroup {
}

if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
parameters = this.updateOverrideParameters(parameters);
parameters = this.updateOverrideParameters(template, parameters);
}

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

if (utils.isNonEmpty(this.taskParameters.csmParametersFileLink)) {
if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
var contents = await this.downloadParametersFile(this.taskParameters.csmParametersFileLink)
var contents = await this.downloadFile(this.taskParameters.csmParametersFileLink);
parameters = JSON.parse(contents).parameters;
}
else {
Expand All @@ -246,7 +270,17 @@ export class ResourceGroup {
}

if (utils.isNonEmpty(this.taskParameters.overrideParameters)) {
parameters = this.updateOverrideParameters(parameters);
tl.debug("Downloading CSM Template File.. " + this.taskParameters.csmFileLink);
var templateFile = await this.downloadFile(this.taskParameters.csmFileLink);
var template;
try {
var template = JSON.parse(templateFile);
tl.debug("Loaded CSM File");
}
catch (error) {
throw (tl.loc("TemplateParsingFailed", utils.getError(error.message)));
}
parameters = this.updateOverrideParameters(template, parameters);
deployment.properties["parameters"] = parameters;
}

Expand Down
7 changes: 4 additions & 3 deletions Tasks/AzureResourceGroupDeployment/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 4
"Patch": 5
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -254,7 +254,7 @@
"CreatedRG": "Resource Group created successfully.",
"CreatingTemplateDeployment": "Creating deployment parameters.",
"TemplateParsingFailed": "Ensure the Template file is valid. Task failed while parsing with following error: %s",
"ParametersFileFetchFailed": "Failed to download the parameters file. Error: %s",
"FileFetchFailed": "Failed to download the file. URL: '%s'. Error: %s",
"ParametersFileParsingFailed": "Ensure the Parameters file is valid. Task failed while parsing with following error: %s",
"StartingDeployment": "Starting Deployment.",
"CreateTemplateDeploymentSucceeded": "Successfully deployed the template.",
Expand Down Expand Up @@ -369,6 +369,7 @@
"TimeoutWhileWaiting": "Timed out while waiting",
"InvalidTemplateLocation": "The template location supplied is invalid. Task only supports 'Linked artifact' or 'URL of the file'",
"EncodingNotSupported": "Encoding of the file '%s' is '%s' which is not supported. Supported encodings are ['utf-8', 'utf-16le']",
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'"
"CouldNotDetectEncoding": "Could not detect encoding of file '%s'",
"ErrorWhileParsingParameter": "There was an error while overriding '%s' parameter because of '%s'."
}
}
7 changes: 4 additions & 3 deletions Tasks/AzureResourceGroupDeployment/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 4
"Patch": 5
},
"demands": [],
"minimumAgentVersion": "2.0.0",
Expand Down Expand Up @@ -254,7 +254,7 @@
"CreatedRG": "ms-resource:loc.messages.CreatedRG",
"CreatingTemplateDeployment": "ms-resource:loc.messages.CreatingTemplateDeployment",
"TemplateParsingFailed": "ms-resource:loc.messages.TemplateParsingFailed",
"ParametersFileFetchFailed": "ms-resource:loc.messages.ParametersFileFetchFailed",
"FileFetchFailed": "ms-resource:loc.messages.FileFetchFailed",
"ParametersFileParsingFailed": "ms-resource:loc.messages.ParametersFileParsingFailed",
"StartingDeployment": "ms-resource:loc.messages.StartingDeployment",
"CreateTemplateDeploymentSucceeded": "ms-resource:loc.messages.CreateTemplateDeploymentSucceeded",
Expand Down Expand Up @@ -369,6 +369,7 @@
"TimeoutWhileWaiting": "ms-resource:loc.messages.TimeoutWhileWaiting",
"InvalidTemplateLocation": "ms-resource:loc.messages.InvalidTemplateLocation",
"EncodingNotSupported": "ms-resource:loc.messages.EncodingNotSupported",
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding"
"CouldNotDetectEncoding": "ms-resource:loc.messages.CouldNotDetectEncoding",
"ErrorWhileParsingParameter": "ms-resource:loc.messages.ErrorWhileParsingParameter"
}
}

0 comments on commit c2a6a12

Please sign in to comment.