diff --git a/services/api-db/docker-entrypoint-initdb.d/03-procedures.sql b/services/api-db/docker-entrypoint-initdb.d/03-procedures.sql index 30a9dc2300..88d63411cf 100644 --- a/services/api-db/docker-entrypoint-initdb.d/03-procedures.sql +++ b/services/api-db/docker-entrypoint-initdb.d/03-procedures.sql @@ -284,59 +284,7 @@ CREATE OR REPLACE PROCEDURE END; $$ -CREATE OR REPLACE PROCEDURE - CreateOpenshift - ( - IN id int, - IN name varchar(50), - IN console_url varchar(300), - IN token varchar(2000), - IN router_pattern varchar(300), - IN project_user varchar(100), - IN ssh_host varchar(300), - IN ssh_port varchar(50), - IN monitoring_config varchar(2048) - ) - BEGIN - DECLARE new_oid int; - - IF (id IS NULL) THEN - SET id = 0; - END IF; - - INSERT INTO openshift ( - id, - name, - console_url, - token, - router_pattern, - project_user, - ssh_host, - ssh_port, - monitoring_config - ) VALUES ( - id, - name, - console_url, - token, - router_pattern, - project_user, - ssh_host, - ssh_port, - monitoring_config - ); - - IF (id = 0) THEN - SET new_oid = LAST_INSERT_ID(); - ELSE - SET new_oid = id; - END IF; - - SELECT - o.* - FROM openshift o - WHERE o.id = new_oid; - END; +DROP PROCEDURE IF EXISTS CreateOpenshift; $$ CREATE OR REPLACE PROCEDURE diff --git a/services/api/src/resources/openshift/resolvers.ts b/services/api/src/resources/openshift/resolvers.ts index 7fe78937f9..2e6ebb4eea 100644 --- a/services/api/src/resources/openshift/resolvers.ts +++ b/services/api/src/resources/openshift/resolvers.ts @@ -19,24 +19,10 @@ export const addOpenshift: ResolverFn = async ( ) => { await hasPermission('openshift', 'add'); - const rows = await query( - sqlClientPool, - `CALL CreateOpenshift( - :id, - :name, - :console_url, - ${input.token ? ':token' : 'NULL'}, - ${input.routerPattern ? ':router_pattern' : 'NULL'}, - ${input.projectUser ? ':project_user' : 'NULL'}, - ${input.sshHost ? ':ssh_host' : 'NULL'}, - ${input.sshPort ? ':ssh_port' : 'NULL'}, - ${input.monitoringConfig ? ':monitoring_config' : 'NULL'} - );`, - input - ); - const openshift = R.path([0, 0], rows); + const { insertId } = await query(sqlClientPool, Sql.insertOpenshift(input)); - return openshift; + const rows = await query(sqlClientPool, Sql.selectOpenshift(insertId)); + return R.prop(0, rows); }; export const deleteOpenshift: ResolverFn = async ( @@ -68,7 +54,7 @@ export const getOpenshiftByProjectId: ResolverFn = async ( { sqlClientPool, hasPermission } ) => { await hasPermission('openshift', 'view', { - project: pid, + project: pid }); const rows = await query( @@ -105,7 +91,7 @@ export const getOpenshiftByDeployTargetId: ResolverFn = async ( // check permissions on the project await hasPermission('openshift', 'view', { - project: projectrows[0].project, + project: projectrows[0].project }); const rows = await query( diff --git a/services/api/src/resources/openshift/sql.ts b/services/api/src/resources/openshift/sql.ts index cd401e896a..190a79e0d7 100644 --- a/services/api/src/resources/openshift/sql.ts +++ b/services/api/src/resources/openshift/sql.ts @@ -1,7 +1,47 @@ import { knex } from '../../util/db'; export const Sql = { - updateOpenshift: ({ id, patch }: { id: number; patch: { [key: string]: any } }) => + insertOpenshift: ({ + id, + name, + consoleUrl, + token, + routerPattern, + projectUser, + sshHost, + sshPort, + monitoringConfig + }: { + id?: number; + name: string; + consoleUrl: string; + token?: string; + routerPattern?: string; + projectUser?: string; + sshHost?: string; + sshPort?: string; + monitoringConfig?: JSON; + }) => + knex('openshift') + .insert({ + id, + name, + consoleUrl, + token, + routerPattern, + projectUser, + sshHost, + sshPort, + monitoringConfig + }) + .toString(), + updateOpenshift: ({ + id, + patch + }: { + id: number; + patch: { [key: string]: any }; + }) => knex('openshift') .where('id', '=', id) .update(patch)