summaryrefslogtreecommitdiff
path: root/openstack_dashboard/api/heat.py
diff options
context:
space:
mode:
Diffstat (limited to 'openstack_dashboard/api/heat.py')
-rw-r--r--openstack_dashboard/api/heat.py265
1 files changed, 0 insertions, 265 deletions
diff --git a/openstack_dashboard/api/heat.py b/openstack_dashboard/api/heat.py
deleted file mode 100644
index c31c61d..0000000
--- a/openstack_dashboard/api/heat.py
+++ /dev/null
@@ -1,265 +0,0 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import contextlib
14
15from django.conf import settings
16from heatclient import client as heat_client
17from heatclient.common import template_format
18from heatclient.common import template_utils
19from heatclient.common import utils as heat_utils
20from oslo_serialization import jsonutils
21import six
22from six.moves.urllib import request
23
24from horizon import exceptions
25from horizon.utils import functions as utils
26from horizon.utils.memoized import memoized
27from openstack_dashboard.api import base
28from openstack_dashboard.contrib.developer.profiler import api as profiler
29
30
31def format_parameters(params):
32 parameters = {}
33 for count, p in enumerate(params, 1):
34 parameters['Parameters.member.%d.ParameterKey' % count] = p
35 parameters['Parameters.member.%d.ParameterValue' % count] = params[p]
36 return parameters
37
38
39@memoized
40def heatclient(request, password=None):
41 api_version = "1"
42 insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
43 cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
44 endpoint = base.url_for(request, 'orchestration')
45 kwargs = {
46 'token': request.user.token.id,
47 'insecure': insecure,
48 'ca_file': cacert,
49 'username': request.user.username,
50 'password': password
51 # 'timeout': args.timeout,
52 # 'ca_file': args.ca_file,
53 # 'cert_file': args.cert_file,
54 # 'key_file': args.key_file,
55 }
56 client = heat_client.Client(api_version, endpoint, **kwargs)
57 client.format_parameters = format_parameters
58 return client
59
60
61@profiler.trace
62def stacks_list(request, marker=None, sort_dir='desc', sort_key='created_at',
63 paginate=False, filters=None):
64 limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
65 page_size = utils.get_page_size(request)
66
67 if paginate:
68 request_size = page_size + 1
69 else:
70 request_size = limit
71
72 kwargs = {'sort_dir': sort_dir, 'sort_key': sort_key}
73 if marker:
74 kwargs['marker'] = marker
75
76 if filters:
77 kwargs.update(filters)
78 if 'status' in kwargs:
79 kwargs['status'] = kwargs['status'].replace(' ', '_').upper()
80
81 stacks_iter = heatclient(request).stacks.list(limit=request_size,
82 **kwargs)
83
84 has_prev_data = False
85 has_more_data = False
86 stacks = list(stacks_iter)
87
88 if paginate:
89 if len(stacks) > page_size:
90 stacks.pop()
91 has_more_data = True
92 if marker is not None:
93 has_prev_data = True
94 elif sort_dir == 'asc' and marker is not None:
95 has_more_data = True
96 elif marker is not None:
97 has_prev_data = True
98 return (stacks, has_more_data, has_prev_data)
99
100
101def _ignore_if(key, value):
102 if key != 'get_file' and key != 'type':
103 return True
104 if not isinstance(value, six.string_types):
105 return True
106 if (key == 'type' and
107 not value.endswith(('.yaml', '.template'))):
108 return True
109 return False
110
111
112@profiler.trace
113def get_template_files(template_data=None, template_url=None):
114 if template_data:
115 tpl = template_data
116 elif template_url:
117 with contextlib.closing(request.urlopen(template_url)) as u:
118 tpl = u.read()
119 else:
120 return {}, None
121 if not tpl:
122 return {}, None
123 if isinstance(tpl, six.binary_type):
124 tpl = tpl.decode('utf-8')
125 template = template_format.parse(tpl)
126 files = {}
127 _get_file_contents(template, files)
128 return files, template
129
130
131def _get_file_contents(from_data, files):
132 if not isinstance(from_data, (dict, list)):
133 return
134 if isinstance(from_data, dict):
135 recurse_data = from_data.values()
136 for key, value in from_data.items():
137 if _ignore_if(key, value):
138 continue
139 if not value.startswith(('http://', 'https://')):
140 raise exceptions.GetFileError(value, 'get_file')
141 if value not in files:
142 file_content = heat_utils.read_url_content(value)
143 if template_utils.is_template(file_content):
144 template = get_template_files(template_url=value)[1]
145 file_content = jsonutils.dumps(template)
146 files[value] = file_content
147 else:
148 recurse_data = from_data
149 for value in recurse_data:
150 _get_file_contents(value, files)
151
152
153@profiler.trace
154def stack_delete(request, stack_id):
155 return heatclient(request).stacks.delete(stack_id)
156
157
158@profiler.trace
159def stack_get(request, stack_id):
160 return heatclient(request).stacks.get(stack_id)
161
162
163@profiler.trace
164def template_get(request, stack_id):
165 return heatclient(request).stacks.template(stack_id)
166
167
168@profiler.trace
169def stack_create(request, password=None, **kwargs):
170 return heatclient(request, password).stacks.create(**kwargs)
171
172
173@profiler.trace
174def stack_preview(request, password=None, **kwargs):
175 return heatclient(request, password).stacks.preview(**kwargs)
176
177
178@profiler.trace
179def stack_update(request, stack_id, password=None, **kwargs):
180 return heatclient(request, password).stacks.update(stack_id, **kwargs)
181
182
183@profiler.trace
184def snapshot_create(request, stack_id):
185 return heatclient(request).stacks.snapshot(stack_id)
186
187
188@profiler.trace
189def snapshot_list(request, stack_id):
190 return heatclient(request).stacks.snapshot_list(stack_id)
191
192
193@profiler.trace
194def snapshot_show(request, stack_id, snapshot_id):
195 return heatclient(request).stacks.snapshot_show(stack_id, snapshot_id)
196
197
198@profiler.trace
199def snapshot_delete(request, stack_id, snapshot_id):
200 return heatclient(request).stacks.snapshot_delete(stack_id, snapshot_id)
201
202
203@profiler.trace
204def events_list(request, stack_name):
205 return heatclient(request).events.list(stack_name)
206
207
208@profiler.trace
209def resources_list(request, stack_name):
210 return heatclient(request).resources.list(stack_name)
211
212
213@profiler.trace
214def resource_get(request, stack_id, resource_name):
215 return heatclient(request).resources.get(stack_id, resource_name)
216
217
218@profiler.trace
219def resource_metadata_get(request, stack_id, resource_name):
220 return heatclient(request).resources.metadata(stack_id, resource_name)
221
222
223@profiler.trace
224def template_validate(request, **kwargs):
225 return heatclient(request).stacks.validate(**kwargs)
226
227
228@profiler.trace
229def action_check(request, stack_id):
230 return heatclient(request).actions.check(stack_id)
231
232
233@profiler.trace
234def action_suspend(request, stack_id):
235 return heatclient(request).actions.suspend(stack_id)
236
237
238@profiler.trace
239def action_resume(request, stack_id):
240 return heatclient(request).actions.resume(stack_id)
241
242
243@profiler.trace
244def resource_types_list(request, filters=None):
245 return heatclient(request).resource_types.list(filters=filters)
246
247
248@profiler.trace
249def resource_type_get(request, resource_type):
250 return heatclient(request).resource_types.get(resource_type)
251
252
253@profiler.trace
254def service_list(request):
255 return heatclient(request).services.list()
256
257
258@profiler.trace
259def template_version_list(request):
260 return heatclient(request).template_versions.list()
261
262
263@profiler.trace
264def template_function_list(request, template_version):
265 return heatclient(request).template_versions.get(template_version)