@@ -2,7 +2,10 @@ import * as R from 'ramda';
22// @ts -ignore
33import { sendToLagoonLogs } from '@lagoon/commons/dist/logs/lagoon-logger' ;
44// @ts -ignore
5- import { createRemoveTask } from '@lagoon/commons/dist/tasks' ;
5+ import {
6+ createRemoveTask ,
7+ createMiscTask
8+ } from '@lagoon/commons/dist/tasks' ;
69import { ResolverFn } from '../' ;
710import { logger } from '../../loggers/logger' ;
811import { isPatchEmpty , query , knex } from '../../util/db' ;
@@ -668,7 +671,8 @@ export const updateEnvironment: ResolverFn = async (
668671 route : input . patch . route ,
669672 routes : input . patch . routes ,
670673 autoIdle : input . patch . autoIdle ,
671- created : input . patch . created
674+ created : input . patch . created ,
675+ idled : input . patch . idled
672676 }
673677 } )
674678 ) ;
@@ -693,7 +697,8 @@ export const updateEnvironment: ResolverFn = async (
693697 route : input . patch . route ,
694698 routes : input . patch . routes ,
695699 autoIdle : input . patch . autoIdle ,
696- created : input . patch . created
700+ created : input . patch . created ,
701+ idled : input . patch . idled
697702 } ,
698703 data : withK8s
699704 }
@@ -808,3 +813,129 @@ export const userCanSshToEnvironment: ResolverFn = async (
808813 return null ;
809814 }
810815} ;
816+
817+ export const idleEnvironment = async (
818+ root ,
819+ args ,
820+ { sqlClientPool, hasPermission, userActivityLogger }
821+ ) => {
822+ const environment = await Helpers ( sqlClientPool ) . getEnvironmentById ( args . id ) ;
823+
824+ if ( ! environment ) {
825+ throw new Error (
826+ 'Invalid environment ID'
827+ ) ;
828+ }
829+
830+ await hasPermission ( 'environment' , 'view' , {
831+ project : environment . project
832+ } ) ;
833+
834+ if ( ! environment . idled ) {
835+ const project = await projectHelpers ( sqlClientPool ) . getProjectById (
836+ environment . project
837+ ) ;
838+
839+ await hasPermission ( 'deployment' , 'cancel' , {
840+ project : project . id
841+ } ) ;
842+
843+ const data = {
844+ environment,
845+ project,
846+ } ;
847+
848+ userActivityLogger ( `User requested environment idling for '${ environment . name } '` , {
849+ project : '' ,
850+ event : 'api:idleEnvironment' ,
851+ payload : {
852+ project : project . name ,
853+ environment : environment . name
854+ }
855+ } ) ;
856+
857+ try {
858+ await createMiscTask ( { key : 'idle:environment' , data } ) ;
859+ return 'success' ;
860+ } catch ( error ) {
861+ sendToLagoonLogs (
862+ 'error' ,
863+ '' ,
864+ '' ,
865+ 'api:idleEnvironment' ,
866+ { environment : environment . id } ,
867+ `Environment idle attempt possibly failed, reason: ${ error } `
868+ ) ;
869+ throw new Error (
870+ error . message
871+ ) ;
872+ }
873+ } else {
874+ throw new Error (
875+ `environment is already idled`
876+ ) ;
877+ }
878+ } ;
879+
880+ export const unidleEnvironment = async (
881+ root ,
882+ args ,
883+ { sqlClientPool, hasPermission, userActivityLogger }
884+ ) => {
885+ const environment = await Helpers ( sqlClientPool ) . getEnvironmentById ( args . id ) ;
886+
887+ if ( ! environment ) {
888+ throw new Error (
889+ 'Invalid environment ID'
890+ ) ;
891+ }
892+
893+ await hasPermission ( 'environment' , 'view' , {
894+ project : environment . project
895+ } ) ;
896+
897+ if ( environment . idled ) {
898+ const project = await projectHelpers ( sqlClientPool ) . getProjectById (
899+ environment . project
900+ ) ;
901+
902+ await hasPermission ( 'deployment' , 'cancel' , {
903+ project : project . id
904+ } ) ;
905+
906+ const data = {
907+ environment,
908+ project,
909+ } ;
910+
911+ userActivityLogger ( `User requested environment unidle for '${ environment . name } '` , {
912+ project : '' ,
913+ event : 'api:unidleEnvironment' ,
914+ payload : {
915+ project : project . name ,
916+ environment : environment . name
917+ }
918+ } ) ;
919+
920+ try {
921+ await createMiscTask ( { key : 'unidle:environment' , data } ) ;
922+ return 'success' ;
923+ } catch ( error ) {
924+ sendToLagoonLogs (
925+ 'error' ,
926+ '' ,
927+ '' ,
928+ 'api:unidleEnvironment' ,
929+ { environment : environment . id } ,
930+ `Environment unidle attempt possibly failed, reason: ${ error } `
931+ ) ;
932+ throw new Error (
933+ error . message
934+ ) ;
935+ }
936+ } else {
937+ throw new Error (
938+ `environment is already unidled`
939+ ) ;
940+ }
941+ } ;
0 commit comments