<s> from contextlib import closing <EOL> import logging <EOL> import tempfile <EOL> import zipfile <EOL> from django import http <EOL> from django import shortcuts <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import messages <EOL> from openstack_dashboard import api <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def download_ec2_bundle ( request ) : <EOL> tenant_id = request . user . tenant_id <EOL> tenant_name = request . user . tenant_name <EOL> try : <EOL> credentials = api . nova . get_x509_credentials ( request ) <EOL> cacert = api . nova . get_x509_root_certificate ( request ) <EOL> all_keys = api . keystone . list_ec2_credentials ( request , <EOL> request . user . id ) <EOL> keys = None <EOL> for key in all_keys : <EOL> if key . tenant_id == tenant_id : <EOL> keys = key <EOL> if keys is None : <EOL> keys = api . keystone . create_ec2_credentials ( request , <EOL> request . user . id , <EOL> tenant_id ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = request . build_absolute_uri ( ) ) <EOL> try : <EOL> s3_endpoint = api . base . url_for ( request , <EOL> '<STR_LIT>' , <EOL> endpoint_type = '<STR_LIT>' ) <EOL> except exceptions . ServiceCatalogException : <EOL> s3_endpoint = None <EOL> try : <EOL> ec2_endpoint = api . base . url_for ( request , <EOL> '<STR_LIT>' , <EOL> endpoint_type = '<STR_LIT>' ) <EOL> except exceptions . ServiceCatalogException : <EOL> ec2_endpoint = None <EOL> context = { '<STR_LIT>' : keys . access , <EOL> '<STR_LIT>' : keys . secret , <EOL> '<STR_LIT>' : ec2_endpoint , <EOL> '<STR_LIT>' : s3_endpoint } <EOL> template = '<STR_LIT>' <EOL> try : <EOL> temp_zip = tempfile . NamedTemporaryFile ( delete = True ) <EOL> with closing ( zipfile . ZipFile ( temp_zip . name , mode = '<STR_LIT:w>' ) ) as archive : <EOL> archive . writestr ( '<STR_LIT>' , credentials . private_key ) <EOL> archive . writestr ( '<STR_LIT>' , credentials . data ) <EOL> archive . writestr ( '<STR_LIT>' , cacert . data ) <EOL> archive . writestr ( '<STR_LIT>' , render_to_string ( template , context ) ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) , <EOL> redirect = request . build_absolute_uri ( ) ) <EOL> response = http . HttpResponse ( mimetype = '<STR_LIT>' ) <EOL> response . write ( temp_zip . read ( ) ) <EOL> response [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % tenant_name ) <EOL> response [ '<STR_LIT>' ] = temp_zip . tell ( ) <EOL> return response <EOL> def download_rc_file ( request ) : <EOL> tenant_id = request . user . tenant_id <EOL> tenant_name = request . user . tenant_name <EOL> template = '<STR_LIT>' <EOL> try : <EOL> keystone_url = api . base . url_for ( request , <EOL> '<STR_LIT>' , <EOL> endpoint_type = '<STR_LIT>' ) <EOL> context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : keystone_url , <EOL> '<STR_LIT>' : tenant_id , <EOL> '<STR_LIT>' : tenant_name } <EOL> response = shortcuts . render ( request , <EOL> template , <EOL> context , <EOL> content_type = "<STR_LIT>" ) <EOL> response [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % tenant_name ) <EOL> response [ '<STR_LIT>' ] = str ( len ( response . content ) ) <EOL> return response <EOL> except Exception as e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> messages . error ( request , _ ( '<STR_LIT>' ) % e ) <EOL> return shortcuts . redirect ( request . build_absolute_uri ( ) ) </s>
