Laurent Michel
Laurent Michel

Reputation: 1321

How can I query an internally deployed API from my Jelastic environment?

I am writing a Jelastic manifest where I deploy two nodes. On one node, I have an API that I need to query in order to setup the second. Something along these lines:

type: install
name: My test

nodes:
- count: 1
  cloudlets: 4
  nodeGroup: auth
  nodeType: docker
  image: my-api:latest
- count: 1
  cloudlets: 16
  nodeGroup: cp
  nodeType: docker
  image: some-service:latest

onInstall:
  - script: |
      import com.hivext.api.core.utils.Transport;
        
      try {
        const body = new Transport().get("http://${nodes.auth.master.intIP}:9011/api/key", {
            "Authorization": "my-api-key"
        });    
        
        return { result: 0, body: body };
      } catch (e) {
        return {
            type: "error",
            message: "unknown error: " + e
        };
      }

In my script, when I do

const body = new Transport().get("http://www.google.com"); 

it works, I get the body content of the google page. However,

const body = new Transport().get("http://${nodes.auth.master.intIP}:9011/api/key", {
  "Authorization": "my-api-key"
});

returns

ERROR: script.response: {"type":"error","message":"unknown error: JavaException: java.io.IOException: Failed to select a proxy"}

What am I doing wrong? How can I query my service in a script like in the above snippet? When I curl it through regular cmd, then it works:

curl -s -H "Authorization: my-api-key" http://${nodes.auth.master.intIP}:9011/api/key

Also, incidentally, where can I find the documentation of com.hivext.api.core.utils.Transport?

Upvotes: 1

Views: 282

Answers (1)

Virtuozzo
Virtuozzo

Reputation: 1993

You can't access your environment node via internal IP from the script (at least it's not guaranteed). As a workaround, you can access your node via public IP or endpoint. If public IP or endpoint is not applicable in your case and your service must be accessed only internally, you can try to access your node via curl and ExecCmd API. For example:

type: install
name: My test

nodes:
- count: 1
  cloudlets: 4
  nodeGroup: auth
  nodeType: docker
  image: my-api:latest
- count: 1
  cloudlets: 16
  nodeGroup: cp
  nodeType: docker
  image: some-service:latest
  
onInstall:
  - script: |
      function execInternalApi(nodeId, url) {
        let resp = api.env.control.ExecCmdById({
          envName: '${env.name}', 
          nodeId: nodeId,
          commandList: toJSON([{
            command: 'curl -fSsl -H "Authorization: my-api-key" \'' + url + '\''
          }])
        })
        
        if (resp.result != 0) return resp
        
        return { result: 0, out: resp.responses[0].out }
      }
      
      let body = execInternalApi(${nodes.auth.master.id}, 'http://localhost:9011/api/key');
      
      return { result: 0, body: body };

Upvotes: 1

Related Questions