このトピックでは、Alibaba Cloud SDK for Pythonを使用してEIP (elastic IP address) の最大帯域幅を変更する方法について説明します。
前提条件
Alibaba Cloud SDK for Pythonを使用する前に、次の要件が満たされていることを確認してください。
- Alibaba CloudアカウントとAccessKeyペアが取得されます。 Alibaba Cloud管理コンソールの [セキュリティ管理] ページで、AccessKeyペアを作成して表示できます。
- Alibaba Cloud SDK for Pythonがインストールされています。
- VPC Pythonサンプルライブラリがダウンロードされます。 setup.pyファイルが保存されているディレクトリに移動し、次のコマンドを実行して環境を初期化します。
python setup.py install
背景情報
このトピックのサンプルコードには、次の操作が含まれます。- 中国 (杭州) リージョンでEIPを作成します。
- EIPの最大帯域幅値を50 Mbit/sに設定します。
- 最大帯域幅値を変更したEIPを照会します。
- EIPの最大帯域幅値を10 Mbit/sに設定します。
- 最大帯域幅値を変更したEIPを照会します。
- EIPをリリースします。
手順
- ダウンロードしたSDKファイルのディレクトリで、aliyun-openapi-python-sdk-examples\sdk_examples\examples\eipフォルダーを開きます。
- テキストエディターでeip_modify_attribute.pyファイルを開きます。 必要なパラメーターを設定し、設定を保存してから、エディターを終了します。 次のサンプルコードが表示されます。
#encoding=utf-8 インポートsys jsonのインポート alibabacloud_credentials.clientからCredClientとしてのインポートクライアント からaliyunsdkcore.acs_exception.exceptions import ServerException, ClientException aliyunsdkvpc.request.v20160428からAllocateEipAddressRequestをインポート aliyunsdkvpc.request.v20160428からAssociateEipAddressRequestをインポート aliyunsdkvpc.request.v20160428からDescribeEipAddressesRequestをインポート aliyunsdkvpc.request.v20160428からUnassociateEipAddressRequestをインポート aliyunsdkvpc.request.v20160428からModifyEipAddressAttributeRequestをインポート aliyunsdkvpc.request.v20160428からReleaseEipAddressRequestをインポート sdk_lib.exception import ExceptionHandlerから sdk_lib.check_statusからCheckStatusをインポートする sdk_lib.consts import * から sdk_lib.common_utilインポートからCommonUtil """ 1. EIP を作成します。 2. EIPの最大帯域幅を50 Mbit/sに設定します。 3. EIPを照会します。 4. EIPの最大帯域幅を10 Mbit/sに設定します。 5. EIPを照会します。 6. EIPをリリースします。 """ クラスEip (オブジェクト): def __init__(self, client): self.client=クライアント def allocate_eip_address(self, params): """ allocate_eip_address: EIPを申請します。 """ 試してみてください。 request = AllocateEipAddressRequest。AllocateEipAddressRequest() response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME, self.de scribe_eip_status、 利用可能、response_json["AllocationId"]): 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を関連付けます。 """ 試してみてください。 request = AssociateEipAddressRequest。AssociateEipAddressRequest() # EIPのID。 request.set_AllocationId(params['allocation_id']) # EIPを関連付けるクラウドリソースのタイプ。 request.set_InstanceType(params['instance_type']) # EIPを関連付けるクラウドリソースのID。 request.set_InstanceId(params['instance_id']) response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME, self.de scribe_eip_status、 InUse、params['allocation_id']): 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を照会します。 """ 試してみてください。 request = DescribeEipAddressesRequest.DescribeEipAddressesRequest() # EIPのID。 request.set_AllocationId(allocation_id) response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) 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のステータスを照会します。 """ # EIPのID。 response = self.de scribe_eip_address(allocation_id) リターンレスポンス ["EipAddresses"]["EipAddress"][0]["Status"] def unassociate_eip_address(self, params): """ unassociate_eip_address: 関連するクラウドリソースからEIPの関連付けを解除します。 """ 試してみてください。 request = UnassociateEipAddressRequest。UnassociateEipAddressRequest() # EIPのID。 request.set_AllocationId(params['allocation_id']) # EIPの関連付けを解除するクラウドリソースのタイプ。 request.set_InstanceType(params['instance_type']) # EIPの関連付けを解除するクラウドリソースのID。 request.set_InstanceId(params['instance_id']) response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME, self.de scribe_eip_status、 利用可能、params['allocation_id']): response_jsonを返します 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の名前、説明、および最大帯域幅値を変更します。 """ 試してみてください。 request = ModifyEipAddressAttributeRequest。ModifyEipAddressAttributeRequest() # EIPのID。 request.set_AllocationId(params['allocation_id']) # EIPの最大帯域幅値。 単位:Mbit/秒。 request.set_Bandwidth(params['bandwidth']) # EIPの名前。 request.set_Name(params['name']) response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) 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をリリースします。 """ 試してみてください。 request = ReleaseEipAddressRequest。ReleaseEipAddressRequest() # リリースするEIPのID。 request.set_AllocationId(params['allocation_id']) response = self.client.do_action_with_exception(request) response_json = json.loads (レスポンス) response_jsonを返します except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def main(): # Alibaba CloudアカウントのAccessKeyペアを使用すると、アカウントにすべてのAPI操作に対する権限があるため、セキュリティ上のリスクが発生する可能性があります。 RAMユーザーを使用してAPI操作を呼び出したり、ルーチンのO&Mを実行することを推奨します。 # AccessKey IDとAccessKey secretをプロジェクトコードに保存することはお勧めしません。 そうしないと、AccessKeyペアが漏洩し、リソースのセキュリティが侵害される可能性があります。 # この例では、AccessKeyペアは、Alibaba Cloud Credentialsツールを使用してAPIアクセスを認証することによって取得されます。 変数の設定方法の詳細については、「https://help.aliyun.com/document_detail/378659.htmlhttps:// 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 (クライアント) 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 Mbit/sに設定します。 params['name'] = EIP_NEW_NAME params['bandwidth'] = BANDWIDTH_50 eip_response_json = eip.mo dify_eip_address(params) CommonUtil.log("modify_eip_address", eip_response_json) # EIPを照会します。 eip_response_json = eip.de scribe_eip_address(params['allocation_id']) CommonUtil.log("describe_eip_address", eip_response_json) # EIPの帯域幅の値を10 Mbit/sに設定します。 params['bandwidth'] = BANDWIDTH_10 eip_response_json = eip.mo dify_eip_address(params) CommonUtil.log("modify_eip_address", eip_response_json) # EIPを照会します。 eip_response_json = eip.de scribe_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())
- eip_modify_attribute.pyファイルが保存されているディレクトリに移動し、次のコマンドを実行してEIPの最大帯域幅を変更します。
python eip_modify_attribute.py
結果
次の出力が返されます。
--------------------------- allocate_eip_address ---------------------------
{
"EipAddress": "47.XX. XX.225" 、
"ResourceGroupId": "rg-acfm4od ****" 、
"RequestId": "9318DD7A-F065-4EA6-9EA0-20A9C46EDADC" 、
"AllocationId": "eip-bp15bzjk5djcs ****"
}
--------------------------- 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-bp15bzjk5djcs ****" 、
"PrivateIpAddress": "" 、
"ステータス": "利用可能" 、
"BandwidthPackageId": "" 、
"InstanceId": "" 、
"InstanceRegionId": "" 、
"RegionId": "cn-hangzhou"、
"AvailableRegions": {
"AvailableRegion": [
"cn-hangzhou"
]
},
"ResourceGroupId": "rg-acfm4od ****" 、
"HasReservationData": false、
"InstanceType": "" 、
"AllocationTime": "2019-04-18T04:01:28Z" 、
"名前": "EIP_NEW_NAME" 、
"OperationLocks": {
"LockReason": []
},
"モード": "NAT" 、
"BandwidthPackageType": "" 、
"BandwidthPackageBandwidth": "" 、
"帯域幅": "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-bp15bzjk5djcs ****" 、
"PrivateIpAddress": "" 、
"ステータス": "利用可能" 、
"BandwidthPackageId": "" 、
"InstanceId": "" 、
"InstanceRegionId": "" 、
"RegionId": "cn-hangzhou"、
"AvailableRegions": {
"AvailableRegion": [
"cn-hangzhou"
]
},
"ResourceGroupId": "rg-acfm4od ****" 、
"HasReservationData": false、
"InstanceType": "" 、
"AllocationTime": "2019-04-18T04:01:28Z" 、
"名前": "EIP_NEW_NAME" 、
"OperationLocks": {
"LockReason": []
},
"モード": "NAT" 、
"BandwidthPackageType": "" 、
"BandwidthPackageBandwidth": "" 、
"帯域幅": "10" 、
"HDMonitorStatus": "OFF" 、
"ChargeType": "PostPaid" 、
"SecondLimited": false、
"Descritpion": "
}
]
},
"RequestId": "6F653A80-AB28-4842-84A8-CD444EB81A29"
}
--------------------------- release_eip_address
{
"RequestId": "C407633A-5658-482F-AB5E-069028C3B06C"
}