def handle(self, request, data): def find_or_create_access_keys(request, tenant_id): keys = api.keystone.list_ec2_credentials(request, request.user.id) for key in keys: if key.tenant_id == tenant_id: return key return api.keystone.create_ec2_credentials(request, request.user.id, tenant_id) try: # NOTE(jakedahn): Keystone errors unless we specifically scope # the token to tenant before making the call. api.keystone.token_create_scoped(request, data.get('tenant'), request.user.token.id) credentials = api.nova.get_x509_credentials(request) cacert = api.nova.get_x509_root_certificate(request) keys = find_or_create_access_keys(request, data.get('tenant')) context = { 'ec2_access_key': keys.access, 'ec2_secret_key': keys.secret, 'ec2_endpoint': api.url_for(request, 'ec2', endpoint_type='publicURL') } try: s3_endpoint = api.url_for(request, 's3', endpoint_type='publicURL') except exceptions.ServiceCatalogException: s3_endpoint = None context['s3_endpoint'] = s3_endpoint except: exceptions.handle(request, _('Unable to fetch EC2 credentials.'), redirect=request.build_absolute_uri()) try: temp_zip = tempfile.NamedTemporaryFile(delete=True) with closing(zipfile.ZipFile(temp_zip.name, mode='w')) as archive: archive.writestr('pk.pem', credentials.private_key) archive.writestr('cert.pem', credentials.data) archive.writestr('cacert.pem', cacert.data) archive.writestr( 'ec2rc.sh', render_to_string('settings/ec2/ec2rc.sh.template', context)) except: exceptions.handle(request, _('Error writing zipfile: %(exc)s'), redirect=request.build_absolute_uri()) response = http.HttpResponse(mimetype='application/zip') response.write(temp_zip.read()) response['Content-Disposition'] = 'attachment; \ filename=%s-x509.zip' \ % data.get('tenant') response['Content-Length'] = temp_zip.tell() return response
def handle(self, request, data): try: tenant_id = data['tenant'] tenant_name = dict(self.fields['tenant'].choices)[tenant_id] keystone_url = api.url_for(request, 'identity', endpoint_type='publicURL') context = { 'user': request.user, 'auth_url': keystone_url, 'tenant_id': tenant_id, 'tenant_name': tenant_name } response = shortcuts.render(request, 'settings/project/openrc.sh.template', context, content_type="text/plain") response['Content-Disposition'] = 'attachment; filename=openrc.sh' response['Content-Length'] = str(len(response.content)) return response except Exception, e: LOG.exception("Exception in DownloadOpenRCForm.") messages.error(request, _('Error Downloading RC File: %s') % e) return shortcuts.redirect(request.build_absolute_uri())
def handle(self, request, data): try: tenant_id = data['tenant'] tenant_name = dict(self.fields['tenant'].choices)[tenant_id] keystone_url = api.url_for(request, 'identity', endpoint_type='publicURL') context = {'user': request.user, 'auth_url': keystone_url, 'tenant_id': tenant_id, 'tenant_name': tenant_name} response = shortcuts.render(request, 'settings/project/openrc.sh.template', context, content_type="text/plain") response['Content-Disposition'] = 'attachment; filename=openrc.sh' response['Content-Length'] = str(len(response.content)) return response except Exception, e: LOG.exception("Exception in DownloadOpenRCForm.") messages.error(request, _('Error Downloading RC File: %s') % e) return shortcuts.redirect(request.build_absolute_uri())
def handle(self, request, data): def find_or_create_access_keys(request, tenant_id): keys = api.keystone.list_ec2_credentials(request, request.user.id) for key in keys: if key.tenant_id == tenant_id: return key return api.keystone.create_ec2_credentials(request, request.user.id, tenant_id) try: # NOTE(jakedahn): Keystone errors unless we specifically scope # the token to tenant before making the call. api.keystone.token_create_scoped(request, data.get("tenant"), request.user.token.id) credentials = api.nova.get_x509_credentials(request) cacert = api.nova.get_x509_root_certificate(request) keys = find_or_create_access_keys(request, data.get("tenant")) context = { "ec2_access_key": keys.access, "ec2_secret_key": keys.secret, "ec2_endpoint": api.url_for(request, "ec2", endpoint_type="publicURL"), } try: s3_endpoint = api.url_for(request, "s3", endpoint_type="publicURL") except exceptions.ServiceCatalogException: s3_endpoint = None context["s3_endpoint"] = s3_endpoint except: exceptions.handle(request, _("Unable to fetch EC2 credentials."), redirect=request.build_absolute_uri()) try: temp_zip = tempfile.NamedTemporaryFile(delete=True) with closing(zipfile.ZipFile(temp_zip.name, mode="w")) as archive: archive.writestr("pk.pem", credentials.private_key) archive.writestr("cert.pem", credentials.data) archive.writestr("cacert.pem", cacert.data) archive.writestr("ec2rc.sh", render_to_string("settings/ec2/ec2rc.sh.template", context)) except: exceptions.handle(request, _("Error writing zipfile: %(exc)s"), redirect=request.build_absolute_uri()) response = http.HttpResponse(mimetype="application/zip") response.write(temp_zip.read()) response["Content-Disposition"] = ( "attachment; \ filename=%s-x509.zip" % data.get("tenant") ) response["Content-Length"] = temp_zip.tell() return response