def builder = new groovy.json.JsonBuilder()
def root = builder.auth {
identity {
methods (['password'])
password {
user {
name {
usrName
domain {
id usrDomain
}
password "openstack"
}
}
}
scope {
project {
name prjName
domain {
id 'default'
}
}
}
}
}
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
assert output:
Assertion failed:
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password": {"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
| | |
| | false
| '{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
{"auth":{"methods":["password"],"password":{"user":{"name":{"domain":{"id":"Default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}
Basically the "identity" key is ignored. I have seen that there is a method called "identity" belonging to DefaultGroovyMethods, but I have no ideea how to overcome this. First time writing Groovy, have a little background in Python. Any help is apreciated!
Complete solution done with Szymon Stepniak's answer (and other corrections to json declaration):
def builder = new groovy.json.JsonBuilder()
builder auth: [
identity: {
methods(['password'])
password {
user {
name "$usrName"
domain {
id usrDomain
}
password "openstack"
}
}
},
scope: {
project {
name prjName
domain {
id 'default'
}
}
}
]
assert builder.toString() == '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","domain":{"id":"default"},"password":"openstack"}}},"scope":{"project":{"name":"admin","domain":{"id":"default"}}}}}'
Just in case someone wants to follow along this example.