Hey @Alex,
So you can utilize the “Configuration Phase”, which triggers calls before the payload is submitted.
-
Create this Groovy task:
println("${spec.encodeAsJson().toString()}")
-
Put that task in a provisioning workflow under the “Configuration Phase”.
-
Deploy a new VM with that workflow selected. The configuration phase will run and task output will show the type of data you have available.
Essentially, there is a variable spec
that is available in this phase. You can modify that JSON payload, and then output it.
Note, you must have your last configuration phase task only output the spec JSON, no other stdout. You must also set the Result Type
of the task to “JSON”.
A few examples in the forums:
And an example PowerShell Core code I use. This:
- Requests an IP from an IP Pool via the Morpheus API.
- Sets a temp name on the request for the IP.
- Updates the VM hostname based on the IP address hash.
- Echoes out the spec JSON body to pick the changes up.
$configJson = '<%=spec.encodeAsJson().toString()%>'
$bearer = "<%=morpheus.apiAccessToken%>"
$morphUrl = '<%=morpheus.applianceUrl%>'
$ContentType = 'application/json'
$morphHeader = @{
"Authorization" = "BEARER $bearer"
}
$poolApi = 'api/networks/pools/'
$configJson = $configJson | convertfrom-json -Depth 10
$pool = $configJson.networkInterfaces.network.pool.id
### Functions
function ipv4ToHexString($ipv4){
# Validation
$valid = $true
$ipv4.split(".") | foreach { if([int]$_ -ge 0 -and [int]$_ -le 255) { } else { $valid = $false } }
if($valid -eq $true){
# Conversion
$hexString = ""
$hexList = @("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")
$ipv4.split(".") | foreach {
$mod = [int]$_ % 16
$first = $hexList[(([int]$_ - $mod) / 16)]
$second = $hexList[$mod]
$hexString += $first+$second
}
return $hexString
}
else{
return $false
}
}
#################################################################################
### Script
$tempName = get-random
$poolBody = @"
{
"createDns": false,
"networkPoolIp": {
"hostname": $tempName
}
}
"@
# Request IP
$ip = (Invoke-WebRequest -Method POST -Uri ($morphUrl + $poolApi + $pool + '/ips') -SkipCertificateCheck -Headers $morphHeader -Body $poolBody -ContentType $ContentType).content | convertfrom-json
$ip = $ip.networkPoolIp
# Generate Name Hash
$hashName = (ipv4ToHexString -ipv4 $ip.ipAddress).toUpper()
$hashName = "$hashName"
$configJson.instance.name = $hashName
$configJson.instance.hostName = $hashName
$configJson = $configJson | ConvertTo-Json -Depth 10
$spec = @"
{
"spec": $configJson
}
"@
$spec
#spec #configurationphase