File: recordsets.py

package info (click to toggle)
python-designateclient 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 4,496; makefile: 26; sh: 2
file content (102 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Author: Endre Karlson <endre.karlson@hp.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_utils import uuidutils

from designateclient.v2.base import V2Controller
from designateclient.v2 import utils as v2_utils


class RecordSetController(V2Controller):
    def _canonicalize_record_name(self, zone, name):
        zone_info = None

        # If we get a zone name we'll need to get the ID of it before POST.
        if (isinstance(zone, str) and not
                uuidutils.is_uuid_like(zone)):
            zone_info = self.client.zones.get(zone)
        elif isinstance(zone, dict):
            zone_info = zone

        # We where given a name like "www" vs www.i.io., attempt to fix it on
        # the behalf of the actor.
        if not name.endswith('.'):
            if not isinstance(zone_info, dict):
                zone_info = self.client.zones.get(zone)

            name = '{}.{}'.format(name, zone_info['name'])

        return name, zone_info

    def create(self, zone, name, type_, records, description=None,
               ttl=None):
        name, zone_info = self._canonicalize_record_name(zone, name)

        data = {
            'name': name,
            'type': type_,
            'records': records
        }

        if ttl is not None:
            data['ttl'] = ttl

        if description is not None:
            data['description'] = description

        if zone_info is not None:
            zone_id = zone_info['id']
        else:
            zone_id = zone

        return self._post(f'/zones/{zone_id}/recordsets', data=data)

    def list(self, zone, criterion=None, marker=None, limit=None):
        zone = v2_utils.resolve_by_name(self.client.zones.list, zone)

        url = self.build_url(
            f'/zones/{zone}/recordsets', criterion, marker, limit
        )

        return self._get(url, response_key='recordsets')

    def list_all_zones(self, criterion=None, marker=None, limit=None):
        url = self.build_url('/recordsets', criterion, marker, limit)

        return self._get(url, response_key='recordsets')

    def get(self, zone, recordset):
        zone = v2_utils.resolve_by_name(self.client.zones.list, zone)
        recordset = v2_utils.resolve_by_name(self.list, recordset, zone)

        url = self.build_url(f'/zones/{zone}/recordsets/{recordset}')

        return self._get(url)

    def update(self, zone, recordset, values):
        zone = v2_utils.resolve_by_name(self.client.zones.list, zone)
        recordset = v2_utils.resolve_by_name(self.list, recordset, zone)

        url = f'/zones/{zone}/recordsets/{recordset}'

        return self._put(url, data=values)

    def delete(self, zone, recordset):
        zone = v2_utils.resolve_by_name(self.client.zones.list, zone)
        recordset = v2_utils.resolve_by_name(self.list, recordset, zone)

        url = f'/zones/{zone}/recordsets/{recordset}'

        return self._delete(url)