# vim: tabstop=4 shiftwidth=4 softtabstop=4
# 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 json
import logging
from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from django.core.urlresolvers import reverse # noqa
from django.core.urlresolvers import reverse_lazy # noqa
from django.http import HttpResponse # noqa
from django.utils.translation import ugettext_lazy as _ # noqa
from django.views import generic
from openstack_dashboard import api
from openstack_dashboard.dashboards.project.stacks \
import api as project_api
from openstack_dashboard.dashboards.project.stacks \
import forms as project_forms
from openstack_dashboard.dashboards.project.stacks \
import tables as project_tables
from openstack_dashboard.dashboards.project.stacks \
import tabs as project_tabs
LOG = logging.getLogger(__name__)
[docs]class IndexView(tables.DataTableView):
table_class = project_tables.StacksTable
template_name = 'project/stacks/index.html'
[docs] def get_data(self):
request = self.request
try:
stacks = api.heat.stacks_list(self.request)
except Exception:
exceptions.handle(request, _('Unable to retrieve stack list.'))
stacks = []
return stacks
[docs]class SelectTemplateView(forms.ModalFormView):
form_class = project_forms.TemplateForm
template_name = 'project/stacks/select_template.html'
success_url = reverse_lazy('horizon:project:stacks:launch')
[docs]class CreateStackView(forms.ModalFormView):
form_class = project_forms.StackCreateForm
template_name = 'project/stacks/create.html'
success_url = reverse_lazy('horizon:project:stacks:index')
[docs] def get_initial(self):
initial = {}
if 'template_data' in self.kwargs:
initial['template_data'] = self.kwargs['template_data']
if 'template_url' in self.kwargs:
initial['template_url'] = self.kwargs['template_url']
if 'parameters' in self.kwargs:
initial['parameters'] = json.dumps(self.kwargs['parameters'])
return initial
[docs]class DetailView(tabs.TabView):
tab_group_class = project_tabs.StackDetailTabs
template_name = 'project/stacks/detail.html'
[docs] def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
context["stack"] = self.get_data(self.request)
return context
[docs] def get_data(self, request, **kwargs):
if not hasattr(self, "_stack"):
stack_id = kwargs['stack_id']
try:
stack = api.heat.stack_get(request, stack_id)
self._stack = stack
request.session['stack_id'] = stack.id
request.session['stack_name'] = stack.stack_name
except Exception:
msg = _("Unable to retrieve stack.")
redirect = reverse('horizon:project:stacks:index')
exceptions.handle(request, msg, redirect=redirect)
return self._stack
[docs] def get_tabs(self, request, **kwargs):
stack = self.get_data(request, **kwargs)
return self.tab_group_class(request, stack=stack, **kwargs)
[docs]class ResourceView(tabs.TabView):
tab_group_class = project_tabs.ResourceDetailTabs
template_name = 'project/stacks/resource.html'
[docs] def get_context_data(self, **kwargs):
context = super(ResourceView, self).get_context_data(**kwargs)
context["resource"] = self.get_data(self.request, **kwargs)
context["metadata"] = self.get_metadata(self.request, **kwargs)
return context
[docs] def get_data(self, request, **kwargs):
if not hasattr(self, "_resource"):
try:
resource = api.heat.resource_get(
request,
kwargs['stack_id'],
kwargs['resource_name'])
self._resource = resource
except Exception:
msg = _("Unable to retrieve resource.")
redirect = reverse('horizon:project:stacks:index')
exceptions.handle(request, msg, redirect=redirect)
return self._resource
[docs] def get_tabs(self, request, **kwargs):
resource = self.get_data(request, **kwargs)
metadata = self.get_metadata(request, **kwargs)
return self.tab_group_class(
request, resource=resource, metadata=metadata, **kwargs)
[docs]class JSONView(generic.View):
[docs] def get(self, request, stack_id=''):
return HttpResponse(project_api.d3_data(request, stack_id=stack_id),
content_type="application/json")