Merge "Remove tenant round 2 - Identity API"

This commit is contained in:
Jenkins 2013-07-31 22:29:03 +00:00 committed by Gerrit Code Review
commit 0f63f46a07
4 changed files with 205 additions and 116 deletions

View File

@ -13,11 +13,10 @@
# under the License. # under the License.
# #
"""Tenant action implementations""" """Identity v2 Project action implementations"""
import logging import logging
import six import six
import sys
from cliff import command from cliff import command
from cliff import lister from cliff import lister
@ -26,82 +25,90 @@ from cliff import show
from openstackclient.common import utils from openstackclient.common import utils
class CreateTenant(show.ShowOne): class CreateProject(show.ShowOne):
"""Create tenant command""" """Create new project"""
log = logging.getLogger(__name__ + '.CreateTenant') log = logging.getLogger(__name__ + '.CreateProject')
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(CreateTenant, self).get_parser(prog_name) parser = super(CreateProject, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'tenant_name', 'project_name',
metavar='<tenant-name>', metavar='<project-name>',
help='New tenant name') help='New project name',
)
parser.add_argument( parser.add_argument(
'--description', '--description',
metavar='<tenant-description>', metavar='<project-description>',
help='New tenant description') help='New project description',
)
enable_group = parser.add_mutually_exclusive_group() enable_group = parser.add_mutually_exclusive_group()
enable_group.add_argument( enable_group.add_argument(
'--enable', '--enable',
dest='enabled', dest='enabled',
action='store_true', action='store_true',
default=True, default=True,
help='Enable tenant') help='Enable project',
)
enable_group.add_argument( enable_group.add_argument(
'--disable', '--disable',
dest='enabled', dest='enabled',
action='store_false', action='store_false',
help='Disable tenant') help='Disable project',
)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
tenant = identity_client.tenants.create( project = identity_client.tenants.create(
parsed_args.tenant_name, parsed_args.project_name,
description=parsed_args.description, description=parsed_args.description,
enabled=parsed_args.enabled) enabled=parsed_args.enabled)
info = {} info = {}
info.update(tenant._info) info.update(project._info)
return zip(*sorted(six.iteritems(info))) return zip(*sorted(six.iteritems(info)))
class DeleteTenant(command.Command): class DeleteProject(command.Command):
"""Delete tenant command""" """Delete project"""
log = logging.getLogger(__name__ + '.DeleteTenant') log = logging.getLogger(__name__ + '.DeleteProject')
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(DeleteTenant, self).get_parser(prog_name) parser = super(DeleteProject, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'tenant', 'project',
metavar='<tenant>', metavar='<project>',
help='Name or ID of tenant to delete') help='Project to delete (name or ID)',
)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
identity_client.tenants.delete(tenant.id) parsed_args.project,
)
identity_client.tenants.delete(project.id)
return return
class ListTenant(lister.Lister): class ListProject(lister.Lister):
"""List tenant command""" """List projects"""
log = logging.getLogger(__name__ + '.ListTenant') log = logging.getLogger(__name__ + '.ListProject')
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ListTenant, self).get_parser(prog_name) parser = super(ListProject, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'--long', '--long',
action='store_true', action='store_true',
default=False, default=False,
help='Additional fields are listed in output') help='List additional fields in output',
)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
@ -118,44 +125,51 @@ class ListTenant(lister.Lister):
) for s in data)) ) for s in data))
class SetTenant(command.Command): class SetProject(command.Command):
"""Set tenant command""" """Set project properties"""
log = logging.getLogger(__name__ + '.SetTenant') log = logging.getLogger(__name__ + '.SetProject')
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(SetTenant, self).get_parser(prog_name) parser = super(SetProject, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'tenant', 'project',
metavar='<tenant>', metavar='<project>',
help='Name or ID of tenant to change') help='Project to change (name or ID)',
)
parser.add_argument( parser.add_argument(
'--name', '--name',
metavar='<new-tenant-name>', metavar='<new-project-name>',
help='New tenant name') help='New project name',
)
parser.add_argument( parser.add_argument(
'--description', '--description',
metavar='<tenant-description>', metavar='<project-description>',
help='New tenant description') help='New project description',
)
enable_group = parser.add_mutually_exclusive_group() enable_group = parser.add_mutually_exclusive_group()
enable_group.add_argument( enable_group.add_argument(
'--enable', '--enable',
dest='enabled', dest='enabled',
action='store_true', action='store_true',
default=True, default=True,
help='Enable tenant (default)') help='Enable project (default)',
)
enable_group.add_argument( enable_group.add_argument(
'--disable', '--disable',
dest='enabled', dest='enabled',
action='store_false', action='store_false',
help='Disable tenant') help='Disable project',
)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
parsed_args.project,
)
kwargs = {} kwargs = {}
if parsed_args.name: if parsed_args.name:
kwargs['name'] = parsed_args.name kwargs['name'] = parsed_args.name
@ -164,32 +178,31 @@ class SetTenant(command.Command):
if 'enabled' in parsed_args: if 'enabled' in parsed_args:
kwargs['enabled'] = parsed_args.enabled kwargs['enabled'] = parsed_args.enabled
if kwargs == {}: project.update(**kwargs)
sys.stdout.write("Tenant not updated, no arguments present")
return 0
tenant.update(**kwargs)
return return
class ShowTenant(show.ShowOne): class ShowProject(show.ShowOne):
"""Show tenant command""" """Show project details"""
log = logging.getLogger(__name__ + '.ShowTenant') log = logging.getLogger(__name__ + '.ShowProject')
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ShowTenant, self).get_parser(prog_name) parser = super(ShowProject, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'tenant', 'project',
metavar='<tenant>', metavar='<project>',
help='Name or ID of tenant to display') help='Project to display (name or ID)')
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
parsed_args.project,
)
info = {} info = {}
info.update(tenant._info) info.update(project._info)
return zip(*sorted(six.iteritems(info))) return zip(*sorted(six.iteritems(info)))

