Skip to content

Commit dc92610

Browse files
committed
add dns ptr osc
1 parent 239ccac commit dc92610

File tree

8 files changed

+547
-9
lines changed

8 files changed

+547
-9
lines changed

doc/source/cli/dns.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ Recordset operations
2626

2727
.. autoprogram-cliff:: openstack.dns.v2
2828
:command: dns recordset *
29+
30+
.. _dns_ptr:
31+
32+
PTR operations
33+
--------------
34+
35+
.. autoprogram-cliff:: openstack.dns.v2
36+
:command: dns ptr *

otcextensions/osclient/dns/v2/ptr.py

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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)

otcextensions/sdk/dns/v2/_proxy.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,30 +247,36 @@ def create_ptr(self, region, floating_ip_id, **attrs):
247247
attrs.update({'id': ptr_id})
248248
return self._update(_ptr.PTR, prepend_key=False, **attrs)
249249

250-
def get_ptr(self, region, floating_ip_id):
250+
def get_ptr(self, ptr=None, region=None, floating_ip_id=None):
251251
"""Show FloatingIP's PTR record
252252
253+
:param ptr: ID or instance of
254+
:class:`~otcextensions.sdk.dns.v2.ptr.PTR`
253255
:param region: project region
254256
:param floating_ip_id: the PTR floating ip id
255257
:returns: PTR instance
256258
:rtype: :class:`~otcextensions.sdk.dns.v2.ptr.PTR`
257259
"""
258260
# concat `region:floating_ip_id` as id
259-
ptr_id = region + ':' + floating_ip_id
260-
return self._get(_ptr.PTR, ptr_id)
261+
if not ptr:
262+
ptr = region + ':' + floating_ip_id
263+
return self._get(_ptr.PTR, ptr)
261264

262-
def restore_ptr(self, region, floating_ip_id):
265+
def restore_ptr(self, ptr=None, region=None, floating_ip_id=None):
263266
"""Unset FloatingIP's PTR record
264267
268+
:param ptr: ID or instance of
269+
:class:`~otcextensions.sdk.dns.v2.ptr.PTR`
265270
:param region: project region
266271
:param floating_ip_id: floating ip id
267272
:returns: PTR instance been deleted
268273
:rtype: :class:`~otcextensions.sdk.dns.v2.ptr.PTR`
269274
"""
270275
# concat `region:floating_ip_id` as id
271-
ptr_id = region + ':' + floating_ip_id
276+
if not ptr:
277+
ptr = region + ':' + floating_ip_id
272278
return self._update(_ptr.PTR,
273-
ptr_id,
279+
ptr,
274280
prepend_key=False,
275281
has_body=False,
276282
ptrdname=None)

otcextensions/sdk/dns/v2/ptr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PTR(sdk_resource.Resource):
4242
floating_ip_id = resource.Body('floatingip_id')
4343
#: Links contains a `self` pertaining to this zone or a `next` pertaining
4444
#: to next page
45-
links = resource.Body('links', type=dict)
45+
# links = resource.Body('links', type=dict)
4646
#: PTR domain name
4747
ptrdname = resource.Body('ptrdname')
4848
#: Region

otcextensions/tests/unit/osclient/dns/v2/fakes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from otcextensions.sdk.dns.v2 import zone
2525
from otcextensions.sdk.dns.v2 import nameserver
2626
from otcextensions.sdk.dns.v2 import recordset
27+
from otcextensions.sdk.dns.v2 import ptr
2728

2829

2930
def gen_data(data, columns):
@@ -101,3 +102,21 @@ def generate(cls):
101102
}
102103
obj = recordset.Recordset.existing(**object_info)
103104
return obj
105+
106+
107+
class FakePTR(test_base.Fake):
108+
"""Fake one or more recordset"""
109+
110+
@classmethod
111+
def generate(cls):
112+
object_info = {
113+
'address': uuid.uuid4().hex,
114+
'floating_ip_id': uuid.uuid4().hex,
115+
'region': uuid.uuid4().hex,
116+
'id': uuid.uuid4().hex,
117+
'ptrdname': uuid.uuid4().hex,
118+
'description': uuid.uuid4().hex,
119+
'ttl': random.randint(1, 600),
120+
}
121+
obj = ptr.PTR.existing(**object_info)
122+
return obj

0 commit comments

Comments
 (0)