summaryrefslogtreecommitdiff
path: root/doc/source/usage.rst
blob: 0e7edfc9eed2eb9d30570d53cc0a0c814a1edec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
Using the Client Programmatically
=================================


.. testsetup::

    # Creates some vars we don't show in the docs.
    AUTH_URL="http://localhost:8779/v1.0/auth"

    from troveclient import Dbaas
    from troveclient import auth
    class FakeAuth(auth.Authenticator):

        def authenticate(self):
            class FakeCatalog(object):
                def __init__(self, auth):
                    self.auth = auth

                def get_public_url(self):
                    return "%s/%s" % ('http://localhost:8779/v1.0',
                                      self.auth.tenant)

                def get_token(self):
                    return self.auth.tenant

            return FakeCatalog(self)

    from troveclient import Dbaas
    OLD_INIT = Dbaas.__init__
    def new_init(*args, **kwargs):
        kwargs['auth_strategy'] = FakeAuth
        OLD_INIT(*args, **kwargs)

    # Monkey patch init so it'll work with fake auth.
    Dbaas.__init__ = new_init


    client = Dbaas("jsmith", "abcdef", tenant="12345",
                  auth_url=AUTH_URL)
    client.authenticate()

    # Delete all instances.
    instances = [1]
    while len(instances) > 0:
        instances = client.instances.list()
        for instance in instances:
            try:
                instance.delete()
            except:
                pass

    flavor_id = "1"
    for i in range(30):
        name = "Instance #%d" % i
        client.instances.create(name, flavor_id, None)



Authentication
--------------

Authenticating is necessary to use every feature of the client (except to
discover available versions).

To create the client, create an instance of the Dbaas (Database as a Service)
class. The auth url, auth user, key, and tenant ID must be specified in the
call to the constructor.

.. testcode::

    from troveclient import Dbaas
    global AUTH_URL

    client = Dbaas("jsmith", "abcdef", tenant="12345",
                  auth_url=AUTH_URL)
    client.authenticate()

The default authentication strategy assumes a Keystone compliant auth system.
For Rackspace auth, use the keyword argument "auth_strategy='rax'".


Versions
--------

You can discover the available versions by querying the versions property as
follows:


.. testcode::

    versions = client.versions.index("http://localhost:8779")


The "index" method returns a list of Version objects which have the ID as well
as a list of links, each with a URL to use to reach that particular version.

.. testcode::

    for version in versions:
        print(version.id)
        for link in version.links:
            if link['rel'] == 'self':
                print("    %s" % link['href'])

.. testoutput::

    v1.0
        http://localhost:8779/v1.0/


Instances
---------

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

.. testcode::

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

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

.. testcode::

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

.. testoutput::

    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."

.. testcode::

    # 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

.. testoutput::

    Ready in an ACTIVE state.


Listing instances and Pagination
--------------------------------

To list all instances, use the list method of "instances":

.. testcode::

    instances = client.instances.list()


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.

.. testcode::

    # 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)

.. testoutput::

    20
    False
    10
    True