本文介绍如何使用Python SDK修改EIP的带宽峰值。

背景信息

本次示例分为以下几步,修改EIP的带宽峰值。
  1. 在华东1杭州地域创建一个EIP。
  2. 修改EIP的带宽峰值为50M。
  3. 查询修改带宽峰值后的EIP。
  4. 修改EIP的带宽峰值为10M。
  5. 查询修改带宽峰值后的EIP。
  6. 释放EIP。

操作步骤

  1. 在下载的SDK目录中,打开$aliyun-openapi-python-sdk-examples\sdk_examples\sdk_lib文件夹。
  2. 使用编辑器打开consts.py文件,配置用户鉴权参数ACS_CLIENT
  3. 在下载的SDK目录中,打开$aliyun-openapi-python-sdk-examples\sdk_examples\examples\eip文件夹。
  4. 使用编辑器打开eip_modify_attribute.py文件,根据实际情况配置相关参数,保存退出。
    #encoding=utf-8
    import sys
    import json
    from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
    from aliyunsdkvpc.request.v20160428 import AllocateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import AssociateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import DescribeEipAddressesRequest
    from aliyunsdkvpc.request.v20160428 import UnassociateEipAddressRequest
    from aliyunsdkvpc.request.v20160428 import ModifyEipAddressAttributeRequest
    from aliyunsdkvpc.request.v20160428 import ReleaseEipAddressRequest
    from sdk_lib.exception import ExceptionHandler
    from sdk_lib.check_status import CheckStatus
    from sdk_lib.consts import *
    from sdk_lib.common_util import CommonUtil
    
    """
    创建EIP->修改EIP带宽到50>查询EIP->修改EIP带宽到10->查询EIP->释放EIP
    """
    class Eip(object):
        def __init__(self, client):
            self.client = client
    
        def allocate_eip_address(self, params):
            """
            allocate_eip_address: 申请弹性公网IP(EIP)
            官网API参考: https://help.aliyun.com/document_detail/36016.html
            """
            try:
                request = AllocateEipAddressRequest.AllocateEipAddressRequest()
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            AVAILABLE, response_json["AllocationId"]):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def associate_eip_address(self, params):
            """
            associate_eip_address: 将EIP绑定到同地域的云产品实例上
            官网API参考: https://help.aliyun.com/document_detail/36017.html
            """
            try:
                request = AssociateEipAddressRequest.AssociateEipAddressRequest()
                # EIP的ID
                request.set_AllocationId(params['allocation_id'])
                # 要绑定的云产品实例的类型
                request.set_InstanceType(params['instance_type'])
                # 要绑定的实例ID
                request.set_InstanceId(params['instance_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            InUse, params['allocation_id']):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_eip_address(self, allocation_id):
            """
            describe_eip_status: 查询指定地域已创建的EIP。
            官网API参考: https://help.aliyun.com/document_detail/36018.html
            """
            try:
                request = DescribeEipAddressesRequest.DescribeEipAddressesRequest()
                # EIP的ID
                request.set_AllocationId(allocation_id)
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def describe_eip_status(self, allocation_id):
            """
            describe_eip_status: 查询指定地域已创建的EIP的状态
            官网API参考: https://help.aliyun.com/document_detail/36018.html
            """
            # EIP的ID
            response = self.describe_eip_address(allocation_id)
            return response["EipAddresses"]["EipAddress"][0]["Status"]
    
    
        def unassociate_eip_address(self, params):
            """
            unassociate_eip_address: 将EIP从绑定的云资源上解绑。
            官网API参考: https://help.aliyun.com/document_detail/36021.html
            """
            try:
                request = UnassociateEipAddressRequest.UnassociateEipAddressRequest()
                # EIP的ID
                request.set_AllocationId(params['allocation_id'])
                # 要解绑的资源类型
                request.set_InstanceType(params['instance_type'])
                # 要解绑的云产品的实例ID
                request.set_InstanceId(params['instance_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_eip_status,
                                            AVAILABLE, params['allocation_id']):
                    return response_json
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def modify_eip_address(self, params):
            """
            modify_eip_address: 修改指定EIP的名称、描述信息和带宽峰值
            官网API参考: https://help.aliyun.com/document_detail/36019.html
            """
            try:
                request = ModifyEipAddressAttributeRequest.ModifyEipAddressAttributeRequest()
                # 弹性公网IP的ID
                request.set_AllocationId(params['allocation_id'])
                # EIP的带宽峰值,单位为Mbps
                request.set_Bandwidth(params['bandwidth'])
                # EIP的名称
                request.set_Name(params['name'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def release_eip_address(self, params):
            """
            release_eip_address: 释放指定的EIP。
            官网API参考: https://help.aliyun.com/document_detail/36020.html
            """
            try:
                request = ReleaseEipAddressRequest.ReleaseEipAddressRequest()
                # 要释放的弹性公网IP的ID
                request.set_AllocationId(params['allocation_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
    def main():
    
        client = ACS_CLIENT
        eip = Eip(client)
    
        params = {}
    
        # 创建EIP
        eip_response_json = eip.allocate_eip_address(params)
        CommonUtil.log("allocate_eip_address", eip_response_json)
    
        params['allocation_id'] = eip_response_json["AllocationId"]
    
        #修改EIP带宽到50
        params['name'] = EIP_NEW_NAME
        params['bandwidth'] = BANDWIDTH_50
        eip_response_json = eip.modify_eip_address(params)
        CommonUtil.log("modify_eip_address", eip_response_json)
    
        # 查询EIP
        eip_response_json = eip.describe_eip_address(params['allocation_id'])
        CommonUtil.log("describe_eip_address", eip_response_json)
    
        # 修改EIP带宽到10
        params['bandwidth'] = BANDWIDTH_10
        eip_response_json = eip.modify_eip_address(params)
        CommonUtil.log("modify_eip_address", eip_response_json)
    
        # 查询EIP
        eip_response_json = eip.describe_eip_address(params['allocation_id'])
        CommonUtil.log("describe_eip_address", eip_response_json)
    
        # 释放EIP
        eip_response_json = eip.release_eip_address(params)
        CommonUtil.log("release_eip_address", eip_response_json)
    
    if __name__ == '__main__':
        sys.exit(main())
  5. 进入eip_modify_attribute.py所在的目录,执行如下命令,修改EIP的带宽峰值。
    python eip_modify_attribute.py
    系统显示类似如下:
    ---------------------------allocate_eip_address---------------------------
    {
      "EipAddress": "47.xx.xx.225",
      "ResourceGroupId": "rg-acfm4odxxxxxxxx",
      "RequestId": "9318DD7A-F065-4EA6-9EA0-20A9C46EDADC",
      "AllocationId": "eip-bp15bzjk5djcsxxxxxxxx"
    }
    ---------------------------modify_eip_address---------------------------
    {
      "RequestId": "C39D55A1-6B47-489B-8614-FDB9736EDE73"
    }
    ---------------------------describe_eip_address---------------------------
    {
      "TotalCount": 1,
      "PageNumber": 1,
      "PageSize": 10,
      "EipAddresses": {
        "EipAddress": [
          {
            "ISP": "BGP",
            "ExpiredTime": "",
            "InternetChargeType": "PayByBandwidth",
            "IpAddress": "47.xx.xx.225",
            "AllocationId": "eip-bp15bzjk5djcsxxxxxxxx",
            "PrivateIpAddress": "",
            "Status": "Available",
            "BandwidthPackageId": "",
            "InstanceId": "",
            "InstanceRegionId": "",
            "RegionId": "cn-hangzhou",
            "AvailableRegions": {
              "AvailableRegion": [
                "cn-hangzhou"
              ]
            },
            "ResourceGroupId": "rg-acfm4odxxxxxxxx",
            "HasReservationData": false,
            "InstanceType": "",
            "AllocationTime": "2019-04-18T04:01:28Z",
            "Name": "EIP_NEW_NAME",
            "OperationLocks": {
              "LockReason": []
            },
            "Mode": "NAT",
            "BandwidthPackageType": "",
            "BandwidthPackageBandwidth": "",
            "Bandwidth": "50",
            "HDMonitorStatus": "OFF",
            "ChargeType": "PostPaid",
            "SecondLimited": false,
            "Descritpion": ""
          }
        ]
      },
      "RequestId": "51ECAB45-2518-4A46-89DC-8ADEE1AFDBE9"
    }
    ---------------------------modify_eip_address---------------------------
    {
      "RequestId": "ACAB5724-D05C-46A4-8C2B-6064AEEC792B"
    }
    ---------------------------describe_eip_address---------------------------
    {
      "TotalCount": 1,
      "PageNumber": 1,
      "PageSize": 10,
      "EipAddresses": {
        "EipAddress": [
          {
            "ISP": "BGP",
            "ExpiredTime": "",
            "InternetChargeType": "PayByBandwidth",
            "IpAddress": "47.xx.xx.225",
            "AllocationId": "eip-bp15bzjk5djcsxxxxxxxx",
            "PrivateIpAddress": "",
            "Status": "Available",
            "BandwidthPackageId": "",
            "InstanceId": "",
            "InstanceRegionId": "",
            "RegionId": "cn-hangzhou",
            "AvailableRegions": {
              "AvailableRegion": [
                "cn-hangzhou"
              ]
            },
            "ResourceGroupId": "rg-acfm4odxxxxxxxx",
            "HasReservationData": false,
            "InstanceType": "",
            "AllocationTime": "2019-04-18T04:01:28Z",
            "Name": "EIP_NEW_NAME",
            "OperationLocks": {
              "LockReason": []
            },
            "Mode": "NAT",
            "BandwidthPackageType": "",
            "BandwidthPackageBandwidth": "",
            "Bandwidth": "10",
            "HDMonitorStatus": "OFF",
            "ChargeType": "PostPaid",
            "SecondLimited": false,
            "Descritpion": ""
          }
        ]
      },
      "RequestId": "6F653A80-AB28-4842-84A8-CD444EB81A29"
    }
    ---------------------------release_eip_address---------------------------
    {
      "RequestId": "C407633A-5658-482F-AB5E-069028C3B06C"
    }