Edit Instance Configuration Before Deployment

Good morning!

I’m attempting to edit a GCP instance deployment to attach a public IP in the Configuration Phase of a Workflow using Python. I feel I’m close but something is just not clicking for it to stick I think. This is my example code with the IP changed (task outputs as JSON):

import json
newIP="255.255.255.255"
morpheus['spec']['config']['externalIpType']=newIP
morpheus['spec']['server']['config']['externalIpType']=newIP
morpheus['spec']['customOptions']['externalIpType']=newIP
json.dumps(morpheus)

I’ve also tried not doing the output of JSON and just modifying the dictionary. Through my various tests, the config data from using the GUI to choose the IP and from injecting my code above, end up being almost identical (except for the unique values) at the end of the task output.

If I print the morpheus variable on the instance after deployment using a task, I see the externalIpType was not set like it showed in the task output. Is there a way to “apply” the changes that I’m missing?

1 Like

I have played a bit with the externalIp and I think I have some code on it somewhere

Thanks for the help, Jabro! Here is the final result that worked, for getting the changes to apply. Basically I was missing the print() so the output would go to standard out. This would require the output type of the task to be JSON.

import json
newIP="255.255.255.255"
morpheus['spec']['config']['externalIpType']=newIP
morpheus['spec']['server']['config']['externalIpType']=newIP
morpheus['spec']['customOptions']['externalIpType']=newIP
print(json.dumps(morpheus))

Below is another example using PWSH. Note that Python stores all of the values in a single variable of morpheus but other languages can work differently. In this case, PWSH stores the spec as its own variable, so we work with it directly:

  $newIP = '255.255.255.255'
  $configObject = '<%=spec.encodeAsJson().toString()%>' | ConvertFrom-Json -Depth 10

  $configObject.config.externalIpType = $newIP
  $configObject.server.config.externalIpType = $newIP
  $configObject.customOptions.externalIpType = $newIP

  $configJson = $configObject | ConvertTo-Json -Depth 10

  $spec = @"
  {
      "spec": $configJson
  }
  "@

  Write-Host $spec -NoNewline

Similar one with the logic of a selection of a custom option to set value of another which is hidden input type

import json
# Declare the lifecycle and OU Name option list as a variable with contents
updatedLifecycle={"P":"PROD", "U":"UAT"}
updatedOU={"P":"Prod", "U":"NON-Prod"}

configspec = morpheus['spec']
env=configspec['customOptions']['Env']

# Check if the value if env is present in lifecycle and OU Name
if env in updatedLifecycle and env in updatedOU:
    # Set the value of lifecycle and ou name option type based on the value of env selected by user
    configspec['config']['customOptions']['LifecycleRole'] = updatedLifecycle[env]
    configspec['server']['config']['customOptions']['LifecycleRole'] = updatedLifecycle[env]
    configspec['customOptions']['customOptions']['LifecycleRole'] = updatedLifecycle[env]
    configspec['config']['customOptions']['LifecycleRole'] = updatedLifecycle[env]
    configspec['customOptions']['LifecycleRole'] = updatedLifecycle[env]
    configspec['config']['customOptions']['OUName'] = updatedOU[env]
    configspec['server']['config']['customOptions']['OUName'] = updatedOU[env]
    configspec['customOptions']['customOptions']['OUName'] = updatedOU[env]
    configspec['config']['customOptions']['OUName'] = updatedOU[env]
    configspec['customOptions']['OUName'] = updatedOU[env]
    newspec = {}
    newspec['spec'] = configspec
    print(json.dumps(newspec))