summaryrefslogtreecommitdiff
path: root/troveclient/v1/management.py
blob: 478d086191ebecfb4a54b272695bf3432609d445 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2011 OpenStack Foundation
# Copyright 2013 Rackspace Hosting
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import json

from troveclient import base
from troveclient import common
from troveclient.v1 import clusters
from troveclient.v1 import configurations
from troveclient.v1 import datastores
from troveclient.v1 import flavors
from troveclient.v1 import instances


class RootHistory(base.Resource):
    def __repr__(self):
        return ("<Root History: Instance %s enabled at %s by %s>"
                % (self.id, self.created, self.user))


class Management(base.ManagerWithFind):
    """Manage :class:`Instances` resources."""
    resource_class = instances.Instance

    def show(self, instance):
        """Get details of one instance.

        :rtype: :class:`Instance`.
        """

        return self._get("/mgmt/instances/%s" % base.getid(instance),
                         'instance')

    def index(self, **kwargs):
        """A wrapper for list method."""
        return self.list(**kwargs)

    def list(self, limit=None, marker=None, deleted=False, **kwargs):
        """Get all the database instances."""
        url = "/mgmt/instances"
        kwargs["deleted"] = deleted

        return self._paginated(url, "instances", limit, marker,
                               query_strings=kwargs)

    def root_enabled_history(self, instance):
        """Get root access history of one instance."""
        url = "/mgmt/instances/%s/root" % base.getid(instance)
        resp, body = self.api.client.get(url)
        if not body:
            raise Exception("Call to " + url + " did not return a body.")
        return RootHistory(self, body['root_history'])

    def _action(self, instance_id, body):
        """Perform a server "action" -- reboot/rebuild/resize/etc."""
        url = "/mgmt/instances/%s/action" % instance_id
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def stop(self, instance_id):
        body = {'stop': {}}
        self._action(instance_id, body)

    def reboot(self, instance_id):
        """Reboot the underlying OS.

        :param instance_id: The :class:`Instance` (or its ID) to share onto.
        """
        body = {'reboot': {}}
        self._action(instance_id, body)

    def migrate(self, instance_id, host=None):
        """Migrate the instance.

        :param instance_id: The :class:`Instance` (or its ID) to share onto.
        """
        if host:
            body = {'migrate': {'host': host}}
        else:
            body = {'migrate': {}}
        self._action(instance_id, body)

    def update(self, instance_id):
        """Update the guest agent via apt-get."""
        body = {'update': {}}
        self._action(instance_id, body)

    def reset_task_status(self, instance_id):
        """Set the task status to NONE."""
        body = {'reset-task-status': {}}
        self._action(instance_id, body)

    def rebuild(self, instance_id, image_id):
        """Rebuild the underlying OS."""
        body = {'rebuild': {'image_id': image_id}}
        self._action(instance_id, body)


class MgmtClusters(base.ManagerWithFind):
    """Manage :class:`Cluster` resources."""
    resource_class = clusters.Cluster

    # Appease the abc gods
    def list(self):
        pass

    def show(self, cluster):
        """Get details of one cluster."""
        return self._get("/mgmt/clusters/%s" % base.getid(cluster), 'cluster')

    def index(self, deleted=None, limit=None, marker=None):
        """Show an overview of all local clusters.

        Optionally, filter by deleted status.

        :rtype: list of :class:`Cluster`.
        """
        form = ''
        if deleted is not None:
            if deleted:
                form = "?deleted=true"
            else:
                form = "?deleted=false"

        url = "/mgmt/clusters%s" % form
        return self._paginated(url, "clusters", limit, marker)

    def _action(self, cluster_id, body):
        """Perform a cluster action, e.g. reset-task."""
        url = "/mgmt/clusters/%s/action" % cluster_id
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def reset_task(self, cluster_id):
        """Reset the current cluster task to NONE."""
        body = {'reset-task': {}}
        self._action(cluster_id, body)


class MgmtFlavors(base.ManagerWithFind):
    """Manage :class:`Flavor` resources."""
    resource_class = flavors.Flavor

    def __repr__(self):
        return "<Flavors Manager at %s>" % id(self)

    # Appease the abc gods
    def list(self):
        pass

    def create(self, name, ram, disk, vcpus,
               flavorid="auto", ephemeral=None, swap=None, rxtx_factor=None,
               service_type=None):
        """Create a new flavor."""
        body = {"flavor": {
            "flavor_id": flavorid,
            "name": name,
            "ram": ram,
            "disk": disk,
            "vcpu": vcpus,
            "ephemeral": 0,
            "swap": 0,
            "rxtx_factor": "1.0",
            "is_public": "True"
        }}
        if ephemeral:
            body["flavor"]["ephemeral"] = ephemeral
        if swap:
            body["flavor"]["swap"] = swap
        if rxtx_factor:
            body["flavor"]["rxtx_factor"] = rxtx_factor
        if service_type:
            body["flavor"]["service_type"] = service_type

        return self._create("/mgmt/flavors", body, "flavor")


