0%

Python-脚本调用阿里SDK实现控制SLB

需求

今天哥们提了一个需求,想实现调阿里云SLB的SDK实现脚本调整权重、增加或移除后端ECS实例

脚本
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
# -*- coding: utf-8 -*-
#安装python3和pip
#安装以下sdk模块
#pip install aliyun-python-sdk-core-v3
#pip install aliyun-python-sdk-slb
import sys
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerAttributeRequest
from aliyunsdkslb.request.v20140515 import SetBackendServersRequest
from aliyunsdkslb.request.v20140515 import AddBackendServersRequest
from aliyunsdkslb.request.v20140515 import RemoveBackendServersRequest

# 需填写ak信息,可用区
AccessKey=''
AccessKeySecret=''
RegionId = "cn-beijing"

client = AcsClient(AccessKey,AccessKeySecret,RegionId);
helpInfo='''
Basic Commands :
get 获取SLB信息
edit 配置SLB的后端权重
add 添加SLB的后端实例
remove 删除SLB的后端实例

Usage:
获取所有实例的信息
python slb-0604.py get all
获取某一实例的信息
python slb-0604.py get lb-2zedfhvgznrm7eqbr00uv
编辑单个权重
python slb-0604.py edit lb-2zedfhvgznrm7eqbr00uv [{'ServerId': 'i-2zedna91yczcq12zm60q', 'Weight': 99, 'Type': 'ecs'}]
编辑多个权重(最多20个)
python slb-0604.py edit lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
添加单个后端ecs
python slb-0604.py add lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
添加多个后端ecs
python slb-0604.py add lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
删除后端ecs
python slb-0604.py remove lb-2zedfhvgznrm7eqbr00uv [{"ServerId":"i-2zej4lxhjoq1icue6kup","Weight":"100"},{"ServerId":"i-2ze1u9ywulp5pbvvc7hv","Weight":"100"}]
'''
def main():
print(helpInfo)

def DescribeLoadBalancers(resource):
request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()
response = client.do_action_with_exception(request)
SLBInfo = json.loads(response)
LoadBalancerIdList = []
for SLBInstance in SLBInfo['LoadBalancers']['LoadBalancer']:
LoadBalancerIdList.append(SLBInstance['LoadBalancerId'])

if resource == 'all':
Ali_Slb_Info = {}
for SLBInstance in SLBInfo['LoadBalancers']['LoadBalancer']:
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
request.set_LoadBalancerId(SLBInstance['LoadBalancerId'])
response = client.do_action_with_exception(request)
Ali_Slb_Info[SLBInstance['LoadBalancerId']] = json.loads(response.decode('utf-8'))
print(LoadBalancerIdList)
elif resource in LoadBalancerIdList:
request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
response = json.loads(response.decode('utf-8'))
print(response['BackendServers'])

else:
print("输入错误,请输入 all 或 SLB实例ID !")


def SetBackendServers(resource,BackendServers):
request = SetBackendServersRequest.SetBackendServersRequest()
request.set_accept_format('json')
request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)

response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

def AddBackendServers(resource,BackendServers):
request = AddBackendServersRequest()
request.set_accept_format('json')

request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

def RemoveBackendServers(resource,BackendServers):
request = RemoveBackendServersRequest()
request.set_accept_format('json')

request.set_BackendServers(BackendServers)
request.set_LoadBalancerId(resource)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))

if __name__ == '__main__':
if len(sys.argv) == 1:
main()
else:
userInput = sys.argv[1:]
if userInput[0] == 'get' and userInput[1]:
resource = userInput[1]
DescribeLoadBalancers(resource)
if userInput[0] == 'edit' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
SetBackendServers(resource,BackendServers)
if userInput[0] == 'add' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
AddBackendServers(resource,BackendServers)
if userInput[0] == 'remove' and userInput[1] and userInput[2]:
resource = userInput[1]
BackendServers = userInput[2]
RemoveBackendServers(resource,BackendServers)
else:
print("输入错误")