Model for a resource in UpCloud, e.g. server, storage device, IP address, etc.
Together with the Session class, this class is the most core of the Groovy UpCloud library. You probably don't use this directly, but you could. This class allows for dynamic creation of UpCloud resource representations and their properties.
For example, at the time of this writing, the UpCloud account API returns credits
and username
properties, but no email
property. Not saying that it should, but it could be useful. Now, if a
future revision of the API adds that property, it would be immediately available in the Account
resource of this library, even without a library update.
For example, if the server responded with:
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
{
"account": {
"credits": 1000,
"username": "foo_bar",
"email": "[email protected]"
}
}
Above, the email
JSON field is a new one. In the application code, it would be available like so:
upcloud.account { response ->
assert response.account.email == "[email protected]"
}
Similarly, if a future revision of the UpCloud API would add a completely new property to, say, a server, represented as a new kind of resource, then it would be visible in the responses generated by this library. For example:
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
{
"server": {
...
"docker_containers": {
"docker_container": [
{
"image": "ubuntu:latest",
"state": "running"
},
{
"image": "nginx:latest",
"state": "running"
}
]
},
...
}
}
Above, the docker_containers
JSON field is an imaginary one, but because the server returned it, it
would be available in the application callback as follows:
server.load { response ->
assert response.server instanceof fi.linuxbox.upcloud.resource.Server
// Above is old news.
// Now for the fun part:
assert response.server.dockerContainers instanceof List
def c = response.server.dockerContainers
assert c.every { it.class.name == 'fi.linuxbox.upcloud.resource.DockerContainer' }
assert c[0].state == "running"
}
Here, it is not possible to use instanceof
because the DockerContainer
class is not available
at compile time, i.e. your compiler would complain about the reference. But at runtime the class is as real
as any other resource class.
Type Params | Return Type | Name and description |
---|---|---|
|
Object |
asType(Class clazz) Converts this resource to a Map |
|
AbstractSession<?> |
getHTTP() |
|
def |
methodMissing(String name, def args) MOP method that allows resource properties to be set in a fluent fashion. |
|
def |
proj(List<String> properties) Returns a projection of this resource. |
|
def |
propertyMissing(String name) MOP method that allows resource properties to be missing. |
|
def |
propertyMissing(String name, def arg) MOP method that allows resource properties to be set. |
|
String |
toString() Returns a string representation of the UpCloud resource. |
|
def |
wrapper() Returns this resource wrapped in another resource. |
Designated, and only, constructor.
kwargs.SESSION
- The Session instance. This is used by the resource specific API wrappers, not directly by this class.kwargs.META
- The META instance. This is received from the HTTP implementation.kwargs.repr
- The Map Converts this resource to a Map
This is used as
Instead of using direct object-to-JSON mapping, this intermediary representation is used because
resource as Map
and meant to be invoked only from the Session class, just before the resource
is put into the queue of to-be-sent requests.
clazz
- Map. Nothing else supported.
MOP method that allows resource properties to be set in a fluent fashion.
Allows for code like server.name('my server').description('My server')
to set two properties for
the server resource.
name
- Name of the property.args
- Value of the property (must be a single element argument array).Returns a projection of this resource.
Projection is a copy of this resource with some of the properties removed.
Some of the UpCloud APIs specifically disallow some of the resource properties from being sent in the requests, and this method is used in those API calls.
properties
- A list of property names to exclude.MOP method that allows resource properties to be missing.
Allows for code like if (server.name) ...
without actually defining Java Bean property for the name.
name
- Name of the property.null
.MOP method that allows resource properties to be set.
Allows for code like server.name = "my server"
without actually defining Java Bean property for the
name.
name
- Name of the property.arg
- Value of the property.Returns a string representation of the UpCloud resource.
This is meant to be human readable, not as an API.
Returns this resource wrapped in another resource.
Many of the UpCloud APIs require a wrapping JSON object to be sent into the resources, and this method is used in those places.
Groovy Documentation