
This corrects some typos in earlier documentation patches, removes outdated API reference documentation, and organizes all API reference documentation under one chapter. This also leaves room for future API versions and more specific API reference documentation being broken out into sub-sections. Change-Id: I5391a1acc7d1669207b3d10039a196d026216f40
3.3 KiB
Using the Hosts API
Here we will assume that we already have a ~cratonclient.client.Client
instance configured with the appropriate authentication method (as
demonstrated in usage-auth
).
Listing Hosts
The Hosts API implements pagination. This means that by default, it does not return all hosts known to Craton. To ignore page limits and offsets, we can allow cratonclient to do handle pagination for us:
for host in craton.hosts.list():
print_host_info(host)
By default ~cratonclient.v1.hosts.HostManager.list
will handle
pagination for you. If, instead, you want to handle it yourself you will
want to do something akin to:
= list(craton.hosts.list(autopaginate=False))
first_page_of_hosts = first_page_of_hosts[-1].id
marker_id = list(craton.hosts.list(
second_page_of_hosts =False,
autopaginate=marker_id,
marker
))= second_page_of_hosts[-1].id
marker_id = list(craton.hosts.list(
third_page_of_hosts =False,
autopaginate=marker_id,
marker
))# etc.
A more realistic example, however, might look like this:
= None
hosts_list = None
marker while hosts_list and hosts_list is not None:
= list(craton.hosts.list(
hosts_list =marker,
marker=False,
autopaginate
))# do something with hosts_list
if hosts_list:
= hosts_list[-1].id marker
This will have the effect of stopping the while loop when you
eventually receive an empty list from
craton.hosts.list(...)
.
Creating Hosts
Hosts live inside either a Region or Cell in Craton. To create a host, one needs:
- A unique name
- A unique IP address
- A "device type" (this is freeform), e.g., "server", "container", "nova-vm", etc.
- A cloud ID
- A region ID
= craton.hosts.create(
host ='my-host-0',
name='127.0.1.0',
ip_address='server',
device_type=cloud_id,
cloud_id=region_id,
region_id='This is my host, there are many like it, but this is mine.',
note={
variables'some-var': 'some-var-value',
}, )
Retrieving a Specific Host
Hosts can be retrieved by id.
= craton.hosts.get(1) host
Using a Host's Variables
Once we have a host we can introspect its variables like so:
= craton.hosts.get(host_id)
host = host.variables.get() host_vars
To update them:
= {
updated_vars 'var-a': 'new-var-a',
'var-b': 'new-var-b',
'updated-var': 'updated value',
}**updated_vars) host.variables.update(
To delete them:
'var-a', 'var-b', 'updated-var') host.variables.delete(
Updating a Host
We can update a host's attributes (but not its variables) like so:
craton.hosts.update(
host_id,='new name',
name='Updated note.',
note )
Most attributes that you can specify on creation can also be specified for updating the host as well.
Deleting a Host
We can delete with only its id:
craton.hosts.delete(host_id)