File: ipam.py

package info (click to toggle)
python-pynetbox 7.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,164 kB
  • sloc: python: 3,568; makefile: 12
file content (195 lines) | stat: -rw-r--r-- 5,437 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
(c) 2017 DigitalOcean

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 pynetbox.core.endpoint import DetailEndpoint
from pynetbox.core.response import Record


class IpAddresses(Record):
    def __str__(self):
        return str(self.address)


class IpRanges(Record):
    def __str__(self):
        return str(self.display)

    @property
    def available_ips(self):
        """Represents the ``available-ips`` detail endpoint.

        Returns a DetailEndpoint object that is the interface for
        viewing and creating IP addresses inside an ip range .

        :returns: :py:class:`.DetailEndpoint`

        :Examples:

        >>> ip_range = nb.ipam.ip_ranges.get(24)
        >>> ip_range.available_ips.list()
        [10.0.0.1/24, 10.0.0.2/24, 10.0.0.3/24, 10.0.0.4/24, 10.0.0.5/24, ...]

        To create a single IP:

        >>> ip_range = nb.ipam.ip_ranges.get(24)
        >>> ip_range.available_ips.create()
        10.0.0.1/24


        To create multiple IPs:

        >>> ip_range = nb.ipam.ip_ranges.get(24)
        >>> create = ip_range.available_ips.create([{} for i in range(2)])
        >>> create
        [10.0.0.2/24, 10.0.0.3/24]
        """
        return DetailEndpoint(self, "available-ips", custom_return=IpAddresses)


class Prefixes(Record):
    def __str__(self):
        return str(self.prefix)

    @property
    def available_ips(self):
        """Represents the ``available-ips`` detail endpoint.

        Returns a DetailEndpoint object that is the interface for
        viewing and creating IP addresses inside a prefix.

        :returns: :py:class:`.DetailEndpoint`

        :Examples:

        >>> prefix = nb.ipam.prefixes.get(24)
        >>> prefix.available_ips.list()
        [10.0.0.1/24, 10.0.0.2/24, 10.0.0.3/24, 10.0.0.4/24, 10.0.0.5/24, ...]

        To create a single IP:

        >>> prefix = nb.ipam.prefixes.get(24)
        >>> prefix.available_ips.create()
        10.0.0.1/24


        To create multiple IPs:

        >>> prefix = nb.ipam.prefixes.get(24)
        >>> create = prefix.available_ips.create([{} for i in range(2)])
        >>> create
        [10.0.0.2/24, 10.0.0.3/24]
        """
        return DetailEndpoint(self, "available-ips", custom_return=IpAddresses)

    @property
    def available_prefixes(self):
        """Represents the ``available-prefixes`` detail endpoint.

        Returns a DetailEndpoint object that is the interface for
        viewing and creating prefixes inside a parent prefix.

        Very similar to :py:meth:`~pynetbox.ipam.Prefixes.available_ips`
        , except that dict (or list of dicts) passed to ``.create()``
        needs to have a ``prefix_length`` key/value specifed.

        :returns: :py:class:`.DetailEndpoint`

        :Examples:

        >>> prefix = nb.ipam.prefixes.get(3)
        >>> prefix
        10.0.0.0/16
        >>> prefix.available_prefixes.list()
        [10.0.1.0/24, 10.0.2.0/23, 10.0.4.0/22, 10.0.8.0/21, 10.0.16.0/20, 10.0.32.0/19, 10.0.64.0/18, 10.0.128.0/17]


        Creating a single child prefix:

        >>> prefix = nb.ipam.prefixes.get(1)
        >>> prefix
        10.0.0.0/24
        >>> new_prefix = prefix.available_prefixes.create(
        ...     {"prefix_length": 29}
        ... )
        >>> new_prefix
        10.0.0.16/29

        """
        return DetailEndpoint(self, "available-prefixes", custom_return=Prefixes)


class Aggregates(Record):
    def __str__(self):
        return str(self.prefix)


class Vlans(Record):
    def __str__(self):
        return super().__str__() or str(self.vid)


class VlanGroups(Record):
    @property
    def available_vlans(self):
        """Represents the ``available-vlans`` detail endpoint.

        Returns a DetailEndpoint object that is the interface for
        viewing and creating VLANs inside a VLAN group.

        :returns: :py:class:`.DetailEndpoint`

        :Examples:

        >>> vlan_group = nb.ipam.vlan_groups.get(1)
        >>> vlan_group.available_vlans.list()
        [10, 11, 12]

        To create a new VLAN:

        >>> vlan_group.available_vlans.create({"name": "NewVLAN"})
        NewVLAN (10)
        """
        return DetailEndpoint(self, "available-vlans", custom_return=Vlans)


class AsnRanges(Record):
    @property
    def available_asns(self):
        """
        Represents the ``available-asns`` detail endpoint.

        Returns a DetailEndpoint object that is the interface for
        viewing and creating ASNs inside an ASN range.

        :returns: :py:class:`.DetailEndpoint`

        :Examples:

        >>> asn_range = nb.ipam.asn_ranges.get(1)
        >>> asn_range.available_asns.list()
        [64512, 64513, 64514]

        To create a new ASN:

        >>> asn_range.available_asns.create()
        64512

        To create multiple ASNs:

        >>> asn_range.available_asns.create([{} for i in range(2)])
        [64513, 64514]
        """
        return DetailEndpoint(self, "available-asns")