You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error retrieving ID of template: json: cannot unmarshal object into Go struct field alias.childtemplates of type []interface {} #15
Strange enough, VM deployment using a unit test works, can't reproduce this error with this SDK itself.
Based on the mock test kindly provided by @Pearl1594 (just removed the HTTP mock server and hardcoded required params/values) I've tried the following against an instance with API level 4.11:
func TestVirtualMachineService_DeployVirtualMachineReal(t *testing.T) {
var URL="https://<DOMAIN>/client/api"
var APIKEY = ...
var SECRETKEY = ...
var zoneid = "23ec4069-ede9-46b4-9236-d8e373a4ef59"
var templateid = "450ec62f-16b1-4d04-a00b-a6fd4933e429"
var networkids = []string{"690ef536-3996-4aaf-b45d-5501f079d6eb"}
var serviceofferingid = "171aa1ff-3c93-4030-946a-d16e34774a76"
client := NewAsyncClient(URL, APIKEY, SECRETKEY, true)
params := client.VirtualMachine.NewDeployVirtualMachineParams(serviceofferingid, templateid, zoneid)
params.SetName("testDummyVM")
params.SetNetworkids(networkids)
resp, err := client.VirtualMachine.DeployVirtualMachine(params)
if err != nil {
t.Errorf("Failed to deploy VM with given specs due to %v", err)
return
}
if resp == nil || resp.Name != "testDummyVM" {
t.Errorf("Failed to deploy VM with given specs")
}
}
Run single unit test:
go test -v -run TestVirtualMachineService_DeployVirtualMachineReal
Result: VM appears in the management UI and the test reports success.
=== RUN TestVirtualMachineService_DeployVirtualMachineReal
--- PASS: TestVirtualMachineService_DeployVirtualMachineReal (79.34s)
This appears to be an issue with parsing the listTemplate API response, @nishiokay could you :
List the template (which has child templates) via the sdk
Provide the response to the listTemplate response with the templateid 450ec62f-16b1-4d04-a00b-a6fd4933e429 (or whichever one you're having issues with on terraform)
Still reproduces on current main (2026). Minimal repro:
body:=`{"id":"t1","name":"tmpl","childtemplates":{"foo":"bar"}}`vartmpl cloudstack.Templateerr:=json.Unmarshal([]byte(body), &tmpl)
// json: cannot unmarshal object into Go struct field alias.childtemplates of type []interface {}
Root cause:childtemplates is declared "type": "set" in listApis.json, and the generator maps set → []interface{} (mapType, generate.go). But some CloudStack/CloudPlatform versions return childtemplates as a JSON object, not an array, so json.Unmarshal fails — and because it is hit through the struct’s custom UnmarshalJSON (type alias Template), the whole template (and thus listTemplates / GetTemplateID) fails. The field appears as []interface{} in 7 template structs (Template, CopyTemplateResponse, CreateTemplateResponse, PrepareTemplateResponse, RegisterTemplate, UpdateTemplateResponse, …).
Suggested fix: narrowly special-case this field — make childtemplatesinterface{} instead of []interface{} (interface{} accepts an object, an array, or null). It must be a field-level special-case, not a change to the set→[]interface{} mapping, which would touch ~100 unrelated fields. It is a small type change (slice → interface{}) on a field that is already opaque and currently unusable on the affected versions, so net it restores functionality.
I have this fix + a regression test ready (the repro above passes after the change, fails before). Happy to open a PR if a maintainer is OK with the interface{} approach — flagging the minor type change for visibility before I do.
Activity
synergiator commentedon Nov 28, 2021
Strange enough, VM deployment using a unit test works, can't reproduce this error with this SDK itself.
Based on the mock test kindly provided by @Pearl1594 (just removed the HTTP mock server and hardcoded required params/values) I've tried the following against an instance with API level 4.11:
Run single unit test:
Result: VM appears in the management UI and the test reports success.
yadvr commentedon Nov 29, 2021
Perhaps this is no longer an issue now, can you check and confirm @nishiokay @synergiator ? Thanks.
davidjumani commentedon Nov 29, 2021
This appears to be an issue with parsing the listTemplate API response, @nishiokay could you :
synergiator commentedon Dec 27, 2021
In the test asset, there is no method to test listing templates.
jmsperu commentedon Jun 20, 2026
Still reproduces on current
main(2026). Minimal repro:Root cause:
childtemplatesis declared"type": "set"inlistApis.json, and the generator mapsset→[]interface{}(mapType, generate.go). But some CloudStack/CloudPlatform versions returnchildtemplatesas a JSON object, not an array, sojson.Unmarshalfails — and because it is hit through the struct’s customUnmarshalJSON(type alias Template), the whole template (and thuslistTemplates/GetTemplateID) fails. The field appears as[]interface{}in 7 template structs (Template, CopyTemplateResponse, CreateTemplateResponse, PrepareTemplateResponse, RegisterTemplate, UpdateTemplateResponse, …).Suggested fix: narrowly special-case this field — make
childtemplatesinterface{}instead of[]interface{}(interface{} accepts an object, an array, or null). It must be a field-level special-case, not a change to theset→[]interface{}mapping, which would touch ~100 unrelated fields. It is a small type change (slice → interface{}) on a field that is already opaque and currently unusable on the affected versions, so net it restores functionality.I have this fix + a regression test ready (the repro above passes after the change, fails before). Happy to open a PR if a maintainer is OK with the
interface{}approach — flagging the minor type change for visibility before I do.