Skip to content

Commit 8192b51

Browse files
committed
Refactor deleteEnvironment api to trigger openshiftremove service
1 parent 43d35fb commit 8192b51

File tree

6 files changed

+100
-25
lines changed

6 files changed

+100
-25
lines changed

node-packages/commons/src/api.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -731,24 +731,24 @@ const updateEnvironment = (
731731
async function deleteEnvironment(
732732
name: string,
733733
project: string,
734+
execute: boolean = true,
734735
): Promise<Object> {
735-
const result = await graphqlapi.query(`
736-
{
737-
project:projectByName(name: "${project}"){
738-
id
739-
}
740-
}
741-
`);
742-
743-
if (!result || !result.project) {
744-
throw new ProjectNotFound(`Cannot load id for project ${project}`);
736+
return graphqlapi.mutate(
737+
`
738+
($name: String!, $project: String!, $execute: Boolean) {
739+
deleteEnvironment(input: {
740+
name: $name
741+
project: $project
742+
execute: $execute
743+
})
745744
}
746-
747-
return graphqlapi.query(`
748-
mutation {
749-
deleteEnvironment(input: {name: "${name}", project: ${result.project.id}})
750-
}
751-
`);
745+
`,
746+
{
747+
name,
748+
project,
749+
execute,
750+
},
751+
);
752752
}
753753

754754
const getOpenShiftInfoForProject = (project: string): Promise<Object> =>

services/api/src/resources/environment/resolvers.js

+65-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// @flow
22

33
const R = require('ramda');
4+
const { sendToLagoonLogs } = require('@lagoon/commons/src/logs');
5+
const { createRemoveTask } = require('@lagoon/commons/src/tasks');
46
const esClient = require('../../clients/esClient');
57
const sqlClient = require('../../clients/sqlClient');
68
const {
@@ -12,6 +14,8 @@ const {
1214
whereAnd,
1315
} = require('../../util/db');
1416
const Sql = require('./sql');
17+
const projectSql = require('../project/sql');
18+
const projectHelpers = require('../project/helpers');
1519

1620
/* ::
1721
@@ -472,17 +476,72 @@ const addOrUpdateEnvironmentStorage = async (
472476

473477
const deleteEnvironment = async (
474478
root,
475-
{ input },
476-
{ credentials: { role } },
479+
{
480+
input,
481+
input: {
482+
project: projectName,
483+
name,
484+
execute,
485+
},
486+
},
487+
{ credentials: { role, permissions: { customers, projects } } },
477488
) => {
478489
if (role !== 'admin') {
479-
throw new Error('Unauthorized');
490+
const prep = prepare(sqlClient, 'SELECT `id` AS `pid`, `customer` AS `cid` FROM project WHERE `name` = :name');
491+
const rows = await query(sqlClient, prep({ name: projectName }));
492+
493+
if (
494+
!R.contains(R.path(['0', 'pid'], rows), projects) &&
495+
!R.contains(R.path(['0', 'cid'], rows), customers)
496+
) {
497+
throw new Error('Unauthorized.');
498+
}
499+
}
500+
501+
const projectId = await projectHelpers.getProjectIdByName(projectName);
502+
503+
const projectRows = await query(
504+
sqlClient,
505+
projectSql.selectProject(projectId),
506+
);
507+
const project = projectRows[0];
508+
509+
const environmentRows = await query(
510+
sqlClient,
511+
Sql.selectEnvironmentByNameAndProject(name, projectId),
512+
);
513+
const environment = environmentRows[0];
514+
515+
if (!environment) {
516+
throw new Error(`Environment "${name}" does not exist in project "${projectId}"`);
480517
}
481518

482-
const prep = prepare(sqlClient, 'CALL DeleteEnvironment(:name, :project)');
483-
await query(sqlClient, prep(input));
519+
if (role !== 'admin' && environment.environmentType === 'production') {
520+
throw new Error('Unauthorized - You may not delete a production environment');
521+
}
522+
523+
// Deleting environment in api w/o executing the openshift remove.
524+
// This gets called by openshiftremove service after successful remove.
525+
if (role === 'admin' && execute === false) {
526+
const prep = prepare(sqlClient, 'CALL DeleteEnvironment(:name, :project)');
527+
await query(sqlClient, prep({ name, project: projectId }));
528+
529+
// TODO: maybe check rows for changed result
530+
return 'success';
531+
}
532+
533+
const data = {
534+
projectName: project.name,
535+
branch: name,
536+
type: environment.deployType,
537+
forceDeleteProductionEnvironment: role === 'admin',
538+
};
539+
540+
await createRemoveTask(data);
541+
sendToLagoonLogs('info', data.projectName, '', 'api:deleteEnvironment', {},
542+
`*[${data.projectName}]* Deleting environment \`${data.branch}\``
543+
);
484544

485-
// TODO: maybe check rows for changed result
486545
return 'success';
487546
};
488547

services/api/src/resources/environment/sql.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const Sql /* : SqlObj */ = {
1818
knex('environment')
1919
.where('id', '=', id)
2020
.toString(),
21+
selectEnvironmentByNameAndProject: (name /* : string */, projectId /* : numbere */) =>
22+
knex('environment')
23+
.where('name', '=', name)
24+
.andWhere('project', '=', projectId)
25+
.toString(),
2126
truncateEnvironment: () =>
2227
knex('environment')
2328
.truncate()

services/api/src/resources/project/sql.js

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ const Sql /* : SqlObj */ = {
8787
.where('project_via_user.id', projectId)
8888
.orWhere('project_via_customer.id', projectId)
8989
.toString(),
90+
selectPermsForProject: (id /* : number */) =>
91+
knex('project')
92+
.select({ pid: 'id', cid: 'customer' })
93+
.where('id', id)
94+
.toString(),
9095
updateProject: ({ id, patch } /* : {id: number, patch: {[string]: any}} */) =>
9196
knex('project')
9297
.where('id', '=', id)

services/api/src/typeDefs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ const typeDefs = gql`
421421
422422
input DeleteEnvironmentInput {
423423
name: String!
424-
project: Int!
424+
project: String!
425+
execute: Boolean
425426
}
426427
427428
type Query {

services/openshiftremove/src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ const messageConsumer = async function(msg) {
5252
environmentName = branch
5353
openshiftProject = `${safeProjectName}-${safeBranchName}`
5454
break;
55+
56+
case 'promote':
57+
environmentName = branch
58+
openshiftProject = `${projectName}-${branch}`
59+
break;
5560
}
5661

5762
} catch(error) {
@@ -98,7 +103,7 @@ const messageConsumer = async function(msg) {
98103
)
99104
// Update GraphQL API that the Environment has been deleted
100105
try {
101-
await deleteEnvironment(environmentName, projectName)
106+
await deleteEnvironment(environmentName, projectName, false)
102107
logger.info(`${openshiftProject}: Deleted Environment '${environmentName}' in API`)
103108
} catch (err) {
104109
logger.error(err)
@@ -160,7 +165,7 @@ const messageConsumer = async function(msg) {
160165

161166
// Update GraphQL API that the Environment has been deleted
162167
try {
163-
await deleteEnvironment(environmentName, projectName)
168+
await deleteEnvironment(environmentName, projectName, false)
164169
logger.info(`${openshiftProject}: Deleted Environment '${environmentName}' in API`)
165170
} catch (err) {
166171
logger.error(err)

0 commit comments

Comments
 (0)