1
1
// @flow
2
2
3
3
const R = require ( 'ramda' ) ;
4
+ const { sendToLagoonLogs } = require ( '@lagoon/commons/src/logs' ) ;
5
+ const { createRemoveTask } = require ( '@lagoon/commons/src/tasks' ) ;
4
6
const esClient = require ( '../../clients/esClient' ) ;
5
7
const sqlClient = require ( '../../clients/sqlClient' ) ;
6
8
const {
@@ -12,6 +14,8 @@ const {
12
14
whereAnd,
13
15
} = require ( '../../util/db' ) ;
14
16
const Sql = require ( './sql' ) ;
17
+ const projectSql = require ( '../project/sql' ) ;
18
+ const projectHelpers = require ( '../project/helpers' ) ;
15
19
16
20
/* ::
17
21
@@ -472,17 +476,72 @@ const addOrUpdateEnvironmentStorage = async (
472
476
473
477
const deleteEnvironment = async (
474
478
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 } } } ,
477
488
) => {
478
489
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 } "` ) ;
480
517
}
481
518
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
+ ) ;
484
544
485
- // TODO: maybe check rows for changed result
486
545
return 'success' ;
487
546
} ;
488
547
0 commit comments