summaryrefslogtreecommitdiff
path: root/heat/objects/resource.py
blob: 67cc24a0c9dc0cc6e0f3bddcdf304c2a4f735bfc (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
# Copyright 2014 Intel Corp.
#
# 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.


"""Resource object."""

import collections

from oslo_config import cfg
from oslo_log import log as logging
from oslo_versionedobjects import base
from oslo_versionedobjects import fields
import six
import tenacity

from heat.common import crypt
from heat.common import exception
from heat.common.i18n import _
from heat.db.sqlalchemy import api as db_api
from heat.objects import base as heat_base
from heat.objects import fields as heat_fields
from heat.objects import resource_data
from heat.objects import resource_properties_data as rpd

cfg.CONF.import_opt('encrypt_parameters_and_properties', 'heat.common.config')

LOG = logging.getLogger(__name__)


def retry_on_conflict(func):
    wrapper = tenacity.retry(
        stop=tenacity.stop_after_attempt(11),
        wait=tenacity.wait_random(max=2),
        retry=tenacity.retry_if_exception_type(
            exception.ConcurrentTransaction),
        reraise=True)
    return wrapper(func)


class ResourceCache(object):

    def __init__(self):
        self.delete_all()

    def delete_all(self):
        self.by_stack_id_name = collections.defaultdict(dict)

    def set_by_stack_id(self, resources):
        for res in six.itervalues(resources):
            self.by_stack_id_name[res.stack_id][res.name] = res


class Resource(
    heat_base.HeatObject,
    base.VersionedObjectDictCompat,
    base.ComparableVersionedObject,
):
    fields = {
        'id': fields.IntegerField(),
        'uuid': fields.StringField(),
        'stack_id': fields.StringField(),
        'created_at': fields.DateTimeField(read_only=True),
        'updated_at': fields.DateTimeField(nullable=True),
        'physical_resource_id': fields.StringField(nullable=True),
        'name': fields.StringField(nullable=True),
        'status': fields.StringField(nullable=True),
        'status_reason': fields.StringField(nullable=True),
        'action': fields.StringField(nullable=True),
        'attr_data': fields.ObjectField(
            rpd.ResourcePropertiesData, nullable=True),
        'attr_data_id': fields.IntegerField(nullable=True),
        'rsrc_metadata': heat_fields.JsonField(nullable=True),
        'data': fields.ListOfObjectsField(
            resource_data.ResourceData,
            nullable=True
        ),
        'rsrc_prop_data_id': fields.ObjectField(
            fields.IntegerField(nullable=True)),
        'engine_id': fields.StringField(nullable=True),
        'atomic_key': fields.IntegerField(nullable=True),
        'current_template_id': fields.IntegerField(),
        'needed_by': heat_fields.ListField(nullable=True, default=None),
        'requires': heat_fields.ListField(nullable=True, default=None),
        'replaces': fields.IntegerField(nullable=True),
        'replaced_by': fields.IntegerField(nullable=True),
        'root_stack_id': fields.StringField(nullable=True),
    }

    @staticmethod
    def _from_db_object(resource, context, db_resource, only_fields=None):
        if db_resource is None:
            return None
        for field in resource.fields:
            if (only_fields is not None and field not in only_fields
                    and field != 'id'):
                continue
            if field == 'data':
                resource['data'] = [resource_data.ResourceData._from_db_object(
                    resource_data.ResourceData(context), resd
                ) for resd in db_resource.data]
            elif field != 'attr_data':
                resource[field] = db_resource[field]

        if db_resource['rsrc_prop_data_id'] is not None:
            if hasattr(db_resource, '__dict__'):
                rpd_obj = db_resource.__dict__.get('rsrc_prop_data')
            else:
                rpd_obj = None
            if rpd_obj is not None:
                # Object is already eager loaded
                rpd_obj = (
                    rpd.ResourcePropertiesData._from_db_object(
                        rpd.ResourcePropertiesData(),
                        context,
                        rpd_obj))
                resource._properties_data = rpd_obj.data
            else:
                resource._properties_data = {}
            if db_resource['properties_data']:
                LOG.error(
                    'Unexpected condition where resource.rsrc_prop_data '
                    'and resource.properties_data are both not null. '
                    'rsrc_prop_data.id: %(rsrc_prop_data_id)s, '
                    'resource id: %(res_id)s',
                    {'rsrc_prop_data_id': resource['rsrc_prop_data'].id,
                     'res_id': resource['id']})
        elif db_resource['properties_data']:  # legacy field
            if db_resource['properties_data_encrypted']:
                decrypted_data = crypt.decrypted_dict(
                    db_resource['properties_data'])
                resource._properties_data = decrypted_data
            else:
                resource._properties_data = db_resource['properties_data']
        else:
            resource._properties_data = None

        if db_resource['attr_data'] is not None:
            resource._attr_data = rpd.ResourcePropertiesData._from_db_object(
                rpd.ResourcePropertiesData(context), context,
                db_resource['attr_data']).data
        else:
            resource._attr_data = None

        resource._context = context
        resource.obj_reset_changes()
        return resource

    @property
    def attr_data(self):
        return self._attr_data

    @property
    def properties_data(self):
        if (not self._properties_data and
                self.rsrc_prop_data_id is not None):
            LOG.info('rsrp_prop_data lazy load')
            rpd_obj = rpd.ResourcePropertiesData.get_by_id(
                self._context, self.rsrc_prop_data_id)
            self._properties_data = rpd_obj.data or {}
        return self._properties_data

    @classmethod
    def get_obj(cls, context, resource_id, refresh=False, fields=None):
        if fields is None or 'data' in fields:
            refresh_data = refresh
        else:
            refresh_data = False
        resource_db = db_api.resource_get(context, resource_id,
                                          refresh=refresh,
                                          refresh_data=refresh_data)
        return cls._from_db_object(cls(context), context, resource_db,
                                   only_fields=fields)

    @classmethod
    def get_all(cls, context):
        resources_db = db_api.resource_get_all(context)
        resources = [
            (
                resource_name,
                cls._from_db_object(cls(context), context, resource_db)
            )
            for resource_name, resource_db in six.iteritems(resources_db)
        ]
        return dict(resources)

    @classmethod
    def create(cls, context, values):
        return cls._from_db_object(cls(context), context,
                                   db_api.resource_create(context, values))

    @classmethod
    def replacement(cls, context,
                    existing_res_id, existing_res_values,
                    new_res_values,
                    atomic_key=0, expected_engine_id=None):
        replacement = db_api.resource_create_replacement(context,
                                                         existing_res_id,
                                                         existing_res_values,
                                                         new_res_values,
                                                         atomic_key,
                                                         expected_engine_id)
        if replacement is None:
            return None
        return cls._from_db_object(cls(context), context, replacement)

    @classmethod
    def delete(cls, context, resource_id):
        db_api.resource_delete(context, resource_id)

    @classmethod
    def attr_data_delete(cls, context, resource_id, attr_id):
        db_api.resource_attr_data_delete(context, resource_id, attr_id)

    @classmethod
    def exchange_stacks(cls, context, resource_id1, resource_id2):
        return db_api.resource_exchange_stacks(
            context,
            resource_id1,
            resource_id2)

    @classmethod
    def get_all_by_stack(cls, context, stack_id, filters=None):
        cache = context.cache(ResourceCache)
        resources = cache.by_stack_id_name.get(stack_id)
        if resources:
            return dict(resources)
        resources_db = db_api.resource_get_all_by_stack(context, stack_id,
                                                        filters)
        return cls._resources_to_dict(context, resources_db)

    @classmethod
    def _resources_to_dict(cls, context, resources_db):
        resources = [
            (
                resource_name,
                cls._from_db_object(cls(context), context, resource_db)
            )
            for resource_name, resource_db in six.iteritems(resources_db)
        ]
        return dict(resources)

    @classmethod
    def get_all_active_by_stack(cls, context, stack_id):
        resources_db = db_api.resource_get_all_active_by_stack(context,
                                                               stack_id)
        resources = [
            (
                resource_id,
                cls._from_db_object(cls(context), context, resource_db)
            )
            for resource_id, resource_db in six.iteritems(resources_db)
        ]
        return dict(resources)

    @classmethod
    def get_all_by_root_stack(cls, context, stack_id, filters, cache=False):
        resources_db = db_api.resource_get_all_by_root_stack(
            context,
            stack_id,
            filters)
        all = cls._resources_to_dict(context, resources_db)
        if cache:
            context.cache(ResourceCache).set_by_stack_id(all)
        return all

    @classmethod
    def purge_deleted(cls, context, stack_id):
        return db_api.resource_purge_deleted(context, stack_id)

    @classmethod
    def get_by_name_and_stack(cls, context, resource_name, stack_id):
        resource_db = db_api.resource_get_by_name_and_stack(
            context,
            resource_name,
            stack_id)
        return cls._from_db_object(cls(context), context, resource_db)

    @classmethod
    def get_all_by_physical_resource_id(cls, context, physical_resource_id):
        matches = db_api.resource_get_all_by_physical_resource_id(
            context,
            physical_resource_id)
        return [cls._from_db_object(cls(context), context, resource_db)
                for resource_db in matches]

    @classmethod
    def update_by_id(cls, context, resource_id, values):
        db_api.resource_update_and_save(context, resource_id, values)

    def update_and_save(self, values):
        db_api.resource_update_and_save(self._context, self.id, values)

    def select_and_update(self, values, expected_engine_id=None,
                          atomic_key=0):
        return db_api.resource_update(self._context, self.id, values,
                                      atomic_key=atomic_key,
                                      expected_engine_id=expected_engine_id)

    @classmethod
    def select_and_update_by_id(cls, context, resource_id,
                                values, expected_engine_id=None,
                                atomic_key=0):
        return db_api.resource_update(context, resource_id, values,
                                      atomic_key=atomic_key,
                                      expected_engine_id=expected_engine_id)

    @classmethod
    def store_attributes(cls, context, resource_id, atomic_key,
                         attr_data, attr_id):
        attr_id = rpd.ResourcePropertiesData.create_or_update(
            context, attr_data, attr_id).id
        if db_api.resource_attr_id_set(
                context, resource_id, atomic_key, attr_id):
            return attr_id
        return None

    def refresh(self):
        resource_db = db_api.resource_get(self._context, self.id, refresh=True)
        return self.__class__._from_db_object(
            self,
            self._context,
            resource_db)

    @staticmethod
    def encrypt_properties_data(data):
        if cfg.CONF.encrypt_parameters_and_properties and data:
            result = crypt.encrypted_dict(data)
            return (True, result)
        return (False, data)

    def update_metadata(self, metadata):
        if self.rsrc_metadata != metadata:
            rows_updated = self.select_and_update(
                {'rsrc_metadata': metadata}, self.engine_id, self.atomic_key)
            if not rows_updated:
                action = _('metadata setting for resource %s') % self.name
                raise exception.ConcurrentTransaction(action=action)
            return True
        else:
            return False