| 1 |
Using the Client Programmatically |
| 2 |
================================= |
| 3 |
|
| 4 |
Authentication |
| 5 |
-------------- |
| 6 |
|
| 7 |
Authenticating is necessary to use every feature of the client. |
| 8 |
|
| 9 |
To create the client, create an instance of the Client class. |
| 10 |
The auth url, username, password, and project name must be specified in the |
| 11 |
call to the constructor. |
| 12 |
|
| 13 |
.. testcode:: |
| 14 |
|
| 15 |
from troveclient.v1 import client |
| 16 |
tc = client.Client(username="testuser", |
| 17 |
password="PASSWORD", |
| 18 |
project_id="test_project", |
| 19 |
region_name="EAST", |
| 20 |
auth_url="http://api-server:5000/v2.0") |
| 21 |
|
| 22 |
The default authentication strategy assumes a keystone compliant auth system. |
| 23 |
|
| 24 |
Once you have an authenticated client object you can make calls with it, |
| 25 |
for example: |
| 26 |
|
| 27 |
.. testcode:: |
| 28 |
|
| 29 |
flavors = tc.flavors.list() |
| 30 |
datastores = tc.datastores.list() |
| 31 |
|
| 32 |
Instances |
| 33 |
--------- |
| 34 |
|
| 35 |
The following example creates a 512 MB instance with a 1 GB volume: |
| 36 |
|
| 37 |
.. testcode:: |
| 38 |
|
| 39 |
from troveclient.v1 import client |
| 40 |
tc = client.Client(username="testuser", |
| 41 |
password="PASSWORD", |
| 42 |
project_id="test_project", |
| 43 |
region_name="EAST", |
| 44 |
auth_url="http://api-server:5000/v2.0") |
| 45 |
|
| 46 |
flavor_id = '1' |
| 47 |
volume = {'size':1} |
| 48 |
databases = [{"name": "my_db", |
| 49 |
"character_set": "latin2", # These two fields |
| 50 |
"collate": "latin2_general_ci"}] # are optional. |
| 51 |
datastore = 'mysql' |
| 52 |
datastore_version = '5.6-104' |
| 53 |
users = [{"name": "jsmith", "password": "12345", |
| 54 |
"databases": [{"name": "my_db"}] |
| 55 |
}] |
| 56 |
instance = client.instances.create("My Instance", flavor_id, volume, |
| 57 |
databases, users, datastore=datastore, |
| 58 |
datastore_version=datastore_version) |
| 59 |
|
| 60 |
To retrieve the instance, use the "get" method of "instances": |
| 61 |
|
| 62 |
.. testcode:: |
| 63 |
|
| 64 |
updated_instance = client.instances.get(instance.id) |
| 65 |
print(updated_instance.name) |
| 66 |
print(" Status=%s Flavor=%s" % |
| 67 |
(updated_instance.status, updated_instance.flavor['id'])) |
| 68 |
|
| 69 |
.. testoutput:: |
| 70 |
|
| 71 |
My Instance |
| 72 |
Status=BUILD Flavor=1 |
| 73 |
|
| 74 |
You can delete an instance by calling "delete" on the instance object itself, |
| 75 |
or by using the delete method on "instances." |
| 76 |
|
| 77 |
.. testcode:: |
| 78 |
|
| 79 |
# Wait for the instance to be ready before we delete it. |
| 80 |
import time |
| 81 |
from troveclient.exceptions import NotFound |
| 82 |
|
| 83 |
while instance.status == "BUILD": |
| 84 |
instance.get() |
| 85 |
time.sleep(1) |
| 86 |
print("Ready in an %s state." % instance.status) |
| 87 |
instance.delete() |
| 88 |
# Delete and wait for the instance to go away. |
| 89 |
while True: |
| 90 |
try: |
| 91 |
instance = client.instances.get(instance.id) |
| 92 |
assert instance.status == "SHUTDOWN" |
| 93 |
except NotFound: |
| 94 |
break |
| 95 |
|
| 96 |
.. testoutput:: |
| 97 |
|
| 98 |
Ready in an ACTIVE state. |
| 99 |
|
| 100 |
|
| 101 |
Listing Items and Pagination |
| 102 |
-------------------------------- |
| 103 |
|
| 104 |
Lists paginate after twenty items, meaning you'll only get twenty items back |
| 105 |
even if there are more. To see the next set of items, send a marker. The marker |
| 106 |
is a key value (in the case of instances, the ID) which is the non-inclusive |
| 107 |
starting point for all returned items. |
| 108 |
|
| 109 |
The lists returned by the client always include a "next" property. This |
| 110 |
can be used as the "marker" argument to get the next section of the list |
| 111 |
back from the server. If no more items are available, then the next property |
| 112 |
is None. |
| 113 |
|
| 114 |
Pagination applies to all listed objects, like instances, datastores, etc. |
| 115 |
The example below is for instances. |
| 116 |
|
| 117 |
.. testcode:: |
| 118 |
|
| 119 |
# There are currently 30 instances. |
| 120 |
|
| 121 |
instances = client.instances.list() |
| 122 |
print(len(instances)) |
| 123 |
print(instances.next is None) |
| 124 |
|
| 125 |
instances2 = client.instances.list(marker=instances.next) |
| 126 |
print(len(instances2)) |
| 127 |
print(instances2.next is None) |
| 128 |
|
| 129 |
.. testoutput:: |
| 130 |
|
| 131 |
20 |
| 132 |
False |
| 133 |
10 |
| 134 |
True |
| 135 |
|