-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathvpc.py
More file actions
125 lines (96 loc) · 3.43 KB
/
vpc.py
File metadata and controls
125 lines (96 loc) · 3.43 KB
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
from dataclasses import dataclass
from typing import List, Optional
from linode_api4.errors import UnexpectedResponseError
from linode_api4.objects import Base, DerivedBase, Property, Region
from linode_api4.objects.networking import VPCIPAddress
from linode_api4.objects.serializable import JSONObject
from linode_api4.paginated_list import PaginatedList
@dataclass
class VPCSubnetLinodeInterface(JSONObject):
id: int = 0
config_id: Optional[int] = None
active: bool = False
@dataclass
class VPCSubnetLinode(JSONObject):
id: int = 0
interfaces: Optional[List[VPCSubnetLinodeInterface]] = None
@dataclass
class VPCSubnetDatabase(JSONObject):
id: int = 0
ipv4_range: Optional[str] = None
ipv6_ranges: Optional[List[str]] = None
class VPCSubnet(DerivedBase):
"""
An instance of a VPC subnet.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-vpc-subnet
"""
api_endpoint = "/vpcs/{vpc_id}/subnets/{id}"
derived_url_path = "subnets"
parent_id_name = "vpc_id"
properties = {
"id": Property(identifier=True),
"label": Property(mutable=True),
"ipv4": Property(),
"linodes": Property(json_object=VPCSubnetLinode, unordered=True),
"databases": Property(json_object=VPCSubnetDatabase, unordered=True),
"created": Property(is_datetime=True),
"updated": Property(is_datetime=True),
}
class VPC(Base):
"""
An instance of a VPC.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-vpc
"""
api_endpoint = "/vpcs/{id}"
properties = {
"id": Property(identifier=True),
"label": Property(mutable=True),
"description": Property(mutable=True),
"region": Property(slug_relationship=Region),
"subnets": Property(derived_class=VPCSubnet),
"created": Property(is_datetime=True),
"updated": Property(is_datetime=True),
}
def subnet_create(
self,
label: str,
ipv4: Optional[str] = None,
**kwargs,
) -> VPCSubnet:
"""
Creates a new Subnet object under this VPC.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-vpc-subnet
:param label: The label of this subnet.
:type label: str
:param ipv4: The IPv4 range of this subnet in CIDR format.
:type ipv4: str
:param ipv6: The IPv6 range of this subnet in CIDR format.
:type ipv6: str
"""
params = {
"label": label,
}
if ipv4 is not None:
params["ipv4"] = ipv4
params.update(kwargs)
result = self._client.post(
"{}/subnets".format(VPC.api_endpoint), model=self, data=params
)
self.invalidate()
if not "id" in result:
raise UnexpectedResponseError(
"Unexpected response creating Subnet", json=result
)
d = VPCSubnet(self._client, result["id"], self.id, result)
return d
@property
def ips(self) -> PaginatedList:
"""
Get all the IP addresses under this VPC.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-vpc-ips
:returns: A list of VPCIPAddresses the acting user can access.
:rtype: PaginatedList of VPCIPAddress
"""
return self._client._get_and_filter(
VPCIPAddress, endpoint="/vpcs/{}/ips".format(self.id)
)