Skip to content

Commit

Permalink
Setting up Azure Active cloud corresponding to service endpoint (#9003)
Browse files Browse the repository at this point in the history
* Setting up Azure Active cloud corresponding to service endpoint

* Fix review comment
  • Loading branch information
vineetmimrot authored Dec 5, 2018
1 parent e0b8867 commit 936033a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"loc.input.help.args": "Arguments passed to the script",
"loc.input.label.addSpnToEnvironment": "Access service principal details in script",
"loc.input.help.addSpnToEnvironment": "Adds service principal id and key of the Azure endpoint you chose to the script's execution environment. You can use these variables: `$servicePrincipalId` and `$servicePrincipalKey` in your script",
"loc.input.label.useGlobalConfig": "Use global Azure CLI configuration",
"loc.input.help.useGlobalConfig": "If this is false, this task will use its own separate [Azure CLI configuration directory](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases",
"loc.input.label.cwd": "Working Directory",
"loc.input.help.cwd": "Current working directory where the script is run. Empty is the root of the repo (build) or artifacts (release), which is $(System.DefaultWorkingDirectory)",
"loc.input.label.useGlobalConfig": "Use global Azure CLI configuration",
"loc.input.help.useGlobalConfig": "If this is false, this task will use its own separate [*AzCLI configuration directory*](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases",
"loc.input.label.failOnStandardError": "Fail on Standard Error",
"loc.input.help.failOnStandardError": "If this is true, this task will fail when any errors are written to the StandardError stream. Unselect the checkbox to ignore standard errors and rely on exit codes to determine the status",
"loc.messages.ScriptReturnCode": "Script exited with return code: %d",
Expand All @@ -30,5 +30,6 @@
"loc.messages.FailedToLogout": "The following error occurred while logging out: %s",
"loc.messages.LoginFailed": "Azure login failed",
"loc.messages.ErrorInSettingUpSubscription": "Error in setting up subscription",
"loc.messages.SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s"
"loc.messages.SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s",
"loc.messages.SettingAzureCloud": "Setting active cloud to: %s"
}
19 changes: 13 additions & 6 deletions Tasks/AzureCLIV1/azureclitask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export class azureclitask {
tool = tl.tool(tl.which(scriptPath, true));
}
this.throwIfError(tl.execSync("az", "--version"));
this.useGlobalConfig = tl.getBoolInput("useGlobalConfig");
// set az cli config dir
this.setConfigDirectory();
this.setAzureCloudBasedOnServiceEndpoint();
this.loginAzure();

tool.line(args); // additional args should always call line. line() parses quoted arg strings
Expand Down Expand Up @@ -115,7 +117,6 @@ export class azureclitask {
private static isLoggedIn: boolean = false;
private static cliPasswordPath: string = null;
private static azCliConfigPath: string;
private static useGlobalConfig: boolean = true;
private static servicePrincipalId: string = null;
private static servicePrincipalKey: string = null;

Expand Down Expand Up @@ -146,9 +147,6 @@ export class azureclitask {
var tenantId: string = tl.getEndpointAuthorizationParameter(connectedService, "tenantid", false);
var subscriptionID: string = tl.getEndpointDataParameter(connectedService, "SubscriptionID", true);

// set az cli config dir
this.setConfigDirectory();

//login using svn
this.throwIfError(tl.execSync("az", "login --service-principal -u \"" + servicePrincipalId + "\" -p \"" + cliPassword + "\" --tenant \"" + tenantId + "\""), tl.loc("LoginFailed"));
this.isLoggedIn = true;
Expand All @@ -158,7 +156,7 @@ export class azureclitask {
}

private static setConfigDirectory(): void {
if (this.useGlobalConfig) {
if (tl.getBoolInput("useGlobalConfig")) {
return;
}

Expand All @@ -178,6 +176,15 @@ export class azureclitask {
}
}

private static setAzureCloudBasedOnServiceEndpoint(): void {
var connectedService: string = tl.getInput("connectedServiceNameARM", true);
var environment = tl.getEndpointDataParameter(connectedService, 'environment', true);
if(!!environment) {
console.log(tl.loc('SettingAzureCloud', environment));
this.throwIfError(tl.execSync("az", "cloud set -n " + environment));
}
}

private static logoutAzure() {
try {
tl.execSync("az", " account clear");
Expand Down
7 changes: 4 additions & 3 deletions Tasks/AzureCLIV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": {
"Major": 1,
"Minor": 144,
"Patch": 2
"Patch": 3
},
"minimumAgentVersion": "2.0.0",
"instanceNameFormat": "Azure CLI $(scriptPath)",
Expand Down Expand Up @@ -103,7 +103,7 @@
"name": "useGlobalConfig",
"type": "boolean",
"label": "Use global Azure CLI configuration",
"defaultValue": "true",
"defaultValue": "false",
"required": false,
"helpMarkDown": "If this is false, this task will use its own separate [Azure CLI configuration directory](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest#cli-configuration-file). This can be used to run Azure CLI tasks in *parallel* releases",
"groupName": "advanced"
Expand Down Expand Up @@ -144,6 +144,7 @@
"FailedToLogout": "The following error occurred while logging out: %s",
"LoginFailed": "Azure login failed",
"ErrorInSettingUpSubscription": "Error in setting up subscription",
"SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s"
"SettingAzureConfigDir": "Setting AZURE_CONFIG_DIR env variable to: %s",
"SettingAzureCloud": "Setting active cloud to: %s"
}
}
23 changes: 12 additions & 11 deletions Tasks/AzureCLIV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"version": {
"Major": 1,
"Minor": 144,
"Patch": 2
"Patch": 3
},
"minimumAgentVersion": "2.0.0",
"instanceNameFormat": "ms-resource:loc.instanceNameFormat",
Expand Down Expand Up @@ -99,6 +99,15 @@
"helpMarkDown": "ms-resource:loc.input.help.addSpnToEnvironment",
"groupName": "advanced"
},
{
"name": "useGlobalConfig",
"type": "boolean",
"label": "ms-resource:loc.input.label.useGlobalConfig",
"defaultValue": "false",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.useGlobalConfig",
"groupName": "advanced"
},
{
"name": "cwd",
"aliases": [
Expand All @@ -111,15 +120,6 @@
"helpMarkDown": "ms-resource:loc.input.help.cwd",
"groupName": "advanced"
},
{
"name": "useGlobalConfig",
"type": "boolean",
"label": "ms-resource:loc.input.label.useGlobalConfig",
"defaultValue": "true",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.useGlobalConfig",
"groupName": "advanced"
},
{
"name": "failOnStandardError",
"type": "boolean",
Expand All @@ -144,6 +144,7 @@
"FailedToLogout": "ms-resource:loc.messages.FailedToLogout",
"LoginFailed": "ms-resource:loc.messages.LoginFailed",
"ErrorInSettingUpSubscription": "ms-resource:loc.messages.ErrorInSettingUpSubscription",
"SettingAzureConfigDir": "ms-resource:loc.messages.SettingAzureConfigDir"
"SettingAzureConfigDir": "ms-resource:loc.messages.SettingAzureConfigDir",
"SettingAzureCloud": "ms-resource:loc.messages.SettingAzureCloud"
}
}

0 comments on commit 936033a

Please sign in to comment.