2013-07-03 18:12:58 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
|
|
|
|
2013-07-29 11:29:16 -05:00
|
|
|
"""Identity v2 Role action implementations"""
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
import logging
|
2013-07-03 18:12:58 -05:00
|
|
|
import six
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
from cliff import command
|
2012-05-16 16:22:03 -05:00
|
|
|
from cliff import lister
|
|
|
|
from cliff import show
|
2014-12-17 18:17:56 +10:00
|
|
|
from keystoneclient import exceptions as ksc_exc
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-08-27 16:57:30 -05:00
|
|
|
from openstackclient.common import exceptions
|
2012-05-16 16:22:03 -05:00
|
|
|
from openstackclient.common import utils
|
2014-10-03 22:54:42 -04:00
|
|
|
from openstackclient.i18n import _ # noqa
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class AddRole(show.ShowOne):
|
2013-07-29 11:29:16 -05:00
|
|
|
"""Add role to project:user"""
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.AddRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(AddRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Role to add to <project>:<user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2012-05-16 16:22:03 -05:00
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
role = identity_client.roles.add_user_role(
|
2013-08-27 16:57:30 -05:00
|
|
|
user.id,
|
|
|
|
role.id,
|
|
|
|
project.id,
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class CreateRole(show.ShowOne):
|
2012-05-16 16:22:03 -05:00
|
|
|
"""Create new role"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.CreateRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role_name',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<name>',
|
|
|
|
help=_('New role name'),
|
|
|
|
)
|
2014-11-12 10:48:47 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--or-show',
|
|
|
|
action='store_true',
|
|
|
|
help=_('Return existing role'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2014-11-12 10:48:47 -05:00
|
|
|
try:
|
|
|
|
role = identity_client.roles.create(parsed_args.role_name)
|
|
|
|
except ksc_exc.Conflict as e:
|
|
|
|
if parsed_args.or_show:
|
|
|
|
role = utils.find_resource(
|
|
|
|
identity_client.roles,
|
|
|
|
parsed_args.role_name,
|
|
|
|
)
|
|
|
|
self.log.info('Returning existing role %s', role.name)
|
|
|
|
else:
|
|
|
|
raise e
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class DeleteRole(command.Command):
|
2014-12-10 14:09:01 +08:00
|
|
|
"""Delete role(s)"""
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.DeleteRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2014-12-10 14:09:01 +08:00
|
|
|
'roles',
|
2012-05-16 16:22:03 -05:00
|
|
|
metavar='<role>',
|
2014-12-10 14:09:01 +08:00
|
|
|
nargs="+",
|
|
|
|
help=_('Role(s) to delete (name or ID)'),
|
2014-11-18 15:11:32 -06:00
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-09-09 14:55:07 -05:00
|
|
|
|
2014-12-10 14:09:01 +08:00
|
|
|
for role in parsed_args.roles:
|
|
|
|
role_obj = utils.find_resource(
|
|
|
|
identity_client.roles,
|
|
|
|
role,
|
|
|
|
)
|
|
|
|
identity_client.roles.delete(role_obj.id)
|
2012-05-16 16:22:03 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class ListRole(lister.Lister):
|
2012-05-16 16:22:03 -05:00
|
|
|
"""List roles"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.ListRole')
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
columns = ('ID', 'Name')
|
|
|
|
data = self.app.client_manager.identity.roles.list()
|
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
2013-01-31 13:31:41 -06:00
|
|
|
) for s in data))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class ListUserRole(lister.Lister):
|
2012-05-16 16:22:03 -05:00
|
|
|
"""List user-role assignments"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.ListUserRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListUserRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'user',
|
|
|
|
metavar='<user>',
|
|
|
|
nargs='?',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('User to list (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Filter users by <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-08-27 16:57:30 -05:00
|
|
|
auth_ref = self.app.client_manager.auth_ref
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-08-27 16:57:30 -05:00
|
|
|
# Project and user are required, if not included in command args
|
|
|
|
# default to the values used for authentication. For token-flow
|
|
|
|
# authentication they must be included on the command line.
|
2013-07-29 11:29:16 -05:00
|
|
|
if not parsed_args.project:
|
2013-08-27 16:57:30 -05:00
|
|
|
if self.app.client_manager.auth_ref:
|
|
|
|
parsed_args.project = auth_ref.project_id
|
|
|
|
else:
|
2014-10-03 22:54:42 -04:00
|
|
|
msg = _("Project must be specified")
|
2013-08-27 16:57:30 -05:00
|
|
|
raise exceptions.CommandError(msg)
|
2012-05-16 16:22:03 -05:00
|
|
|
if not parsed_args.user:
|
2013-08-27 16:57:30 -05:00
|
|
|
if self.app.client_manager.auth_ref:
|
|
|
|
parsed_args.user = auth_ref.user_id
|
|
|
|
else:
|
2014-10-03 22:54:42 -04:00
|
|
|
msg = _("User must be specified")
|
2013-08-27 16:57:30 -05:00
|
|
|
raise exceptions.CommandError(msg)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-07-29 11:29:16 -05:00
|
|
|
data = identity_client.roles.roles_for_user(user.id, project.id)
|
|
|
|
|
|
|
|
columns = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Project',
|
|
|
|
'User',
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
# Add the names to the output even though they will be constant
|
|
|
|
for role in data:
|
2013-07-29 11:29:16 -05:00
|
|
|
role.user = user.name
|
|
|
|
role.project = project.name
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
2013-01-31 13:31:41 -06:00
|
|
|
) for s in data))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class RemoveRole(command.Command):
|
2013-07-29 11:29:16 -05:00
|
|
|
"""Remove role from project:user"""
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.RemoveRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(RemoveRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Role to remove from <project>:<user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2012-05-16 16:22:03 -05:00
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client.roles.remove_user_role(
|
|
|
|
user.id,
|
|
|
|
role.id,
|
2013-07-29 11:29:16 -05:00
|
|
|
project.id)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class ShowRole(show.ShowOne):
|
2012-05-16 16:22:03 -05:00
|
|
|
"""Show single role"""
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__ + '.ShowRole')
|
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Role to show (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2014-05-20 13:11:19 +02:00
|
|
|
self.log.debug('take_action(%s)', parsed_args)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|