@@ -51,36 +51,24 @@ def _get_repsonse(self, repsonse):
51
51
return repsonse
52
52
53
53
def _add_routes_and_ip_assignment (self , config ):
54
+ """
55
+ Adds ZeroTier network routes
56
+ and IP assignmentpools through OpenWISP subnet
57
+
58
+ Params:
59
+ config (dict): ZeroTier network config dict
60
+ """
54
61
config ['routes' ] = [{'target' : str (self .subnet ), 'via' : '' }]
55
62
ip_end = str (self .subnet .broadcast_address )
56
63
ip_start = str (next (self .subnet .hosts ()))
57
64
config ['ipAssignmentPools' ] = [{"ipRangeEnd" : ip_end , "ipRangeStart" : ip_start }]
58
65
return config
59
66
60
- def join_network (self , network_id ):
61
- url = f'{ self .url } /network/{ network_id } '
62
- response = requests .post (
63
- url , json = {}, headers = self .headers , timeout = REQUEST_TIMEOUT
64
- )
65
- return response
66
-
67
- def leave_network (self , network_id ):
68
- url = f'{ self .url } /network/{ network_id } '
69
- response = requests .delete (url , headers = self .headers , timeout = REQUEST_TIMEOUT )
70
- return response
71
-
72
- def update_network_member (self , node_id , network_id , member_ip ):
73
- url = f'{ self .url } /controller/network/{ network_id } /member/{ node_id } '
74
- # Authorize and assign ip to the network member
75
- response = requests .post (
76
- url ,
77
- json = {'authorized' : True , 'ipAssignments' : [str (member_ip )]},
78
- headers = self .headers ,
79
- timeout = 5 ,
80
- )
81
- return response
82
-
83
67
def get_node_status (self ):
68
+ """
69
+ Fetches the status of the running ZeroTier controller
70
+ This method is used for host validation during VPN creation
71
+ """
84
72
url = f'{ self .url } /status'
85
73
try :
86
74
response = requests .get (url , headers = self .headers , timeout = REQUEST_TIMEOUT )
@@ -94,7 +82,41 @@ def get_node_status(self):
94
82
}
95
83
)
96
84
85
+ def join_network (self , network_id ):
86
+ """
87
+ Adds ZeroTier Controller to the specified network
88
+
89
+ Params:
90
+ network_id (str): ID of the network to join
91
+ """
92
+ url = f'{ self .url } /network/{ network_id } '
93
+ response = requests .post (
94
+ url , json = {}, headers = self .headers , timeout = REQUEST_TIMEOUT
95
+ )
96
+ return response
97
+
98
+ def leave_network (self , network_id ):
99
+ """
100
+ Removes ZeroTier Controller from the specified network
101
+
102
+ Params:
103
+ network_id (str): ID of the network to leave
104
+ """
105
+ url = f'{ self .url } /network/{ network_id } '
106
+ response = requests .delete (url , headers = self .headers , timeout = REQUEST_TIMEOUT )
107
+ return response
108
+
97
109
def create_network (self , node_id , config ):
110
+ """
111
+ Creates a new network in the ZeroTier Controller
112
+
113
+ Params:
114
+ node_id (str): ID of the controller node
115
+ config (dict): Configuration of the new network
116
+
117
+ Returns:
118
+ network_config(dict): Filtered response from the ZeroTier Controller API
119
+ """
98
120
url = f"{ self .url } { self ._get_endpoint ('network' , 'create' , node_id )} "
99
121
config = self ._add_routes_and_ip_assignment (config )
100
122
try :
@@ -110,6 +132,13 @@ def create_network(self, node_id, config):
110
132
)
111
133
112
134
def update_network (self , config , network_id ):
135
+ """
136
+ Update configuration of an existing ZeroTier Controller network
137
+
138
+ Params:
139
+ config (dict): New configuration data for the network
140
+ network_id (str): ID of the network to update
141
+ """
113
142
url = f"{ self .url } { self ._get_endpoint ('network' , 'update' , network_id )} "
114
143
config = self ._add_routes_and_ip_assignment (config )
115
144
response = requests .post (
@@ -118,6 +147,49 @@ def update_network(self, config, network_id):
118
147
return response , self ._get_repsonse (response .json ())
119
148
120
149
def delete_network (self , network_id ):
150
+ """
151
+ Deletes ZeroTier Controller network
152
+
153
+ Params:
154
+ network_id (str): ID of the ZeroTier network to be deleted
155
+ """
121
156
url = f"{ self .url } { self ._get_endpoint ('network' , 'delete' , network_id )} "
122
157
response = requests .delete (url , headers = self .headers , timeout = REQUEST_TIMEOUT )
123
158
return response
159
+
160
+ def update_network_member (self , node_id , network_id , member_ip ):
161
+ """
162
+ Update ZeroTier Network Member Configuration
163
+
164
+ This method is currently used to authorize, enable the bridge
165
+ and assign an IP address to a network member
166
+
167
+ Params:
168
+ node_id (str): Node ID of the network member
169
+ network_id (str): Network ID to which the member belongs
170
+ member_ip (str): IP address to be assigned to the network member
171
+ """
172
+ url = f'{ self .url } /controller/network/{ network_id } /member/{ node_id } '
173
+ response = requests .post (
174
+ url ,
175
+ json = {
176
+ 'authorized' : True ,
177
+ 'activeBridge' : True ,
178
+ 'ipAssignments' : [str (member_ip )],
179
+ },
180
+ headers = self .headers ,
181
+ timeout = 5 ,
182
+ )
183
+ return response
184
+
185
+ def remove_network_member (self , node_id , network_id ):
186
+ """
187
+ Remove a member from ZeroTier network
188
+
189
+ Params:
190
+ node_id (str): ID of the network member
191
+ network_id (str): ID of the ZeroTier network
192
+ """
193
+ url = f'{ self .url } /controller/network/{ network_id } /member/{ node_id } '
194
+ response = requests .delete (url , headers = self .headers , timeout = 5 )
195
+ return response
0 commit comments