|
1 | 1 | import { v4 as uuid } from 'uuid';
|
2 |
| -import { ResourceManagementModels } from '@azure/arm-resources'; |
3 | 2 | import { WebSiteManagementClient, WebSiteManagementModels } from '@azure/arm-appservice';
|
4 | 3 | import { TokenCredentialsBase } from '@azure/ms-rest-nodeauth';
|
5 | 4 |
|
6 |
| -import { AzureResourceClient } from './azureResourceClient'; |
7 |
| -import { WebAppKind, ParsedAzureResourceId } from '../../model/models'; |
| 5 | +import { WebAppKind, ValidatedSite } from '../../model/models'; |
8 | 6 | import { Messages } from '../../../messages';
|
9 | 7 |
|
10 |
| -export class AppServiceClient extends AzureResourceClient { |
| 8 | +export class AppServiceClient { |
11 | 9 |
|
12 |
| - private static resourceType = 'Microsoft.Web/sites'; |
13 | 10 | private webSiteManagementClient: WebSiteManagementClient;
|
14 | 11 | private tenantId: string;
|
15 | 12 | private portalUrl: string;
|
16 | 13 |
|
17 | 14 | constructor(credentials: TokenCredentialsBase, tenantId: string, portalUrl: string, subscriptionId: string) {
|
18 |
| - super(credentials, subscriptionId); |
19 | 15 | this.webSiteManagementClient = new WebSiteManagementClient(credentials, subscriptionId);
|
20 | 16 | this.tenantId = tenantId;
|
21 | 17 | this.portalUrl = portalUrl;
|
22 | 18 | }
|
23 | 19 |
|
24 |
| - public async getAppServiceResource(resourceId: string): Promise<ResourceManagementModels.GenericResource> { |
25 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
26 |
| - return await this.webSiteManagementClient.webApps.get(parsedResourceId.resourceGroup, parsedResourceId.resourceName); |
27 |
| - } |
28 |
| - |
29 |
| - public async GetAppServices(filterForResourceKind: WebAppKind): Promise<ResourceManagementModels.ResourceListResult> { |
30 |
| - let resourceList = await this.getResourceList(AppServiceClient.resourceType); |
31 |
| - if (!!filterForResourceKind) { |
32 |
| - resourceList = resourceList.filter(resource => resource.kind === filterForResourceKind); |
33 |
| - } |
34 |
| - |
35 |
| - return resourceList; |
| 20 | + public async getAppServices(filterForResourceKind: WebAppKind): Promise<WebSiteManagementModels.Site[]> { |
| 21 | + const sites = await this.webSiteManagementClient.webApps.list(); |
| 22 | + return sites.filter(site => site.kind === filterForResourceKind); |
36 | 23 | }
|
37 | 24 |
|
38 | 25 | public async getDeploymentCenterUrl(resourceId: string): Promise<string> {
|
39 | 26 | return `${this.portalUrl}/#@${this.tenantId}/resource/${resourceId}/vstscd`;
|
40 | 27 | }
|
41 | 28 |
|
42 |
| - public async getAzurePipelineUrl(resourceId: string): Promise<string> { |
43 |
| - let metadata = await this.getAppServiceMetadata(resourceId); |
44 |
| - if (metadata.properties['VSTSRM_BuildDefinitionWebAccessUrl']) { |
| 29 | + public async getAzurePipelineUrl(site: ValidatedSite): Promise<string> { |
| 30 | + const metadata = await this.getAppServiceMetadata(site); |
| 31 | + if (metadata.properties?.['VSTSRM_BuildDefinitionWebAccessUrl']) { |
45 | 32 | return metadata.properties['VSTSRM_BuildDefinitionWebAccessUrl'];
|
46 | 33 | }
|
47 | 34 |
|
48 | 35 | throw new Error(Messages.cannotFindPipelineUrlInMetaDataException);
|
49 | 36 | }
|
50 | 37 |
|
51 |
| - public async getAppServiceConfig(resourceId: string): Promise<WebSiteManagementModels.SiteConfigResource> { |
52 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
53 |
| - return this.webSiteManagementClient.webApps.getConfiguration(parsedResourceId.resourceGroup, parsedResourceId.resourceName); |
| 38 | + public async getAppServiceConfig(site: ValidatedSite): Promise<WebSiteManagementModels.SiteConfigResource> { |
| 39 | + return this.webSiteManagementClient.webApps.getConfiguration(site.resourceGroup, site.name); |
54 | 40 | }
|
55 | 41 |
|
56 |
| - public async updateScmType(resourceId: string): Promise<WebSiteManagementModels.SiteConfigResource> { |
57 |
| - let siteConfig = await this.getAppServiceConfig(resourceId); |
| 42 | + public async updateScmType(site: ValidatedSite): Promise<WebSiteManagementModels.SiteConfigResource> { |
| 43 | + const siteConfig = await this.getAppServiceConfig(site); |
58 | 44 | siteConfig.scmType = ScmType.VSTSRM;
|
59 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
60 |
| - return this.webSiteManagementClient.webApps.updateConfiguration(parsedResourceId.resourceGroup, parsedResourceId.resourceName, siteConfig); |
| 45 | + return this.webSiteManagementClient.webApps.updateConfiguration(site.resourceGroup, site.name, siteConfig); |
61 | 46 | }
|
62 | 47 |
|
63 |
| - public async getAppServiceMetadata(resourceId: string): Promise<WebSiteManagementModels.StringDictionary> { |
64 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
65 |
| - return this.webSiteManagementClient.webApps.listMetadata(parsedResourceId.resourceGroup, parsedResourceId.resourceName); |
| 48 | + public async getAppServiceMetadata(site: ValidatedSite): Promise<WebSiteManagementModels.StringDictionary> { |
| 49 | + return this.webSiteManagementClient.webApps.listMetadata(site.resourceGroup, site.name); |
66 | 50 | }
|
67 | 51 |
|
68 |
| - public async updateAppServiceMetadata(resourceId: string, metadata: WebSiteManagementModels.StringDictionary): Promise<WebSiteManagementModels.StringDictionary> { |
69 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
70 |
| - return this.webSiteManagementClient.webApps.updateMetadata(parsedResourceId.resourceGroup, parsedResourceId.resourceName, metadata); |
| 52 | + public async updateAppServiceMetadata(site: ValidatedSite, metadata: WebSiteManagementModels.StringDictionary): Promise<WebSiteManagementModels.StringDictionary> { |
| 53 | + return this.webSiteManagementClient.webApps.updateMetadata(site.resourceGroup, site.name, metadata); |
71 | 54 | }
|
72 | 55 |
|
73 |
| - public async publishDeploymentToAppService(resourceId: string, buildDefinitionUrl: string, releaseDefinitionUrl: string, triggeredBuildUrl: string): Promise<WebSiteManagementModels.Deployment> { |
74 |
| - let parsedResourceId: ParsedAzureResourceId = new ParsedAzureResourceId(resourceId); |
75 |
| - |
| 56 | + public async publishDeploymentToAppService(site: ValidatedSite, buildDefinitionUrl: string, releaseDefinitionUrl: string, triggeredBuildUrl: string): Promise<WebSiteManagementModels.Deployment> { |
76 | 57 | // create deployment object
|
77 |
| - let deploymentId = uuid(); |
78 |
| - let deployment = this.createDeploymentObject(deploymentId, buildDefinitionUrl, releaseDefinitionUrl, triggeredBuildUrl); |
79 |
| - return this.webSiteManagementClient.webApps.createDeployment(parsedResourceId.resourceGroup, parsedResourceId.resourceName, deploymentId, deployment); |
| 58 | + const deploymentId = uuid(); |
| 59 | + const deployment = this.createDeploymentObject(deploymentId, buildDefinitionUrl, releaseDefinitionUrl, triggeredBuildUrl); |
| 60 | + return this.webSiteManagementClient.webApps.createDeployment(site.resourceGroup, site.name, deploymentId, deployment); |
80 | 61 | }
|
81 | 62 |
|
82 | 63 | private createDeploymentObject(deploymentId: string, buildDefinitionUrl: string, releaseDefinitionUrl: string, triggeredBuildUrl: string): WebSiteManagementModels.Deployment {
|
83 |
| - let deployment: WebSiteManagementModels.Deployment = { |
84 |
| - id: deploymentId, |
85 |
| - status: 4, |
86 |
| - author: 'VSTS', |
87 |
| - deployer: 'VSTS' |
88 |
| - }; |
89 |
| - |
90 |
| - let deploymentMessage: DeploymentMessage = { |
| 64 | + const message: DeploymentMessage = { |
91 | 65 | type: "CDDeploymentConfiguration",
|
92 | 66 | message: "Successfully set up continuous delivery from VS Code and triggered deployment to Azure Web App.",
|
93 | 67 | VSTSRM_BuildDefinitionWebAccessUrl: `${buildDefinitionUrl}`,
|
94 | 68 | VSTSRM_ConfiguredCDEndPoint: '',
|
95 | 69 | VSTSRM_BuildWebAccessUrl: `${triggeredBuildUrl}`,
|
96 | 70 | };
|
97 |
| - deployment.message = JSON.stringify(deploymentMessage); |
98 |
| - return deployment; |
| 71 | + |
| 72 | + return { |
| 73 | + id: deploymentId, |
| 74 | + status: 4, |
| 75 | + author: 'VSTS', |
| 76 | + deployer: 'VSTS', |
| 77 | + message: JSON.stringify(message), |
| 78 | + }; |
99 | 79 | }
|
100 | 80 | }
|
101 | 81 |
|
|
0 commit comments