本文介绍如何使用Alibaba Cloud SDK for Python创建和删除终端节点。

前提条件

在使用Alibaba Cloud SDK for Python前,您需要完成以下准备工作:
  • 使用Alibaba Cloud SDK for Python,您需要一个阿里云账号和访问密钥(AccessKey)。请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Python。更多信息,请参见aliyun-python-sdk-privatelink 1.0.1
  • 进入setup.py所在的目录,执行如下命令,完成环境初始化配置。
    python setup.py install

背景信息

在德国(法兰克福)地域创建一个终端节点。终端节点创建成功后,删除终端节点。

完整代码

本文的完整代码如下所示,您可以自行根据实际情况修改和设置参数取值。
#!/usr/bin/env python
#coding=utf-8
#创建一个终端节点
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkprivatelink.request.v20200415.CreateVpcEndpointRequest import CreateVpcEndpointRequest

client = AcsClient('<accessKeyId>', '<accessSecret>', 'eu-central-1')
request = CreateVpcEndpointRequest()
request.set_accept_format('json')request.set_VpcId("vpc-hp356stwkxg3fn2xe****")

request.set_SecurityGroupIds(["sg-hp33bw6ynvm2yb0e****"])
request.set_ServiceId("epsrv-hp3xdsq46ael67lo****")
request.set_ClientToken("0c593ea1-3bea-11e9-b96b-88e9fe637760")
request.set_DryRun(False)
request.set_EndpointName("test")
request.set_EndpointDescription("This is my Endpoint.")
request.set_ServiceName("com.aliyuncs.privatelink.cn-huhehaote.epsrv-hp3vpx8yqxblby3i****")
request.set_Zones([
  {    "VSwitchId": "vsw-gw8frrm3q5zrkx1******",
    "ZoneId": "Frankfurt Zone A"  }
])

response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))

#删除一个终端节点
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkprivatelink.request.v20200415.DeleteVpcEndpointRequest import DeleteVpcEndpointRequest

client = AcsClient('<accessKeyId>', '<accessSecret>', 'eu-central-1')
request = DeleteVpcEndpointRequest()
request.set_accept_format('json')
request.set_EndpointId("ep-hp33b2e43fays7s8****")
request.set_ClientToken("0c593ea1-3bea-11e9-b96b-88e9fe637760")
request.set_DryRun(False)

response = client.do_action_with_exception(request)
# python2:  print(response) 
print(str(response, encoding='utf-8'))
系统显示执行结果如下:
---------------------------create_endpoint---------------------------
{
  "EndpointName": "test-doc-endpoint",
  "VpcId": "vpc-hp356stwkxg3fn2xe****",
  "VSwitchId": "vsw-gw8frrm3q5zrkx1******",
  "ZoneId": "Frankfurt Zone A",
  "SecurityGroupIds": "sg-hp33bw6ynvm2yb0e****"
  "ServiceId": "epsrv-hp3xdsq46ael67lo****"
  "ServiceName": "com.aliyuncs.privatelink.cn-huhehaote.epsrv-hp3vpx8yqxblby3i****"
  "RequestId": "5052F988-75CC-46AD-A1A6-0E9E445BD0D5"
}
---------------------------delete_endpoint---------------------------
{
  "EndpointId": "ep-hp33b2e43fays7s8****",
  "RequestId": "5052F988-75CC-46AD-A1A6-0E9E445BD0D5"
}