summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/magnum/cluster.py
blob: ac2ae5dc94b5bd83c7fd72eb5ff88538d2a355a8 (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
#
#    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 heat.common import exception
from heat.common.i18n import _
from heat.engine import attributes
from heat.engine import constraints
from heat.engine import properties
from heat.engine import resource
from heat.engine import support
from heat.engine import translation


class Cluster(resource.Resource):
    """A resource that creates a magnum cluster.

    This resource creates a magnum cluster, which is a
    collection of node objects where work is scheduled.
    """

    support_status = support.SupportStatus(version='9.0.0')

    default_client_name = 'magnum'

    entity = 'clusters'

    ATTRIBUTES = (
        API_ADDRESS_ATTR, STACK_ID_ATTR, COE_VERSION_ATTR,
        MASTER_ADDRESSES_ATTR, STATUS_ATTR, MASTER_COUNT_ATTR,
        NODE_ADDRESSES_ATTR, STATUS_REASON_ATTR, NODE_COUNT_ATTR,
        NAME_ATTR, CONTAINER_VERSION_ATTR, DISCOVERY_URL_ATTR,
        CLUSTER_TEMPLATE_ID_ATTR, KEYPAIR_ATTR, CREATE_TIMEOUT_ATTR
    ) = (
        'api_address', 'stack_id', 'coe_version',
        'master_addresses', 'status', 'master_count',
        'node_addresses', 'status_reason', 'node_count',
        'name', 'container_version', 'discovery_url',
        'cluster_template_id', 'keypair', 'create_timeout'
    )
    attributes_schema = {
        API_ADDRESS_ATTR: attributes.Schema(
            _('The endpoint URL of COE API exposed to end-users.'),
            type=attributes.Schema.STRING
        ),
        STACK_ID_ATTR: attributes.Schema(
            _('The reference UUID of orchestration stack for this '
              'COE cluster.'),
            type=attributes.Schema.STRING
        ),
        COE_VERSION_ATTR: attributes.Schema(
            _('Version info of chosen COE in cluster for helping client '
              'in picking the right version of client.'),
            type=attributes.Schema.STRING
        ),
        MASTER_ADDRESSES_ATTR: attributes.Schema(
            _('List of floating IP of all master nodes.'),
            type=attributes.Schema.LIST
        ),
        STATUS_ATTR: attributes.Schema(
            _('The status for this COE cluster.'),
            type=attributes.Schema.STRING
        ),
        MASTER_COUNT_ATTR: attributes.Schema(
            _('The number of servers that will serve as master for the '
              'cluster.'),
            type=attributes.Schema.INTEGER
        ),
        NODE_ADDRESSES_ATTR: attributes.Schema(
            _('List of floating IP of all servers that serve as node.'),
            type=attributes.Schema.LIST
        ),
        STATUS_REASON_ATTR: attributes.Schema(
            _('The reason of cluster current status.'),
            type=attributes.Schema.STRING
        ),
        NODE_COUNT_ATTR: attributes.Schema(
            _('The number of servers that will serve as node in the cluster.'),
            type=attributes.Schema.INTEGER
        ),
        NAME_ATTR: attributes.Schema(
            _('Name of the resource.'),
            type=attributes.Schema.STRING
        ),
        CONTAINER_VERSION_ATTR: attributes.Schema(
            _('Version info of constainer engine in the chosen COE in cluster '
              'for helping client in picking the right version of client.'),
            type=attributes.Schema.STRING
        ),
        DISCOVERY_URL_ATTR: attributes.Schema(
            _('The custom discovery url for node discovery.'),
            type=attributes.Schema.STRING
        ),
        CLUSTER_TEMPLATE_ID_ATTR: attributes.Schema(
            _('The UUID of the cluster template.'),
            type=attributes.Schema.STRING
        ),
        KEYPAIR_ATTR: attributes.Schema(
            _('The name of the keypair.'),
            type=attributes.Schema.STRING
        ),
        CREATE_TIMEOUT_ATTR: attributes.Schema(
            _('The timeout for cluster creation in minutes.'),
            type=attributes.Schema.INTEGER
        )}

    PROPERTIES = (
        NAME, CLUSTER_TEMPLATE, KEYPAIR, NODE_COUNT, MASTER_COUNT,
        DISCOVERY_URL, CREATE_TIMEOUT
    ) = (
        'name', 'cluster_template', 'keypair', 'node_count', 'master_count',
        'discovery_url', 'create_timeout'
    )

    properties_schema = {
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('The cluster name.'),
        ),
        CLUSTER_TEMPLATE: properties.Schema(
            properties.Schema.STRING,
            _('The name or ID of the cluster template.'),
            constraints=[
                constraints.CustomConstraint('magnum.cluster_template')
            ],
            required=True
        ),
        KEYPAIR: properties.Schema(
            properties.Schema.STRING,
            _('The name of the keypair. If not presented, use keypair in '
              'cluster template.'),
            constraints=[
                constraints.CustomConstraint('nova.keypair')
            ]
        ),
        NODE_COUNT: properties.Schema(
            properties.Schema.INTEGER,
            _('The node count for this cluster.'),
            constraints=[constraints.Range(min=1)],
            update_allowed=True,
            default=1
        ),
        MASTER_COUNT: properties.Schema(
            properties.Schema.INTEGER,
            _('The number of master nodes for this cluster.'),
            constraints=[constraints.Range(min=1)],
            update_allowed=True,
            default=1
        ),
        DISCOVERY_URL: properties.Schema(
            properties.Schema.STRING,
            _('Specifies a custom discovery url for node discovery.'),
            update_allowed=True,
        ),
        CREATE_TIMEOUT: properties.Schema(
            properties.Schema.INTEGER,
            _('Timeout for creating the cluster in minutes. '
              'Set to 0 for no timeout.'),
            constraints=[constraints.Range(min=0)],
            update_allowed=True,
            default=60
        )
    }

    def translation_rules(self, props):
        return [
            translation.TranslationRule(
                props,
                translation.TranslationRule.RESOLVE,
                [self.CLUSTER_TEMPLATE],
                client_plugin=self.client_plugin('magnum'),
                finder='get_cluster_template')
        ]

    def _resolve_attribute(self, name):
        if self.resource_id is None:
            return
        cluster = self.client().clusters.get(self.resource_id)
        return getattr(cluster, name, None)

    def handle_create(self):
        args = dict(self.properties.items())

        args['cluster_template_id'] = self.properties[self.CLUSTER_TEMPLATE]
        del args[self.CLUSTER_TEMPLATE]
        cluster = self.client().clusters.create(**args)
        self.resource_id_set(cluster.uuid)
        return cluster.uuid

    def check_create_complete(self, id):
        cluster = self.client().clusters.get(id)
        if cluster.status == 'CREATE_IN_PROGRESS':
            return False
        elif cluster.status == 'CREATE_COMPLETE':
            return True
        elif cluster.status == 'CREATE_FAILED':
            msg = (_("Failed to create Cluster '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': cluster.status_reason})
            raise exception.ResourceInError(status_reason=msg,
                                            resource_status=cluster.status)
        else:
            msg = (_("Unknown status creating Cluster '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': cluster.status_reason})
            raise exception.ResourceUnknownStatus(
                status_reason=msg, resource_status=cluster.status)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if prop_diff:
            patch = [{'op': 'replace', 'path': '/' + k, 'value': v}
                     for k, v in prop_diff.items()]
            self.client().clusters.update(self.resource_id, patch)
            return self.resource_id

    def check_update_complete(self, id):
        cluster = self.client().clusters.get(id)
        # Check update complete request might get status before the status
        # got changed to update in progress, so we allow `CREATE_COMPLETE`
        # for it.
        # TODO(ricolin): we should find way to make sure status check will
        # only perform after action really started.
        if cluster.status in ['UPDATE_IN_PROGRESS', 'CREATE_COMPLETE']:
            return False
        elif cluster.status == 'UPDATE_COMPLETE':
            return True
        elif cluster.status == 'UPDATE_FAILED':
            msg = (_("Failed to update Cluster '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': cluster.status_reason})
            raise exception.ResourceInError(
                status_reason=msg, resource_status=cluster.status)

        else:
            msg = (_("Unknown status updating Cluster '%(name)s' - %(reason)s")
                   % {'name': self.name, 'reason': cluster.status_reason})
            raise exception.ResourceUnknownStatus(
                status_reason=msg, resource_status=cluster.status)

    def check_delete_complete(self, id):
        if not id:
            return True
        try:
            self.client().clusters.get(id)
        except Exception as exc:
            self.client_plugin().ignore_not_found(exc)
            return True
        return False


def resource_mapping():
    return {
        'OS::Magnum::Cluster': Cluster
    }