View File

@ -13,7 +13,7 @@
# under the License. # under the License.
# #
"""Role action implementations""" """Identity v2 Role action implementations"""
import logging import logging
import six import six
@ -26,7 +26,7 @@ from openstackclient.common import utils
class AddRole(show.ShowOne): class AddRole(show.ShowOne):
"""Add role to tenant:user""" """Add role to project:user"""
log = logging.getLogger(__name__ + '.AddRole') log = logging.getLogger(__name__ + '.AddRole')
@ -37,10 +37,11 @@ class AddRole(show.ShowOne):
metavar='<role>', metavar='<role>',
help='Role name or ID to add to user') help='Role name or ID to add to user')
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
required=True, required=True,
help='Name or ID of tenant to include') help='Include project (name or ID)',
)
parser.add_argument( parser.add_argument(
'--user', '--user',
metavar='<user>', metavar='<user>',
@ -52,13 +53,15 @@ class AddRole(show.ShowOne):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
role = utils.find_resource(identity_client.roles, parsed_args.role) role = utils.find_resource(identity_client.roles, parsed_args.role)
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
parsed_args.project,
)
user = utils.find_resource(identity_client.users, parsed_args.user) user = utils.find_resource(identity_client.users, parsed_args.user)
role = identity_client.roles.add_user_role( role = identity_client.roles.add_user_role(
user, user,
role, role,
tenant) project)
info = {} info = {}
info.update(role._info) info.update(role._info)
@ -138,34 +141,43 @@ class ListUserRole(lister.Lister):
nargs='?', nargs='?',
help='Name or ID of user to include') help='Name or ID of user to include')
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
help='Name or ID of tenant to include') help='Include project (name or ID)',
)
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
columns = ('ID', 'Name', 'Tenant ID', 'User ID')
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
# user-only roles are not supported in KSL so we are # user-only roles are not supported in KSL so we are
# required to have a user and tenant; default to the # required to have a user and project; default to the
# values used for authentication if not specified # values used for authentication if not specified
if not parsed_args.tenant: if not parsed_args.project:
parsed_args.tenant = identity_client.auth_tenant_id parsed_args.project = identity_client.auth_tenant_id
if not parsed_args.user: if not parsed_args.user:
parsed_args.user = identity_client.auth_user_id parsed_args.user = identity_client.auth_user_id
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
parsed_args.project,
)
user = utils.find_resource(identity_client.users, parsed_args.user) user = utils.find_resource(identity_client.users, parsed_args.user)
data = identity_client.roles.roles_for_user(user.id, tenant.id) data = identity_client.roles.roles_for_user(user.id, project.id)
columns = (
'ID',
'Name',
'Project',
'User',
)
# Add the names to the output even though they will be constant # Add the names to the output even though they will be constant
for role in data: for role in data:
role.user_id = user.name role.user = user.name
role.tenant_id = tenant.name role.project = project.name
return (columns, return (columns,
(utils.get_item_properties( (utils.get_item_properties(
@ -175,7 +187,7 @@ class ListUserRole(lister.Lister):
class RemoveRole(command.Command): class RemoveRole(command.Command):
"""Remove role from tenant:user""" """Remove role from project:user"""
log = logging.getLogger(__name__ + '.RemoveRole') log = logging.getLogger(__name__ + '.RemoveRole')
@ -186,10 +198,11 @@ class RemoveRole(command.Command):
metavar='<role>', metavar='<role>',
help='Role name or ID to remove from user') help='Role name or ID to remove from user')
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
required=True, required=True,
help='Name or ID of tenant') help='Project to include (name or ID)',
)
parser.add_argument( parser.add_argument(
'--user', '--user',
metavar='<user>', metavar='<user>',
@ -201,13 +214,15 @@ class RemoveRole(command.Command):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
role = utils.find_resource(identity_client.roles, parsed_args.role) role = utils.find_resource(identity_client.roles, parsed_args.role)
tenant = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant) identity_client.tenants,
parsed_args.project,
)
user = utils.find_resource(identity_client.users, parsed_args.user) user = utils.find_resource(identity_client.users, parsed_args.user)
identity_client.roles.remove_user_role( identity_client.roles.remove_user_role(
user.id, user.id,
role.id, role.id,
tenant.id) project.id)
class ShowRole(show.ShowOne): class ShowRole(show.ShowOne):

View File

@ -46,9 +46,10 @@ class CreateUser(show.ShowOne):
metavar='<user-email>', metavar='<user-email>',
help='New user email address') help='New user email address')
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
help='New default tenant name or ID') help='Set default project (name or ID)',
)
enable_group = parser.add_mutually_exclusive_group() enable_group = parser.add_mutually_exclusive_group()
enable_group.add_argument( enable_group.add_argument(
'--enable', '--enable',
@ -66,17 +67,23 @@ class CreateUser(show.ShowOne):
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
if parsed_args.tenant: if parsed_args.project:
tenant_id = utils.find_resource(identity_client.tenants, project_id = utils.find_resource(
parsed_args.tenant).id identity_client.tenants,
parsed_args.project,
).id
else: else:
tenant_id = None project_id = None
user = identity_client.users.create( user = identity_client.users.create(
parsed_args.name, parsed_args.name,
parsed_args.password, parsed_args.password,
parsed_args.email, parsed_args.email,
tenant_id=tenant_id, tenant_id=project_id,
enabled=parsed_args.enabled) enabled=parsed_args.enabled,
)
user._info.update(
{'project_id': user._info.pop('tenantId')}
)
info = {} info = {}
info.update(user._info) info.update(user._info)
@ -112,9 +119,10 @@ class ListUser(lister.Lister):
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ListUser, self).get_parser(prog_name) parser = super(ListUser, self).get_parser(prog_name)
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
help='Name or ID of tenant to filter users') help='Filter users by project (name or ID)',
)
parser.add_argument( parser.add_argument(
'--long', '--long',
action='store_true', action='store_true',
@ -124,15 +132,57 @@ class ListUser(lister.Lister):
def take_action(self, parsed_args): def take_action(self, parsed_args):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
def _format_project(project):
if not project:
return ""
if project in project_cache.keys():
return project_cache[project].name
else:
return project
if parsed_args.long: if parsed_args.long:
columns = ('ID', 'Name', 'Tenant Id', 'Email', 'Enabled') columns = (
'ID',
'Name',
'tenantId',
'Email',
'Enabled',
)
column_headers = (
'ID',
'Name',
'Project',
'Email',
'Enabled',
)
# Cache the project list
project_cache = {}
try:
for p in self.app.client_manager.identity.tenants.list():
project_cache[p.id] = p
except Exception:
# Just forget it if there's any trouble
pass
else: else:
columns = ('ID', 'Name') columns = column_headers = ('ID', 'Name')
data = self.app.client_manager.identity.users.list() data = self.app.client_manager.identity.users.list()
return (columns,
if parsed_args.long:
# FIXME(dtroyer): Sometimes user objects have 'tenant_id' instead
# of 'tenantId'. Why? Dunno yet, but until that
# is fixed we need to handle it; auth_token.py
# only looks for 'tenantId'.
for d in data:
if 'tenant_id' in d._info:
d._info['tenantId'] = d._info.pop('tenant_id')
d._add_details(d._info)
return (column_headers,
(utils.get_item_properties( (utils.get_item_properties(
s, columns, s, columns,
formatters={}, mixed_case_fields=('tenantId',),
formatters={'tenantId': _format_project},
) for s in data)) ) for s in data))
@ -160,9 +210,10 @@ class SetUser(command.Command):
metavar='<user-email>', metavar='<user-email>',
help='New user email address') help='New user email address')
parser.add_argument( parser.add_argument(
'--tenant', '--project',
metavar='<tenant>', metavar='<project>',
help='New default tenant name or ID') help='New default project (name or ID)',
)
enable_group = parser.add_mutually_exclusive_group() enable_group = parser.add_mutually_exclusive_group()
enable_group.add_argument( enable_group.add_argument(
'--enable', '--enable',
@ -186,10 +237,12 @@ class SetUser(command.Command):
kwargs['name'] = parsed_args.name kwargs['name'] = parsed_args.name
if parsed_args.email: if parsed_args.email:
kwargs['email'] = parsed_args.email kwargs['email'] = parsed_args.email
if parsed_args.tenant: if parsed_args.project:
tenant_id = utils.find_resource(identity_client.tenants, project = utils.find_resource(
parsed_args.tenant).id identity_client.tenants,
kwargs['tenantId'] = tenant_id parsed_args.project,
)
kwargs['tenantId'] = project.id
if 'enabled' in parsed_args: if 'enabled' in parsed_args:
kwargs['enabled'] = parsed_args.enabled kwargs['enabled'] = parsed_args.enabled
@ -217,6 +270,14 @@ class ShowUser(show.ShowOne):
self.log.debug('take_action(%s)' % parsed_args) self.log.debug('take_action(%s)' % parsed_args)
identity_client = self.app.client_manager.identity identity_client = self.app.client_manager.identity
user = utils.find_resource(identity_client.users, parsed_args.user) user = utils.find_resource(identity_client.users, parsed_args.user)
if 'tenantId' in user._info:
user._info.update(
{'project_id': user._info.pop('tenantId')}
)
if 'tenant_id' in user._info:
user._info.update(
{'project_id': user._info.pop('tenant_id')}
)
info = {} info = {}
info.update(user._info) info.update(user._info)

View File

@ -47,6 +47,12 @@ openstack.identity.v2_0 =
endpoint_list = openstackclient.identity.v2_0.endpoint:ListEndpoint endpoint_list = openstackclient.identity.v2_0.endpoint:ListEndpoint
endpoint_show = openstackclient.identity.v2_0.endpoint:ShowEndpoint endpoint_show = openstackclient.identity.v2_0.endpoint:ShowEndpoint
project_create = openstackclient.identity.v2_0.project:CreateProject
project_delete = openstackclient.identity.v2_0.project:DeleteProject
project_list = openstackclient.identity.v2_0.project:ListProject
project_set = openstackclient.identity.v2_0.project:SetProject
project_show = openstackclient.identity.v2_0.project:ShowProject
role_add = openstackclient.identity.v2_0.role:AddRole role_add = openstackclient.identity.v2_0.role:AddRole
role_create = openstackclient.identity.v2_0.role:CreateRole role_create = openstackclient.identity.v2_0.role:CreateRole
role_delete = openstackclient.identity.v2_0.role:DeleteRole role_delete = openstackclient.identity.v2_0.role:DeleteRole
@ -59,12 +65,6 @@ openstack.identity.v2_0 =
service_list =openstackclient.identity.v2_0.service:ListService service_list =openstackclient.identity.v2_0.service:ListService
service_show =openstackclient.identity.v2_0.service:ShowService service_show =openstackclient.identity.v2_0.service:ShowService
tenant_create = openstackclient.identity.v2_0.tenant:CreateTenant
tenant_delete = openstackclient.identity.v2_0.tenant:DeleteTenant
tenant_list = openstackclient.identity.v2_0.tenant:ListTenant
tenant_set = openstackclient.identity.v2_0.tenant:SetTenant
tenant_show = openstackclient.identity.v2_0.tenant:ShowTenant
user_role_list = openstackclient.identity.v2_0.role:ListUserRole user_role_list = openstackclient.identity.v2_0.role:ListUserRole
user_create = openstackclient.identity.v2_0.user:CreateUser user_create = openstackclient.identity.v2_0.user:CreateUser