python-troveclient/doc/source/usage.rst
jiansong 7b9c7e9b7d Use lowercase project name for doc
Projectname according to the relevant provisions of the new document
style should use lowercase, Service should be used service name instead
of project name[1].

[1]:http://docs.openstack.org/contributor-guide/writing-style/openstack-components.html

Change-Id: I385aa21b141528f7766f54cd7f067fb67eb8478e
Depends-On: I386efb2d5c147417af7ea25704405977c9b6bbcd
Depends-On: I5c1714b7839b2736c50f2daa2f4506c4006815a1
2016-11-29 01:40:22 +00:00

3.8 KiB

Using the Client Programmatically

Authentication

Authenticating is necessary to use every feature of the client.

To create the client, create an instance of the Client class. The auth url, username, password, and project name must be specified in the call to the constructor.

from troveclient.v1 import client tc = client.Client(username="testuser", password="PASSWORD", project_id="test_project", region_name="EAST", auth_url="http://api-server:5000/v2.0")

The default authentication strategy assumes a keystone compliant auth system.

Once you have an authenticated client object you can make calls with it, for example:

flavors = tc.flavors.list() datastores = tc.datastores.list()

Instances

The following example creates a 512 MB instance with a 1 GB volume:

from troveclient.v1 import client tc = client.Client(username="testuser", password="PASSWORD", project_id="test_project", region_name="EAST", auth_url="http://api-server:5000/v2.0")

flavor_id = '1' volume = {'size':1} databases = [{"name": "my_db", "character_set": "latin2", # These two fields "collate": "latin2_general_ci"}] # are optional. datastore = 'mysql' datastore_version = '5.6-104' users = [{"name": "jsmith", "password": "12345", "databases": [{"name": "my_db"}] }] instance = client.instances.create("My Instance", flavor_id, volume, databases, users, datastore=datastore, datastore_version=datastore_version)

To retrieve the instance, use the "get" method of "instances":

updated_instance = client.instances.get(instance.id) print(updated_instance.name) print(" Status=%s Flavor=%s" % (updated_instance.status, updated_instance.flavor['id']))

My Instance

Status=BUILD Flavor=1

You can delete an instance by calling "delete" on the instance object itself, or by using the delete method on "instances."

# Wait for the instance to be ready before we delete it. import time from troveclient.exceptions import NotFound

while instance.status == "BUILD":

instance.get() time.sleep(1)

print("Ready in an %s state." % instance.status) instance.delete() # Delete and wait for the instance to go away. while True: try: instance = client.instances.get(instance.id) assert instance.status == "SHUTDOWN" except NotFound: break

Ready in an ACTIVE state.

Listing Items and Pagination

Lists paginate after twenty items, meaning you'll only get twenty items back even if there are more. To see the next set of items, send a marker. The marker is a key value (in the case of instances, the ID) which is the non-inclusive starting point for all returned items.

The lists returned by the client always include a "next" property. This can be used as the "marker" argument to get the next section of the list back from the server. If no more items are available, then the next property is None.

Pagination applies to all listed objects, like instances, datastores, etc. The example below is for instances.

# There are currently 30 instances.

instances = client.instances.list() print(len(instances)) print(instances.next is None)

instances2 = client.instances.list(marker=instances.next) print(len(instances2)) print(instances2.next is None)

20 False 10 True