|
| 1 | +# Copyright 2012 OpenStack LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | +# |
| 16 | +# vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 17 | + |
| 18 | +""" |
| 19 | +Tenant action implementations |
| 20 | +""" |
| 21 | + |
| 22 | +import logging |
| 23 | + |
| 24 | +from cliff import lister |
| 25 | +from cliff import show |
| 26 | + |
| 27 | +from openstackclient.common import command |
| 28 | +from openstackclient.common import utils |
| 29 | + |
| 30 | + |
| 31 | +def get_tenant_properties(tenant, fields, formatters={}): |
| 32 | + """Return a tuple containing the server properties. |
| 33 | +
|
| 34 | + :param server: a single Server resource |
| 35 | + :param fields: tuple of strings with the desired field names |
| 36 | + :param formatters: dictionary mapping field names to callables |
| 37 | + to format the values |
| 38 | + """ |
| 39 | + row = [] |
| 40 | + mixed_case_fields = [] |
| 41 | + |
| 42 | + for field in fields: |
| 43 | + if field in formatters: |
| 44 | + row.append(formatters[field](tenant)) |
| 45 | + else: |
| 46 | + if field in mixed_case_fields: |
| 47 | + field_name = field.replace(' ', '_') |
| 48 | + else: |
| 49 | + field_name = field.lower().replace(' ', '_') |
| 50 | + data = getattr(tenant, field_name, '') |
| 51 | + row.append(data) |
| 52 | + return tuple(row) |
| 53 | + |
| 54 | + |
| 55 | +class List_Tenant(command.OpenStackCommand, lister.Lister): |
| 56 | + "List tenant command." |
| 57 | + |
| 58 | + api = 'identity' |
| 59 | + log = logging.getLogger(__name__) |
| 60 | + |
| 61 | + def get_data(self, parsed_args): |
| 62 | + self.log.debug('v2.List_Service.run(%s)' % parsed_args) |
| 63 | + columns = ('ID', 'Name', 'Enabled') |
| 64 | + data = self.app.client_manager.identity.tenants.list() |
| 65 | + return (columns, |
| 66 | + (get_tenant_properties( |
| 67 | + s, columns, |
| 68 | + formatters={}, |
| 69 | + ) for s in data), |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +class Show_Tenant(command.OpenStackCommand, show.ShowOne): |
| 74 | + "Show server command." |
| 75 | + |
| 76 | + api = 'identity' |
| 77 | + log = logging.getLogger(__name__) |
| 78 | + |
| 79 | + def get_parser(self, prog_name): |
| 80 | + parser = super(Show_Tenant, self).get_parser(prog_name) |
| 81 | + parser.add_argument( |
| 82 | + 'tenant', |
| 83 | + metavar='<tenant>', |
| 84 | + help='Name or ID of tenant to display') |
| 85 | + return parser |
| 86 | + |
| 87 | + def get_data(self, parsed_args): |
| 88 | + self.log.debug('v2.Show_Tenant.run(%s)' % parsed_args) |
| 89 | + identity_client = self.app.client_manager.identity |
| 90 | + tenant = utils.find_resource( |
| 91 | + identity_client.tenants, parsed_args.tenant) |
| 92 | + |
| 93 | + info = {} |
| 94 | + info.update(tenant._info) |
| 95 | + |
| 96 | + # Remove a couple of values that are long and not too useful |
| 97 | + #info.pop('links', None) |
| 98 | + |
| 99 | + columns = sorted(info.keys()) |
| 100 | + values = [info[c] for c in columns] |
| 101 | + return (columns, values) |
0 commit comments