Skip to content

Commit 76e06d1

Browse files
author
Steve Martinelli
committed
Add policy to identity v3
use file for data blobs add create/set/delete/show/list policy for v3 Change-Id: I5f68ef89dfb2241ea1aca00736ee6df5f6f03a9b
1 parent 73fb88e commit 76e06d1

2 files changed

Lines changed: 192 additions & 0 deletions

File tree

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 Policy 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 CreatePolicy(show.ShowOne):
29+
"""Create policy command"""
30+
31+
api = 'identity'
32+
log = logging.getLogger(__name__ + '.CreatePolicy')
33+
34+
def get_parser(self, prog_name):
35+
parser = super(CreatePolicy, self).get_parser(prog_name)
36+
parser.add_argument(
37+
'--type',
38+
metavar='<policy-type>',
39+
default="application/json",
40+
help='New MIME Type of the policy blob - i.e.: application/json',
41+
)
42+
parser.add_argument(
43+
'blob_file',
44+
metavar='<blob-file>',
45+
help='New policy rule set itself, as a serialized blob, in a file',
46+
)
47+
return parser
48+
49+
def take_action(self, parsed_args):
50+
self.log.debug('take_action(%s)' % parsed_args)
51+
blob = _read_blob_file_contents(parsed_args.blob_file)
52+
53+
identity_client = self.app.client_manager.identity
54+
policy = identity_client.policies.create(
55+
blob, type=parsed_args.type
56+
)
57+
58+
return zip(*sorted(policy._info.iteritems()))
59+
60+
61+
class DeletePolicy(command.Command):
62+
"""Delete policy command"""
63+
64+
api = 'identity'
65+
log = logging.getLogger(__name__ + '.DeletePolicy')
66+
67+
def get_parser(self, prog_name):
68+
parser = super(DeletePolicy, self).get_parser(prog_name)
69+
parser.add_argument(
70+
'policy',
71+
metavar='<policy-id>',
72+
help='ID of policy to delete',
73+
)
74+
return parser
75+
76+
def take_action(self, parsed_args):
77+
self.log.debug('take_action(%s)' % parsed_args)
78+
identity_client = self.app.client_manager.identity
79+
identity_client.policies.delete(parsed_args.policy)
80+
return
81+
82+
83+
class ListPolicy(lister.Lister):
84+
"""List policy command"""
85+
86+
api = 'identity'
87+
log = logging.getLogger(__name__ + '.ListPolicy')
88+
89+
def get_parser(self, prog_name):
90+
parser = super(ListPolicy, self).get_parser(prog_name)
91+
parser.add_argument(
92+
'--include-blob',
93+
action='store_true',
94+
default=False,
95+
help='Additional fields are listed in output',
96+
)
97+
return parser
98+
99+
def take_action(self, parsed_args):
100+
self.log.debug('take_action(%s)' % parsed_args)
101+
if parsed_args.include_blob:
102+
columns = ('ID', 'Type', 'Blob')
103+
else:
104+
columns = ('ID', 'Type')
105+
data = self.app.client_manager.identity.policies.list()
106+
return (columns,
107+
(utils.get_item_properties(
108+
s, columns,
109+
formatters={},
110+
) for s in data))
111+
112+
113+
class SetPolicy(command.Command):
114+
"""Set policy command"""
115+
116+
api = 'identity'
117+
log = logging.getLogger(__name__ + '.SetPolicy')
118+
119+
def get_parser(self, prog_name):
120+
parser = super(SetPolicy, self).get_parser(prog_name)
121+
parser.add_argument(
122+
'policy',
123+
metavar='<policy-id>',
124+
help='ID of policy to change',
125+
)
126+
parser.add_argument(
127+
'--type',
128+
metavar='<policy-type>',
129+
help='New MIME Type of the policy blob - i.e.: application/json',
130+
)
131+
parser.add_argument(
132+
'--blob-file',
133+
metavar='<blob_file>',
134+
help='New policy rule set itself, as a serialized blob, in a file',
135+
)
136+
return parser
137+
138+
def take_action(self, parsed_args):
139+
self.log.debug('take_action(%s)' % parsed_args)
140+
identity_client = self.app.client_manager.identity
141+
blob = None
142+
143+
if parsed_args.blob_file:
144+
blob = _read_blob_file_contents(parsed_args.blob_file)
145+
146+
kwargs = {}
147+
if blob:
148+
kwargs['blob'] = blob
149+
if parsed_args.type:
150+
kwargs['type'] = parsed_args.type
151+
152+
if not kwargs:
153+
sys.stdout.write("Policy not updated, no arguments present \n")
154+
return
155+
identity_client.policies.update(parsed_args.policy, **kwargs)
156+
return
157+
158+
159+
class ShowPolicy(show.ShowOne):
160+
"""Show policy command"""
161+
162+
api = 'identity'
163+
log = logging.getLogger(__name__ + '.ShowPolicy')
164+
165+
def get_parser(self, prog_name):
166+
parser = super(ShowPolicy, self).get_parser(prog_name)
167+
parser.add_argument(
168+
'policy',
169+
metavar='<policy-id>',
170+
help='ID of policy to display',
171+
)
172+
return parser
173+
174+
def take_action(self, parsed_args):
175+
self.log.debug('take_action(%s)' % parsed_args)
176+
identity_client = self.app.client_manager.identity
177+
policy = utils.find_resource(identity_client.policies,
178+
parsed_args.policy)
179+
180+
return zip(*sorted(policy._info.iteritems()))
181+
182+
183+
def _read_blob_file_contents(blob_file):
184+
with open(blob_file) as file:
185+
blob = file.read().strip()
186+
return blob

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ def read(fname):
134134
'set_group=openstackclient.identity.v3.group:SetGroup',
135135
'show_group=openstackclient.identity.v3.group:ShowGroup',
136136

137+
'create_policy=openstackclient.identity.v3.policy:CreatePolicy',
138+
'delete_policy=openstackclient.identity.v3.policy:DeletePolicy',
139+
'list_policy=openstackclient.identity.v3.policy:ListPolicy',
140+
'set_policy=openstackclient.identity.v3.policy:SetPolicy',
141+
'show_policy=openstackclient.identity.v3.policy:ShowPolicy',
142+
137143
'create_project='
138144
'openstackclient.identity.v3.project:CreateProject',
139145
'delete_project='

0 commit comments

Comments
 (0)