Skip to content

Commit 60ed9aa

Browse files
author
Dean Troyer
committed
Begin to add Keystone auth
1 parent d8c6415 commit 60ed9aa

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎openstackclient/compute/v2/server.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import logging
2323

24-
from cliff.command import Command
25-
24+
from openstackclient.common import command
2625
from openstackclient.common import utils
2726

2827

@@ -57,9 +56,10 @@ def _print_server(cs, server):
5756

5857
utils.print_dict(info)
5958

60-
class List_Server(Command):
59+
class List_Server(command.OpenStackCommand):
6160
"List server command."
6261

62+
api = 'compute'
6363
log = logging.getLogger(__name__)
6464

6565
def get_parser(self, prog_name):
@@ -74,9 +74,10 @@ def get_parser(self, prog_name):
7474
def run(self, parsed_args):
7575
self.log.info('v2.List_Server.run(%s)' % parsed_args)
7676

77-
class Show_Server(Command):
77+
class Show_Server(command.OpenStackCommand):
7878
"Show server command."
7979

80+
api = 'compute'
8081
log = logging.getLogger(__name__)
8182

8283
def get_parser(self, prog_name):

‎openstackclient/shell.py‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
from cliff.app import App
2828
from cliff.commandmanager import CommandManager
2929

30+
from keystoneclient.v2_0 import client as ksclient
31+
32+
from openstackclient.common import exceptions as exc
3033
from openstackclient.common import utils
3134

3235

@@ -58,6 +61,22 @@ def __init__(self):
5861
command_manager=CommandManager('openstack.cli'),
5962
)
6063

64+
def _authenticate(self, **kwargs):
65+
"""Get an auth token from Keystone
66+
67+
:param username: name of user
68+
:param password: user's password
69+
:param tenant_id: unique identifier of tenant
70+
:param tenant_name: name of tenant
71+
:param auth_url: endpoint to authenticate against
72+
"""
73+
_ksclient = ksclient.Client(username=kwargs.get('username'),
74+
password=kwargs.get('password'),
75+
tenant_id=kwargs.get('tenant_id'),
76+
tenant_name=kwargs.get('tenant_name'),
77+
auth_url=kwargs.get('auth_url'))
78+
return _ksclient.auth_token
79+
6180
def build_option_parser(self, description, version):
6281
parser = super(OpenStackShell, self).build_option_parser(
6382
description,
@@ -97,20 +116,20 @@ def build_option_parser(self, description, version):
97116
parser.add_argument('--os-compute-api-version',
98117
metavar='<compute-api-version>',
99118
default=env('OS_COMPUTE_API_VERSION', default='2'),
100-
help='Compute API version, default=2.0 (Env: OS_COMPUTE_API_VERSION)')
119+
help='Compute API version, default=2 (Env: OS_COMPUTE_API_VERSION)')
101120

102121
parser.add_argument('--os-image-api-version',
103122
metavar='<image-api-version>',
104123
default=env('OS_IMAGE_API_VERSION', default='1.0'),
105124
help='Image API version, default=1.0 (Env: OS_IMAGE_API_VERSION)')
106125

107-
parser.add_argument('--service-token', metavar='<service-token>',
108-
default=env('SERVICE_TOKEN'),
109-
help=argparse.SUPPRESS)
126+
parser.add_argument('--os-token', metavar='<token>',
127+
default=env('OS_TOKEN'),
128+
help='Defaults to env[OS_TOKEN]')
110129

111-
parser.add_argument('--service-endpoint', metavar='<service-endpoint>',
112-
default=env('SERVICE_ENDPOINT'),
113-
help=argparse.SUPPRESS)
130+
parser.add_argument('--os-url', metavar='<url>',
131+
default=env('OS_URL'),
132+
help='Defaults to env[OS_URL]')
114133

115134
return parser
116135

@@ -130,6 +149,39 @@ def prepare_to_run_command(self, cmd):
130149
print "API: Identity=%s Compute=%s Image=%s" % (self.api_version['identity'], self.api_version['compute'], self.api_version['image'])
131150
print "cmd: %s" % cmd
132151

152+
# do checking of os_username, etc here
153+
if (self.options.os_token and self.options.os_url):
154+
# do token auth
155+
endpoint = self.options.os_url
156+
token = self.options.os_token
157+
else:
158+
if not self.options.os_username:
159+
raise exc.CommandError("You must provide a username via"
160+
" either --os-username or env[OS_USERNAME]")
161+
162+
if not self.options.os_password:
163+
raise exc.CommandError("You must provide a password via"
164+
" either --os-password or env[OS_PASSWORD]")
165+
166+
if not (self.options.os_tenant_id or self.options.os_tenant_name):
167+
raise exc.CommandError("You must provide a tenant_id via"
168+
" either --os-tenant-id or via env[OS_TENANT_ID]")
169+
170+
if not self.options.os_auth_url:
171+
raise exc.CommandError("You must provide an auth url via"
172+
" either --os-auth-url or via env[OS_AUTH_URL]")
173+
kwargs = {
174+
'username': self.options.os_username,
175+
'password': self.options.os_password,
176+
'tenant_id': self.options.os_tenant_id,
177+
'tenant_name': self.options.os_tenant_name,
178+
'auth_url': self.options.os_auth_url
179+
}
180+
token = self._authenticate(**kwargs)
181+
# get service catalog via cmd.api
182+
# get client instance here
183+
print "api: %s" % cmd.api
184+
133185
def clean_up(self, cmd, result, err):
134186
self.log.debug('clean_up %s', cmd.__class__.__name__)
135187
if err:

0 commit comments

Comments
 (0)