Skip to content

Commit

Permalink
Merge pull request #2955 from uselagoon/workflows/taskDefinitionResol…
Browse files Browse the repository at this point in the history
…vers

updating advanced task resolver and permissions
  • Loading branch information
Tim Clifford authored Dec 16, 2021
2 parents e5a3b43 + df7fb77 commit 2f3dc1c
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 140 deletions.
2 changes: 1 addition & 1 deletion services/api-db/docker-entrypoint-initdb.d/00-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ CREATE TABLE IF NOT EXISTS advanced_task_definition_argument (
id int NOT NULL auto_increment PRIMARY KEY,
advanced_task_definition int REFERENCES advanved_task_definition(id),
name varchar(300) NOT NULL UNIQUE,
type ENUM('NUMERIC', 'STRING')
type ENUM('NUMERIC', 'STRING', 'ENVIRONMENT_SOURCE_NAME')
);

CREATE TABLE IF NOT EXISTS notification_webhook (
Expand Down
24 changes: 24 additions & 0 deletions services/api-db/docker-entrypoint-initdb.d/01-migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,29 @@ CREATE OR REPLACE PROCEDURE
END;
$$


CREATE OR REPLACE PROCEDURE
add_environment_type_to_advanced_task_argument()

BEGIN
DECLARE column_type_argument_type varchar(74);

SELECT COLUMN_TYPE INTO column_type_argument_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'advanced_task_definition_argument'
AND table_schema = 'infrastructure'
AND column_name = 'type';

IF (
column_type_argument_type = "enum('NUMERIC','STRING')"
) THEN
ALTER TABLE advanced_task_definition_argument
MODIFY type ENUM('NUMERIC', 'STRING', 'ENVIRONMENT_SOURCE_NAME');
END IF;
END;
$$

CREATE OR REPLACE PROCEDURE
add_openshift_to_environment()

Expand Down Expand Up @@ -1546,6 +1569,7 @@ CALL add_enum_webhook_to_type_in_project_notification();
CALL add_index_for_deployment_environment();
CALL add_index_for_task_environment();
CALL add_router_pattern_to_project();
CALL add_environment_type_to_advanced_task_argument();
CALL add_openshift_to_environment();
CALL add_openshift_project_pattern_to_environment();
CALL add_deployments_disabled_to_project();
Expand Down
2 changes: 2 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const {

const {
addAdvancedTaskDefinition,
updateAdvancedTaskDefinition,
advancedTaskDefinitionById,
resolveTasksForEnvironment,
getRegisteredTasksByEnvironmentId,
Expand Down Expand Up @@ -546,6 +547,7 @@ const resolvers = {
deleteEnvVariable,
addTask,
addAdvancedTaskDefinition,
updateAdvancedTaskDefinition,
deleteAdvancedTaskDefinition,
invokeRegisteredTask,
taskDrushArchiveDump,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { query } from '../../../util/db';
import * as R from 'ramda';

export class ArgumentBase {
async validateInput(input): Promise<boolean> {
return true;
}

public static typeName() {
return "BASE";
}

public async getArgumentRange() {
return [];
}

}

export class EnvironmentSourceArgument extends ArgumentBase {

protected sqlClientPool;
protected environmentId;
protected environmentNameList = [];

constructor(sqlClientPool, environmentId) {
super();
this.sqlClientPool = sqlClientPool;
this.environmentId = environmentId;
}

public static typeName() {
return "ENVIRONMENT_SOURCE_NAME";
}

public async getArgumentRange() {
await this.loadEnvNames();
return this.environmentNameList;
}

protected async loadEnvNames() {
const rows = await query(
this.sqlClientPool,
`select e.name as name from environment as e inner join environment as p on e.project = p.project where p.id = ${this.environmentId}`
);
this.environmentNameList = R.pluck('name')(rows);
}

/**
*
* @param input Environment name
* @returns boolean
*/
async validateInput(input): Promise<boolean> {
await this.loadEnvNames();
return this.environmentNameList.includes(input);
}
}

export class StringArgument extends ArgumentBase {

public static typeName() {
return "STRING";
}

async validateInput(input): Promise<boolean> {
return true;
}

public async getArgumentRange() {
return null;
}
}


export class NumberArgument {

public static typeName() {
return "NUMBER";
}

async validateInput(input): Promise<boolean> {
return true;
}

public async getArgumentRange() {
return null;
}
}



/**
* This function will match a
*
* @param name The name of the advancedTaskDefinition type (stored in field)
*/
export const advancedTaskDefinitionTypeFactory = (sqlClientPool, task, environment) => (name) => {
switch(name) {
case(EnvironmentSourceArgument.typeName()):
return new EnvironmentSourceArgument(sqlClientPool, environment);
break;
case(StringArgument.typeName()):
return new StringArgument();
break;
case(NumberArgument.typeName()):
return new NumberArgument();
break;
default:
throw new Error(`Unable to find AdvancedTaskDefinitionType ${name}`);
break;
}
}
47 changes: 47 additions & 0 deletions services/api/src/resources/task/models/taskRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,50 @@ export class TaskRegistration {
image: string;
permission: string;
}

export interface AdvancedTaskDefinitionInterface {
id?: number;
type: string;
name: string;
description?: string;
environment?: number;
project?: number;
groupName?: string;
created: string;
deleted: string;
permission?: string;
command?: string;
service?: string;
image?: string;
advancedTaskDefinitionArguments?: Partial<AdvancedTaskDefinitionArguments>;
}


export const AdvancedTaskDefinitionType = {
command: 'COMMAND',
image: 'IMAGE'
};

export interface AdvancedTaskDefinitionArguments {
name?: string;
type?: string;
range?: string;
advancedTaskDefinition?: number;
};

export const getAdvancedTaskDefinitionType = (taskDef:AdvancedTaskDefinitionInterface) => {
if(taskDef.type.toLowerCase() == AdvancedTaskDefinitionType.command.toLowerCase()) {
return AdvancedTaskDefinitionType.command;
}
return AdvancedTaskDefinitionType.image;
}

export const isAdvancedTaskDefinitionSystemLevelTask = (taskDef:AdvancedTaskDefinitionInterface): boolean => {
return taskDef.project == null && taskDef.environment == null && taskDef.groupName == null;
}

export const doesAdvancedTaskDefinitionNeedAdminRights = (taskDef:AdvancedTaskDefinitionInterface): boolean => {
return isAdvancedTaskDefinitionSystemLevelTask(taskDef)
|| getAdvancedTaskDefinitionType(taskDef) == AdvancedTaskDefinitionType.image
|| taskDef.groupName != undefined;
}
22 changes: 18 additions & 4 deletions services/api/src/resources/task/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export const Sql = {
type
})
.toString(),
updateAdvancedTaskDefinition: ({ id, patch }: { id: number; patch: { [key: string]: any } }) =>
knex('advanced_task_definition')
.where('id', id)
.update(patch)
.toString(),
selectAdvancedTaskDefinitionEnvironmentLinkById: (id: number) =>
knex('task_registration')
.where('task_registration.id', '=', id)
Expand Down Expand Up @@ -160,6 +165,11 @@ export const Sql = {
knex('advanced_task_definition_argument')
.where('advanced_task_definition_argument.id', '=', id)
.toString(),
deleteAdvancedTaskDefinitionArgumentByTaskDef:(advanced_task_definition: number) =>
knex('advanced_task_definition_argument')
.where('advanced_task_definition_argument.advanced_task_definition', '=', advanced_task_definition)
.del()
.toString(),
selectAdvancedTaskDefinitionByName:(name: string) =>
knex('advanced_task_definition')
.where('advanced_task_definition.name', '=', name)
Expand Down Expand Up @@ -194,8 +204,12 @@ export const Sql = {
.where('group_name', 'in', groups)
.toString(),
deleteAdvancedTaskDefinition:(id: number) =>
knex('advanced_task_definition')
.where('id', id)
.del()
.toString(),
knex('advanced_task_definition')
.where('id', id)
.del()
.toString(),
deleteAdvancedTaskDefinitionArgumentsForTask:(taskId: number) => knex('advanced_task_definition_argument')
.where('advanced_task_definition', taskId)
.del()
.toString(),
};
Loading

0 comments on commit 2f3dc1c

Please sign in to comment.