|
| 1 | +(function execute(inputs, outputs) { |
| 2 | + /* |
| 3 | + This funtion retrieve the IngressHost URL |
| 4 | + Then terminate or resume the workflow |
| 5 | + */ |
| 6 | + |
| 7 | + // Inputs |
| 8 | + var url=inputs['cf_url'] + '/2.0/api/graphql'; |
| 9 | + var token=inputs['token']; |
| 10 | + var workflowName=inputs['workflow_name']; |
| 11 | + var runtime=inputs['runtime']; |
| 12 | + var action=inputs['action']; |
| 13 | + |
| 14 | + /* |
| 15 | + action value is oringally from the Classic side so approve or deny |
| 16 | + It needs to be translated into workflow action: resumeWorkflow or terminateWorkflow |
| 17 | + */ |
| 18 | + if (action == "approve"){ |
| 19 | + action="resumeWorkflow" |
| 20 | + } else { |
| 21 | + action="terminateWorkflow" |
| 22 | + } |
| 23 | + |
| 24 | + // Variables |
| 25 | + var requestBody; |
| 26 | + var responseBody; |
| 27 | + var status; |
| 28 | + var sm; |
| 29 | + var data; |
| 30 | + var ingressHost; // the URL of the runtime ingress |
| 31 | + var namespace; // the namespace where the runtime is installed as |
| 32 | + // it could be different |
| 33 | + /* |
| 34 | + Getting the ingress host |
| 35 | + */ |
| 36 | + try { |
| 37 | + sm = new sn_ws.RESTMessageV2(); |
| 38 | + sm.setEndpoint(url); |
| 39 | + sm.setHttpMethod("post"); |
| 40 | + sm.setRequestHeader("Authorization", token); |
| 41 | + sm.setRequestHeader("Content-type", "application/json"); |
| 42 | + data=JSON.stringify({ |
| 43 | + "query": "query getRuntime($runtime: String!) { runtime(name: \$runtime) { metadata {\n name\n namespace}\n ingressHost}}", |
| 44 | + "variables": { |
| 45 | + "runtime": runtime |
| 46 | + } |
| 47 | + }); |
| 48 | + sm.setRequestBody(data); |
| 49 | + response = sm.execute(); //Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password. |
| 50 | + responseBody = response.haveError() ? response.getErrorMessage() : response.getBody(); |
| 51 | + status = response.getStatusCode(); |
| 52 | + } catch(ex) { |
| 53 | + responseBody = ex.getMessage(); |
| 54 | + status = '500'; |
| 55 | + } finally { |
| 56 | + requestBody = sm ? sm.getRequestBody():null; |
| 57 | + } |
| 58 | + gs.info("Response ingress: " + responseBody); |
| 59 | + gs.info("HTTP Status: " + status); |
| 60 | + |
| 61 | + ingressHost=JSON.parse(responseBody).data.runtime.ingressHost |
| 62 | + namespace=JSON.parse(responseBody).data.runtime.metadata.namespace |
| 63 | + |
| 64 | + /* |
| 65 | + Stopping or resuming the workflow |
| 66 | + */ |
| 67 | + try{ |
| 68 | + sm = new sn_ws.RESTMessageV2(); |
| 69 | + sm.setEndpoint(ingressHost + '/app-proxy/api/graphql'); |
| 70 | + sm.setHttpMethod("post"); |
| 71 | + sm.setRequestHeader("Authorization",token); |
| 72 | + sm.setRequestHeader("Content-type", "application/json"); |
| 73 | + data=JSON.stringify({ |
| 74 | + "query": "mutation " + action +"($namespace: String!, $workflowName: String!) { " + action + "(namespace: \$namespace, workflowName: \$workflowName)}", |
| 75 | + "variables": { |
| 76 | + "namespace": namespace, |
| 77 | + "workflowName": workflowName |
| 78 | + } |
| 79 | + }); |
| 80 | + sm.setRequestBody(data); |
| 81 | + |
| 82 | + response = sm.execute(); //Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password. |
| 83 | + responseBody = response.haveError() ? response.getErrorMessage() : response.getBody(); |
| 84 | + status = response.getStatusCode(); |
| 85 | + } catch(ex) { |
| 86 | + responseBody = ex.getMessage(); |
| 87 | + status = '500'; |
| 88 | + } finally { |
| 89 | + requestBody = sm ? sm.getRequestBody():null; |
| 90 | + } |
| 91 | + gs.info("Request Body: " + requestBody); |
| 92 | + gs.info("Response: " + responseBody); |
| 93 | + gs.info("HTTP Status: " + status); |
| 94 | + |
| 95 | +})(inputs, outputs); |
0 commit comments