@@ -1108,67 +1108,72 @@ export const getServiceContainersByServiceId: ResolverFn = async (
11081108 return await rows ;
11091109} ;
11101110
1111- export const environmentIdling = async (
1111+ export const idleOrUnidleEnvironment = async (
11121112 root ,
1113- input ,
1113+ {
1114+ input : {
1115+ environment,
1116+ project,
1117+ idle,
1118+ disableAutomaticUnidling
1119+ }
1120+ } ,
11141121 { sqlClientPool, hasPermission, userActivityLogger }
11151122) => {
1116- const environment = await Helpers ( sqlClientPool ) . getEnvironmentById ( input . id ) ;
1117- if ( ! environment ) {
1123+ const projectId = await projectHelpers ( sqlClientPool ) . getProjectIdByName ( project )
1124+ const projectData = await projectHelpers ( sqlClientPool ) . getProjectById ( projectId )
1125+ // @TODO : add permission to idle/unidle environment
1126+ await hasPermission ( 'deployment' , 'cancel' , {
1127+ project : projectId
1128+ } ) ;
1129+
1130+ const env = await Helpers ( sqlClientPool ) . getEnvironmentByNameAndProject ( environment , projectId )
1131+ const environmentData = env [ 0 ]
1132+ if ( ! environmentData ) {
11181133 throw new Error (
11191134 `Unauthorized: You don't have permission to "cancel" on "deployment"`
11201135 ) ;
11211136 }
11221137
1123- // @TODO : add permission to idle/unidle environment
1124- await hasPermission ( 'deployment' , 'cancel' , {
1125- project : environment . project
1126- } ) ;
1127-
11281138 // don't try idle if the environment is already idled or unidled
1129- if ( environment . idled && input . idle ) {
1139+ if ( environmentData . idled && idle ) {
11301140 throw new Error (
11311141 `Environment is already idled`
11321142 ) ;
11331143 }
1134- if ( ! environment . idled && ! input . idle ) {
1144+ if ( ! environmentData . idled && ! idle ) {
11351145 throw new Error (
11361146 `Environment is already unidled`
11371147 ) ;
11381148 }
11391149
1140- const project = await projectHelpers ( sqlClientPool ) . getProjectById (
1141- environment . project
1142- ) ;
1143-
11441150 const data = {
1145- environment,
1146- project,
1151+ environment : environmentData ,
1152+ project : projectData ,
11471153 idling : {
1148- idle : input . idle ,
1149- forceScale : input . disableAutomaticUnidling
1154+ idle : idle ,
1155+ forceScale : disableAutomaticUnidling
11501156 }
11511157 } ;
1158+
11521159 const auditLog : AuditLog = {
11531160 resource : {
1154- id : project . id . toString ( ) ,
1161+ id : projectData . id . toString ( ) ,
11551162 type : AuditType . PROJECT ,
1156- details : project . name ,
1163+ details : projectData . name ,
11571164 } ,
11581165 linkedResource : {
1159- id : environment . id . toString ( ) ,
1166+ id : environmentData . id . toString ( ) ,
11601167 type : AuditType . ENVIRONMENT ,
1161- details : `${ environment . name } , idled: ${ input . idle } , unidlingDisabled: ${ input . disableAutomaticUnidling } ` ,
1168+ details : `${ environmentData . name } , idled: ${ idle } , unidlingDisabled: ${ disableAutomaticUnidling } ` ,
11621169 } ,
11631170 } ;
1164- userActivityLogger ( `User requested environment idling for '${ environment . name } '` , {
1171+ userActivityLogger ( `User requested environment idling for '${ environmentData . name } '` , {
11651172 project : '' ,
1166- event : 'api:environmentIdling ' ,
1173+ event : 'api:idleOrUnidleEnvironment ' ,
11671174 payload : {
1168- project : project . name ,
1169- environment : environment . name ,
1170- idle : input . idle ,
1171- disableAutomaticUnidling : input . disableAutomaticUnidling ,
1175+ project : projectData . name ,
1176+ environment : environmentData . name ,
11721177 ...auditLog ,
11731178 }
11741179 } ) ;
@@ -1181,8 +1186,8 @@ export const environmentIdling = async (
11811186 'error' ,
11821187 '' ,
11831188 '' ,
1184- 'api:environmentIdling ' ,
1185- { environment : environment . id } ,
1189+ 'api:idleOrUnidleEnvironment ' ,
1190+ { environment : environmentData . id } ,
11861191 `Environment idle attempt possibly failed, reason: ${ error } `
11871192 ) ;
11881193 throw new Error (
@@ -1191,67 +1196,95 @@ export const environmentIdling = async (
11911196 }
11921197} ;
11931198
1194- export const environmentService = async (
1199+ export const stopOrStartEnvironmentService = async (
11951200 root ,
1196- input ,
1201+ {
1202+ input : {
1203+ environment,
1204+ project,
1205+ serviceName,
1206+ state
1207+ }
1208+ } ,
11971209 { sqlClientPool, hasPermission, userActivityLogger }
11981210) => {
1199- const environment = await Helpers ( sqlClientPool ) . getEnvironmentById ( input . id ) ;
1200- if ( ! environment ) {
1211+ const projectId = await projectHelpers ( sqlClientPool ) . getProjectIdByName ( project )
1212+ const projectData = await projectHelpers ( sqlClientPool ) . getProjectById ( projectId )
1213+ // @TODO : add permission to stop/start/restart environment
1214+ await hasPermission ( 'deployment' , 'cancel' , {
1215+ project : projectId
1216+ } ) ;
1217+
1218+ const env = await Helpers ( sqlClientPool ) . getEnvironmentByNameAndProject ( environment , projectId )
1219+ const environmentData = env [ 0 ]
1220+ if ( ! environmentData ) {
12011221 throw new Error (
12021222 `Unauthorized: You don't have permission to "cancel" on "deployment"`
12031223 ) ;
12041224 }
12051225
1206- // @TODO : add permission to restart service in environment
1207- await hasPermission ( 'deployment' , 'cancel' , {
1208- project : environment . project
1209- } ) ;
1210-
1211- const rows = await Helpers ( sqlClientPool ) . getEnvironmentServices ( environment . id )
1226+ const rows = await Helpers ( sqlClientPool ) . getEnvironmentServices ( environmentData . id )
12121227 let serviceExists = false ;
1213- if ( rows && rows . find ( e => e . name === input . name ) ) {
1214- serviceExists = true
1228+ if ( rows ) {
1229+ for ( const service of rows ) {
1230+ if ( service . name != serviceName ) {
1231+ continue ;
1232+ }
1233+ switch ( state ) {
1234+ case 'stop' :
1235+ if ( service . state === 'stopped' ) {
1236+ throw new Error (
1237+ `Service ${ serviceName } is already stopped`
1238+ ) ;
1239+ }
1240+ break ;
1241+ case 'start' :
1242+ if ( service . state === 'running' ) {
1243+ throw new Error (
1244+ `Service ${ serviceName } is already running`
1245+ ) ;
1246+ }
1247+ break ;
1248+ default :
1249+ break ;
1250+ }
1251+ serviceExists = true
1252+ }
12151253 }
12161254 if ( ! serviceExists ) {
12171255 throw new Error (
1218- `Service ${ input . serviceName } doesn't exist on environment`
1256+ `Service ${ serviceName } doesn't exist on environment`
12191257 ) ;
12201258 }
12211259
1222- const project = await projectHelpers ( sqlClientPool ) . getProjectById (
1223- environment . project
1224- ) ;
1225-
12261260 const data = {
1227- environment,
1228- project,
1261+ environment : environmentData ,
1262+ project : projectData ,
12291263 lagoonService : {
1230- name : input . serviceName ,
1231- state : input . state
1264+ name : serviceName ,
1265+ state : state
12321266 }
12331267 } ;
12341268
12351269 const auditLog : AuditLog = {
12361270 resource : {
1237- id : project . id . toString ( ) ,
1271+ id : projectData . id . toString ( ) ,
12381272 type : AuditType . PROJECT ,
1239- details : project . name ,
1273+ details : projectData . name ,
12401274 } ,
12411275 linkedResource : {
1242- id : environment . id . toString ( ) ,
1276+ id : environmentData . id . toString ( ) ,
12431277 type : AuditType . ENVIRONMENT ,
1244- details : `${ environment . name } , service: ${ input . serviceName } , state: ${ input . state } ` ,
1278+ details : `${ environmentData . name } , service: ${ serviceName } , state: ${ state } ` ,
12451279 } ,
12461280 } ;
1247- userActivityLogger ( `User requested environment service change for '${ environment . name } '` , {
1281+
1282+ userActivityLogger ( `User requested environment service change for '${ environmentData . name } '` , {
12481283 project : '' ,
1249- event : 'api:environmentService ' ,
1284+ event : 'api:stopOrStartEnvironmentService ' ,
12501285 payload : {
1251- project : project . name ,
1252- environment : environment . name ,
1253- idle : input . idle ,
1254- disableAutomaticUnidling : input . disableAutomaticUnidling ,
1286+ project : projectData . name ,
1287+ environment : environmentData . name ,
12551288 ...auditLog
12561289 }
12571290 } ) ;
@@ -1264,8 +1297,8 @@ export const environmentService = async (
12641297 'error' ,
12651298 '' ,
12661299 '' ,
1267- 'api:environmentService ' ,
1268- { environment : environment . id } ,
1300+ 'api:stopOrStartEnvironmentService ' ,
1301+ { environment : environmentData . id } ,
12691302 `Environment idle attempt possibly failed, reason: ${ error } `
12701303 ) ;
12711304 throw new Error (
0 commit comments