class MgmtConfigurationParameters(configurations.ConfigurationParameters):
    def create(self, version, name, restart_required, data_type,
               max_size=None, min_size=None):
        """Mgmt call to create a new configuration parameter."""
        body = {
            "configuration-parameter": {
                "name": name,
                "restart_required": int(restart_required),
                "data_type": data_type,
            }
        }
        if max_size is not None:
            body["configuration-parameter"]["max_size"] = max_size
        if min_size is not None:
            body["configuration-parameter"]["min_size"] = min_size

        url = "/mgmt/datastores/versions/%s/parameters" % version
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def list_all_parameter_by_version(self, version):
        """List all configuration parameters deleted or not."""
        return self._list("/mgmt/datastores/versions/%s/parameters" %
                          version, "configuration-parameters")

    def get_any_parameter_by_version(self, version, key):
        """Get any configuration parameter deleted or not."""
        return self._get("/mgmt/datastores/versions/%s/parameters/%s" %
                         (version, key))

    def modify(self, version, name, restart_required, data_type,
               max_size=None, min_size=None):
        """Mgmt call to modify an existing configuration parameter."""
        body = {
            "configuration-parameter": {
                "name": name,
                "restart_required": int(restart_required),
                "data_type": data_type,
            }
        }
        if max_size is not None:
            body["configuration-parameter"]["max_size"] = max_size
        if min_size is not None:
            body["configuration-parameter"]["min_size"] = min_size
        output = {
            'version': version,
            'parameter_name': name
        }
        url = ("/mgmt/datastores/versions/%(version)s/"
               "parameters/%(parameter_name)s" % output)
        resp, body = self.api.client.put(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def delete(self, version, name):
        """Mgmt call to delete a configuration parameter."""
        output = {
            'version_id': version,
            'parameter_name': name
        }
        url = ("/mgmt/datastores/versions/%(version_id)s/"
               "parameters/%(parameter_name)s" % output)
        resp, body = self.api.client.delete(url)
        common.check_for_exceptions(resp, body, url)


class MgmtDatastoreVersions(base.ManagerWithFind):
    """Manage :class:`DatastoreVersion` resources."""
    resource_class = datastores.DatastoreVersion

    def list(self, limit=None, marker=None):
        """List all datastore versions."""
        return self._paginated("/mgmt/datastore-versions", "versions",
                               limit, marker)

    def get(self, datastore_version_id):
        """Get details of a datastore version."""
        return self._get("/mgmt/datastore-versions/%s" % datastore_version_id,
                         "version")

    def create(self, name, datastore_name, datastore_manager, image,
               packages=None, active='true', default='false', image_tags=[]):
        """Create a new datastore version."""
        packages = packages or []
        body = {
            "version": {
                "name": name,
                "datastore_name": datastore_name,
                "datastore_manager": datastore_manager,
                "image_tags": image_tags,
                "packages": packages,
                "active": json.loads(active),
                "default": json.loads(default)
            }
        }
        if image:
            body['version']['image'] = image

        return self._create("/mgmt/datastore-versions", body, None, True)

    def edit(self, datastore_version_id, datastore_manager=None, image=None,
             packages=None, active=None, default=None, image_tags=None):
        """Update a datastore-version."""
        packages = packages or []
        body = {}
        if datastore_manager is not None:
            body['datastore_manager'] = datastore_manager
        if image is not None:
            body['image'] = image
        if packages:
            body['packages'] = packages
        if active is not None:
            body['active'] = json.loads(active)
        if default is not None:
            body['default'] = json.loads(default)
        if image_tags is not None:
            body['image_tags'] = image_tags

        url = ("/mgmt/datastore-versions/%s" % datastore_version_id)
        resp, body = self.api.client.patch(url, body=body)
        common.check_for_exceptions(resp, body, url)

    def delete(self, datastore_version_id):
        """Delete a datastore version."""
        url = ("/mgmt/datastore-versions/%s" % datastore_version_id)
        resp, body = self.api.client.delete(url)
        common.check_for_exceptions(resp, body, url)