Refactored the managers to allow to add library clients

This commit is contained in:
George Peristerakis 2014-09-19 12:33:18 -04:00
parent 9dbb72d73a
commit 2671f9b5d4

View File

@ -30,109 +30,171 @@ from novaclient.v1_1 import client as nova_client
class KeystoneManager(object): class KeystoneManager(object):
"""Manages Keystone queries.""" """Manages Keystone queries."""
client = None _client = None
def __init__(self, username, password, project, auth_url, insecure): def __init__(self, username, password, project, auth_url, insecure):
self.client = keystone_client.Client( self.username = username
username=username, password=password, self.password = password
tenant_name=project, auth_url=auth_url, insecure=insecure) self.project = project
self.auth_url = auth_url
self.insecure = insecure
def client(self):
if not self._client:
self._client = keystone_client.Client(username=self.username,
password=self.password,
tenant_name=self.project,
auth_url=self.auth_url,
insecure=self.insecure)
return self._client
def set_client(client):
self._client = client
def get_token(self): def get_token(self):
return self.client.auth_token return self.client().auth_token
def get_endpoint(self, service_type, endpoint_type="publicURL"): def get_endpoint(self, service_type, endpoint_type="publicURL"):
catalog = self.client.service_catalog.get_endpoints() catalog = self.client().service_catalog.get_endpoints()
return catalog[service_type][0][endpoint_type] return catalog[service_type][0][endpoint_type]
def get_project_id(self): def get_project_id(self):
return self.client.tenant_id return self.client().tenant_id
class NeutronManager(object): class NeutronManager(object):
_client = None
_project_id = None
def __init__(self, username, password, project, auth_url, insecure): def __init__(self, username, password, project, auth_url, insecure):
self.client = neutron_client.Client( self.username = username
username=username, password=password, self.password = password
tenant_name=project, auth_url=auth_url, self.project = project
insecure=insecure) self.auth_url = auth_url
keystone_mgr = KeystoneManager(username, password, project, self.insecure = insecure
auth_url, insecure)
self.project_id = keystone_mgr.get_project_id() def client(self):
if not self._client:
self._client = neutron_client.Client(username=self.username,
password=self.password,
tenant_name=self.project,
auth_url=self.auth_url,
insecure=self.insecure)
if not self._project_id:
keystone_mgr = KeystoneManager(self.username, self.password,
self.project, self.auth_url,
self.insecure)
self._project_id = keystone_mgr.get_project_id()
return self._client
def set_client(self, client):
self._client = client
def set_project_id(self, project_id):
self._project_id = project_id
def router_list(self): def router_list(self):
return filter(self._owned_resource, return filter(self._owned_resource,
self.client.list_routers()['routers']) self.client().list_routers()['routers'])
def router_interfaces_list(self, router): def router_interfaces_list(self, router):
return self.client.list_ports(device_id=router['id'])['ports'] return self._client.list_ports(device_id=router['id'])['ports']
def port_list(self): def port_list(self):
return self.client.list_ports()['ports'] return self.client().list_ports()['ports']
def network_list(self): def network_list(self):
return filter(self._owned_resource, return filter(self._owned_resource,
self.client.list_networks()['networks']) self.client().list_networks()['networks'])
def secgroup_list(self): def secgroup_list(self):
return filter(self._owned_resource, return filter(self._owned_resource,
self.client.list_security_groups()['security_groups']) self.client().list_security_groups()['security_groups'])
def floatingip_list(self): def floatingip_list(self):
return filter(self._owned_resource, return filter(self._owned_resource,
self.client.list_floatingips()['floatingips']) self.client().list_floatingips()['floatingips'])
def subnet_list(self): def subnet_list(self):
return filter(self._owned_resource, return filter(self._owned_resource,
self.client.list_subnets()['subnets']) self.client().list_subnets()['subnets'])
def _owned_resource(self, res): def _owned_resource(self, res):
# Only considering resources owned by project # Only considering resources owned by project
return res['tenant_id'] == self.project_id return res['tenant_id'] == self._project_id
class NovaManager(object): class NovaManager(object):
"""Manage nova resources.""" """Manage nova resources."""
_client = None
def __init__(self, username, password, project, auth_url, insecure): def __init__(self, username, password, project, auth_url, insecure):
self.client = nova_client.Client(username, password, project, self.username = username
auth_url, insecure=insecure) self.password = password
self.project = project
self.auth_url = auth_url
self.insecure = insecure
def client(self):
if not self._client:
self._client = nova_client.Client(self.username, self.password,
self.project, self.auth_url,
insecure=self.insecure)
return self._client
def set_client(self, client):
self._client = client
def server_list(self): def server_list(self):
return self.client.servers.list() return self.client().servers.list()
def floating_ip_list(self): def floating_ip_list(self):
return self.client.floating_ips.list() return self.client().floating_ips.list()
def flavor_list(self): def flavor_list(self):
return self.client.flavors.list() return self.client().flavors.list()
def flavor_get(self, id): def flavor_get(self, id):
return self.client.flavors.get(id) return self.client().flavors.get(id)
def keypair_list(self): def keypair_list(self):
return self.client.keypairs.list() return self.client().keypairs.list()
def keypair_show(self, keypair): def keypair_show(self, keypair):
return self.client.keypairs.get(keypair) return self.client().keypairs.get(keypair)
def server_security_group_list(self, server): def server_security_group_list(self, server):
return self.client.servers.list_security_group(server) return self.client().servers.list_security_group(server)
class CinderManager(object): class CinderManager(object):
"""Manage Cinder resources.""" """Manage Cinder resources."""
_client = None
def __init__(self, username, password, project, auth_url, insecure): def __init__(self, username, password, project, auth_url, insecure):
self.client = cinder_client.Client(username, self.username = username
password, self.password = password
project, self.project = project
auth_url, self.auth_url = auth_url
insecure=insecure) self.insecure = insecure
def client(self):
if not self._client:
self._client = cinder_client.Client(self.username,
self.password,
self.project,
self.auth_url,
insecure=self.insecure)
return self._client
def set_client(self, client):
self._client = client
def volume_list(self): def volume_list(self):
volumes = [] volumes = []
for vol in self.client.volumes.list(): for vol in self.client().volumes.list():
volumes.append(self.client.volumes.get(vol.id)) volumes.append(self.client().volumes.get(vol.id))
return volumes return volumes
def snapshot_list(self): def snapshot_list(self):
return self.client.volume_snapshots.list() return self.client().volume_snapshots.list()