Skip to content

Commit

Permalink
Merge pull request #81 from liprec/develop
Browse files Browse the repository at this point in the history
Release 2.5.0
  • Loading branch information
liprec authored Apr 6, 2022
2 parents bbf9713 + dd71c92 commit 60fefca
Show file tree
Hide file tree
Showing 33 changed files with 2,909 additions and 2,234 deletions.
79 changes: 56 additions & 23 deletions delete-adf-items/v2/deleteadfitems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
loginWithAppServiceMSI,
ApplicationTokenCredentials,
MSIAppServiceTokenCredentials,
AzureTokenCredentialsOptions,
} from "@azure/ms-rest-nodeauth";
import { AzureServiceClient } from "@azure/ms-rest-azure-js";
import { HttpOperationResponse, RequestPrepareOptions } from "@azure/ms-rest-js";
Expand All @@ -53,7 +54,13 @@ import { wildcardFilter } from "./lib/helpers";

setResourcePath(join(__dirname, "../task.json"));

function loginAzure(clientId: string, key: string, tenantID: string, scheme: string): Promise<AzureServiceClient> {
function loginAzure(
clientId: string,
key: string,
tenantID: string,
scheme: string,
audience?: string
): Promise<AzureServiceClient> {
return new Promise<AzureServiceClient>((resolve, reject) => {
if (scheme.toLocaleLowerCase() === "managedserviceidentity") {
loginWithAppServiceMSI()
Expand All @@ -67,7 +74,12 @@ function loginAzure(clientId: string, key: string, tenantID: string, scheme: str
}
});
} else {
loginWithServicePrincipalSecret(clientId, key, tenantID)
const options: AzureTokenCredentialsOptions = audience
? {
tokenAudience: audience,
}
: {};
loginWithServicePrincipalSecret(clientId, key, tenantID, options)
.then((credentials: ApplicationTokenCredentials) => {
resolve(new AzureServiceClient(credentials, {}));
})
Expand All @@ -84,12 +96,13 @@ function loginAzure(clientId: string, key: string, tenantID: string, scheme: str
function checkDataFactory(datafactoryOption: DatafactoryOptions): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
const azureClient: AzureServiceClient = datafactoryOption.azureClient as AzureServiceClient,
environmentUrl: string = datafactoryOption.environmentUrl,
subscriptionId: string = datafactoryOption.subscriptionId,
resourceGroup: string = datafactoryOption.resourceGroup,
dataFactoryName: string = datafactoryOption.dataFactoryName;
resourceGroup: string | undefined = datafactoryOption.resourceGroup,
dataFactoryName: string | undefined = datafactoryOption.dataFactoryName;
const options: RequestPrepareOptions = {
method: "GET",
url: `https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}?api-version=2018-06-01`,
url: `https://${environmentUrl}/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}?api-version=2018-06-01`,
};
azureClient
.sendRequest(options)
Expand All @@ -98,6 +111,7 @@ function checkDataFactory(datafactoryOption: DatafactoryOptions): Promise<boolea
error(loc("Generic_CheckDataFactory2", dataFactoryName));
reject(loc("Generic_CheckDataFactory2", dataFactoryName));
} else {
debug(`Datafactory '${dataFactoryName}' exist`);
resolve(true);
}
})
Expand All @@ -118,9 +132,15 @@ function getObjects(
): Promise<DatafactoryTaskObject[]> {
return new Promise<DatafactoryTaskObject[]>((resolve, reject) => {
const azureClient: AzureServiceClient = datafactoryOption.azureClient as AzureServiceClient,
environmentUrl: string = datafactoryOption.environmentUrl,
subscriptionId: string = datafactoryOption.subscriptionId,
resourceGroup: string = datafactoryOption.resourceGroup,
dataFactoryName: string = datafactoryOption.dataFactoryName;
workspaceUrl: string | undefined = datafactoryOption.workspaceUrl,
resourceGroup: string | undefined = datafactoryOption.resourceGroup,
dataFactoryName: string | undefined = datafactoryOption.dataFactoryName;
const endPoint = workspaceUrl
? `https://${workspaceUrl}`
: `https://${environmentUrl}/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}`;
const apiVersion = workspaceUrl ? "2020-12-01" : "2018-06-01";
let objectType;
switch (datafactoryType) {
case DatafactoryTypes.Dataset:
Expand All @@ -141,14 +161,14 @@ function getObjects(
}
const options: RequestPrepareOptions = {
method: "GET",
url: `https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}/${objectType}?api-version=2018-06-01`,
url: `${endPoint}/${objectType}?api-version=${apiVersion}`,
};
azureClient
.sendRequest(options)
.then(async (result: HttpOperationResponse) => {
if (result && result.status !== 200) {
debug(loc("DeleteAdfItems_GetObjects2", datafactoryType));
reject(loc("DeleteAdfItems_GetObjects2", datafactoryType));
debug(loc("DeleteAdfItems_GetObjects", datafactoryType, result.bodyAsText));
reject(loc("DeleteAdfItems_GetObjects", datafactoryType, result.bodyAsText));
} else {
let objects = JSON.parse(JSON.stringify(result.parsedBody));
let items = objects.value;
Expand Down Expand Up @@ -206,8 +226,8 @@ function getObjects(
})
.catch((err: Error) => {
if (err) {
error(loc("DeleteAdfItems_GetObjects", datafactoryType, err.message));
reject(loc("DeleteAdfItems_GetObjects", datafactoryType, err.message));
error(loc("DeleteAdfItems_GetObjects", datafactoryType, err));
reject(loc("DeleteAdfItems_GetObjects", datafactoryType, err));
}
});
});
Expand Down Expand Up @@ -235,8 +255,14 @@ function deleteItem(
return new Promise<boolean>((resolve, reject) => {
const azureClient: AzureServiceClient = datafactoryOption.azureClient as AzureServiceClient,
subscriptionId: string = datafactoryOption.subscriptionId,
resourceGroup: string = datafactoryOption.resourceGroup,
dataFactoryName: string = datafactoryOption.dataFactoryName;
environmentUrl: string = datafactoryOption.environmentUrl,
workspaceUrl: string | undefined = datafactoryOption.workspaceUrl,
resourceGroup: string | undefined = datafactoryOption.resourceGroup,
dataFactoryName: string | undefined = datafactoryOption.dataFactoryName;
const endPoint = workspaceUrl
? `https://${workspaceUrl}`
: `https://${environmentUrl}/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}`;
const apiVersion = workspaceUrl ? "2020-12-01" : "2018-06-01";
const objectName = item.name;
let objectType;
switch (item.type) {
Expand All @@ -258,7 +284,7 @@ function deleteItem(
}
const options: RequestPrepareOptions = {
method: "DELETE",
url: `https://management.azure.com/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.DataFactory/factories/${dataFactoryName}/${objectType}/${objectName}?api-version=2018-06-01`,
url: `${endPoint}/${objectType}/${objectName}?api-version=${apiVersion}`,
};
azureClient
.sendRequest(options)
Expand All @@ -276,7 +302,7 @@ function deleteItem(
} else if (result && result.status === 204) {
debug(`'${item.name}' not found.`);
resolve(true);
} else if (result && result.status === 200) {
} else if (result && (result.status === 200 || result.status === 202)) {
console.log(`Deleted ${item.type} '${item.name}' in chunk: ${item.bucket}.`);
resolve(true);
} else {
Expand Down Expand Up @@ -406,6 +432,7 @@ async function main(): Promise<boolean> {
debug("Task execution started ...");
taskParameters = new TaskParameters();
const connectedServiceName = taskParameters.ConnectedServiceName;
const workspaceUrl = taskParameters.WorkspaceUrl;
const resourceGroup = taskParameters.ResourceGroupName;
const dataFactoryName = taskParameters.DatafactoryName;

Expand All @@ -423,25 +450,31 @@ async function main(): Promise<boolean> {
};

azureModels = new AzureModels(connectedServiceName);
const clientId = azureModels.getServicePrincipalClientId();
const key = azureModels.getServicePrincipalKey();
const tenantID = azureModels.getTenantId();
const clientId = azureModels.ServicePrincipalClientId;
const key = azureModels.ServicePrincipalKey;
const tenantID = azureModels.TenantId;
const datafactoryOption: DatafactoryOptions = {
subscriptionId: azureModels.getSubscriptionId(),
subscriptionId: azureModels.SubscriptionId,
environmentUrl: azureModels.EnvironmentUrl,
workspaceUrl: workspaceUrl,
resourceGroup: resourceGroup,
dataFactoryName: dataFactoryName,
};
const scheme = azureModels.AuthScheme;
debug("Parsed task inputs");

loginAzure(clientId, key, tenantID, scheme)
loginAzure(clientId, key, tenantID, scheme, taskParameters.Audience)
.then((azureClient: AzureServiceClient) => {
datafactoryOption.azureClient = azureClient;
debug("Azure client retrieved.");
return checkDataFactory(datafactoryOption);
if (!datafactoryOption.workspaceUrl) {
return checkDataFactory(datafactoryOption);
} else {
console.log(loc("DeleteAdfJson_SynapseWarning"));
return true;
}
})
.then(() => {
debug(`Datafactory '${dataFactoryName}' exist`);
const deleteTasks: DeleteTask[] = [];
if (triggerFilter) {
deleteTasks.push({ filter: triggerFilter, type: DatafactoryTypes.Trigger });
Expand Down
6 changes: 4 additions & 2 deletions delete-adf-items/v2/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import { SortingDirection, DatafactoryTypes } from "./enums";

export interface DatafactoryOptions {
azureClient?: AzureServiceClient;
environmentUrl: string;
subscriptionId: string;
resourceGroup: string;
dataFactoryName: string;
workspaceUrl?: string;
resourceGroup?: string;
dataFactoryName?: string;
}

export interface DatafactoryTaskOptions {
Expand Down
23 changes: 17 additions & 6 deletions delete-adf-items/v2/models/azureModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class AzureModels {
private servicePrincipalClientId: string;
private servicePrincipalKey: string;
private environmentAuthorityUrl?: string;
private environmentUrl: string;
private tenantId: string;
private url?: string;

Expand All @@ -55,6 +56,7 @@ export class AzureModels {
this.subscriptionName = getInput("subscriptionname", true) as string;
this.servicePrincipalClientId = getInput("serviceprincipalid", true) as string;
this.servicePrincipalKey = getInput("serviceprincipalkey", true) as string;
this.environmentUrl = getInput("environmentUrl", true) as string;
this.environmentAuthorityUrl = getInput("environmentAuthorityUrl", true) as string;
this.tenantId = getInput("tenantid", true) as string;
this.url = getInput("connectedServiceNameUrl", true) as string;
Expand All @@ -72,6 +74,11 @@ export class AzureModels {
"serviceprincipalkey",
true
) as string;
this.environmentUrl = getEndpointDataParameter(
this.connectedServiceName,
"environmentUrl",
true
) as string;
this.environmentAuthorityUrl = getEndpointDataParameter(
this.connectedServiceName,
"environmentAuthorityUrl",
Expand All @@ -93,27 +100,31 @@ export class AzureModels {
return this.authScheme || "ServicePrincipal";
}

public getSubscriptionId(): string {
public get SubscriptionId(): string {
return this.subscriptionId as string;
}

public getSubscriptionName(): string {
public get SubscriptionName(): string {
return this.subscriptionName as string;
}

public getServicePrincipalClientId(): string {
public get ServicePrincipalClientId(): string {
return this.servicePrincipalClientId;
}

public getServicePrincipalKey(): string {
public get ServicePrincipalKey(): string {
return this.servicePrincipalKey;
}

public getEnvironmentAuthorityUrl(): string {
public get EnvironmentUrl(): string {
return new URL(this.environmentUrl).hostname as string;
}

public get EnvironmentAuthorityUrl(): string {
return this.environmentAuthorityUrl as string;
}

public getTenantId(): string {
public get TenantId(): string {
return this.tenantId;
}

Expand Down
41 changes: 34 additions & 7 deletions delete-adf-items/v2/models/taskParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
* THE SOFTWARE.
*/

import { getBoolInput, getInput, loc } from "azure-pipelines-task-lib";
import { getBoolInput, getInput, loc, warning } from "azure-pipelines-task-lib";

import { SortingDirection } from "../lib/enums";

export class TaskParameters {
private connectedServiceName: string;
private resourceGroupName: string;
private datafactoryName: string;
private resourceGroupName?: string;
private datafactoryName?: string;
private workspaceUrl?: string;

private serviceFilter: string | undefined;
private pipelineFilter: string | undefined;
Expand All @@ -49,8 +50,26 @@ export class TaskParameters {
constructor() {
try {
this.connectedServiceName = getInput("ConnectedServiceName", true) as string;
this.resourceGroupName = getInput("ResourceGroupName", true) as string;
this.datafactoryName = getInput("DatafactoryName", true) as string;
this.resourceGroupName = getInput("ResourceGroupName", false) as string;
this.datafactoryName = getInput("DatafactoryName", false) as string;
this.workspaceUrl = getInput("WorkspaceUrl", false) as string;

if (!this.workspaceUrl) {
if (!this.resourceGroupName) {
throw new Error(loc("TaskParameters_MissingResourceGroup"));
}
if (!this.datafactoryName) {
throw new Error(loc("TaskParameters_MissingDataFactoryName"));
}
}
if (this.workspaceUrl) {
if (this.resourceGroupName) {
warning(loc("TaskParameters_IgnoredParameter", "ResourceGroupName"));
}
if (this.datafactoryName) {
warning(loc("TaskParameters_IgnoredParameter", "DatafactoryName"));
}
}

this.serviceFilter = getInput("ServiceFilter", false);
this.pipelineFilter = getInput("PipelineFilter", false);
Expand Down Expand Up @@ -86,14 +105,22 @@ export class TaskParameters {
return this.connectedServiceName;
}

public get ResourceGroupName(): string {
public get ResourceGroupName(): string | undefined {
return this.resourceGroupName;
}

public get DatafactoryName(): string {
public get DatafactoryName(): string | undefined {
return this.datafactoryName;
}

public get WorkspaceUrl(): string | undefined {
if (this.workspaceUrl) return new URL(this.workspaceUrl as string).hostname;
}

public get Audience(): string | undefined {
if (this.workspaceUrl) return "https://dev.azuresynapse.net/";
}

public get ServiceFilter(): string | undefined {
return this.serviceFilter;
}
Expand Down
Loading

0 comments on commit 60fefca

Please sign in to comment.