<s> from oslo_config import cfg <EOL> from six . moves import http_client <EOL> from six . moves . urllib import parse <EOL> from swiftclient import client as swift_client <EOL> from swiftclient import exceptions as swift_exceptions <EOL> from swiftclient import utils as swift_utils <EOL> from ironic . common import exception <EOL> from ironic . common . i18n import _ <EOL> from ironic . common import keystone <EOL> swift_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:2> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( swift_opts , group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , <EOL> group = '<STR_LIT>' ) <EOL> class SwiftAPI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> user = None , <EOL> tenant_name = None , <EOL> key = None , <EOL> auth_url = None , <EOL> auth_version = None , <EOL> region_name = None ) : <EOL> """<STR_LIT>""" <EOL> user = user or CONF . keystone_authtoken . admin_user <EOL> tenant_name = tenant_name or CONF . keystone_authtoken . admin_tenant_name <EOL> key = key or CONF . keystone_authtoken . admin_password <EOL> auth_url = auth_url or CONF . keystone_authtoken . auth_uri <EOL> auth_version = auth_version or CONF . keystone_authtoken . auth_version <EOL> auth_url = keystone . get_keystone_url ( auth_url , auth_version ) <EOL> params = { '<STR_LIT>' : CONF . swift . swift_max_retries , <EOL> '<STR_LIT>' : CONF . keystone_authtoken . insecure , <EOL> '<STR_LIT>' : CONF . keystone_authtoken . cafile , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : tenant_name , <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : auth_url , <EOL> '<STR_LIT>' : auth_version } <EOL> region_name = region_name or CONF . keystone_authtoken . region_name <EOL> if region_name : <EOL> params [ '<STR_LIT>' ] = { '<STR_LIT>' : region_name } <EOL> self . connection = swift_client . Connection ( ** params ) <EOL> def create_object ( self , container , object , filename , <EOL> object_headers = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . connection . put_container ( container ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> raise exception . SwiftOperationError ( operation = operation , error = e ) <EOL> with open ( filename , "<STR_LIT:r>" ) as fileobj : <EOL> try : <EOL> obj_uuid = self . connection . put_object ( container , <EOL> object , <EOL> fileobj , <EOL> headers = object_headers ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> raise exception . SwiftOperationError ( operation = operation , <EOL> error = e ) <EOL> return obj_uuid <EOL> def get_temp_url ( self , container , object , timeout ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> account_info = self . connection . head_account ( ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> raise exception . SwiftOperationError ( operation = operation , <EOL> error = e ) <EOL> storage_url , token = self . connection . get_auth ( ) <EOL> parse_result = parse . urlparse ( storage_url ) <EOL> swift_object_path = '<STR_LIT:/>' . join ( ( parse_result . path , container , object ) ) <EOL> temp_url_key = account_info [ '<STR_LIT>' ] <EOL> url_path = swift_utils . generate_temp_url ( swift_object_path , timeout , <EOL> temp_url_key , '<STR_LIT:GET>' ) <EOL> return parse . urlunparse ( ( parse_result . scheme , <EOL> parse_result . netloc , <EOL> url_path , <EOL> None , <EOL> None , <EOL> None ) ) <EOL> def delete_object ( self , container , object ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . connection . delete_object ( container , object ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> if e . http_status == http_client . NOT_FOUND : <EOL> raise exception . SwiftObjectNotFoundError ( object = object , <EOL> container = container , <EOL> operation = operation ) <EOL> raise exception . SwiftOperationError ( operation = operation , error = e ) <EOL> def head_object ( self , container , object ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . connection . head_object ( container , object ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> raise exception . SwiftOperationError ( operation = operation , error = e ) <EOL> def update_object_meta ( self , container , object , object_headers ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . connection . post_object ( container , object , object_headers ) <EOL> except swift_exceptions . ClientException as e : <EOL> operation = _ ( "<STR_LIT>" ) <EOL> raise exception . SwiftOperationError ( operation = operation , error = e ) </s>
