Skip to content

Commit ebfa669

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add token create subcommand for identity v2 api"
2 parents 81d33a5 + 4848d3c commit ebfa669

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2014 eBay Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
"""Identity v2 Token action implementations"""
17+
18+
import logging
19+
import six
20+
21+
from cliff import show
22+
23+
24+
class CreateToken(show.ShowOne):
25+
"""Create token command"""
26+
27+
log = logging.getLogger(__name__ + '.CreateToken')
28+
29+
def get_parser(self, prog_name):
30+
parser = super(CreateToken, self).get_parser(prog_name)
31+
return parser
32+
33+
def take_action(self, parsed_args):
34+
self.log.debug('take_action(%s)' % parsed_args)
35+
identity_client = self.app.client_manager.identity
36+
token = identity_client.service_catalog.get_token()
37+
token['project_id'] = token.pop('tenant_id')
38+
return zip(*sorted(six.iteritems(token)))

openstackclient/tests/identity/v2_0/fakes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,22 @@
7070
'enabled': True,
7171
}
7272

73+
token_expires = '2014-01-01T00:00:00Z'
74+
token_id = 'tttttttt-tttt-tttt-tttt-tttttttttttt'
75+
76+
TOKEN = {
77+
'expires': token_expires,
78+
'id': token_id,
79+
'tenant_id': project_id,
80+
'user_id': user_id,
81+
}
82+
7383

7484
class FakeIdentityv2Client(object):
7585
def __init__(self, **kwargs):
7686
self.roles = mock.Mock()
7787
self.roles.resource_class = fakes.FakeResource(None, {})
88+
self.service_catalog = mock.Mock()
7889
self.services = mock.Mock()
7990
self.services.resource_class = fakes.FakeResource(None, {})
8091
self.tenants = mock.Mock()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2014 eBay Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
from openstackclient.identity.v2_0 import token
17+
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
18+
19+
20+
class TestToken(identity_fakes.TestIdentityv2):
21+
22+
def setUp(self):
23+
super(TestToken, self).setUp()
24+
25+
# Get a shortcut to the Service Catalog Mock
26+
self.sc_mock = self.app.client_manager.identity.service_catalog
27+
self.sc_mock.reset_mock()
28+
29+
30+
class TestTokenCreate(TestToken):
31+
32+
def setUp(self):
33+
super(TestTokenCreate, self).setUp()
34+
35+
self.sc_mock.get_token.return_value = identity_fakes.TOKEN
36+
self.cmd = token.CreateToken(self.app, None)
37+
38+
def test_token_create(self):
39+
arglist = []
40+
verifylist = []
41+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
42+
43+
# DisplayCommandBase.take_action() returns two tuples
44+
columns, data = self.cmd.take_action(parsed_args)
45+
46+
self.sc_mock.get_token.assert_called_with()
47+
48+
collist = ('expires', 'id', 'project_id', 'user_id')
49+
self.assertEqual(columns, collist)
50+
datalist = (
51+
identity_fakes.token_expires,
52+
identity_fakes.token_id,
53+
identity_fakes.project_id,
54+
identity_fakes.user_id,
55+
)
56+
self.assertEqual(data, datalist)

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ openstack.identity.v2_0 =
151151
service_list =openstackclient.identity.v2_0.service:ListService
152152
service_show =openstackclient.identity.v2_0.service:ShowService
153153

154+
token_create =openstackclient.identity.v2_0.token:CreateToken
155+
154156
user_role_list = openstackclient.identity.v2_0.role:ListUserRole
155157

156158
user_create = openstackclient.identity.v2_0.user:CreateUser

0 commit comments

Comments
 (0)