diff --git a/flameclient/managers.py b/flameclient/managers.py index c8f384c..152c6e1 100644 --- a/flameclient/managers.py +++ b/flameclient/managers.py @@ -30,109 +30,171 @@ from novaclient.v1_1 import client as nova_client class KeystoneManager(object): """Manages Keystone queries.""" - client = None + _client = None def __init__(self, username, password, project, auth_url, insecure): - self.client = keystone_client.Client( - username=username, password=password, - tenant_name=project, auth_url=auth_url, insecure=insecure) + self.username = username + self.password = password + 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): - return self.client.auth_token + return self.client().auth_token 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] def get_project_id(self): - return self.client.tenant_id + return self.client().tenant_id class NeutronManager(object): + _client = None + _project_id = None + def __init__(self, username, password, project, auth_url, insecure): - self.client = neutron_client.Client( - username=username, password=password, - tenant_name=project, auth_url=auth_url, - insecure=insecure) - keystone_mgr = KeystoneManager(username, password, project, - auth_url, insecure) - self.project_id = keystone_mgr.get_project_id() + self.username = username + self.password = password + self.project = project + self.auth_url = auth_url + self.insecure = insecure + + 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): return filter(self._owned_resource, - self.client.list_routers()['routers']) + self.client().list_routers()['routers']) 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): - return self.client.list_ports()['ports'] + return self.client().list_ports()['ports'] def network_list(self): return filter(self._owned_resource, - self.client.list_networks()['networks']) + self.client().list_networks()['networks']) def secgroup_list(self): return filter(self._owned_resource, - self.client.list_security_groups()['security_groups']) + self.client().list_security_groups()['security_groups']) def floatingip_list(self): return filter(self._owned_resource, - self.client.list_floatingips()['floatingips']) + self.client().list_floatingips()['floatingips']) def subnet_list(self): return filter(self._owned_resource, - self.client.list_subnets()['subnets']) + self.client().list_subnets()['subnets']) def _owned_resource(self, res): # Only considering resources owned by project - return res['tenant_id'] == self.project_id + return res['tenant_id'] == self._project_id class NovaManager(object): """Manage nova resources.""" + _client = None def __init__(self, username, password, project, auth_url, insecure): - self.client = nova_client.Client(username, password, project, - auth_url, insecure=insecure) + self.username = username + 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): - return self.client.servers.list() + return self.client().servers.list() def floating_ip_list(self): - return self.client.floating_ips.list() + return self.client().floating_ips.list() def flavor_list(self): - return self.client.flavors.list() + return self.client().flavors.list() def flavor_get(self, id): - return self.client.flavors.get(id) + return self.client().flavors.get(id) def keypair_list(self): - return self.client.keypairs.list() + return self.client().keypairs.list() def keypair_show(self, keypair): - return self.client.keypairs.get(keypair) + return self.client().keypairs.get(keypair) 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): """Manage Cinder resources.""" + _client = None def __init__(self, username, password, project, auth_url, insecure): - self.client = cinder_client.Client(username, - password, - project, - auth_url, - insecure=insecure) + self.username = username + self.password = password + self.project = project + self.auth_url = auth_url + 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): volumes = [] - for vol in self.client.volumes.list(): - volumes.append(self.client.volumes.get(vol.id)) + for vol in self.client().volumes.list(): + volumes.append(self.client().volumes.get(vol.id)) return volumes def snapshot_list(self): - return self.client.volume_snapshots.list() + return self.client().volume_snapshots.list()