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.
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:
- The instance must have a
connectionInfo
property. - 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