2013-07-03 18:12:58 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2013-01-17 22:11:05 -05: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
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2013-01-31 13:31:41 -06:00
|
|
|
"""Project action implementations"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
2015-10-21 12:01:56 -05:00
|
|
|
from keystoneauth1 import exceptions as ks_exc
|
2016-05-13 14:14:06 -05:00
|
|
|
from osc_lib.cli import parseractions
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
|
|
|
import six
|
2013-01-17 22:11:05 -05:00
|
|
|
|
2016-05-13 13:14:02 -07:00
|
|
|
from openstackclient.i18n import _
|
2014-05-30 10:38:20 -06:00
|
|
|
from openstackclient.identity import common
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class CreateProject(command.ShowOne):
|
2013-09-09 14:55:07 -05:00
|
|
|
"""Create new project"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateProject, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2013-09-09 14:55:07 -05:00
|
|
|
'name',
|
2013-01-17 22:11:05 -05:00
|
|
|
metavar='<project-name>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('New project name'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<domain>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Domain owning the project (name or ID)'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2014-09-22 14:04:01 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--parent',
|
|
|
|
metavar='<project>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Parent of the project (name or ID)'),
|
2014-09-22 14:04:01 +00:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--description',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<description>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Project description'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
enable_group = parser.add_mutually_exclusive_group()
|
|
|
|
enable_group.add_argument(
|
|
|
|
'--enable',
|
|
|
|
action='store_true',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Enable project'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
enable_group.add_argument(
|
|
|
|
'--disable',
|
2013-09-09 14:55:07 -05:00
|
|
|
action='store_true',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Disable project'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2014-03-06 12:50:37 -07:00
|
|
|
parser.add_argument(
|
|
|
|
'--property',
|
|
|
|
metavar='<key=value>',
|
|
|
|
action=parseractions.KeyValueAction,
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Add a property to <name> '
|
|
|
|
'(repeat option to set multiple properties)'),
|
2014-03-06 12:50:37 -07:00
|
|
|
)
|
2014-11-14 01:59:14 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--or-show',
|
|
|
|
action='store_true',
|
|
|
|
help=_('Return existing project'),
|
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
|
2015-04-19 02:41:04 -04:00
|
|
|
domain = None
|
2013-01-17 22:11:05 -05:00
|
|
|
if parsed_args.domain:
|
2014-10-07 16:30:56 -07:00
|
|
|
domain = common.find_domain(identity_client,
|
|
|
|
parsed_args.domain).id
|
2013-01-31 13:31:41 -06:00
|
|
|
|
2014-09-22 14:04:01 +00:00
|
|
|
parent = None
|
|
|
|
if parsed_args.parent:
|
|
|
|
parent = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
parsed_args.parent,
|
|
|
|
).id
|
|
|
|
|
2013-09-09 14:55:07 -05:00
|
|
|
enabled = True
|
|
|
|
if parsed_args.disable:
|
|
|
|
enabled = False
|
2014-03-06 12:50:37 -07:00
|
|
|
kwargs = {}
|
|
|
|
if parsed_args.property:
|
|
|
|
kwargs = parsed_args.property.copy()
|
2013-09-09 14:55:07 -05:00
|
|
|
|
2014-11-14 01:59:14 -05:00
|
|
|
try:
|
|
|
|
project = identity_client.projects.create(
|
|
|
|
name=parsed_args.name,
|
|
|
|
domain=domain,
|
2014-09-22 14:04:01 +00:00
|
|
|
parent=parent,
|
2014-11-14 01:59:14 -05:00
|
|
|
description=parsed_args.description,
|
|
|
|
enabled=enabled,
|
|
|
|
**kwargs
|
|
|
|
)
|
2015-10-21 12:01:56 -05:00
|
|
|
except ks_exc.Conflict as e:
|
2014-11-14 01:59:14 -05:00
|
|
|
if parsed_args.or_show:
|
|
|
|
project = utils.find_resource(identity_client.projects,
|
|
|
|
parsed_args.name,
|
|
|
|
domain_id=domain)
|
2016-06-05 10:58:48 +08:00
|
|
|
self.log.info(_('Returning existing project %s'), project.name)
|
2014-11-14 01:59:14 -05:00
|
|
|
else:
|
|
|
|
raise e
|
2013-01-17 22:11:05 -05:00
|
|
|
|
2014-10-03 00:07:45 -04:00
|
|
|
project._info.pop('links')
|
|
|
|
return zip(*sorted(six.iteritems(project._info)))
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteProject(command.Command):
|
2014-12-10 14:09:01 +08:00
|
|
|
"""Delete project(s)"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteProject, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2014-12-10 14:09:01 +08:00
|
|
|
'projects',
|
2013-01-17 22:11:05 -05:00
|
|
|
metavar='<project>',
|
2014-12-10 14:09:01 +08:00
|
|
|
nargs="+",
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Project(s) to delete (name or ID)'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2014-11-19 15:31:25 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
|
|
|
metavar='<domain>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Domain owning <project> (name or ID)'),
|
2014-11-19 15:31:25 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
2013-09-09 14:55:07 -05:00
|
|
|
|
2014-12-10 14:09:01 +08:00
|
|
|
domain = None
|
2014-11-19 15:31:25 -05:00
|
|
|
if parsed_args.domain:
|
|
|
|
domain = common.find_domain(identity_client, parsed_args.domain)
|
2014-12-10 14:09:01 +08:00
|
|
|
for project in parsed_args.projects:
|
|
|
|
if domain is not None:
|
|
|
|
project_obj = utils.find_resource(identity_client.projects,
|
|
|
|
project,
|
|
|
|
domain_id=domain.id)
|
|
|
|
else:
|
|
|
|
project_obj = utils.find_resource(identity_client.projects,
|
|
|
|
project)
|
|
|
|
identity_client.projects.delete(project_obj.id)
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListProject(command.Lister):
|
2013-09-09 14:55:07 -05:00
|
|
|
"""List projects"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListProject, self).get_parser(prog_name)
|
2014-11-18 15:11:32 -06:00
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
|
|
|
metavar='<domain>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Filter projects by <domain> (name or ID)'),
|
2014-11-18 15:11:32 -06:00
|
|
|
)
|
2014-11-20 18:42:00 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Filter projects by <user> (name or ID)'),
|
2014-11-20 18:42:00 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--long',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('List additional fields in output'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
2014-03-07 18:25:41 +00:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-17 22:11:05 -05:00
|
|
|
if parsed_args.long:
|
|
|
|
columns = ('ID', 'Name', 'Domain ID', 'Description', 'Enabled')
|
|
|
|
else:
|
|
|
|
columns = ('ID', 'Name')
|
2014-03-07 18:25:41 +00:00
|
|
|
kwargs = {}
|
2014-11-20 18:42:00 -05:00
|
|
|
|
|
|
|
domain_id = None
|
2014-03-07 18:25:41 +00:00
|
|
|
if parsed_args.domain:
|
2014-11-20 18:42:00 -05:00
|
|
|
domain_id = common.find_domain(identity_client,
|
|
|
|
parsed_args.domain).id
|
|
|
|
kwargs['domain'] = domain_id
|
|
|
|
|
|
|
|
if parsed_args.user:
|
|
|
|
if parsed_args.domain:
|
|
|
|
user_id = utils.find_resource(identity_client.users,
|
|
|
|
parsed_args.user,
|
|
|
|
domain_id=domain_id).id
|
|
|
|
else:
|
|
|
|
user_id = utils.find_resource(identity_client.users,
|
|
|
|
parsed_args.user).id
|
|
|
|
|
|
|
|
kwargs['user'] = user_id
|
|
|
|
|
2014-03-07 18:25:41 +00:00
|
|
|
data = identity_client.projects.list(**kwargs)
|
2013-01-17 22:11:05 -05:00
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
2013-01-31 13:31:41 -06:00
|
|
|
) for s in data))
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SetProject(command.Command):
|
2013-09-09 14:55:07 -05:00
|
|
|
"""Set project properties"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(SetProject, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'project',
|
|
|
|
metavar='<project>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Project to modify (name or ID)'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--name',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<name>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Set project name'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2015-05-29 11:50:30 -04:00
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
|
|
|
metavar='<domain>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Domain owning <project> (name or ID)'),
|
2015-05-29 11:50:30 -04:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--description',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<description>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Set project description'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
enable_group = parser.add_mutually_exclusive_group()
|
|
|
|
enable_group.add_argument(
|
|
|
|
'--enable',
|
|
|
|
action='store_true',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Enable project'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
enable_group.add_argument(
|
|
|
|
'--disable',
|
2013-09-09 14:55:07 -05:00
|
|
|
action='store_true',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Disable project'),
|
2013-09-09 14:55:07 -05:00
|
|
|
)
|
2014-03-06 12:50:37 -07:00
|
|
|
parser.add_argument(
|
|
|
|
'--property',
|
|
|
|
metavar='<key=value>',
|
|
|
|
action=parseractions.KeyValueAction,
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Set a property on <project> '
|
|
|
|
'(repeat option to set multiple properties)'),
|
2014-03-06 12:50:37 -07:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
2013-09-09 14:55:07 -05:00
|
|
|
|
|
|
|
if (not parsed_args.name
|
2015-05-29 11:50:30 -04:00
|
|
|
and not parsed_args.domain
|
2013-09-09 14:55:07 -05:00
|
|
|
and not parsed_args.description
|
|
|
|
and not parsed_args.enable
|
2014-03-06 12:50:37 -07:00
|
|
|
and not parsed_args.property
|
2013-09-09 14:55:07 -05:00
|
|
|
and not parsed_args.disable):
|
|
|
|
return
|
2016-02-28 09:43:44 +00:00
|
|
|
project = common.find_project(identity_client,
|
|
|
|
parsed_args.project,
|
2015-12-29 17:23:42 +05:30
|
|
|
parsed_args.domain)
|
2013-09-09 14:55:07 -05:00
|
|
|
|
2014-10-03 00:07:45 -04:00
|
|
|
kwargs = {}
|
2013-01-17 22:11:05 -05:00
|
|
|
if parsed_args.name:
|
|
|
|
kwargs['name'] = parsed_args.name
|
|
|
|
if parsed_args.description:
|
|
|
|
kwargs['description'] = parsed_args.description
|
2013-09-09 14:55:07 -05:00
|
|
|
if parsed_args.enable:
|
|
|
|
kwargs['enabled'] = True
|
|
|
|
if parsed_args.disable:
|
|
|
|
kwargs['enabled'] = False
|
2014-03-06 12:50:37 -07:00
|
|
|
if parsed_args.property:
|
|
|
|
kwargs.update(parsed_args.property)
|
2013-09-09 14:55:07 -05:00
|
|
|
|
|
|
|
identity_client.projects.update(project.id, **kwargs)
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ShowProject(command.ShowOne):
|
2015-01-09 19:13:03 -05:00
|
|
|
"""Display project details"""
|
2013-01-17 22:11:05 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowProject, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'project',
|
|
|
|
metavar='<project>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Project to display (name or ID)'),
|
2014-10-07 02:15:15 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--domain',
|
|
|
|
metavar='<domain>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Domain owning <project> (name or ID)'),
|
2014-10-07 02:15:15 -04:00
|
|
|
)
|
2015-03-20 17:51:02 -03:00
|
|
|
parser.add_argument(
|
|
|
|
'--parents',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Show the project\'s parents as a list'),
|
2015-03-20 17:51:02 -03:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--children',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Show project\'s subtree (children) as a list'),
|
2015-03-20 17:51:02 -03:00
|
|
|
)
|
2013-01-17 22:11:05 -05:00
|
|
|
return parser
|
|
|
|
|
|
|
|
def take_action(self, parsed_args):
|
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
|
2014-10-07 02:15:15 -04:00
|
|
|
if parsed_args.domain:
|
|
|
|
domain = common.find_domain(identity_client, parsed_args.domain)
|
2015-03-20 17:51:02 -03:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
parsed_args.project,
|
|
|
|
domain_id=domain.id,
|
|
|
|
parents_as_list=parsed_args.parents,
|
|
|
|
subtree_as_list=parsed_args.children)
|
2014-10-07 02:15:15 -04:00
|
|
|
else:
|
2015-03-20 17:51:02 -03:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
parsed_args.project,
|
|
|
|
parents_as_list=parsed_args.parents,
|
|
|
|
subtree_as_list=parsed_args.children)
|
|
|
|
|
|
|
|
if project._info.get('parents'):
|
|
|
|
project._info['parents'] = [str(p['project']['id'])
|
|
|
|
for p in project._info['parents']]
|
|
|
|
if project._info.get('subtree'):
|
|
|
|
project._info['subtree'] = [str(p['project']['id'])
|
|
|
|
for p in project._info['subtree']]
|
2014-10-07 02:15:15 -04:00
|
|
|
|
2014-10-03 00:07:45 -04:00
|
|
|
project._info.pop('links')
|
2014-10-07 02:15:15 -04:00
|
|
|
return zip(*sorted(six.iteritems(project._info)))
|