|
| 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 | +'''DNS PTR v2 action implementations''' |
| 14 | +import logging |
| 15 | + |
| 16 | +from osc_lib import utils |
| 17 | +from osc_lib.command import command |
| 18 | + |
| 19 | +from otcextensions.i18n import _ |
| 20 | +from otcextensions.common import sdk_utils |
| 21 | + |
| 22 | +LOG = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +_formatters = { |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def _get_columns(item): |
| 30 | + column_map = { |
| 31 | + } |
| 32 | + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) |
| 33 | + |
| 34 | + |
| 35 | +class ListPTR(command.Lister): |
| 36 | + _description = _('List PTR records') |
| 37 | + columns = ( |
| 38 | + 'id', 'name', 'type', 'status', 'description' |
| 39 | + ) |
| 40 | + |
| 41 | + def get_parser(self, prog_name): |
| 42 | + parser = super(ListPTR, self).get_parser(prog_name) |
| 43 | + |
| 44 | + return parser |
| 45 | + |
| 46 | + def take_action(self, parsed_args): |
| 47 | + client = self.app.client_manager.dns |
| 48 | + |
| 49 | + query = {} |
| 50 | + |
| 51 | + data = client.ptrs(**query) |
| 52 | + |
| 53 | + table = (self.columns, |
| 54 | + (utils.get_item_properties( |
| 55 | + s, self.columns, formatters=_formatters |
| 56 | + ) for s in data)) |
| 57 | + return table |
| 58 | + |
| 59 | + |
| 60 | +class ShowPTR(command.ShowOne): |
| 61 | + _description = _('Show the PTR record details') |
| 62 | + |
| 63 | + def get_parser(self, prog_name): |
| 64 | + parser = super(ShowPTR, self).get_parser(prog_name) |
| 65 | + |
| 66 | + group = parser.add_mutually_exclusive_group() |
| 67 | + group.add_argument( |
| 68 | + '--ptr', |
| 69 | + metavar='<ID>', |
| 70 | + help=_('PTR record ID.') |
| 71 | + ) |
| 72 | + fgrp = group.add_argument_group() |
| 73 | + fgrp.add_argument( |
| 74 | + '--region', |
| 75 | + metavar='<region>', |
| 76 | + default='en-de', |
| 77 | + help=_('Region of the FloatingIP.') |
| 78 | + ) |
| 79 | + fgrp.add_argument( |
| 80 | + '--floating_ip', |
| 81 | + metavar='<UUID>', |
| 82 | + help=_('FloatingIP ID.') |
| 83 | + ) |
| 84 | + |
| 85 | + return parser |
| 86 | + |
| 87 | + def take_action(self, parsed_args): |
| 88 | + |
| 89 | + client = self.app.client_manager.dns |
| 90 | + |
| 91 | + query = {} |
| 92 | + |
| 93 | + if parsed_args.ptr: |
| 94 | + query['ptr'] = parsed_args.ptr |
| 95 | + else: |
| 96 | + query['region'] = parsed_args.region |
| 97 | + query['floating_ip_id'] = parsed_args.floating_ip |
| 98 | + |
| 99 | + obj = client.get_ptr(**query) |
| 100 | + |
| 101 | + display_columns, columns = _get_columns(obj) |
| 102 | + data = utils.get_item_properties(obj, columns) |
| 103 | + |
| 104 | + return (display_columns, data) |
| 105 | + |
| 106 | + |
| 107 | +class DeletePTR(command.Command): |
| 108 | + _description = _('Delete (restore) PTR record') |
| 109 | + |
| 110 | + def get_parser(self, prog_name): |
| 111 | + parser = super(DeletePTR, self).get_parser(prog_name) |
| 112 | + |
| 113 | + group = parser.add_mutually_exclusive_group() |
| 114 | + group.add_argument( |
| 115 | + '--ptr', |
| 116 | + metavar='<ID>', |
| 117 | + help=_('PTR record ID.') |
| 118 | + ) |
| 119 | + fgrp = group.add_argument_group() |
| 120 | + fgrp.add_argument( |
| 121 | + '--region', |
| 122 | + metavar='<region>', |
| 123 | + default='en-de', |
| 124 | + help=_('Region of the FloatingIP.') |
| 125 | + ) |
| 126 | + fgrp.add_argument( |
| 127 | + '--floating_ip', |
| 128 | + metavar='<UUID>', |
| 129 | + help=_('FloatingIP ID.') |
| 130 | + ) |
| 131 | + |
| 132 | + return parser |
| 133 | + |
| 134 | + def take_action(self, parsed_args): |
| 135 | + client = self.app.client_manager.dns |
| 136 | + |
| 137 | + query = {} |
| 138 | + |
| 139 | + if parsed_args.ptr: |
| 140 | + query['ptr'] = parsed_args.ptr |
| 141 | + else: |
| 142 | + query['region'] = parsed_args.region |
| 143 | + query['floating_ip_id'] = parsed_args.floating_ip |
| 144 | + |
| 145 | + return client.restore_ptr(**query) |
| 146 | + |
| 147 | + |
| 148 | +class SetPTR(command.ShowOne): |
| 149 | + _description = _('Set PTR record') |
| 150 | + |
| 151 | + def get_parser(self, prog_name): |
| 152 | + parser = super(SetPTR, self).get_parser(prog_name) |
| 153 | + |
| 154 | + fgrp = parser.add_argument_group() |
| 155 | + fgrp.add_argument( |
| 156 | + '--region', |
| 157 | + metavar='<region>', |
| 158 | + default='en-de', |
| 159 | + required=True, |
| 160 | + help=_('Region of the FloatingIP.') |
| 161 | + ) |
| 162 | + fgrp.add_argument( |
| 163 | + '--floating_ip', |
| 164 | + metavar='<UUID>', |
| 165 | + required=True, |
| 166 | + help=_('FloatingIP ID.') |
| 167 | + ) |
| 168 | + parser.add_argument( |
| 169 | + '--ptrdname', |
| 170 | + metavar='<ptrdomain>', |
| 171 | + required=True, |
| 172 | + help=_('Domain of the PTR record.') |
| 173 | + ) |
| 174 | + parser.add_argument( |
| 175 | + '--description', |
| 176 | + metavar='<description>', |
| 177 | + help=_('Description for this record.') |
| 178 | + ) |
| 179 | + parser.add_argument( |
| 180 | + '--ttl', |
| 181 | + metavar='<300-2147483647>', |
| 182 | + type=int, |
| 183 | + # NOTE: py2 does not support such big int, skip unless py3-only |
| 184 | + # choices=range(300, 2147483647), |
| 185 | + help=_('TTL (Time to Live) for the zone.') |
| 186 | + ) |
| 187 | + |
| 188 | + return parser |
| 189 | + |
| 190 | + def take_action(self, parsed_args): |
| 191 | + |
| 192 | + client = self.app.client_manager.dns |
| 193 | + |
| 194 | + attrs = {} |
| 195 | + |
| 196 | + if parsed_args.ptrdname: |
| 197 | + attrs['ptrdname'] = parsed_args.ptrdname |
| 198 | + if parsed_args.description: |
| 199 | + attrs['description'] = parsed_args.description |
| 200 | + if parsed_args.ttl: |
| 201 | + attrs['ttl'] = parsed_args.ttl |
| 202 | + |
| 203 | + obj = client.create_ptr( |
| 204 | + region=parsed_args.region, |
| 205 | + floating_ip_id=parsed_args.floating_ip, |
| 206 | + **attrs |
| 207 | + ) |
| 208 | + |
| 209 | + display_columns, columns = _get_columns(obj) |
| 210 | + data = utils.get_item_properties(obj, columns) |
| 211 | + |
| 212 | + return (display_columns, data) |
0 commit comments