summaryrefslogtreecommitdiff
path: root/openstack_dashboard/api/heat.py
blob: c31c61dad9fa253c73ffbea882aecf3ab7b47adf (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
# 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.

import contextlib

from django.conf import settings
from heatclient import client as heat_client
from heatclient.common import template_format
from heatclient.common import template_utils
from heatclient.common import utils as heat_utils
from oslo_serialization import jsonutils
import six
from six.moves.urllib import request

from horizon import exceptions
from horizon.utils import functions as utils
from horizon.utils.memoized import memoized
from openstack_dashboard.api import base
from openstack_dashboard.contrib.developer.profiler import api as profiler


def format_parameters(params):
    parameters = {}
    for count, p in enumerate(params, 1):
        parameters['Parameters.member.%d.ParameterKey' % count] = p
        parameters['Parameters.member.%d.ParameterValue' % count] = params[p]
    return parameters


@memoized
def heatclient(request, password=None):
    api_version = "1"
    insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
    cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
    endpoint = base.url_for(request, 'orchestration')
    kwargs = {
        'token': request.user.token.id,
        'insecure': insecure,
        'ca_file': cacert,
        'username': request.user.username,
        'password': password
        # 'timeout': args.timeout,
        # 'ca_file': args.ca_file,
        # 'cert_file': args.cert_file,
        # 'key_file': args.key_file,
    }
    client = heat_client.Client(api_version, endpoint, **kwargs)
    client.format_parameters = format_parameters
    return client


@profiler.trace
def stacks_list(request, marker=None, sort_dir='desc', sort_key='created_at',
                paginate=False, filters=None):
    limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
    page_size = utils.get_page_size(request)

    if paginate:
        request_size = page_size + 1
    else:
        request_size = limit

    kwargs = {'sort_dir': sort_dir, 'sort_key': sort_key}
    if marker:
        kwargs['marker'] = marker

    if filters:
        kwargs.update(filters)
        if 'status' in kwargs:
            kwargs['status'] = kwargs['status'].replace(' ', '_').upper()

    stacks_iter = heatclient(request).stacks.list(limit=request_size,
                                                  **kwargs)

    has_prev_data = False
    has_more_data = False
    stacks = list(stacks_iter)

    if paginate:
        if len(stacks) > page_size:
            stacks.pop()
            has_more_data = True
            if marker is not None:
                has_prev_data = True
        elif sort_dir == 'asc' and marker is not None:
            has_more_data = True
        elif marker is not None:
            has_prev_data = True
    return (stacks, has_more_data, has_prev_data)


def _ignore_if(key, value):
    if key != 'get_file' and key != 'type':
        return True
    if not isinstance(value, six.string_types):
        return True
    if (key == 'type' and
            not value.endswith(('.yaml', '.template'))):
        return True
    return False


@profiler.trace
def get_template_files(template_data=None, template_url=None):
    if template_data:
        tpl = template_data
    elif template_url:
        with contextlib.closing(request.urlopen(template_url)) as u:
            tpl = u.read()
    else:
        return {}, None
    if not tpl:
        return {}, None
    if isinstance(tpl, six.binary_type):
        tpl = tpl.decode('utf-8')
    template = template_format.parse(tpl)
    files = {}
    _get_file_contents(template, files)
    return files, template


def _get_file_contents(from_data, files):
    if not isinstance(from_data, (dict, list)):
        return
    if isinstance(from_data, dict):
        recurse_data = from_data.values()
        for key, value in from_data.items():
            if _ignore_if(key, value):
                continue
            if not value.startswith(('http://', 'https://')):
                raise exceptions.GetFileError(value, 'get_file')
            if value not in files:
                file_content = heat_utils.read_url_content(value)
                if template_utils.is_template(file_content):
                    template = get_template_files(template_url=value)[1]
                    file_content = jsonutils.dumps(template)
                files[value] = file_content
    else:
        recurse_data = from_data
    for value in recurse_data:
        _get_file_contents(value, files)


@profiler.trace
def stack_delete(request, stack_id):
    return heatclient(request).stacks.delete(stack_id)


@profiler.trace
def stack_get(request, stack_id):
    return heatclient(request).stacks.get(stack_id)


@profiler.trace
def template_get(request, stack_id):
    return heatclient(request).stacks.template(stack_id)


@profiler.trace
def stack_create(request, password=None, **kwargs):
    return heatclient(request, password).stacks.create(**kwargs)


@profiler.trace
def stack_preview(request, password=None, **kwargs):
    return heatclient(request, password).stacks.preview(**kwargs)


@profiler.trace
def stack_update(request, stack_id, password=None, **kwargs):
    return heatclient(request, password).stacks.update(stack_id, **kwargs)


@profiler.trace
def snapshot_create(request, stack_id):
    return heatclient(request).stacks.snapshot(stack_id)


@profiler.trace
def snapshot_list(request, stack_id):
    return heatclient(request).stacks.snapshot_list(stack_id)


@profiler.trace
def snapshot_show(request, stack_id, snapshot_id):
    return heatclient(request).stacks.snapshot_show(stack_id, snapshot_id)


@profiler.trace
def snapshot_delete(request, stack_id, snapshot_id):
    return heatclient(request).stacks.snapshot_delete(stack_id, snapshot_id)


@profiler.trace
def events_list(request, stack_name):
    return heatclient(request).events.list(stack_name)


@profiler.trace
def resources_list(request, stack_name):
    return heatclient(request).resources.list(stack_name)


@profiler.trace
def resource_get(request, stack_id, resource_name):
    return heatclient(request).resources.get(stack_id, resource_name)


@profiler.trace
def resource_metadata_get(request, stack_id, resource_name):
    return heatclient(request).resources.metadata(stack_id, resource_name)


@profiler.trace
def template_validate(request, **kwargs):
    return heatclient(request).stacks.validate(**kwargs)


@profiler.trace
def action_check(request, stack_id):
    return heatclient(request).actions.check(stack_id)


@profiler.trace
def action_suspend(request, stack_id):
    return heatclient(request).actions.suspend(stack_id)


@profiler.trace
def action_resume(request, stack_id):
    return heatclient(request).actions.resume(stack_id)


@profiler.trace
def resource_types_list(request, filters=None):
    return heatclient(request).resource_types.list(filters=filters)


@profiler.trace
def resource_type_get(request, resource_type):
    return heatclient(request).resource_types.get(resource_type)


@profiler.trace
def service_list(request):
    return heatclient(request).services.list()


@profiler.trace
def template_version_list(request):
    return heatclient(request).template_versions.list()


@profiler.trace
def template_function_list(request, template_version):
    return heatclient(request).template_versions.get(template_version)