summaryrefslogtreecommitdiff
path: root/heat/engine/resources/openstack/mistral/external_resource.py
blob: a38e35b4ed935915a010ae10c9d65fb0b2b70e31 (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
#
#    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_serialization import jsonutils

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 MistralExternalResource(resource.Resource):
    """A plugin for managing user-defined resources via Mistral workflows.

    This resource allows users to manage resources that are not known to Heat.
    The user may specify a Mistral workflow to handle each resource action,
    such as CREATE, UPDATE, or DELETE.

    The workflows may return an output named 'resource_id', which will be
    treated as the physical ID of the resource by Heat.

    Once the resource is created, subsequent workflow runs will receive the
    output of the last workflow execution in the 'heat_extresource_data' key
    in the workflow environment (accessible as ``env().heat_extresource_data``
    in the workflow).

    The template author may specify a subset of inputs as causing replacement
    of the resource when they change, as an alternative to running the
    UPDATE workflow.
    """

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

    default_client_name = 'mistral'

    entity = 'executions'

    _ACTION_PROPERTIES = (
        WORKFLOW, PARAMS
    ) = (
        'workflow', 'params'
    )

    PROPERTIES = (
        EX_ACTIONS,
        INPUT,
        DESCRIPTION,
        REPLACE_ON_CHANGE,
        ALWAYS_UPDATE
    ) = (
        'actions',
        'input',
        'description',
        'replace_on_change_inputs',
        'always_update'
    )

    ATTRIBUTES = (
        OUTPUT,
    ) = (
        'output',
    )

    _action_properties_schema = properties.Schema(
        properties.Schema.MAP,
        _('Dictionary which defines the workflow to run and its params.'),
        schema={
            WORKFLOW: properties.Schema(
                properties.Schema.STRING,
                _('Workflow to execute.'),
                required=True,
                constraints=[
                    constraints.CustomConstraint('mistral.workflow')
                ],
            ),
            PARAMS: properties.Schema(
                properties.Schema.MAP,
                _('Workflow additional parameters. If workflow is reverse '
                  'typed, params requires "task_name", which defines '
                  'initial task.'),
                default={}
            ),
        }
    )

    properties_schema = {
        EX_ACTIONS: properties.Schema(
            properties.Schema.MAP,
            _('Resource action which triggers a workflow execution.'),
            schema={
                resource.Resource.CREATE: _action_properties_schema,
                resource.Resource.UPDATE: _action_properties_schema,
                resource.Resource.SUSPEND: _action_properties_schema,
                resource.Resource.RESUME: _action_properties_schema,
                resource.Resource.DELETE: _action_properties_schema,
            },
            required=True
        ),
        INPUT: properties.Schema(
            properties.Schema.MAP,
            _('Dictionary which contains input for the workflows.'),
            update_allowed=True,
            default={}
        ),
        DESCRIPTION: properties.Schema(
            properties.Schema.STRING,
            _('Workflow execution description.'),
            default='Heat managed'
        ),
        REPLACE_ON_CHANGE: properties.Schema(
            properties.Schema.LIST,
            _('A list of inputs that should cause the resource to be replaced '
              'when their values change.'),
            default=[]
        ),
        ALWAYS_UPDATE: properties.Schema(
            properties.Schema.BOOLEAN,
            _('Triggers UPDATE action execution even if input is '
              'unchanged.'),
            default=False
        ),
    }

    attributes_schema = {
        OUTPUT: attributes.Schema(
            _('Output from the execution.'),
            type=attributes.Schema.MAP
        ),
    }

    def _check_execution(self, action, execution_id):
        """Check execution status.

        Returns False if in IDLE, RUNNING or PAUSED
        returns True if in SUCCESS
        raises ResourceFailure if in ERROR, CANCELLED
        raises ResourceUnknownState otherwise.
        """
        execution = self.client().executions.get(execution_id)
        LOG.debug('Mistral execution %(id)s is in state '
                  '%(state)s' % {'id': execution_id,
                                 'state': execution.state})

        if execution.state in ('IDLE', 'RUNNING', 'PAUSED'):
            return False, {}

        if execution.state in ('SUCCESS',):
            return True, jsonutils.loads(execution.output)

        if execution.state in ('ERROR', 'CANCELLED'):
            raise exception.ResourceFailure(
                exception_or_error=execution.state_info,
                resource=self,
                action=action)

        raise exception.ResourceUnknownStatus(
            resource_status=execution.state,
            result=_('Mistral execution is in unknown state.'))

    def _handle_action(self, action, inputs=None):
        action_data = self.properties[self.EX_ACTIONS].get(action)
        if action_data:
            # bring forward output from previous executions into env
            if self.resource_id:
                old_outputs = jsonutils.loads(self.data().get('outputs', '{}'))
                action_env = action_data[self.PARAMS].get('env', {})
                action_env['heat_extresource_data'] = old_outputs
                action_data[self.PARAMS]['env'] = action_env
            # inputs is not None when inputs changed on stack UPDATE
            if not inputs:
                inputs = self.properties[self.INPUT]
            execution = self.client().executions.create(
                action_data[self.WORKFLOW],
                workflow_input=jsonutils.dumps(inputs),
                description=self.properties[self.DESCRIPTION],
                **action_data[self.PARAMS])
            LOG.debug('Mistral execution %(id)s params set to '
                      '%(params)s' % {'id': execution.id,
                                      'params': action_data[self.PARAMS]})
            return execution.id

    def _check_action(self, action, execution_id):
        success = True
        # execution_id is None when no data is available for a given action
        if execution_id:
            rsrc_id = execution_id
            success, output = self._check_execution(action, execution_id)
            # merge output with outputs of previous executions
            outputs = jsonutils.loads(self.data().get('outputs', '{}'))
            outputs.update(output)
            self.data_set('outputs', jsonutils.dumps(outputs))
            # set resource id using output, if found
            if output.get('resource_id'):
                rsrc_id = output.get('resource_id')
                LOG.debug('ExternalResource id set to %(rid)s from Mistral '
                          'execution %(eid)s output' % {'eid': execution_id,
                                                        'rid': rsrc_id})
            self.resource_id_set(str(rsrc_id)[:255])
        return success

    def _resolve_attribute(self, name):
        if self.resource_id and name == self.OUTPUT:
            return self.data().get('outputs')

    def _needs_update(self, after, before, after_props, before_props,
                      prev_resource, check_init_complete=True):
        # check if we need to force replace first
        old_inputs = before_props[self.INPUT]
        new_inputs = after_props[self.INPUT]
        for i in after_props[self.REPLACE_ON_CHANGE]:
            if old_inputs.get(i) != new_inputs.get(i):
                LOG.debug('Replacing ExternalResource %(id)s instead of '
                          'updating due to change to input "%(i)s"' %
                          {"id": self.resource_id,
                           "i": i})
                raise resource.UpdateReplace(self)
        # honor always_update if found
        if self.properties[self.ALWAYS_UPDATE]:
            return True
        # call super in all other scenarios
        else:
            return super(MistralExternalResource,
                         self)._needs_update(after,
                                             before,
                                             after_props,
                                             before_props,
                                             prev_resource,
                                             check_init_complete)

    def handle_create(self):
        return self._handle_action(self.CREATE)

    def check_create_complete(self, execution_id):
        return self._check_action(self.CREATE, execution_id)

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        new_inputs = prop_diff.get(self.INPUT)
        return self._handle_action(self.UPDATE, new_inputs)

    def check_update_complete(self, execution_id):
        return self._check_action(self.UPDATE, execution_id)

    def handle_suspend(self):
        return self._handle_action(self.SUSPEND)

    def check_suspend_complete(self, execution_id):
        return self._check_action(self.SUSPEND, execution_id)

    def handle_resume(self):
        return self._handle_action(self.RESUME)

    def check_resume_complete(self, execution_id):
        return self._check_action(self.RESUME, execution_id)

    def handle_delete(self):
        return self._handle_action(self.DELETE)

    def check_delete_complete(self, execution_id):
        return self._check_action(self.DELETE, execution_id)


def resource_mapping():
    return {
        'OS::Mistral::ExternalResource': MistralExternalResource
    }