Dynamically calculate and set the maxMemory and maxCores property using customOptions in a catalog

This code is used to dynamically calculate and set the maxMemory property based on the value of customOptions?.ram in a catalog config. If customOptions?.ram is defined and not empty, it calculates the maximum memory in bytes (assuming customOptions?.ram is provided in gigabytes). Otherwise, it sets maxMemory to 0%. Similarly, it can be used to dynamically set the maximum number of CPU cores based on the value of customOptions.cpu. If customOptions.cpu is provided, it uses that value. Otherwise, it defaults to 0.

  "servicePlanOptions": {
      "maxCores": "<%=customOptions.cpu ?: 0%>",
      "coresPerSocket": 1,
      "maxMemory": "<%=if (customOptions?.ram && customOptions?.ram != ''){customOptions?.ram?.toLong() * 1024 * 1024 * 1024} else {0}%>",
      "forPlan": 1052,
     },

Putting it all together:

1 - If customOptions.cpu is defined and not null, the value of "maxCores" will be set to customOptions.cpu.
2 - If customOptions.cpu is not defined or null, the value of "maxCores" will be set to 0.
3 - if (customOptions?.ram && customOptions?.ram != ''){...} else {...}: This is a conditional statement. It checks if customOptions?.ram is defined and not an empty string. If the condition is true, it executes the code in the first block, otherwise, it executes the code in the second block.
4 - customOptions?.ram?.toLong() * 1024 * 1024 * 1024: If the condition is true, this line multiplies the value of customOptions?.ram (converted to a long integer) by 1024^3, effectively converting it from gigabytes to bytes.
5 - else {0}: If the condition is false (i.e., customOptions?.ram is either not defined or an empty string), this sets the value to 0.

3 Likes