summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/manila/share.py
blob: 17b164a7c44a65bb521fa15c844bf68d1f05867f (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#
# 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 oslo_log import log as logging
from oslo_utils import encodeutils

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

LOG = logging.getLogger(__name__)


class ManilaShare(resource.Resource):
    """A resource that creates shared mountable file system.

    The resource creates a manila share - shared mountable filesystem that
    can be attached to any client(or clients) that has a network access and
    permission to mount filesystem. Share is a unit of storage with specific
    size that supports pre-defined share protocol and advanced security model
    (access lists, share networks and security services).
    """

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

    _ACCESS_RULE_PROPERTIES = (
        ACCESS_TO, ACCESS_TYPE, ACCESS_LEVEL
    ) = (
        'access_to', 'access_type', 'access_level')

    _SHARE_STATUSES = (
        STATUS_CREATING, STATUS_DELETING, STATUS_ERROR, STATUS_ERROR_DELETING,
        STATUS_AVAILABLE
    ) = (
        'creating', 'deleting', 'error', 'error_deleting',
        'available'
    )

    PROPERTIES = (
        SHARE_PROTOCOL, SIZE, SHARE_SNAPSHOT, NAME, METADATA,
        SHARE_NETWORK, DESCRIPTION, SHARE_TYPE, IS_PUBLIC,
        ACCESS_RULES
    ) = (
        'share_protocol', 'size', 'snapshot', 'name', 'metadata',
        'share_network', 'description', 'share_type', 'is_public',
        'access_rules'
    )

    ATTRIBUTES = (
        AVAILABILITY_ZONE_ATTR, HOST_ATTR, EXPORT_LOCATIONS_ATTR,
        SHARE_SERVER_ID_ATTR, CREATED_AT_ATTR, SHARE_STATUS_ATTR,
        PROJECT_ID_ATTR
    ) = (
        'availability_zone', 'host', 'export_locations',
        'share_server_id', 'created_at', 'status',
        'project_id'
    )

    properties_schema = {
        SHARE_PROTOCOL: properties.Schema(
            properties.Schema.STRING,
            _('Share protocol supported by shared filesystem.'),
            required=True,
            constraints=[constraints.AllowedValues(
                ['NFS', 'CIFS', 'GlusterFS', 'HDFS', 'CEPHFS'])]
        ),
        SIZE: properties.Schema(
            properties.Schema.INTEGER,
            _('Share storage size in GB.'),
            required=True
        ),
        SHARE_SNAPSHOT: properties.Schema(
            properties.Schema.STRING,
            _('Name or ID of shared file system snapshot that '
              'will be restored and created as a new share.'),
            constraints=[constraints.CustomConstraint('manila.share_snapshot')]
        ),
        NAME: properties.Schema(
            properties.Schema.STRING,
            _('Share name.'),
            update_allowed=True
        ),
        METADATA: properties.Schema(
            properties.Schema.MAP,
            _('Metadata key-values defined for share.'),
            update_allowed=True
        ),
        SHARE_NETWORK: properties.Schema(
            properties.Schema.STRING,
            _('Name or ID of shared network defined for shared filesystem.'),
            constraints=[constraints.CustomConstraint('manila.share_network')]
        ),
        DESCRIPTION: properties.Schema(
            properties.Schema.STRING,
            _('Share description.'),
            update_allowed=True
        ),
        SHARE_TYPE: properties.Schema(
            properties.Schema.STRING,
            _('Name or ID of shared filesystem type. Types defines some share '
              'filesystem profiles that will be used for share creation.'),
            constraints=[constraints.CustomConstraint("manila.share_type")]
        ),
        IS_PUBLIC: properties.Schema(
            properties.Schema.BOOLEAN,
            _('Defines if shared filesystem is public or private.'),
            default=False,
            update_allowed=True
        ),
        ACCESS_RULES: properties.Schema(
            properties.Schema.LIST,
            _('A list of access rules that define access from IP to Share.'),
            schema=properties.Schema(
                properties.Schema.MAP,
                schema={
                    ACCESS_TO: properties.Schema(
                        properties.Schema.STRING,
                        _('IP or other address information about guest that '
                          'allowed to access to Share.'),
                        required=True
                    ),
                    ACCESS_TYPE: properties.Schema(
                        properties.Schema.STRING,
                        _('Type of access that should be provided to guest.'),
                        constraints=[constraints.AllowedValues(
                            ['ip', 'user', 'cert', 'cephx'])],
                        required=True
                    ),
                    ACCESS_LEVEL: properties.Schema(
                        properties.Schema.STRING,
                        _('Level of access that need to be provided for '
                          'guest.'),
                        constraints=[constraints.AllowedValues(['ro', 'rw'])]
                    )
                }
            ),
            update_allowed=True,
            default=[]
        )
    }

    attributes_schema = {
        AVAILABILITY_ZONE_ATTR: attributes.Schema(
            _('The availability zone of shared filesystem.'),
            type=attributes.Schema.STRING
        ),
        HOST_ATTR: attributes.Schema(
            _('Share host.'),
            type=attributes.Schema.STRING
        ),
        EXPORT_LOCATIONS_ATTR: attributes.Schema(
            _('Export locations of share.'),
            type=attributes.Schema.LIST
        ),
        SHARE_SERVER_ID_ATTR: attributes.Schema(
            _('ID of server (VM, etc...) on host that is used for '
              'exporting network file-system.'),
            type=attributes.Schema.STRING
        ),
        CREATED_AT_ATTR: attributes.Schema(
            _('Datetime when a share was created.'),
            type=attributes.Schema.STRING
        ),
        SHARE_STATUS_ATTR: attributes.Schema(
            _('Current share status.'),
            type=attributes.Schema.STRING
        ),
        PROJECT_ID_ATTR: attributes.Schema(
            _('Share project ID.'),
            type=attributes.Schema.STRING
        )
    }

    default_client_name = 'manila'

    entity = 'shares'

    def _request_share(self):
        return self.client().shares.get(self.resource_id)

    def _resolve_attribute(self, name):
        if self.resource_id is None:
            return
        share = self._request_share()
        return str(getattr(share, name))

    def handle_create(self):
        # Request IDs of entities from manila
        # if name of the entity defined in template
        share_net_identity = self.properties[self.SHARE_NETWORK]
        if share_net_identity:
            share_net_identity = self.client_plugin().get_share_network(
                share_net_identity).id
        snapshot_identity = self.properties[self.SHARE_SNAPSHOT]
        if snapshot_identity:
            snapshot_identity = self.client_plugin().get_share_snapshot(
                snapshot_identity).id
        share_type_identity = self.properties[self.SHARE_TYPE]
        if share_type_identity:
            share_type_identity = self.client_plugin().get_share_type(
                share_type_identity).id

        share = self.client().shares.create(
            share_proto=self.properties[self.SHARE_PROTOCOL],
            size=self.properties[self.SIZE],
            snapshot_id=snapshot_identity,
            name=self.properties[self.NAME],
            description=self.properties[self.DESCRIPTION],
            metadata=self.properties[self.METADATA],
            share_network=share_net_identity,
            share_type=share_type_identity,
            is_public=self.properties[self.IS_PUBLIC])

        self.resource_id_set(share.id)

    def check_create_complete(self, *args):
        share_status = self._request_share().status
        if share_status == self.STATUS_CREATING:
            return False
        elif share_status == self.STATUS_AVAILABLE:
            LOG.info('Applying access rules to created Share.')
            # apply access rules to created share. please note that it is not
            # possible to define rules for share with share_status = creating
            access_rules = self.properties.get(self.ACCESS_RULES)
            try:
                if access_rules:
                    for rule in access_rules:
                        self.client().shares.allow(
                            share=self.resource_id,
                            access_type=rule.get(self.ACCESS_TYPE),
                            access=rule.get(self.ACCESS_TO),
                            access_level=rule.get(self.ACCESS_LEVEL))
                return True
            except Exception as ex:
                err_msg = encodeutils.exception_to_unicode(ex)
                reason = _(
                    'Error during applying access rules to share "{0}". '
                    'The root cause of the problem is the following: {1}.'
                ).format(self.resource_id, err_msg)
                raise exception.ResourceInError(
                    status_reason=reason, resource_status=share_status)
        elif share_status == self.STATUS_ERROR:
            reason = _('Error during creation of share "{0}"').format(
                self.resource_id)
            raise exception.ResourceInError(status_reason=reason,
                                            resource_status=share_status)
        else:
            reason = _('Unknown share_status during creation of share "{0}"'
                       ).format(self.resource_id)
            raise exception.ResourceUnknownStatus(
                status_reason=reason, resource_status=share_status)

    def check_delete_complete(self, *args):
        if not self.resource_id:
            return True

        try:
            share = self._request_share()
        except Exception as ex:
            self.client_plugin().ignore_not_found(ex)
            return True
        else:
            # when share creation is not finished proceed listening
            if share.status == self.STATUS_DELETING:
                return False
            elif share.status in (self.STATUS_ERROR,
                                  self.STATUS_ERROR_DELETING):
                raise exception.ResourceInError(
                    status_reason=_(
                        'Error during deleting share "{0}".'
                    ).format(self.resource_id),
                    resource_status=share.status)
            else:
                reason = _('Unknown status during deleting share '
                           '"{0}"').format(self.resource_id)
                raise exception.ResourceUnknownStatus(
                    status_reason=reason, resource_status=share.status)

    def handle_check(self):
        share = self._request_share()
        expected_statuses = [self.STATUS_AVAILABLE]
        checks = [{'attr': 'status', 'expected': expected_statuses,
                   'current': share.status}]
        self._verify_check_conditions(checks)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        kwargs = {}
        if self.IS_PUBLIC in prop_diff:
            kwargs['is_public'] = prop_diff.get(self.IS_PUBLIC)
        if self.NAME in prop_diff:
            kwargs['display_name'] = prop_diff.get(self.NAME)
        if self.DESCRIPTION in prop_diff:
            kwargs['display_description'] = prop_diff.get(self.DESCRIPTION)
        if kwargs:
            self.client().shares.update(self.resource_id,
                                        **kwargs)

        if self.METADATA in prop_diff:
            metadata = prop_diff.get(self.METADATA)
            self.client().shares.update_all_metadata(
                self.resource_id, metadata)

        if self.ACCESS_RULES in prop_diff:
            actual_old_rules = []
            for rule in self.client().shares.access_list(self.resource_id):
                old_rule = {
                    self.ACCESS_TO: getattr(rule, self.ACCESS_TO),
                    self.ACCESS_TYPE: getattr(rule, self.ACCESS_TYPE),
                    self.ACCESS_LEVEL: getattr(rule, self.ACCESS_LEVEL)
                }
                if old_rule in prop_diff[self.ACCESS_RULES]:
                    actual_old_rules.append(old_rule)
                else:
                    self.client().shares.deny(share=self.resource_id,
                                              id=rule.id)
            for rule in prop_diff[self.ACCESS_RULES]:
                if rule not in actual_old_rules:
                    self.client().shares.allow(
                        share=self.resource_id,
                        access_type=rule.get(self.ACCESS_TYPE),
                        access=rule.get(self.ACCESS_TO),
                        access_level=rule.get(self.ACCESS_LEVEL)
                    )

    def parse_live_resource_data(self, resource_properties, resource_data):
        result = super(ManilaShare, self).parse_live_resource_data(
            resource_properties, resource_data)

        rules = self.client().shares.access_list(self.resource_id)
        result[self.ACCESS_RULES] = []
        for rule in rules:
            result[self.ACCESS_RULES].append(
                {(k, v) for (k, v) in rule.items()
                 if k in self._ACCESS_RULE_PROPERTIES})
        return result


def resource_mapping():
    return {'OS::Manila::Share': ManilaShare}