If-else condition in instance service catalogue

Hello Experts,

I need some guidelines. Below is my sample JSON parameters from instance builder service catalogue.

“volumes”: [
{
“id”: 19206,
“name”: “root”,
“controllerId”: 20068,
“controllerMountPoint”: “20068:0:6:0”,
“resizeable”: true,
“planResizable”: true,
“rootVolume”: true,
“unitNumber”: “0”,
“typeId”: 1,
“configurableIOPS”: false,
“maxStorage”: 21474836480,
“displayOrder”: 0,
“maxIOPS”: null,
“uuid”: “4d91d7f2-3fcc-4af3-95c5-b4221d3d10b4”,
“uniqueId”: null,
“externalId”: “2000”,
“internalId”: “[datastore3-1] cent-image/cent-image.vmdk”,
“virtualImageId”: 1609,
“size”: 20,
“minStorage”: 17179869184,
“vId”: 1609,
“volumeCustomizable”: true,
“readonlyName”: false,
“storageType”: 1,
“datastoreId”: “auto”
}

At the vCenter level, there are multiple datastores. So, I am trying to achieve like when user define the virtual disk size less than 50GB then datastore Id will be 123 (example value), if user define the size above 100GB then datastore Id will be 456 etc…

How we can achieve for that kind of use case ?

Thanks,
Aung

So you would do something like:

  1. Attach an Input named Disk Size with a field name of diskSize. This would create the variable <%=customOptions.diskSize%>
  2. Map the variable into the config similar to this
“volumes”: [
{
“id”: 19206,
“name”: “root”,
“controllerId”: 20068,
“controllerMountPoint”: “20068:0:6:0”,
“resizeable”: true,
“planResizable”: true,
“rootVolume”: true,
“unitNumber”: “0”,
“typeId”: 1,
“configurableIOPS”: false,
“maxStorage”: 21474836480,
“displayOrder”: 0,
“maxIOPS”: null,
“uuid”: “4d91d7f2-3fcc-4af3-95c5-b4221d3d10b4”,
“uniqueId”: null,
“externalId”: “2000”,
“internalId”: “[datastore3-1] cent-image/cent-image.vmdk”,
“virtualImageId”: 1609,
“size”: "<%=customOptions.diskSize.toLong()%>",
“minStorage”: 17179869184,
“vId”: 1609,
“volumeCustomizable”: true,
“readonlyName”: false,
“storageType”: 1,
“datastoreId”: "<%= if (customOptions.diskSize.toLong() <= 50) {123} else {456}%>"
}

Note the ‘toLong()’ is to convert a string to an integer. Overall, the variables are groovy and you can use standard groovy language to create more complex decisions.

Thanks a lot, @cbunge. I will try this.

Another query for the load balancer. Let’s say if I says enable load balancer " Yes/No" using radio list or selection list. Can I replace {123} block with below load balancer json block.

"loadBalancer": [
  {
    "internalPort": 443,
    "externalPort": 443,
    "loadBalancePort": null,
    "loadBalanceProtocol": "https",
    "externalAddressCheck": false,
    "protocol": "https",
    "balanceMode": "leastconnections",
    "vipPort": 443,
    "vipHostname": "telkom-indonesia-(master)-024.localdomain",
    "name": "${tenant}-${sequence.toString().padLeft(3,'0')}",
    "vipName": "vs_testlb_https",
    "id": 17,
    "sslCert": 0,
    "vipAddress": "10.3.27.30",
    "vipBalance": "leastconnections",
    "vipSticky": "sourceip",
    "vipShared": "no"
  }

If we select “No” Input LB json will be “loadBalancer”: .

Appreciate your feedback.

Thanks,
Aung

Unfortunately I have not found a way to null or input an object into the JSON. The config validates JSON on submit, and since the input is missing the catalog won’t render correctly.

Sometimes we’re able to have the objects already within the config and have an invalid value that would result in it not being created. Then if you say ‘yes’ to a loadbalancer you input the correct value.

Check out this example @rboyd created about disk adds. Uses the same concept:

Noted @cbunge, I will check this.

Thanks,
Aung

1 Like