<s> import urlparse <EOL> try : <EOL> from keystoneclient . v2_0 import client as keystone_client <EOL> import glanceclient <EOL> from novaclient . v1_1 import client as nova_client <EOL> from neutronclient . v2_0 import client as quantum_client <EOL> has_openstack = True <EOL> except : <EOL> has_openstack = False <EOL> from xos . config import Config <EOL> def require_enabled ( callable ) : <EOL> def wrapper ( * args , ** kwds ) : <EOL> if has_openstack : <EOL> return callable ( * args , ** kwds ) <EOL> else : <EOL> return None <EOL> return wrapper <EOL> def parse_novarc ( filename ) : <EOL> opts = { } <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> for line in f : <EOL> try : <EOL> line = line . replace ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> parts = line . split ( '<STR_LIT:=>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> value = parts [ <NUM_LIT:1> ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> value = value . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> opts [ parts [ <NUM_LIT:0> ] ] = value <EOL> except : <EOL> pass <EOL> f . close ( ) <EOL> return opts <EOL> class Client : <EOL> def __init__ ( self , username = None , password = None , tenant = None , url = None , token = None , endpoint = None , controller = None , cacert = None , admin = True , * args , ** kwds ) : <EOL> self . has_openstack = has_openstack <EOL> self . url = controller . auth_url <EOL> if admin : <EOL> self . username = controller . admin_user <EOL> self . password = controller . admin_password <EOL> self . tenant = controller . admin_tenant <EOL> else : <EOL> self . username = None <EOL> self . password = None <EOL> self . tenant = None <EOL> if username : <EOL> self . username = username <EOL> if password : <EOL> self . password = password <EOL> if tenant : <EOL> self . tenant = tenant <EOL> if url : <EOL> self . url = url <EOL> if token : <EOL> self . token = token <EOL> if endpoint : <EOL> self . endpoint = endpoint <EOL> self . cacert = cacert <EOL> class KeystoneClient ( Client ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . client = keystone_client . Client ( username = self . username , <EOL> password = self . password , <EOL> tenant_name = self . tenant , <EOL> auth_url = self . url <EOL> ) <EOL> @ require_enabled <EOL> def connect ( self , * args , ** kwds ) : <EOL> self . __init__ ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class Glance ( Client ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . client = glanceclient . get_client ( host = '<STR_LIT>' , <EOL> username = self . username , <EOL> password = self . password , <EOL> tenant = self . tenant , <EOL> auth_url = self . url ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class GlanceClient ( Client ) : <EOL> def __init__ ( self , version , endpoint , token , cacert = None , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . client = glanceclient . Client ( version , <EOL> endpoint = endpoint , <EOL> token = token , <EOL> cacert = cacert <EOL> ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class NovaClient ( Client ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . client = nova_client . Client ( username = self . username , <EOL> api_key = self . password , <EOL> project_id = self . tenant , <EOL> auth_url = self . url , <EOL> region_name = '<STR_LIT>' , <EOL> extensions = [ ] , <EOL> service_type = '<STR_LIT>' , <EOL> service_name = '<STR_LIT>' , <EOL> ) <EOL> @ require_enabled <EOL> def connect ( self , * args , ** kwds ) : <EOL> self . __init__ ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class NovaDB ( Client ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . ctx = get_admin_context ( ) <EOL> nova_db_api . FLAGS ( default_config_files = [ '<STR_LIT>' ] ) <EOL> self . client = nova_db_api <EOL> @ require_enabled <EOL> def connect ( self , * args , ** kwds ) : <EOL> self . __init__ ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class QuantumClient ( Client ) : <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> Client . __init__ ( self , * args , ** kwds ) <EOL> if has_openstack : <EOL> self . client = quantum_client . Client ( username = self . username , <EOL> password = self . password , <EOL> tenant_name = self . tenant , <EOL> auth_url = self . url , <EOL> ca_cert = self . cacert ) <EOL> @ require_enabled <EOL> def connect ( self , * args , ** kwds ) : <EOL> self . __init__ ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . client , name ) <EOL> class OpenStackClient : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwds ) : <EOL> self . keystone = KeystoneClient ( * args , ** kwds ) <EOL> url_parsed = urlparse . urlparse ( self . keystone . url ) <EOL> hostname = url_parsed . netloc . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> token = self . keystone . client . tokens . authenticate ( username = self . keystone . username , password = self . keystone . password , tenant_name = self . keystone . tenant ) <EOL> glance_endpoint = self . keystone . service_catalog . url_for ( service_type = '<STR_LIT:image>' , endpoint_type = '<STR_LIT>' ) <EOL> self . glanceclient = GlanceClient ( '<STR_LIT:1>' , endpoint = glance_endpoint , token = token . id , ** kwds ) <EOL> self . nova = NovaClient ( * args , ** kwds ) <EOL> self . quantum = QuantumClient ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def connect ( self , * args , ** kwds ) : <EOL> self . __init__ ( * args , ** kwds ) <EOL> @ require_enabled <EOL> def authenticate ( self ) : <EOL> return self . keystone . authenticate ( ) </s>
