OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # Copyright 2018 SUSE Linux GmbH
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. from django.conf import settings
  15. from django import http
  16. from django.template.loader import render_to_string
  17. from django.urls import reverse
  18. from django.urls import reverse_lazy
  19. from django.utils.translation import ugettext_lazy as _
  20. from horizon import exceptions
  21. from horizon import forms
  22. from horizon import tables
  23. from horizon.utils import memoized
  24. from horizon import views
  25. from openstack_dashboard import api
  26. from openstack_dashboard.dashboards.identity.application_credentials \
  27. import forms as project_forms
  28. from openstack_dashboard.dashboards.identity.application_credentials \
  29. import tables as project_tables
  30. INDEX_URL = "horizon:identity:application_credentials:index"
  31. class IndexView(tables.DataTableView):
  32. table_class = project_tables.ApplicationCredentialsTable
  33. template_name = 'identity/application_credentials/index.html'
  34. page_title = _("Application Credentials")
  35. def needs_filter_first(self, table):
  36. return self._needs_filter_first
  37. def get_data(self):
  38. app_creds = []
  39. filters = self.get_filters()
  40. self._needs_filter_first = False
  41. # If filter_first is set and if there are not other filters
  42. # selected, then search criteria must be provided
  43. # and return an empty list
  44. filter_first = settings.FILTER_DATA_FIRST
  45. if filter_first['identity.application_credentials'] and not filters:
  46. self._needs_filter_first = True
  47. return app_creds
  48. try:
  49. app_creds = api.keystone.application_credential_list(
  50. self.request, filters=filters)
  51. except Exception:
  52. exceptions.handle(
  53. self.request,
  54. _('Unable to retrieve application credential list.'))
  55. return app_creds
  56. class CreateView(forms.ModalFormView):
  57. template_name = 'identity/application_credentials/create.html'
  58. form_id = 'create_application_credential_form'
  59. form_class = project_forms.CreateApplicationCredentialForm
  60. submit_label = _("Create Application Credential")
  61. submit_url = reverse_lazy(
  62. 'horizon:identity:application_credentials:create')
  63. success_url = reverse_lazy(
  64. 'horizon:identity:application_credentials:success')
  65. page_title = _("Create Application Credential")
  66. def get_form_kwargs(self):
  67. kwargs = super(CreateView, self).get_form_kwargs()
  68. kwargs['next_view'] = CreateSuccessfulView
  69. return kwargs
  70. def get_context_data(self, **kwargs):
  71. context = super(CreateView, self).get_context_data(**kwargs)
  72. context['kubeconfig_enabled'] = settings.KUBECONFIG_ENABLED
  73. return context
  74. class CreateSuccessfulView(forms.ModalFormView):
  75. template_name = 'identity/application_credentials/success.html'
  76. page_title = _("Your Application Credential")
  77. form_class = project_forms.CreateSuccessfulForm
  78. model_id = "create_application_credential_successful_modal"
  79. success_url = reverse_lazy(
  80. 'horizon:identity:application_credentials:index')
  81. cancel_label = _("Close")
  82. download_openrc_label = _("Download openrc file")
  83. download_clouds_yaml_label = _("Download clouds.yaml")
  84. download_kubeconfig_label = _("Download kubeconfig file")
  85. def get_context_data(self, **kwargs):
  86. context = super(CreateSuccessfulView, self).get_context_data(**kwargs)
  87. context['download_openrc_label'] = self.download_openrc_label
  88. context['download_clouds_yaml_label'] = self.download_clouds_yaml_label
  89. context['download_kubeconfig_label'] = self.download_kubeconfig_label
  90. context['download_openrc_url'] = reverse(
  91. 'horizon:identity:application_credentials:download_openrc')
  92. context['download_clouds_yaml_url'] = reverse(
  93. 'horizon:identity:application_credentials:download_clouds_yaml')
  94. if settings.KUBECONFIG_ENABLED:
  95. context['download_kubeconfig_url'] = reverse(
  96. 'horizon:identity:application_credentials:download_kubeconfig')
  97. return context
  98. def get_initial(self):
  99. app_cred = self.request.session['application_credential']
  100. return {
  101. 'app_cred_id': app_cred['id'],
  102. 'app_cred_name': app_cred['name'],
  103. 'app_cred_secret': app_cred['secret']
  104. }
  105. def _get_context(request):
  106. auth_url = api.base.url_for(request,
  107. 'identity',
  108. endpoint_type='publicURL')
  109. interface = 'public'
  110. region = getattr(request.user, 'services_region', '')
  111. app_cred = request.session['application_credential']
  112. context = {
  113. 'auth_url': auth_url,
  114. 'interface': interface,
  115. 'region': region,
  116. 'user': request.user,
  117. 'application_credential_id': app_cred['id'],
  118. 'application_credential_name': app_cred['name'],
  119. 'application_credential_secret': app_cred['secret'],
  120. 'kubernetes_namespace': app_cred['kubernetes_namespace'],
  121. 'kubernetes_url': settings.KUBECONFIG_KUBERNETES_URL,
  122. 'kubernetes_certificate_authority_data':
  123. settings.KUBECONFIG_CERTIFICATE_AUTHORITY_DATA}
  124. return context
  125. def _render_attachment(filename, template, context, request):
  126. content = render_to_string(template, context, request=request)
  127. disposition = 'attachment; filename="%s"' % filename
  128. response = http.HttpResponse(content, content_type="text/plain")
  129. response['Content-Disposition'] = disposition.encode('utf-8')
  130. response['Content-Length'] = str(len(response.content))
  131. return response
  132. def download_rc_file(request):
  133. context = _get_context(request)
  134. template = 'identity/application_credentials/openrc.sh.template'
  135. filename = 'app-cred-%s-openrc.sh' % context['application_credential_name']
  136. response = _render_attachment(filename, template, context, request)
  137. return response
  138. def download_clouds_yaml_file(request):
  139. context = _get_context(request)
  140. context['cloud_name'] = getattr(
  141. settings, "OPENSTACK_CLOUDS_YAML_NAME", 'openstack')
  142. context['profile'] = getattr(
  143. settings, "OPENSTACK_CLOUDS_YAML_PROFILE", None)
  144. context['regions'] = [
  145. region_tuple[1] for region_tuple in getattr(
  146. settings, "AVAILABLE_REGIONS", [])
  147. ]
  148. template = 'identity/application_credentials/clouds.yaml.template'
  149. filename = 'clouds.yaml'
  150. return _render_attachment(filename, template, context, request)
  151. def download_kubeconfig_file(request):
  152. context = _get_context(request)
  153. template = 'identity/application_credentials/kubeconfig.template'
  154. filename = 'app-cred-%s-kubeconfig' % context['application_credential_name']
  155. response = _render_attachment(filename, template, context, request)
  156. return response
  157. class DetailView(views.HorizonTemplateView):
  158. template_name = 'identity/application_credentials/detail.html'
  159. page_title = "{{ application_credential.name }}"
  160. def get_context_data(self, **kwargs):
  161. context = super(DetailView, self).get_context_data(**kwargs)
  162. app_cred = self.get_data()
  163. table = project_tables.ApplicationCredentialsTable(self.request)
  164. context["application_credential"] = app_cred
  165. context["url"] = reverse(INDEX_URL)
  166. context["actions"] = table.render_row_actions(app_cred)
  167. return context
  168. @memoized.memoized_method
  169. def get_data(self):
  170. try:
  171. app_cred_id = self.kwargs['application_credential_id']
  172. app_cred = api.keystone.application_credential_get(self.request,
  173. app_cred_id)
  174. except Exception:
  175. exceptions.handle(
  176. self.request,
  177. _('Unable to retrieve application credential details.'),
  178. redirect=reverse(INDEX_URL))
  179. return app_cred