Skip to content

Commit 0c252b6

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "add domain, credential to identity v3 api"
2 parents d5a4216 + 55462e8 commit 0c252b6

3 files changed

Lines changed: 407 additions & 0 deletions

File tree

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# Copyright 2012-2013 OpenStack, LLC.
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 v3 Credential action implementations"""
17+
18+
import logging
19+
import sys
20+
21+
from cliff import command
22+
from cliff import lister
23+
from cliff import show
24+
25+
from openstackclient.common import utils
26+
27+
28+
class CreateCredential(show.ShowOne):
29+
"""Create credential command"""
30+
31+
api = 'identity'
32+
log = logging.getLogger(__name__ + '.CreateCredential')
33+
34+
def get_parser(self, prog_name):
35+
parser = super(CreateCredential, self).get_parser(prog_name)
36+
parser.add_argument(
37+
'user',
38+
metavar='<user>',
39+
help='Name or ID of user that owns the credential',
40+
)
41+
parser.add_argument(
42+
'--type',
43+
default="cert",
44+
metavar='<type>',
45+
choices=['ec2', 'cert'],
46+
help='New credential type',
47+
)
48+
parser.add_argument(
49+
'data',
50+
metavar='<data>',
51+
help='New credential data',
52+
)
53+
parser.add_argument(
54+
'--project',
55+
metavar='<project>',
56+
help='Project name or ID which limits the scope of the credential',
57+
)
58+
return parser
59+
60+
def take_action(self, parsed_args):
61+
self.log.debug('take_action(%s)' % parsed_args)
62+
identity_client = self.app.client_manager.identity
63+
user_id = utils.find_resource(identity_client.users,
64+
parsed_args.user).id
65+
if parsed_args.project:
66+
project = utils.find_resource(identity_client.projects,
67+
parsed_args.project).id
68+
else:
69+
project = None
70+
credential = identity_client.credentials.create(
71+
user_id,
72+
parsed_args.type,
73+
parsed_args.data,
74+
project=project)
75+
76+
return zip(*sorted(credential._info.iteritems()))
77+
78+
79+
class DeleteCredential(command.Command):
80+
"""Delete credential command"""
81+
82+
api = 'identity'
83+
log = logging.getLogger(__name__ + '.DeleteCredential')
84+
85+
def get_parser(self, prog_name):
86+
parser = super(DeleteCredential, self).get_parser(prog_name)
87+
parser.add_argument(
88+
'credential',
89+
metavar='<credential-id>',
90+
help='ID of credential to delete',
91+
)
92+
return parser
93+
94+
def take_action(self, parsed_args):
95+
self.log.debug('take_action(%s)' % parsed_args)
96+
identity_client = self.app.client_manager.identity
97+
identity_client.credentials.delete(parsed_args.credential)
98+
return
99+
100+
101+
class ListCredential(lister.Lister):
102+
"""List credential command"""
103+
104+
api = 'identity'
105+
log = logging.getLogger(__name__ + '.ListCredential')
106+
107+
def take_action(self, parsed_args):
108+
self.log.debug('take_action(%s)' % parsed_args)
109+
columns = ('ID', 'Type', 'User ID', 'Data', 'Project ID')
110+
data = self.app.client_manager.identity.credentials.list()
111+
return (columns,
112+
(utils.get_item_properties(
113+
s, columns,
114+
formatters={},
115+
) for s in data))
116+
117+
118+
class SetCredential(command.Command):
119+
"""Set credential command"""
120+
121+
api = 'identity'
122+
log = logging.getLogger(__name__ + '.SetCredential')
123+
124+
def get_parser(self, prog_name):
125+
parser = super(SetCredential, self).get_parser(prog_name)
126+
parser.add_argument(
127+
'credential',
128+
metavar='<credential-id>',
129+
help='ID of credential to change',
130+
)
131+
parser.add_argument(
132+
'--user',
133+
metavar='<user>',
134+
help='Name or ID of user that owns the credential',
135+
)
136+
parser.add_argument(
137+
'--type',
138+
metavar='<type>',
139+
choices=['ec2', 'cert'],
140+
help='New credential type',
141+
)
142+
parser.add_argument(
143+
'--data',
144+
metavar='<data>',
145+
help='New credential data',
146+
)
147+
parser.add_argument(
148+
'--project',
149+
metavar='<project>',
150+
help='Project name or ID which limits the scope of the credential',
151+
)
152+
return parser
153+
154+
def take_action(self, parsed_args):
155+
self.log.debug('take_action(%s)' % parsed_args)
156+
identity_client = self.app.client_manager.identity
157+
user_id = utils.find_resource(identity_client.users,
158+
parsed_args.user).id
159+
kwargs = {}
160+
if user_id:
161+
kwargs['user'] = user_id
162+
if parsed_args.type:
163+
kwargs['type'] = parsed_args.type
164+
if parsed_args.data:
165+
kwargs['data'] = parsed_args.data
166+
if parsed_args.project:
167+
project = utils.find_resource(identity_client.projects,
168+
parsed_args.project).id
169+
kwargs['project'] = project
170+
171+
if not kwargs:
172+
sys.stdout.write("Credential not updated, no arguments present")
173+
return
174+
identity_client.credentials.update(parsed_args.credential, **kwargs)
175+
return
176+
177+
178+
class ShowCredential(show.ShowOne):
179+
"""Show credential command"""
180+
181+
api = 'identity'
182+
log = logging.getLogger(__name__ + '.ShowCredential')
183+
184+
def get_parser(self, prog_name):
185+
parser = super(ShowCredential, self).get_parser(prog_name)
186+
parser.add_argument(
187+
'credential',
188+
metavar='<credential-id>',
189+
help='ID of credential to display',
190+
)
191+
return parser
192+
193+
def take_action(self, parsed_args):
194+
self.log.debug('take_action(%s)' % parsed_args)
195+
identity_client = self.app.client_manager.identity
196+
credential = utils.find_resource(identity_client.credentials,
197+
parsed_args.credential)
198+
199+
return zip(*sorted(credential._info.iteritems()))
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# Copyright 2012-2013 OpenStack, LLC.
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 v3 Domain action implementations"""
17+
18+
import logging
19+
import sys
20+
21+
from cliff import command
22+
from cliff import lister
23+
from cliff import show
24+
25+
from openstackclient.common import utils
26+
27+
28+
class CreateDomain(show.ShowOne):
29+
"""Create domain command"""
30+
31+
api = 'identity'
32+
log = logging.getLogger(__name__ + '.CreateDomain')
33+
34+
def get_parser(self, prog_name):
35+
parser = super(CreateDomain, self).get_parser(prog_name)
36+
parser.add_argument(
37+
'name',
38+
metavar='<domain-name>',
39+
help='New domain name',
40+
)
41+
parser.add_argument(
42+
'--description',
43+
metavar='<domain-description>',
44+
help='New domain description',
45+
)
46+
enable_group = parser.add_mutually_exclusive_group()
47+
enable_group.add_argument(
48+
'--enable',
49+
dest='enabled',
50+
action='store_true',
51+
default=True,
52+
help='Enable domain')
53+
enable_group.add_argument(
54+
'--disable',
55+
dest='enabled',
56+
action='store_false',
57+
help='Disable domain')
58+
return parser
59+
60+
def take_action(self, parsed_args):
61+
self.log.debug('take_action(%s)' % parsed_args)
62+
identity_client = self.app.client_manager.identity
63+
domain = identity_client.domains.create(
64+
parsed_args.name,
65+
parsed_args.description,
66+
enabled=parsed_args.enabled,
67+
)
68+
69+
return zip(*sorted(domain._info.iteritems()))
70+
71+
72+
class DeleteDomain(command.Command):
73+
"""Delete domain command"""
74+
75+
api = 'identity'
76+
log = logging.getLogger(__name__ + '.DeleteDomain')
77+
78+
def get_parser(self, prog_name):
79+
parser = super(DeleteDomain, self).get_parser(prog_name)
80+
parser.add_argument(
81+
'domain',
82+
metavar='<domain>',
83+
help='Name or ID of domain to delete',
84+
)
85+
return parser
86+
87+
def take_action(self, parsed_args):
88+
self.log.debug('take_action(%s)' % parsed_args)
89+
identity_client = self.app.client_manager.identity
90+
domain = utils.find_resource(identity_client.domains,
91+
parsed_args.domain)
92+
identity_client.domains.delete(domain.id)
93+
return
94+
95+
96+
class ListDomain(lister.Lister):
97+
"""List domain command"""
98+
99+
api = 'identity'
100+
log = logging.getLogger(__name__ + '.ListDomain')
101+
102+
def take_action(self, parsed_args):
103+
self.log.debug('take_action(%s)' % parsed_args)
104+
columns = ('ID', 'Name', 'Enabled', 'Description')
105+
data = self.app.client_manager.identity.domains.list()
106+
return (columns,
107+
(utils.get_item_properties(
108+
s, columns,
109+
formatters={},
110+
) for s in data))
111+
112+
113+
class SetDomain(command.Command):
114+
"""Set domain command"""
115+
116+
api = 'identity'
117+
log = logging.getLogger(__name__ + '.SetDomain')
118+
119+
def get_parser(self, prog_name):
120+
parser = super(SetDomain, self).get_parser(prog_name)
121+
parser.add_argument(
122+
'domain',
123+
metavar='<domain>',
124+
help='Name or ID of domain to change',
125+
)
126+
parser.add_argument(
127+
'--name',
128+
metavar='<new-domain-name>',
129+
help='New domain name',
130+
)
131+
parser.add_argument(
132+
'--description',
133+
metavar='<domain-description>',
134+
help='New domain description',
135+
)
136+
enable_group = parser.add_mutually_exclusive_group()
137+
enable_group.add_argument(
138+
'--enable',
139+
dest='enabled',
140+
action='store_true',
141+
default=True,
142+
help='Enable domain (default)',
143+
)
144+
enable_group.add_argument(
145+
'--disable',
146+
dest='enabled',
147+
action='store_false',
148+
help='Disable domain',
149+
)
150+
return parser
151+
152+
def take_action(self, parsed_args):
153+
self.log.debug('take_action(%s)' % parsed_args)
154+
identity_client = self.app.client_manager.identity
155+
domain = utils.find_resource(identity_client.domains,
156+
parsed_args.domain)
157+
kwargs = {}
158+
if parsed_args.name:
159+
kwargs['name'] = parsed_args.name
160+
if parsed_args.description:
161+
kwargs['description'] = parsed_args.description
162+
if 'enabled' in parsed_args:
163+
kwargs['enabled'] = parsed_args.enabled
164+
165+
if not kwargs:
166+
sys.stdout.write("Domain not updated, no arguments present")
167+
return
168+
identity_client.domains.update(domain.id, **kwargs)
169+
return
170+
171+
172+
class ShowDomain(show.ShowOne):
173+
"""Show domain command"""
174+
175+
api = 'identity'
176+
log = logging.getLogger(__name__ + '.ShowDomain')
177+
178+
def get_parser(self, prog_name):
179+
parser = super(ShowDomain, self).get_parser(prog_name)
180+
parser.add_argument(
181+
'domain',
182+
metavar='<domain>',
183+
help='Name or ID of domain to display',
184+
)
185+
return parser
186+
187+
def take_action(self, parsed_args):
188+
self.log.debug('take_action(%s)' % parsed_args)
189+
identity_client = self.app.client_manager.identity
190+
domain = utils.find_resource(identity_client.domains,
191+
parsed_args.domain)
192+
193+
return zip(*sorted(domain._info.iteritems()))

0 commit comments

Comments
 (0)