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
This commit is contained in:
Doug Hellmann 2012-05-02 17:02:08 -04:00
parent 3548fc55e7
commit 3a17779a2f
3 changed files with 126 additions and 60 deletions

2
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.log
*.log.*
*.pyc
*.swp
*~
@ -10,3 +11,4 @@ dist
python_openstackclient.egg-info
.tox/
ChangeLog
TAGS

View File

@ -0,0 +1,107 @@
"""Manage access to the clients, including authenticating when needed.
"""
import logging
from openstackclient.common import exceptions as exc
from openstackclient.compute import client as compute_client
from keystoneclient.v2_0 import client as keystone_client
LOG = logging.getLogger(__name__)
class ClientCache(object):
"""Descriptor class for caching created client handles.
"""
def __init__(self, factory):
self.factory = factory
self._handle = None
def __get__(self, instance, owner):
# Tell the ClientManager to login to keystone
if self._handle is None:
instance.init_token()
self._handle = self.factory(instance)
return self._handle
class ClientManager(object):
"""Manages access to API clients, including authentication.
"""
compute = ClientCache(compute_client.make_client)
def __init__(self, token=None, url=None,
auth_url=None,
tenant_name=None, tenant_id=None,
username=None, password=None,
region_name=None,
identity_api_version=None,
compute_api_version=None,
image_api_version=None,
):
self._token = token
self._url = url
self._auth_url = auth_url
self._tenant_name = tenant_name
self._tenant_id = tenant_id
self._username = username
self._password = password
self._region_name = region_name
self._identity_api_version = identity_api_version
self._compute_api_version = compute_api_version
self._image_api_version = image_api_version
def init_token(self):
"""Return the auth token and endpoint.
"""
if self._token:
LOG.debug('using existing auth token')
return
LOG.debug('validating authentication options')
if not self._username:
raise exc.CommandError(
"You must provide a username via"
" either --os-username or env[OS_USERNAME]")
if not self._password:
raise exc.CommandError(
"You must provide a password via"
" either --os-password or env[OS_PASSWORD]")
if not (self._tenant_id or self._tenant_name):
raise exc.CommandError(
"You must provide a tenant_id via"
" either --os-tenant-id or via env[OS_TENANT_ID]")
if not self._auth_url:
raise exc.CommandError(
"You must provide an auth url via"
" either --os-auth-url or via env[OS_AUTH_URL]")
kwargs = {
'username': self._username,
'password': self._password,
'tenant_id': self._tenant_id,
'tenant_name': self._tenant_name,
'auth_url': self._auth_url
}
self._auth_client = keystone_client.Client(**kwargs)
self._token = self._auth_client.auth_token
return
def get_endpoint_for_service_type(self, service_type):
"""Return the endpoint URL for the service type.
"""
# See if we are using password flow auth, i.e. we have a
# service catalog to select endpoints from
if self._auth_client and self._auth_client.service_catalog:
endpoint = self._auth_client.service_catalog.url_for(
service_type=service_type)
else:
# Hope we were given the correct URL.
endpoint = self._url
return endpoint

View File

@ -19,7 +19,6 @@
Command-line interface to the OpenStack APIs
"""
import argparse
import logging
import os
import sys
@ -27,9 +26,7 @@ import sys
from cliff.app import App
from cliff.commandmanager import CommandManager
from keystoneclient.v2_0 import client as ksclient
from openstackclient.common import exceptions as exc
from openstackclient.common import clientmanager
from openstackclient.common import utils
@ -144,72 +141,32 @@ class OpenStackShell(App):
'image': self.options.os_image_api_version,
}
self.client_manager = clientmanager.ClientManager(
token=self.options.os_token,
url=self.options.os_url,
auth_url=self.options.os_auth_url,
tenant_name=self.options.os_tenant_name,
tenant_id=self.options.os_tenant_id,
username=self.options.os_username,
password=self.options.os_password,
region_name=self.options.os_region_name,
identity_api_version=self.options.os_identity_api_version,
compute_api_version=self.options.os_compute_api_version,
image_api_version=self.options.os_image_api_version,
)
self.log.debug("API: Identity=%s Compute=%s Image=%s" % (
self.api_version['identity'],
self.api_version['compute'],
self.api_version['image'])
)
# do checking of os_username, etc here
if (self.options.os_token and self.options.os_url):
# do token auth
self.endpoint = self.options.os_url
self.token = self.options.os_token
else:
if not self.options.os_username:
raise exc.CommandError("You must provide a username via"
" either --os-username or env[OS_USERNAME]")
if not self.options.os_password:
raise exc.CommandError("You must provide a password via"
" either --os-password or env[OS_PASSWORD]")
if not (self.options.os_tenant_id or self.options.os_tenant_name):
raise exc.CommandError("You must provide a tenant_id via"
" either --os-tenant-id or via env[OS_TENANT_ID]")
if not self.options.os_auth_url:
raise exc.CommandError("You must provide an auth url via"
" either --os-auth-url or via env[OS_AUTH_URL]")
kwargs = {
'username': self.options.os_username,
'password': self.options.os_password,
'tenant_id': self.options.os_tenant_id,
'tenant_name': self.options.os_tenant_name,
'auth_url': self.options.os_auth_url
}
self.auth_client = ksclient.Client(
username=kwargs.get('username'),
password=kwargs.get('password'),
tenant_id=kwargs.get('tenant_id'),
tenant_name=kwargs.get('tenant_name'),
auth_url=kwargs.get('auth_url'),
)
self.token = self.auth_client.auth_token
# Since we don't know which command is being executed yet, defer
# selection of a service API until later
self.endpoint = None
self.log.debug("token: %s" % self.token)
self.log.debug("endpoint: %s" % self.endpoint)
def prepare_to_run_command(self, cmd):
"""Set up auth and API versions"""
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
self.log.debug("api: %s" % cmd.api)
# See if we are using password flow auth, i.e. we have a
# service catalog to select endpoints from
if self.auth_client and self.auth_client.service_catalog:
self.endpoint = self.auth_client.service_catalog.url_for(
service_type=cmd.api)
# self.endpoint == None here is an error...
if not self.endpoint:
raise RuntimeError('no endpoint found')
# get a client for the desired api here
self.log.debug("api: %s" % cmd.api if hasattr(cmd, 'api') else None)
return
def clean_up(self, cmd, result, err):
self.log.debug('clean_up %s', cmd.__class__.__name__)