Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
VScode is caching template and provisioning service clients first tim…
Browse files Browse the repository at this point in the history
…e when deploy to azure command runs (#202)

* Initial changes

* change variable name

* change variable name

* Changes env name

* Review comments

* review comments

* Review comment

* commit to run CI

* test commit for testing check runs

* Update remoteServiceUrlHelper.ts

* Update remoteServiceUrlHelper.ts

* Update remoteServiceUrlHelper.ts

* revert test changes

* URL bug fix

* Cachced client fix

* Cachced client fix

* updated permissions

* Changes desgined

* Removed new line

* removed unsed params

* review comments

Co-authored-by: Bishal Prasad <[email protected]>
  • Loading branch information
tiwarishub and bishal-pdMSFT authored Dec 23, 2020
1 parent 52765a3 commit f8a38b7
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 22 deletions.
Binary file modified ghpatpermissions.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 18 additions & 3 deletions src/configure/clients/TemplateServiceClientFactory.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import { ServiceClientCredentials, TokenCredentials } from "ms-rest";
import { RemoteServiceUrlHelper, ServiceFramework } from "../helper/remoteServiceUrlHelper";
import { Messages } from '../resources/messages';
import { TemplateServiceClient } from "./github/TemplateServiceClient";
import { ITemplateServiceClient } from "./ITemplateServiceClient";
const UserAgent = "deploy-to-azure-vscode";

export class TemplateServiceClientFactory {

public static async getClient(credentials?: ServiceClientCredentials, githubPatToken?: string): Promise<ITemplateServiceClient> {
public static async getClient(): Promise<ITemplateServiceClient> {
if (!!this.client) {
return this.client;
}

if (!this.githubPatToken || !this.credentials) {
throw new Error(Messages.UndefinedClientCredentials);
}

const serviceDefinition = await RemoteServiceUrlHelper.getTemplateServiceDefinition();
if (serviceDefinition.serviceFramework === ServiceFramework.Vssf) {
this.client = new TemplateServiceClient(serviceDefinition.serviceUrl, credentials, {
this.client = new TemplateServiceClient(serviceDefinition.serviceUrl, this.credentials, {
"Content-Type": "application/json; charset=utf-8"
});
} else {
this.client = new TemplateServiceClient(serviceDefinition.serviceUrl, new TokenCredentials(githubPatToken, "token"), {
this.client = new TemplateServiceClient(serviceDefinition.serviceUrl, new TokenCredentials(this.githubPatToken, "token"), {
"User-Agent": UserAgent,
"Content-Type": "application/json; charset=utf-8"
});
}

return this.client;
}

public static initialize(credentials: ServiceClientCredentials, githubPatToken: string): void {
this.client = null;
this.credentials = credentials;
this.githubPatToken = githubPatToken
}

private static client: ITemplateServiceClient;
private static credentials: ServiceClientCredentials;
private static githubPatToken: string;
}
23 changes: 19 additions & 4 deletions src/configure/clients/provisioningServiceClientFactory.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { ServiceClientCredentials, TokenCredentials } from "ms-rest";
import { RemoteServiceUrlHelper, ServiceFramework } from "../helper/remoteServiceUrlHelper";
import { Messages } from '../resources/messages';
import { IProvisioningServiceClient } from "./IProvisioningServiceClient";
import { ProvisioningServiceClient } from "./ProvisioningServiceClient";


export class ProvisioningServiceClientFactory {
public static async getClient(githubPatToken: string, credentials?: ServiceClientCredentials): Promise<IProvisioningServiceClient> {
public static async getClient(): Promise<IProvisioningServiceClient> {
if (!!this.client) {
return this.client;
}

if (!this.githubPatToken || !this.credentials) {
throw new Error(Messages.UndefinedClientCredentials);
}

const defaultHeaders: { [propertyName: string]: string } = { "Content-Type": "application/json" };
const serviceDefinition = await RemoteServiceUrlHelper.getProvisioningServiceDefinition();
if (serviceDefinition.serviceFramework === ServiceFramework.Vssf) {
defaultHeaders["X-GITHUB-TOKEN"] = "token " + githubPatToken;
this.client = new ProvisioningServiceClient(serviceDefinition, defaultHeaders, credentials);
defaultHeaders["X-GITHUB-TOKEN"] = "token " + this.githubPatToken;
this.client = new ProvisioningServiceClient(serviceDefinition, defaultHeaders, this.credentials);
} else {
this.client = new ProvisioningServiceClient(serviceDefinition, defaultHeaders, new TokenCredentials(githubPatToken, "token"));
this.client = new ProvisioningServiceClient(serviceDefinition, defaultHeaders, new TokenCredentials(this.githubPatToken, "token"));
}

return this.client;
}

public static initialize(credentials: ServiceClientCredentials, githubPatToken: string): void {
this.client = null;
this.credentials = credentials;
this.githubPatToken = githubPatToken
}

private static client: IProvisioningServiceClient;
private static credentials: ServiceClientCredentials;
private static githubPatToken: string;
}
10 changes: 9 additions & 1 deletion src/configure/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as vscode from 'vscode';
import { UserCancelledError } from 'vscode-azureextensionui';
import { AppServiceClient } from './clients/azure/appServiceClient';
import { AzureResourceClient } from './clients/azure/azureResourceClient';
import { ProvisioningServiceClientFactory } from './clients/provisioningServiceClientFactory';
import { TemplateServiceClientFactory } from './clients/TemplateServiceClientFactory';
import { Configurer } from './configurers/configurerBase';
import { ConfigurerFactory } from './configurers/configurerFactory';
import { ProvisioningConfigurer } from './configurers/provisioningConfigurer';
Expand Down Expand Up @@ -203,6 +205,7 @@ class Orchestrator {

const repoAnalysisResult = await this.getRepositoryAnalysis();

this.initializeClientFactories();
this.setPipelineType();
await this.getTemplatesByRepoAnalysis(repoAnalysisResult);
try {
Expand Down Expand Up @@ -234,7 +237,7 @@ class Orchestrator {
private async getTemplateParameters() {
if (this.inputs.pipelineConfiguration.template.templateType === TemplateType.REMOTE) {
const template = this.inputs.pipelineConfiguration.template as RemotePipelineTemplate;
const extendedPipelineTemplate = await templateHelper.getTemplateParameters(this.inputs.azureSession, template.id, this.inputs.githubPATToken);
const extendedPipelineTemplate = await templateHelper.getTemplateParameters(template.id);
template.attributes = extendedPipelineTemplate.attributes;
template.parameters = extendedPipelineTemplate.parameters;
const controlProvider = new InputControlProvider(this.inputs.azureSession, extendedPipelineTemplate, this.context);
Expand Down Expand Up @@ -731,6 +734,11 @@ class Orchestrator {
telemetryHelper.setCurrentStep('DisplayCreatedPipeline');
pipelineConfigurer.browseQueuedPipeline();
}

private initializeClientFactories(): void {
TemplateServiceClientFactory.initialize(this.inputs.azureSession.credentials, this.inputs.githubPATToken);
ProvisioningServiceClientFactory.initialize(this.inputs.azureSession.credentials, this.inputs.githubPATToken);
}
}

export async function openBrowseExperience(resourceId: string): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions src/configure/configurers/provisioningConfigurer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ProvisioningConfigurer implements IProvisioningConfigurer {

public async queueProvisioningPipelineJob(provisioningConfiguration: ProvisioningConfiguration, wizardInputs: WizardInputs): Promise<ProvisioningConfiguration> {
try {
this.provisioningServiceClient = await ProvisioningServiceClientFactory.getClient(wizardInputs.githubPATToken, wizardInputs.azureSession.credentials);
this.provisioningServiceClient = await ProvisioningServiceClientFactory.getClient();
const OrgAndRepoDetails = wizardInputs.sourceRepository.repositoryId.split('/');
return await this.provisioningServiceClient.createProvisioningConfiguration(provisioningConfiguration, OrgAndRepoDetails[0], OrgAndRepoDetails[1]);
} catch (error) {
Expand All @@ -57,7 +57,7 @@ export class ProvisioningConfigurer implements IProvisioningConfigurer {

public async getProvisioningPipeline(jobId: string, githubOrg: string, repository: string, wizardInputs: WizardInputs): Promise<ProvisioningConfiguration> {
try {
this.provisioningServiceClient = await ProvisioningServiceClientFactory.getClient(wizardInputs.githubPATToken, wizardInputs.azureSession.credentials);
this.provisioningServiceClient = await ProvisioningServiceClientFactory.getClient();
return await this.provisioningServiceClient.getProvisioningConfiguration(jobId, githubOrg, repository);
} catch (error) {
telemetryHelper.logError(Layer, TracePoints.UnabletoGetProvisioningPipeline, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class RemoteGitHubWorkflowConfigurer extends LocalGitHubWorkflowConfigure
public async getInputs(wizardInputs: WizardInputs): Promise<void> {
this.inputs = wizardInputs;
this.githubClient = new GithubClient(wizardInputs.githubPATToken, wizardInputs.sourceRepository.remoteUrl);
this.templateServiceClient = await TemplateServiceClientFactory.getClient(wizardInputs.azureSession.credentials, wizardInputs.githubPATToken);
this.templateServiceClient = await TemplateServiceClientFactory.getClient();
this.template = wizardInputs.pipelineConfiguration.template as RemotePipelineTemplate;
let extendedPipelineTemplate;
try {
Expand Down
16 changes: 8 additions & 8 deletions src/configure/helper/templateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ async function convertToPipelineTemplate(remoteTemplates: TemplateInfo[]): Promi
return pipelineTemplates;
}

export async function getFilteredTemplates(azureSession: AzureSession, resourceType: string, githubPatToken?: string): Promise<TemplateInfo[]> {
export async function getFilteredTemplates(resourceType: string): Promise<TemplateInfo[]> {

const client = await TemplateServiceClientFactory.getClient(azureSession.credentials, githubPatToken);
const client = await TemplateServiceClientFactory.getClient();
let filteredTemplates: TemplateInfo[];
switch (resourceType) {
case TargetResourceType.AKS:
Expand All @@ -187,8 +187,8 @@ export async function getFilteredTemplates(azureSession: AzureSession, resourceT
}
}

export async function getTemplates(azureSession: AzureSession, repoAnalysisParameters: RepositoryAnalysis, githubPatToken?: string) {
const client = await TemplateServiceClientFactory.getClient(azureSession.credentials, githubPatToken);
export async function getTemplates(repoAnalysisParameters: RepositoryAnalysis) {
const client = await TemplateServiceClientFactory.getClient();
let templates: TemplateInfo[];
await telemetryHelper.executeFunctionWithTimeTelemetry(async () => {
templates = await client.getTemplates(repoAnalysisParameters);
Expand All @@ -206,10 +206,10 @@ export async function analyzeRepoAndListAppropriatePipeline2(azureSession: Azure
try {
if (!!resource) {
localPipelineTemplates = [];
remoteTemplates = await getFilteredTemplates(azureSession, resource.type, githubPatToken);
remoteTemplates = await getFilteredTemplates(resource.type);
}
else if (repoAnalysisParameters && repoAnalysisParameters.applicationSettingsList) {
remoteTemplates = await getTemplates(azureSession, repoAnalysisParameters, githubPatToken);
remoteTemplates = await getTemplates(repoAnalysisParameters);
}
pipelineTemplates = await convertToPipelineTemplate(remoteTemplates);
pipelineTemplates = pipelineTemplates.concat(localPipelineTemplates);
Expand All @@ -230,10 +230,10 @@ export async function analyzeRepoAndListAppropriatePipeline2(azureSession: Azure
}
}

export async function getTemplateParameters(azureSession: AzureSession, templateId: string, githubPatToken?: string): Promise<ExtendedPipelineTemplate> {
export async function getTemplateParameters(templateId: string): Promise<ExtendedPipelineTemplate> {
let parameters: ExtendedPipelineTemplate;
try {
let serviceClient = await TemplateServiceClientFactory.getClient(azureSession.credentials, githubPatToken);
let serviceClient = await TemplateServiceClientFactory.getClient();
parameters = await serviceClient.getTemplateParameters(templateId);
return parameters;
}
Expand Down
7 changes: 4 additions & 3 deletions src/configure/resources/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Messages {
public static failedToCreateAzurePipeline: string = 'Couldn\'t configure pipeline. Error: %s';
public static failedToDetermineAzureRepoDetails: string = 'Failed to determine Azure Repo details from remote url. Please ensure that the remote points to a valid Azure Repos url.';
public static githubPatTokenErrorMessage: string = 'GitHub PAT token cannot be empty.';
public static githubPatTokenHelpMessage: string = 'GitHub personal access token (PAT) with following permissions: full access to repository webhooks and services, read and write access to all repository data.';
public static githubPatTokenHelpMessage: string = 'GitHub personal access token (PAT) with following permissions: Update github action workflows, read and write access to all repository data.';
public static githubPatTokenHelpMessageGithubWorkflow: string = 'GitHub personal access token (PAT) with following permissions: read and write access to all repository data.';
public static modifyAndCommitFile: string = 'Modify and save your YAML file. %s will commit this file, push the branch \'%s\' to remote \'%s\' and proceed with deployment.';
public static modifyAndCommitFileWithGitInitialization: string = 'Modify and save your YAML file to proceed with deployment.';
Expand Down Expand Up @@ -119,6 +119,7 @@ export class Messages {
public static ConfiguringGitubWorkflowFailed = 'Configuring github workflow failed due to %s';
public static NoAzureSubscriptionFound = 'No Azure Subscription Found.';
public static GithubRepoRequired = "The selected folder is not a GitHub repository.Please ensure your repository is hosted on GitHub and try again.";
public static GithubWorkflowSetupMultiFile: string = "The workflow files are pushed to your Github repository([commit URL](%s)) and workflow is set up successfully !";
public static GithubWorkflowSetup: string = "The workflow file is pushed to your Github repository([commit URL](%s)) and workflow is set up successfully !";
public static GithubWorkflowSetupMultiFile: string = "The workflow files are pushed to your Github repository([commit URL](%s)) and workflow is set up successfully!";
public static GithubWorkflowSetup: string = "The workflow file is pushed to your Github repository([commit URL](%s)) and workflow is set up successfully!";
public static UndefinedClientCredentials: string = "Undefined client credentials"
}

0 comments on commit f8a38b7

Please sign in to comment.