Skip to content

Commit 9d224b3

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add tenant commands, work on service"
2 parents bf582a2 + cc0adad commit 9d224b3

3 files changed

Lines changed: 188 additions & 2 deletions

File tree

openstackclient/identity/v2_0/service.py

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,57 @@
2121

2222
import logging
2323

24+
from cliff import lister
25+
from cliff import show
26+
2427
from openstackclient.common import command
2528
from openstackclient.common import utils
2629

2730

28-
class List_Service(command.OpenStackCommand):
31+
def get_service_properties(service, fields, formatters={}):
32+
"""Return a tuple containing the service properties.
33+
34+
:param server: a single Service 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](service))
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(service, field_name, '')
51+
row.append(data)
52+
return tuple(row)
53+
54+
55+
class Create_Service(command.OpenStackCommand):
56+
"Create service command."
57+
58+
api = 'identity'
59+
log = logging.getLogger(__name__)
60+
61+
def get_parser(self, prog_name):
62+
parser = super(Create_Service, self).get_parser(prog_name)
63+
parser.add_argument(
64+
'--long',
65+
action='store_true',
66+
default=False,
67+
help='Additional fields are listed in output')
68+
return parser
69+
70+
def run(self, parsed_args):
71+
self.log.info('v2.Create_Service.run(%s)' % parsed_args)
72+
73+
74+
class List_Service(command.OpenStackCommand, lister.Lister):
2975
"List service command."
3076

3177
api = 'identity'
@@ -40,5 +86,39 @@ def get_parser(self, prog_name):
4086
help='Additional fields are listed in output')
4187
return parser
4288

89+
def get_data(self, parsed_args):
90+
self.log.debug('v2.List_Service.run(%s)' % parsed_args)
91+
if parsed_args.long:
92+
columns = ('ID', 'Name', 'Type', 'Description')
93+
else:
94+
columns = ('ID', 'Name')
95+
data = self.app.client_manager.identity.services.list()
96+
print "data: %s" % data
97+
return (columns,
98+
(get_service_properties(
99+
s, columns,
100+
formatters={},
101+
) for s in data),
102+
)
103+
104+
#def run(self, parsed_args):
105+
# self.log.info('v2.List_Service.run(%s)' % parsed_args)
106+
107+
108+
class Show_Service(command.OpenStackCommand):
109+
"Show service command."
110+
111+
api = 'identity'
112+
log = logging.getLogger(__name__)
113+
114+
def get_parser(self, prog_name):
115+
parser = super(Show_Service, self).get_parser(prog_name)
116+
parser.add_argument(
117+
'--long',
118+
action='store_true',
119+
default=False,
120+
help='Additional fields are listed in output')
121+
return parser
122+
43123
def run(self, parsed_args):
44-
self.log.info('v2.List_Service.run(%s)' % parsed_args)
124+
self.log.info('v2.Show_Service.run(%s)' % parsed_args)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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)

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ def read(fname):
6161
'openstack.cli': [
6262
'list_server=openstackclient.compute.v2.server:List_Server',
6363
'show_server=openstackclient.compute.v2.server:Show_Server',
64+
'create_service=' +
65+
'openstackclient.identity.v2_0.service:Create_Service',
6466
'list_service=openstackclient.identity.v2_0.service:List_Service',
67+
'show_service=openstackclient.identity.v2_0.service:Show_Service',
68+
'list_tenant=openstackclient.identity.v2_0.tenant:List_Tenant',
69+
'show_tenant=openstackclient.identity.v2_0.tenant:Show_Tenant',
6570
]
6671
}
6772
)

0 commit comments

Comments
 (0)