Option Lists : REST APIs - Pass dynamic parameter in source URL

Morpheus should have feature to add Dynamic Param in Source URL for Rest API
Below scenario can explain this in more details.

  • We have created one catalog item for adding tags on cluster node (worker)
  • As per attachment, we have one dropdown to select cluster.
  • We have another dropdown to list all nodes (workers). Our requirement is : When we select cluster from first dropdown, second dropdown should auto-populate workers for the selected cluster
  • We are using this API to achieve this - /api/clusters/clusterId/workers \
  • Here in above API, clusterId is a dynamic parameter which needs to be passed from cluster selection.
  • As per the screenshot attached, we are using REST api for this.
  • We don’t find any way to pass clusterID in Source URL.
  • We need support here.

1 Like

Hi yes, can see how that would be useful. Please consider also proposing this in the ideas section of this forum. Ideas there are tracked and voted on.

In the absence of the ability to do that, I do think you can still achieve your goal.

The clusters API response contains all clusters, if you pass your selected cluster id to the next option list, you could make the same call for all clusters again in that option list and parse out the workers (which are apart of the response, and certainly include name and id for your select box control) from the cluster previously selected using a translation script. The workers are listed under servers in the JSON response but note this also includes the master, so that will need to be filtered out in the translation script.

This translation script achieves the above, parsing the data you need from clusters api response only:

// assumes input which provides cluster id is called clusterID)
let results = [];
let clusters = data["clusters"] ;
for(let i = 0; i < clusters.length; i++){
    if (clusters[i].id == input.clusterID) {
        // we've matched our cluster
        let servers = clusters[i]["servers"];
        for (let i2 = 0; i2 < servers.length; i2++) {
            // iterate the servers (master and workers)
            if(servers[i2]["computeServerType"]["nodeType"]=="kube-worker"){
                // its a worker build data for select
                let workerName = servers[i2]["name"];
                let workerId = servers[i2]["id"];
                results.push({name: workerName,value: workerId});
            }
        }
    }
}

Results array passed to the select node control

[
  { name: 'MKS 1-worker-1', value: 14 },
  { name: 'MKS 1-worker-3', value: 16 },
  { name: 'MKS 1-worker-2', value: 15 }
]
2 Likes

Thank you for your response and explaination first of all.
My question is : What will be the source URL for listing all workers / Nodes?

SOURCE URL <>/api/clusters/97/workers
Right now, this 97 is my cluster ID. How would it be possible to update this value? How my cluster ID will get overridden?

If I understood the ask correctly, the solution above negates the need for you to make that call to get the workers on a specific cluster ID. i.e /api/clusters/97/workers

You will call api/clusters again instead, and the above translation script will take/receive all clusters, filter on the cluster ID passed from the cluster select control (e.g 97) and then present all the workers of that cluster for use in the node/worker select input.

@Ollie_Phillips With some changes, the approach worked for me. Thank you.

1 Like