This topic describes how to run Alibaba Cloud SDK for Python to upgrade multiple CLB instances at a time.

Background information

Create a CLB instance in the China (Zhangjiakou) region. Set the name of the CLB instance to SLB_TEST1, set the primary zone to cn-zhangjiakou-a, and set the secondary zone to cn-zhangjiakou-b. Then, set the billing method of the CLB instance to pay-as-you-go and set the specification of the CLB instance to slb.s1.small. Use the default settings for other parameters. After the CLB instance is created, upgrade the CLB instance. Then, delete the CLB instance.

Prerequisites

  1. An AccessKey pair is obtained.
    An AccessKey ID and an AccessKey secret that are used for identity verification are created for your account. For more information, see Obtain an AccessKey pair.
  2. Install Alibaba Cloud SLB SDK for Python.
  3. Go to the directory where the setup.py file is stored, and run the following command to initialize the environment:
    python setup.py install

Upgrade CLB instances

  1. In the directory to which the SDK is downloaded, open the folder $aliyun-openapi-python-sdk-examples\sdk_examples\examples\slb.
  2. Open slb_quick_start.py in the editor. Set the ACS_CLIENT parameter for user identity verification and set other parameters as needed. Then, save the file and exit.
    Note The SDK verifies the identity of every user. You can set the AcsClient parameter in a constants file and invoke the constants file when you need it.
    # encoding=utf-8
    import sys
    import json
    #Import AcsClient to verify the identity of the API caller.
    from aliyunsdkcore.client import AcsClient
    #Use the exception handling module of Alibaba Cloud SDK.
    from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
    #Import the API operation for querying the CLB instance list.
    from aliyunsdkslb.request.v20140515 import DescribeLoadBalancersRequest
    #Import the API operation for querying the detailed information about a CLB instance.
    from aliyunsdkslb.request.v20140515 import DescribeLoadBalancerAttributeRequest
    #Import the API operation for creating a CLB instance.
    from aliyunsdkslb.request.v20140515 import CreateLoadBalancerRequest
    #Import the API operation for upgrading or downgrading a CLB instance.
    from aliyunsdkslb.request.v20140515 import ModifyLoadBalancerInstanceSpecRequest
    #Import the API operation for deleting a CLB instance.
    from aliyunsdkslb.request.v20140515 import DeleteLoadBalancerRequest
    ##Handle exceptions.
    from sdk_lib.exception import ExceptionHandler
    #Import logs from the command-line interface.
    from sdk_lib.common_util import CommonUtil
    #Set the parameters for user identity verification.
    ACS_CLIENT = AcsClient(
        'your-access-key-id',  # your-access-key-id
        'your-access-key-secret',  # your-access-key-secret
        'cn-zhangjiakou',  # your-region-id
    )
    #Specify the number of retries after a failure.
    TRY_TIME = 3
    '''
    Create a CLB instance -> Query the CLB instance list -> Upgrade multiple CLB instances at a time
    '''
    
    
    def create_load_balancer(params):
        '''
        create_load_balancer: creates a CLB instance.
        For more information about the API operation, see https://www.alibabacloud.com/help/doc-detail/27577.html
        '''
        try:
            #Set the API request parameters for creating a CLB instance.
            request = CreateLoadBalancerRequest.CreateLoadBalancerRequest()
            #The ID of the primary zone to which the CLB instance belongs.
            request.set_MasterZoneId(params["master_zone_id"])
            #The ID of the secondary zone to which the CLB instance belongs.
            request.set_SlaveZoneId(params["slave_zone_id"])
            #The name of the CLB instance.
            request.set_LoadBalancerName(params["load_balancer_name"])
            #The specification of the CLB instance.
            request.set_LoadBalancerSpec(params["load_balancer_spec"])
            #The billing method of the CLB instance.
            request.set_PayType(params["pay_balancer_type"])
            #Make an API request and receive a response.
            response = ACS_CLIENT.do_action_with_exception(request)
            response_json = json.loads(response)
            #API response.
            return response_json
        except ServerException as e:
            ExceptionHandler.server_exception(e)
        except ClientException as e:
            ExceptionHandler.client_exception(e)
    
    
    def describe_load_balancers(params):
        '''
        describe_load_balancers: queries CLB instances.
        '''
        counter = 0
        while counter < TRY_TIME:
            try:
                #Set the API request parameters for adding backend servers to the default server group.
                request = DescribeLoadBalancersRequest.DescribeLoadBalancersRequest()
                #The name of the CLB instance.
                request.set_LoadBalancerName(params["load_balancer_name"])
                #Make an API request and receive a response.
                response = ACS_CLIENT.do_action_with_exception(request)
                response_json = json.loads(response)
                #API 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 describe_load_balancer_attribute(load_balancer_id):
        '''
        describe_load_balancer_attribute: queries detailed information about a CLB instance.
        '''
        counter = 0
        while counter < TRY_TIME:
            try:
                request = DescribeLoadBalancerAttributeRequest.DescribeLoadBalancerAttributeRequest()
                #The ID of the CLB instance.
                request.set_LoadBalancerId(load_balancer_id)
                #Make an API request and receive a response.
                response = ACS_CLIENT.do_action_with_exception(request)
                response_json = json.loads(response)
                #API response.
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
            finally:
                counter += 1
    
    
    def modify_load_balancer_instance_spec(params):
        '''
        modify_load_balancer_instance_spec: upgrades or downgrades a CLB instance.
        '''
        counter = 0
        while counter < TRY_TIME:
            try:
                request = ModifyLoadBalancerInstanceSpecRequest.ModifyLoadBalancerInstanceSpecRequest()
                #The ID of the CLB instance.
                request.set_LoadBalancerId(params["load_balancer_id"])
                #The specification of the CLB instance.
                request.set_LoadBalancerSpec(params["load_balancer_spec"])
                #Specify whether to enable automatic payment.
                request.set_AutoPay(params["auto_pay"])
                #Make an API request and receive a response.
                response = ACS_CLIENT.do_action_with_exception(request)
                response_json = json.loads(response)
                #API response.
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
            finally:
                counter += 1
    
    
    def delete_load_balancer(load_balancer_id):
        '''
        delete_load_balancer: deletes a CLB instance.
        '''
        try:
            #Set the API request parameters for deleting a CLB instance.
            request = DeleteLoadBalancerRequest.DeleteLoadBalancerRequest()
            #The ID of the CLB instance.
            request.set_LoadBalancerId(load_balancer_id)
            #Make an API request and receive a response.
            response = ACS_CLIENT.do_action_with_exception(request)
            response_json = json.loads(response)
            #API response.
            return response_json
        except ServerException as e:
            ExceptionHandler.server_exception(e)
        except ClientException as e:
            ExceptionHandler.client_exception(e)
    
    
    def main():
        params = {}
        #Set the API request parameters for creating a CLB instance.
        #Set the primary zone of the new CLB instance to Zhangjiakou Zone A.
        params["master_zone_id"] = "cn-zhangjiakou-a"
        #Set the secondary zone of the new CLB instance to Zhangjiakou Zone B.
        params["slave_zone_id"] = "cn-zhangjiakou-b"
        #Set the name of the new CLB instance to SLB1.
        params["load_balancer_name"] = "SLB_TEST1"
        #Set the billing method of the new CLB instance to pay-as-you-go.
        params["pay_balancer_type"] = "PayOnDemand"
        #Specify a specification for the new CLB instance.
        params["load_balancer_spec"] = "slb.s1.small"
    
        #Create the CLB instance.
        #Obtain the returned value of create_load_balancer. load_balancer_json is the returned value in a JSON string.
        create_load_balancer_json = create_load_balancer(params)
        #Print the returned value of load_balancer_json. create_load_balancer is the name of the JSON string.
        CommonUtil.log("create_load_balancer", create_load_balancer_json)
        #Query the CLB instance.
        load_balancers_json = describe_load_balancers(params)
        CommonUtil.log("describe_load_balancers", load_balancers_json)
        for load_balancer in load_balancers_json["LoadBalancers"]["LoadBalancer"]:
            #Query the detailed information about the CLB instance to check whether the CLB instance is a shared-resource CLB instance.
            load_balancer_json = describe_load_balancer_attribute(load_balancer["LoadBalancerId"])
            CommonUtil.log("describe_load_balancer_attribute", load_balancer_json)
            if 'LoadBalancerSpec' in load_balancer_json:
                #Set the parameters for upgrading or downgrading the CLB instance.
                modify_params = {}
                #The ID of the CLB instance.
                modify_params["load_balancer_id"] = load_balancer["LoadBalancerId"]
                #The specification of the CLB instance.
                modify_params["load_balancer_spec"] = "slb.s2.small"
                #Specify whether to enable automatic payment.
                modify_params["auto_pay"] = True
                result_json = modify_load_balancer_instance_spec(modify_params)
                CommonUtil.log("modify_load_balancer_instance_spec", result_json)
        #Delete the CLB instance.
        #Delete the CLB instance with the returned LoadBalancerId.
        load_balancer_json = delete_load_balancer(create_load_balancer_json["LoadBalancerId"])
        #Print the returned value of load_balancer_json.
        CommonUtil.log("delete_load_balancer", load_balancer_json)
    
    
    if __name__ == "__main__":
        sys.exit(main())
  3. Go to the directory where the slb_quick_start.py file is stored and run the following command to upgrade a CLB instance and then delete the CLB instance:
    python slb_quick_start.py