REST API Option List to fetch instance list excluding XaaS instances from the list

Hello,

Sharing a customer use case. No rocket science but just thought would be a good example to share which might be useful to some of you. :slight_smile:

While trying to create the REST Option List for fetching the instances, they were getting an error

Error Executing Javascript Translation Script for OptionTypeList CTX LB - Instance Option List - TypeError: Cannot read property ‘ip’ of undefined

Translation Script:

for(var i=0; i<data.instances.length; i++) {
    results.push({name: data.instances[i].name, value: data.instances[i].connectionInfo[0].ip});
}

The reason for failure was the the presence of XaaS instances in the instance list. XaaS instances have an empty value for connectionInfo parameter with no ip property hence was failing to read that property for those XaaS instances.

We modified the translation script which only lists the instances that have an ip flag in the connectionInfo property of the instance API response.

Translation Script:

for(var i=0; i<data.instances.length; i++) {
    if (data.instances[i].connectionInfo.length > 0) {
        results.push({ name: data.instances[i].name, value: data.instances[i].connectionInfo[0].ip });
    }
}

This js script filters out instances that meet the following criteria:

  1. The instance must have a connectionInfo property.
  2. The connectionInfo property must be an array with a length greater than 0.

If both conditions are met, the instance’s name and its associated IP address (if present) will be added to the results array. The code assumes that the connectionInfo property is an array, and if it has at least one element, it is considered valid, and its associated IP address is added to the results array. If connectionInfo is empty or doesn’t exist, the instance will not be included in the results.

Their further requirement was to export input as a tag and display the value as instance_name/ip_address for example testname-001/10.11.12.13.

The following translation script solves that requirement.

for(var i=0; i<data.instances.length; i++) {
    if (data.instances[i].connectionInfo.length > 0) {
        results.push({ name: data.instances[i].name, value: data.instances[i].name+"/"+data.instances[i].connectionInfo[0].ip });
    }
}

Thanks
Deepti

2 Likes

Good catch! Option Lists it’s often important to add if [param] exists statements, so it can pass the validation and account for situations the return is null. A lot of mine look something like this in the request (or) translation script:

if (input.targetGroup) {
  results.siteId = input.targetGroup
}
1 Like