This topic describes how to use Alibaba Cloud SDK for Python to associate an elastic IP address (EIP) with an EIP bandwidth plan.
Prerequisites
Before you use Alibaba Cloud SDK for Python, make sure that the following requirements are met:
- An Alibaba Cloud account and the AccessKey pair are obtained. You can create and view your AccessKey pair on the Security Management page of the Alibaba Cloud Management Console.
- Alibaba Cloud SDK for Python is installed.
- The VPC Python example library is downloaded. Go to the directory where the setup.py file is stored, and run the following command to initialize the environment:
python setup.py install
Background information
The sample code in this topic includes the following operations:- Create an EIP in the China (Hangzhou) region.
- Create an EIP bandwidth plan in the China (Hangzhou) region.
- Associate the EIP with the EIP bandwidth plan.
- Query the EIP bandwidth plan.
- Disassociate the EIP from the EIP bandwidth plan.
- Delete the EIP bandwidth plan.
- Release the EIP.
Procedure
- In the directory of the downloaded SDK files, open the aliyun-openapi-python-sdk-examples\sdk_examples\examples\eip folder.
- Open the eip_add_cbwp.py file in your text editor. Set the required parameters, save the configurations, and then exit the editor. The following sample code is displayed:
#encoding=utf-8 import sys from alibabacloud_credentials.client import Client as CredClient 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 * """ 1. Create an EIP. 2. Create an EIP bandwidth plan. 3. Query the ID of the EIP bandwidth plan. 4. Associate the EIP with the EIP bandwidth plan. 5. Query the EIP bandwidth plan. 6. Disassociate the EIP from the EIP bandwidth plan. 7. Delete the EIP bandwidth plan. 8. 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 EIP bandwidth plan. """ try: request = CreateCommonBandwidthPackageRequest.CreateCommonBandwidthPackageRequest() # The maximum bandwidth value of the EIP bandwidth plan. Unit: Mbit/s. 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: Associate the EIP with the EIP bandwidth plan. """ try: request = AddCommonBandwidthPackageIpRequest.AddCommonBandwidthPackageIpRequest() # The ID of the EIP. request.set_IpInstanceId(params['ip_instance_id']) # The ID of the EIP bandwidth plan. 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 information about the EIP bandwidth plan. """ try: request = DescribeCommonBandwidthPackagesRequest.DescribeCommonBandwidthPackagesRequest() # The ID of the EIP bandwidth plan. 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_status: Query the status of the EIP bandwidth plan. """ # The ID of the EIP bandwidth plan. response = self.describe_cbwp(cbwp_id) return response["CommonBandwidthPackages"]["CommonBandwidthPackage"][0]["Status"] def remove_common_bandwidth_packageIp(self, params): """ remove_common_bandwidth_packageIp: Disassociate the EIP from the EIP bandwidth plan. """ try: request = RemoveCommonBandwidthPackageIpRequest.RemoveCommonBandwidthPackageIpRequest() # The ID of the EIP. request.set_IpInstanceId(params['ip_instance_id']) # The ID of the EIP bandwidth plan. 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 EIP bandwidth plan. """ try: request = DeleteCommonBandwidthPackageRequest.DeleteCommonBandwidthPackageRequest() # The ID of the EIP bandwidth plan. request.set_BandwidthPackageId(params['bandwidth_package_id']) # Specify whether to forcibly delete the EIP bandwidth plan. 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(): # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. # We do not recommend that you save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of your resources may be compromised. # In this example, the AccessKey pair is obtained by using the Alibaba Cloud Credentials tool to authenticate API access. For more information about how to configure the variables, see https://www.alibabacloud.com/help/alibaba-cloud-sdk-262060/latest/configure-credentials-378659. cred = CredClient() access_key_id = cred.get_access_key_id() access_key_secret = cred.get_access_key_secret() # Create an AcsClient instance. client = AcsClient(access_key_id, access_key_secret, '<your-region-id>') 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 EIP bandwidth plan. 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) # Associate the EIP with the EIP bandwidth plan. 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 EIP bandwidth plan. cbwp_repsonse_json = cbwp.describe_cbwp(params['bandwidth_package_id']) CommonUtil.log("add_common_bandwidth_packageIp", cbwp_repsonse_json) # Disassociate the EIP from the EIP bandwidth plan. cbwp_repsonse_json = cbwp.remove_common_bandwidth_packageIp(params) CommonUtil.log("remove_common_bandwidth_packageIp", cbwp_repsonse_json) # Delete the EIP bandwidth plan. 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())
- Go to the directory where the eip_add_cbwp.py file is stored, and run the following command to associate the EIP with the EIP bandwidth plan:
python eip_add_cbwp.py
Result
The following output is returned:
---------------------------allocate_eip_address---------------------------
{
"EipAddress": "118.XX.XX.198",
"ResourceGroupId": "rg-acfm4od****",
"RequestId": "A830A607-B7C4-49FE-A6EE-7237D64CDE2D",
"AllocationId": "eip-bp1mdyvr22qvg****"
}
---------------------------create_common_bandwidth_package----------------------
-----
{
"ResourceGroupId": "rg-acfm4od****",
"BandwidthPackageId": "cbwp-bp12k058pjjie****",
"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-bp1mdyvr22qvg****"
}
]
},
"BusinessStatus": "Normal",
"RegionId": "cn-hangzhou",
"BandwidthPackageId": "cbwp-bp12k058pjjie****",
"Name": "",
"ISP": "BGP",
"CreationTime": "2019-04-18T01:46:17Z",
"ResourceGroupId": "rg-acfm4od****",
"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"
}