def add_backend_servers(params): ''' add_backend_servers:添加后端服务器 官网API参考链接: https://help.aliyun.com/document_detail/27632.html ''' counter = 0 while counter < TRY_TIME: try: request = AddBackendServersRequest.AddBackendServersRequest() #负载均衡实例的ID request.set_LoadBalancerId(params["load_balancer_id"]) #要添加的后端服务器列表 request.set_BackendServers(params["backend_servers"]) response = client.do_action_with_exception(request) response_json = json.loads(response) return response_json except ServerException as e: if ExceptionHandler.server_exception(e): return e except ClientException as e: if ExceptionHandler.client_exception(e): return e finally: counter += 1
def add_backend_server(key): request = AddBackendServersRequest.AddBackendServersRequest() request.set_accept_format('json') request.set_LoadBalancerId('slb_id') request.set_BackendServers([{"ServerId": key, "weight": "100"}]) response = json.loads(client.do_action_with_exception(request), encoding='utf-8') print "-" * 80 print json.dumps(response, sort_keys=True, indent=4, separators=(',', ':')) print "" print "-" * 80
def addbackendecs(self,loadbalancername,ecsip): """添加后端ecs服务器""" loadbalancerid = self.describeloadbalancer(loadbalancername)[0] serverid = self.selectecsinfo(ecsip)[0] if not loadbalancerid or not serverid: print "slb or server is not found!" else: request = AddBackendServersRequest.AddBackendServersRequest() values = {"BackendServers":[{'ServerId':str(serverid),"Weight":"100"}],"LoadBalancerId":str(loadbalancerid)} backendinfo = self.request_api(request, values)['BackendServers'] print backendinfo
def add_slb_backends(self, loadbalancer_id, backend_id): if isinstance(backend_id, str): backend_playload = [{'ServerId': backend_id, "Weight": "100"}] else: backend_playload = backend_id slb = AddBackendServersRequest.AddBackendServersRequest() slb.set_accept_format('json') slb.set_LoadBalancerId(loadbalancer_id) slb.set_BackendServers(backend_playload) slb_r = self.clt.do_action(slb) slb_json = json.loads(slb_r) return slb_json
def add_backend_server(LoadBalancerId, BackendServers): # 设置参数 request = AddBackendServersRequest.AddBackendServersRequest() request.set_accept_format('json') request.add_query_param('LoadBalancerId', LoadBalancerId) request.add_query_param('BackendServers', BackendServers) # 发起请求 response = clt.do_action(request) return response
def aliyun_elb_add_backend(self, LoadBalancerId, BackendServers): """ 添加backend server 到elb :param LoadBalancerId: 负载均衡器的ID :param BackendServers: 后端server ,list格式里面是json :return: """ from aliyunsdkslb.request.v20140515 import AddBackendServersRequest res = AddBackendServersRequest.AddBackendServersRequest() res.set_accept_format("json") res.set_LoadBalancerId(LoadBalancerId=LoadBalancerId) res.set_BackendServers(BackendServers) response = self.client.do_action_with_exception(res) return response
def slbaddbackend(): # 设置参数 request = AddBackendServersRequest.AddBackendServersRequest() request.set_accept_format('json') # SLB的地域和ID号 request.add_query_param('RegionId', 'cn-hangzhou') request.add_query_param('LoadBalancerId', SLBid) for i in ['i-23v2vjj78', 'i-bp1ai95owp4uafu2m251']: request.add_query_param('BackendServers', [{ "ServerId": '%s' % i, "Weight": "100" }]) # 发起请求 response = clt.do_action(request) print '后端服务器添加OK!' print response
def add_server(slbid, server_list): ''' 创建 request,并设置参数 slbid=>'lb-2zekxu2elibyexxoo9hlw' server_list=>[{"ServerId": "slb_id_1", "Weight": "100"}, {"ServerId": "slb_id_2", "Weight": "100"} ] ''' request = AddBackendServersRequest.AddBackendServersRequest() # 发起 API和参数 请求 request.set_accept_format('json') request.set_LoadBalancerId(slbid) request.set_BackendServers(server_list) # 并打印返回 response = client.do_action_with_exception(request) print response
def addbackendserver(self, loadbalancername, serverip): '''添加后端ecs服务器''' loadbalancerids = self.describeloadbalancer(loadbalancername) serverid = self.describeinstance(serverip) if not loadbalancerids or not serverid: print('slb or server is not found!') else: for loadbalancerid in loadbalancerids: request = AddBackendServersRequest.AddBackendServersRequest() values = { "LoadBalancerId": str(loadbalancerid), "BackendServers": [{ 'ServerId': str(serverid[0]), 'Weight': '100' }] } mess = self.request_api(request, values)['BackendServers'] print(mess)
def add_backend_servers(self, servers): """ 添加后端服务器, 默认的权重是100 :param servers: ['i-bp15ikd9yfa7iu91xn2t'] 服务器的instance_id列表 :return: """ if not isinstance(servers, list): servers = [servers] aliyun_client = get_aliyun_client(readonly=False) aliyun_request = AddBackendServersRequest.AddBackendServersRequest() aliyun_request.set_accept_format('json') aliyun_request.set_LoadBalancerId(self.loadbalancer_id) aliyun_request.set_BackendServers( json.dumps([{ "ServerId": server, "Weight": "100" } for server in servers])) aliyun_response = aliyun_client.do_action_with_exception( aliyun_request) return json.loads(aliyun_response)
def addbackserver(LoadBalancerId, backendservers): request = AddBackendServersRequest.AddBackendServersRequest() request.set_LoadBalancerId(LoadBalancerId) request.set_BackendServers(backendservers) response = client.do_action_with_exception(request)