summaryrefslogtreecommitdiff
path: root/troveclient/v1/clusters.py
blob: bf20722103a8e2a38b16aa97a1ec9c8d8368e1f7 (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
# Copyright (c) 2014 eBay Software Foundation
# 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.

from troveclient import base
from troveclient import common


class Cluster(base.Resource):
    """A Cluster is an opaque cluster used to store Database clusters."""
    def __repr__(self):
        return "<Cluster: %s>" % self.name

    def delete(self):
        """Delete the cluster."""
        self.manager.delete(self)


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

    def create(self, name, datastore, datastore_version, instances=None):
        """Create (boot) a new cluster."""
        body = {"cluster": {
            "name": name
        }}
        datastore_obj = {
            "type": datastore,
            "version": datastore_version
        }
        body["cluster"]["datastore"] = datastore_obj
        if instances:
            body["cluster"]["instances"] = instances

        return self._create("/clusters", body, "cluster")

    def list(self, limit=None, marker=None):
        """Get a list of all clusters.

        :rtype: list of :class:`Cluster`.
        """
        return self._paginated("/clusters", "clusters", limit, marker)

    def get(self, cluster):
        """Get a specific cluster.

        :rtype: :class:`Cluster`
        """
        return self._get("/clusters/%s" % base.getid(cluster),
                         "cluster")

    def delete(self, cluster):
        """Delete the specified cluster.

        :param cluster: The cluster to delete
        """
        url = "/clusters/%s" % base.getid(cluster)
        resp, body = self.api.client.delete(url)
        common.check_for_exceptions(resp, body, url)

    def _action(self, cluster, body):
        """Perform a cluster "action" -- grow/shrink/etc."""
        url = "/clusters/%s" % base.getid(cluster)
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)
        if body:
            return self.resource_class(self, body['cluster'], loaded=True)
        return body

    def add_shard(self, cluster):
        """Adds a shard to the specified cluster.

        :param cluster: The cluster to add a shard to
        """
        url = "/clusters/%s" % base.getid(cluster)
        body = {"add_shard": {}}
        resp, body = self.api.client.post(url, body=body)
        common.check_for_exceptions(resp, body, url)
        if body:
            return self.resource_class(self, body, loaded=True)
        return body

    def grow(self, cluster, instances=None):
        """Grow a cluster.

        :param cluster:     The cluster to grow
        :param instances:   List of instances to add
        """
        body = {"grow": instances}
        return self._action(cluster, body)

    def shrink(self, cluster, instances=None):
        """Shrink a cluster.

        :param cluster:     The cluster to shrink
        :param instances:   List of instances to drop
        """
        body = {"shrink": instances}
        return self._action(cluster, body)


class ClusterStatus(object):

    ACTIVE = "ACTIVE"
    BUILD = "BUILD"
    FAILED = "FAILED"
    SHUTDOWN = "SHUTDOWN"