Fix "help" command and implement "list server" and "show server" · bhuvan/python-openstackclient@5e40321 · GitHub
Skip to content

Commit 5e40321

Browse files
author
Doug Hellmann
committed
Fix "help" command and implement "list server" and "show server"
blueprint client-manager blueprint nova-client bug 992841 Move the authentication logic into a new ClientManager class so that only commands that need to authenticate will trigger that code. Implement "list server" and "show server" commands as examples of using the ClientManager, Lister, and ShowOne classes. Change-Id: I9845b70b33bae4b193dbe41871bf0ca8e286a727
1 parent b5a809d commit 5e40321

7 files changed

Lines changed: 324 additions & 106 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Manage access to the clients, including authenticating when needed.
2+
"""
3+
4+
import logging
5+
6+
from openstackclient.common import exceptions as exc
7+
from openstackclient.compute import client as compute_client
8+
9+
from keystoneclient.v2_0 import client as keystone_client
10+
11+
LOG = logging.getLogger(__name__)
12+
13+
14+
class ClientCache(object):
15+
"""Descriptor class for caching created client handles.
16+
"""
17+
18+
def __init__(self, factory):
19+
self.factory = factory
20+
self._handle = None
21+
22+
def __get__(self, instance, owner):
23+
# Tell the ClientManager to login to keystone
24+
if self._handle is None:
25+
instance.init_token()
26+
self._handle = self.factory(instance)
27+
return self._handle
28+
29+
30+
class ClientManager(object):
31+
"""Manages access to API clients, including authentication.
32+
"""
33+
34+
compute = ClientCache(compute_client.make_client)
35+
36+
def __init__(self, token=None, url=None,
37+
auth_url=None,
38+
tenant_name=None, tenant_id=None,
39+
username=None, password=None,
40+
region_name=None,
41+
identity_api_version=None,
42+
compute_api_version=None,
43+
image_api_version=None,
44+
):
45+
self._token = token
46+
self._url = url
47+
self._auth_url = auth_url
48+
self._tenant_name = tenant_name
49+
self._tenant_id = tenant_id
50+
self._username = username
51+
self._password = password
52+
self._region_name = region_name
53+
self._identity_api_version = identity_api_version
54+
self._compute_api_version = compute_api_version
55+
self._image_api_version = image_api_version
56+
57+
def init_token(self):
58+
"""Return the auth token and endpoint.
59+
"""
60+
if self._token:
61+
LOG.debug('using existing auth token')
62+
return
63+
64+
LOG.debug('validating authentication options')
65+
if not self._username:
66+
raise exc.CommandError(
67+
"You must provide a username via"
68+
" either --os-username or env[OS_USERNAME]")
69+
70+
if not self._password:
71+
raise exc.CommandError(
72+
"You must provide a password via"
73+
" either --os-password or env[OS_PASSWORD]")
74+
75+
if not (self._tenant_id or self._tenant_name):
76+
raise exc.CommandError(
77+
"You must provide a tenant_id via"
78+
" either --os-tenant-id or via env[OS_TENANT_ID]")
79+
80+
if not self._auth_url:
81+
raise exc.CommandError(
82+
"You must provide an auth url via"
83+
" either --os-auth-url or via env[OS_AUTH_URL]")
84+
85+
kwargs = {
86+
'username': self._username,
87+
'password': self._password,
88+
'tenant_id': self._tenant_id,
89+
'tenant_name': self._tenant_name,
90+
'auth_url': self._auth_url
91+
}
92+
self._auth_client = keystone_client.Client(**kwargs)
93+
self._token = self._auth_client.auth_token
94+
return
95+
96+
def get_endpoint_for_service_type(self, service_type):
97+
"""Return the endpoint URL for the service type.
98+
"""
99+
# See if we are using password flow auth, i.e. we have a
100+
# service catalog to select endpoints from
101+
if self._auth_client and self._auth_client.service_catalog:
102+
endpoint = self._auth_client.service_catalog.url_for(
103+
service_type=service_type)
104+
else:
105+
# Hope we were given the correct URL.
106+
endpoint = self._url
107+
return endpoint

openstackclient/compute/client.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import logging
2+
3+
from novaclient import client as nova_client
4+
5+
LOG = logging.getLogger(__name__)
6+
7+
8+
def make_client(instance):
9+
"""Returns a compute service client.
10+
"""
11+
LOG.debug('instantiating compute client')
12+
# FIXME(dhellmann): Where is the endpoint value used?
13+
# url = instance.get_endpoint_for_service_type('compute')
14+
client = nova_client.Client(
15+
version=instance._compute_api_version,
16+
username=instance._username,
17+
api_key=instance._password,
18+
project_id=instance._tenant_name,
19+
auth_url=instance._auth_url,
20+
# FIXME(dhellmann): add constructor argument for this
21+
insecure=False,
22+
region_name=instance._region_name,
23+
# FIXME(dhellmann): get endpoint_type from option?
24+
endpoint_type='publicURL',
25+
# FIXME(dhellmann): add extension discovery
26+
extensions=[],
27+
service_type='compute',
28+
# FIXME(dhellmann): what is service_name?
29+
service_name='',
30+
)
31+
client.authenticate()
32+
return client

openstackclient/compute/v2/server.py

Lines changed: 143 additions & 46 deletions

0 commit comments

Comments
 (0)