このトピックでは、Alibaba Cloud Python SDK を使用して、EIP を共有帯域幅パッケージに追加する方法について説明します。

このタスクについて

この例では、次のワークフローを使用して、EIP を共有帯域幅パッケージに追加します。
  1. 中国 (杭州) リージョンで EIP を作成します。
  2. 中国 (杭州) リージョンで共有帯域幅パッケージを作成します。
  3. EIP を共有帯域幅パッケージに追加します。
  4. 共有帯域幅パッケージをクエリします。
  5. 共有帯域幅パッケージから EIP を削除します。
  6. 共有帯域幅パッケージを削除します。
  7. 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_add_cbwp.py ファイルを開き、必要に応じてパラメーターを設定します。 その後、設定を保存してエディターを終了します。
    #encoding=utf-8
    import sys
    from aliyunsdkvpc.request.v20160428 import CreateCommonBandwidthPackageRequest
    from aliyunsdkvpc.request.v20160428 import AddCommonBandwidthPackageIpRequest
    from aliyunsdkvpc.request.v20160428 import DescribeCommonBandwidthPackagesRequest
    from aliyunsdkvpc.request.v20160428 import RemoveCommonBandwidthPackageIpRequest
    from aliyunsdkvpc.request.v20160428 import DeleteCommonBandwidthPackageRequest
    from sdk_lib.common_util import CommonUtil
    from sdk_lib.sdk_eip import *
    
    """
    Create an EIP -> Create an Internet Shared Bandwidth -> Query the Internet Shared Bandwidth ID -> Add the EIP to the Internet Shared Bandwidth -> Query the Internet Shared Bandwidth -> Remove the EIP from the Internet Shared Bandwidth -> Delete the Internet Shared Bandwidth -> Release the EIP
    """
    class CommonBandwidthPackage(object):
        def __init__(self, client):
            self.client = client
    
        def create_common_bandwidth_package(self, params):
            """
            create_common_bandwidth_package: Create an Internet Shared Bandwidth.
            API reference link: https://www.alibabacloud.com/help/doc-detail/55930.htm
            """
            try:
                request = CreateCommonBandwidthPackageRequest.CreateCommonBandwidthPackageRequest()
                # The peak value of the Internet Shared Bandwidth (Mbps)
                request.set_Bandwidth(params['bandwidth'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME,
                                            self.describe_cbwp_status,
                                            AVAILABLE, response_json["BandwidthPackageId"]):
                    return response_json
            except ServerException as e:
                ExceptionHandler.server_exception(e)
            except ClientException as e:
                ExceptionHandler.client_exception(e)
    
        def add_common_bandwidth_packageIp(self, params):
            """
            add_common_bandwidth_packageIp: Add the EIP to the Internet Shared Bandwidth.
            API reference link: https://www.alibabacloud.com/help/doc-detail/55989.htm
            """
            try:
                request = AddCommonBandwidthPackageIpRequest.AddCommonBandwidthPackageIpRequest()
                # The ID of the instance with which the EIP is associated
                request.set_IpInstanceId(params['ip_instance_id'])
                # The ID of the Internet Shared Bandwidth
                request.set_BandwidthPackageId(params['bandwidth_package_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_cbwp(self, cbwp_id):
            """
            describe_cbwp: Query the Internet Shared Bandwidth instance.
            API reference link: https://www.alibabacloud.com/help/doc-detail/55997.htm
            """
            try:
                request = DescribeCommonBandwidthPackagesRequest.DescribeCommonBandwidthPackagesRequest()
                # The ID of the Internet Shared Bandwidth instance
                request.set_BandwidthPackageId(cbwp_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_cbwp_status(self, cbwp_id):
            """
            describe_cbwp: Query the status of the Internet Shared Bandwidth instance.
            API reference link: https://www.alibabacloud.com/help/doc-detail/55997.htm
            """
            # The ID of the Internet Shared Bandwidth instance
            response = self.describe_cbwp(cbwp_id)
            return response["CommonBandwidthPackages"]["CommonBandwidthPackage"][0]["Status"]
    
    
        def remove_common_bandwidth_packageIp(self, params):
            """
            remove_common_bandwidth_packageIp: Remove the EIP from the Internet Shared Bandwidth.
            API reference link: https://www.alibabacloud.com/help/doc-detail/55995.htm
            """
            try:
                request = RemoveCommonBandwidthPackageIpRequest.RemoveCommonBandwidthPackageIpRequest()
                # The ID of the instance with which the EIP is associated
                request.set_IpInstanceId(params['ip_instance_id'])
                # The ID of the Internet Shared Bandwidth instance
                request.set_BandwidthPackageId(params['bandwidth_package_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 delete_common_bandwidth_package(self, params):
            """
            delete_common_bandwidth_package: Delete the Internet Shared Bandwidth.
            API reference link: https://www.alibabacloud.com/help/doc-detail/56000.htm
            """
            try:
                request = DeleteCommonBandwidthPackageRequest.DeleteCommonBandwidthPackageRequest()
                # The ID of the Internet Shared Bandwidth instance
                request.set_BandwidthPackageId(params['bandwidth_package_id'])
                # Determine whether to forcibly delete the Internet Shared Bandwidth instance.
                request.set_Force(params['force'])
                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)
        cbwp = CommonBandwidthPackage(client)
    
        params = {}
    
        # Create an EIP.
        eip_response_json = eip.allocate_eip_address(params)
        CommonUtil.log("allocate_eip_address", eip_response_json)
        params['allocation_id'] = eip_response_json["AllocationId"]
    
        # Create an Internet Shared Bandwidth.
        params['allocation_id'] = eip_response_json["AllocationId"]
        params['ip_instance_id'] = eip_response_json["AllocationId"]
        params['bandwidth'] = BANDWIDTH_10
        cbwp_repsonse_json = cbwp.create_common_bandwidth_package(params)
        CommonUtil.log("create_common_bandwidth_package", cbwp_repsonse_json)
    
        # Add the EIP to the Internet Shared Bandwidth.
        params['bandwidth_package_id'] = cbwp_repsonse_json['BandwidthPackageId']
        cbwp_repsonse_json = cbwp.add_common_bandwidth_packageIp(params)
        CommonUtil.log("add_common_bandwidth_packageIp", cbwp_repsonse_json)
    
        # Query the Internet Shared Bandwidth.
        cbwp_repsonse_json = cbwp.describe_cbwp(params['bandwidth_package_id'])
        CommonUtil.log("add_common_bandwidth_packageIp", cbwp_repsonse_json)
    
        # Remove the EIP from the Internet Shared Bandwidth.
        cbwp_repsonse_json = cbwp.remove_common_bandwidth_packageIp(params)
        CommonUtil.log("remove_common_bandwidth_packageIp", cbwp_repsonse_json)
    
        # Delete the Internet Shared Bandwidth.
        params['force'] = True
        cbwp_repsonse_json = cbwp.delete_common_bandwidth_package(params)
        CommonUtil.log("delete_common_bandwidth_package", cbwp_repsonse_json)
    
        # Release the 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_add_cbwp.py ファイルが存在するディレクトリに移動し、次のコマンドを実行して EIP を共有帯域幅パッケージに追加します。
    python eip_add_cbwp.py
    次の出力が表示されます。
    ---------------------------allocate_eip_address---------------------------
    {
      "EipAddress": "118.xx.xx.198",
      "ResourceGroupId": "rg-acfm4odxxxxxxxx",
      "RequestId": "A830A607-B7C4-49FE-A6EE-7237D64CDE2D",
      "AllocationId": "eip-bp1mdyvr22qvgxxxxxxxx"
    }
    ---------------------------create_common_bandwidth_package----------------------
    -----
    {
      "ResourceGroupId": "rg-acfm4odxxxxxxxx",
      "BandwidthPackageId": "cbwp-bp12k058pjjiexxxxxxxx",
      "RequestId": "93127320-DD79-4F83-A3B9-DC99D0597B0C"
    }
    ---------------------------add_common_bandwidth_packageIp-----------------------
    ----
    {
      "RequestId": "7F314AFE-B398-4348-AF61-B7D27B731286"
    }
    ---------------------------add_common_bandwidth_packageIp-----------------------
    ----
    {
      "TotalCount": 1,
      "CommonBandwidthPackages": {
        "CommonBandwidthPackage": [
          {
            "Status": "Available",
            "PublicIpAddresses": {
              "PublicIpAddresse": [
                {
                  "IpAddress": "118.xx.xx.198",
                  "AllocationId": "eip-bp1mdyvr22qvgxxxxxxxx"
                }
              ]
            },
            "BusinessStatus": "Normal",
            "RegionId": "cn-hangzhou",
            "BandwidthPackageId": "cbwp-bp12k058pjjiexxxxxxxx",
            "Name": "",
            "ISP": "BGP",
            "CreationTime": "2019-04-18T01:46:17Z",
            "ResourceGroupId": "rg-acfm4odxxxxxxxx",
            "Bandwidth": "10",
            "InstanceChargeType": "PostPaid",
            "HasReservationData": false,
            "InternetChargeType": "PayByBandwidth",
            "ExpiredTime": "",
            "Ratio": 100,
            "Description": ""
          }
        ]
      },
      "PageNumber": 1,
      "RequestId": "015DD0FA-742B-4431-92EA-E3F03FDEB8CD",
      "PageSize": 10
    }
    ---------------------------remove_common_bandwidth_packageIp--------------------
    -------
    {
      "RequestId": "A49C9126-B703-4D34-B552-A7FE283FB5DD"
    }
    ---------------------------delete_common_bandwidth_package----------------------
    -----
    {
      "RequestId": "E423F648-C169-4B63-A2CF-5E6C8E441DE1"
    }
    ---------------------------release_eip_address---------------------------
    {
      "RequestId": "7E0D34AE-58C3-468A-B021-378F8938AE6B"
    }