|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +# |
| 13 | + |
| 14 | + |
| 15 | +"""Identity v3 Protocols actions implementations""" |
| 16 | + |
| 17 | +import logging |
| 18 | +import six |
| 19 | + |
| 20 | +from cliff import command |
| 21 | +from cliff import lister |
| 22 | +from cliff import show |
| 23 | + |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class CreateProtocol(show.ShowOne): |
| 28 | + """Create new Federation Protocol tied to an Identity Provider""" |
| 29 | + |
| 30 | + log = logging.getLogger(__name__ + 'CreateProtocol') |
| 31 | + |
| 32 | + def get_parser(self, prog_name): |
| 33 | + parser = super(CreateProtocol, self).get_parser(prog_name) |
| 34 | + parser.add_argument( |
| 35 | + 'federation_protocol', |
| 36 | + metavar='<name>', |
| 37 | + help='Protocol (must be unique per Identity Provider') |
| 38 | + parser.add_argument( |
| 39 | + '--identity-provider', |
| 40 | + metavar='<identity-provider>', |
| 41 | + help=('Identity Provider you want to add the Protocol to ' |
| 42 | + '(must already exist)'), required=True) |
| 43 | + parser.add_argument( |
| 44 | + '--mapping', |
| 45 | + metavar='<mapping>', required=True, |
| 46 | + help='Mapping you want to be used (must already exist)') |
| 47 | + |
| 48 | + return parser |
| 49 | + |
| 50 | + def take_action(self, parsed_args): |
| 51 | + self.log.debug('take_action(%s)', parsed_args) |
| 52 | + identity_client = self.app.client_manager.identity |
| 53 | + protocol = identity_client.federation.protocols.create( |
| 54 | + protocol_id=parsed_args.federation_protocol, |
| 55 | + identity_provider=parsed_args.identity_provider, |
| 56 | + mapping=parsed_args.mapping) |
| 57 | + info = dict(protocol._info) |
| 58 | + # NOTE(marek-denis): Identity provider is not included in a response |
| 59 | + # from Keystone, however it should be listed to the user. Add it |
| 60 | + # manually to the output list, simply reusing value provided by the |
| 61 | + # user. |
| 62 | + info['identity_provider'] = parsed_args.identity_provider |
| 63 | + info['mapping'] = info.pop('mapping_id') |
| 64 | + return zip(*sorted(six.iteritems(info))) |
| 65 | + |
| 66 | + |
| 67 | +class DeleteProtocol(command.Command): |
| 68 | + """Delete Federation Protocol tied to a Identity Provider""" |
| 69 | + |
| 70 | + log = logging.getLogger(__name__ + '.DeleteProtocol') |
| 71 | + |
| 72 | + def get_parser(self, prog_name): |
| 73 | + parser = super(DeleteProtocol, self).get_parser(prog_name) |
| 74 | + parser.add_argument( |
| 75 | + 'federation_protocol', |
| 76 | + metavar='<name>', |
| 77 | + help='Protocol (must be unique per Identity Provider') |
| 78 | + parser.add_argument( |
| 79 | + '--identity-provider', |
| 80 | + metavar='<identity-provider>', required=True, |
| 81 | + help='Identity Provider the Protocol is tied to') |
| 82 | + |
| 83 | + return parser |
| 84 | + |
| 85 | + def take_action(self, parsed_args): |
| 86 | + self.log.debug('take_action(%s)', parsed_args) |
| 87 | + identity_client = self.app.client_manager.identity |
| 88 | + identity_client.federation.protocols.delete( |
| 89 | + parsed_args.identity_provider, parsed_args.federation_protocol) |
| 90 | + return |
| 91 | + |
| 92 | + |
| 93 | +class ListProtocols(lister.Lister): |
| 94 | + """List Protocols tied to an Identity Provider""" |
| 95 | + |
| 96 | + log = logging.getLogger(__name__ + '.ListProtocols') |
| 97 | + |
| 98 | + def get_parser(self, prog_name): |
| 99 | + parser = super(ListProtocols, self).get_parser(prog_name) |
| 100 | + parser.add_argument( |
| 101 | + '--identity-provider', |
| 102 | + metavar='<identity-provider>', required=True, |
| 103 | + help='Identity Provider the Protocol is tied to') |
| 104 | + |
| 105 | + return parser |
| 106 | + |
| 107 | + def take_action(self, parsed_args): |
| 108 | + identity_client = self.app.client_manager.identity |
| 109 | + |
| 110 | + protocols = identity_client.federation.protocols.list( |
| 111 | + parsed_args.identity_provider) |
| 112 | + columns = ('id', 'mapping') |
| 113 | + response_attributes = ('id', 'mapping_id') |
| 114 | + items = [utils.get_item_properties(s, response_attributes) |
| 115 | + for s in protocols] |
| 116 | + return (columns, items) |
| 117 | + |
| 118 | + |
| 119 | +class SetProtocol(command.Command): |
| 120 | + """Set Protocol tied to an Identity Provider""" |
| 121 | + |
| 122 | + log = logging.getLogger(__name__ + '.SetProtocol') |
| 123 | + |
| 124 | + def get_parser(self, prog_name): |
| 125 | + parser = super(SetProtocol, self).get_parser(prog_name) |
| 126 | + parser.add_argument( |
| 127 | + 'federation_protocol', |
| 128 | + metavar='<name>', |
| 129 | + help='Protocol (must be unique per Identity Provider') |
| 130 | + parser.add_argument( |
| 131 | + '--identity-provider', |
| 132 | + metavar='<identity-provider>', required=True, |
| 133 | + help=('Identity Provider you want to add the Protocol to ' |
| 134 | + '(must already exist)')) |
| 135 | + parser.add_argument( |
| 136 | + '--mapping', |
| 137 | + metavar='<mapping>', required=True, |
| 138 | + help='Mapping you want to be used (must already exist)') |
| 139 | + return parser |
| 140 | + |
| 141 | + def take_action(self, parsed_args): |
| 142 | + identity_client = self.app.client_manager.identity |
| 143 | + |
| 144 | + protocol = identity_client.federation.protocols.update( |
| 145 | + parsed_args.identity_provider, parsed_args.federation_protocol, |
| 146 | + parsed_args.mapping) |
| 147 | + info = dict(protocol._info) |
| 148 | + # NOTE(marek-denis): Identity provider is not included in a response |
| 149 | + # from Keystone, however it should be listed to the user. Add it |
| 150 | + # manually to the output list, simply reusing value provided by the |
| 151 | + # user. |
| 152 | + info['identity_provider'] = parsed_args.identity_provider |
| 153 | + info['mapping'] = info.pop('mapping_id') |
| 154 | + return zip(*sorted(six.iteritems(info))) |
| 155 | + |
| 156 | + |
| 157 | +class ShowProtocol(show.ShowOne): |
| 158 | + """Show Protocol tied to an Identity Provider""" |
| 159 | + |
| 160 | + log = logging.getLogger(__name__ + '.ShowProtocol') |
| 161 | + |
| 162 | + def get_parser(self, prog_name): |
| 163 | + parser = super(ShowProtocol, self).get_parser(prog_name) |
| 164 | + parser.add_argument( |
| 165 | + 'federation_protocol', |
| 166 | + metavar='<name>', |
| 167 | + help='Protocol (must be unique per Identity Provider') |
| 168 | + parser.add_argument( |
| 169 | + '--identity-provider', |
| 170 | + metavar='<identity-provider>', required=True, |
| 171 | + help=('Identity Provider you want to add the Protocol to ' |
| 172 | + '(must already exist)')) |
| 173 | + return parser |
| 174 | + |
| 175 | + def take_action(self, parsed_args): |
| 176 | + identity_client = self.app.client_manager.identity |
| 177 | + |
| 178 | + protocol = identity_client.federation.protocols.get( |
| 179 | + parsed_args.identity_provider, parsed_args.federation_protocol) |
| 180 | + info = dict(protocol._info) |
| 181 | + info['mapping'] = info.pop('mapping_id') |
| 182 | + return zip(*sorted(six.iteritems(info))) |
0 commit comments