forked from ericchou-python/A10_Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslb_vipCreate.py
executable file
·144 lines (126 loc) · 3.95 KB
/
slb_vipCreate.py
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
#!/usr/bin/env python
#
# v2, December 20, 2013
# by Eric Chou
#
# Reference: AX_aXAPI_Ref_v2-20121010.pdf
#
# v2 change: added httplib fix
import httplib, json, urllib, urllib2
# patch httplib.HTTPSConnection.connect
def connect_patched(self):
"Connect to a host on a given (SSL) port."
sock = socket.create_connection((self.host, self.port),self.timeout, self.source_address)
if self._tunnel_host:
self.sock = sock
self._tunnel()
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
httplib.HTTPSConnection.connect = connect_patched
# Gets the session ID to host 172.31.31.121 (Student 12)
c = httplib.HTTPSConnection("172.31.31.121")
c.request("GET", "/services/rest/V2/?method=authenticate&username=admin&password=a10&format=json")
response = c.getresponse()
data = json.loads(response.read())
session_id = data['session_id']
print "Session Created. Session ID: " + session_id
###############################
# Step 1. Create Servers #
###############################
# Create slb servers s1 10.0.2.128
# Construct HTTP URL and Post Body
post_body = json.dumps(
{"server":
{
"name": "s1",
"host": "10.0.2.128",
"health_monitor": "(default)",
"port_list": [
{"port_num": 80,
"protocol": 2,}
],
}
}
)
url = "https://172.31.31.121/services/rest/V2/?&session_id=" + session_id + "&format=json&method=slb.server.create"
print "URL Created. URL: " + url + " body: " + post_body
# Making request
req = urllib2.Request(url, post_body)
rsp = urllib2.urlopen(req)
content = rsp.read()
print "Result: " + content
# Create slb server s2 10.0.2.129
post_body = json.dumps(
{"server":
{
"name": "s2",
"host": "10.0.2.129",
"health_monitor": "(default)",
"port_list": [
{"port_num": 80,
"protocol": 2,}
],
}
}
)
url = "https://172.31.31.121/services/rest/V2/?&session_id=" + session_id + "&format=json&method=slb.server.create"
print "URL Created. URL: " + url + " body: " + post_body
# Making request
req = urllib2.Request(url, post_body)
rsp = urllib2.urlopen(req)
content = rsp.read()
print "Result: " + content
####################################
# Step 2. Create Service Group #
####################################
# Create service group http with member server s1 and s2
post_body = json.dumps(
{"service_group":
{
"name": "http",
"protocol": 2,
"health_monitor": "ping",
"member_list": [
{"server": "s1",
"port": 80,},
{"server": "s2",
"port": 80,},
],
}
}
)
url = "https://172.31.31.121/services/rest/V2/?&session_id=" + session_id + "&format=json&method=slb.service_group.create"
print "URL Created. URL: " + url + " body: " + post_body
# Making request
req = urllib2.Request(url, post_body)
rsp = urllib2.urlopen(req)
content = rsp.read()
print "Result: " + content
#####################################
# Step 3. Create Virtual Server #
#####################################
# Create virtual server
post_body = json.dumps(
{"virtual_server":
{
"name": "vip1",
"subnet":
{
"address": "10.0.1.122",
"mask_len": 24,
},
"vport_list": [
{"protocol": 2,
"port": 80,
"service_group": "http",
},
],
}
}
)
url = "https://172.31.31.121/services/rest/V2/?&session_id=" + session_id + "&format=json&method=slb.virtual_server.create"
print "URL Created. URL: " + url + " body: " + post_body
# Making request
req = urllib2.Request(url, post_body)
rsp = urllib2.urlopen(req)
content = rsp.read()
print "Result: " + content