前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >腾讯云API弹性公网IP踩坑

腾讯云API弹性公网IP踩坑

原创
作者头像
用户2174879
修改2018-10-24 09:49:29
31.1K1
修改2018-10-24 09:49:29
举报
文章被收录于专栏:wujianqinjian的云

由于自己管理的云服务器数量比较多,时不时需要更换IP,在管理台上一下下点击,实在浪费时间,于是就想到了通过API调用的方式,将更换IP一系列动作,全部集成到Python代码里面,实现一行命令,完成IP更换,由于人懒,就先

把最核心的代码(虽然都是腾讯云生成的)、流程、坑点贴出来,仅供菜鸟参考,高手无视!

具体步骤:

一 进入 https://cloud.tencent.com/document/api ,

页面左侧列表查找“私有网络”---“弹性公网相关接口”,就可以看到对应接口的文档了

二 选择一个接口,然后点击“API 3.0 Exploper”,进入到开发者工具页面,

坑点一:第二张图,对于菜鸟来说,引入的相关模块直接用“pip install 对应模块名”会报错

看第一张图sdk,相关语言sdk,点击就可以看到github上各语言的库引用方式

三 直接copy开发者工具上的代码,当然你也可以看下图代码

坑点二:"SecretId ","SecretKey" 这两个值是你调用API,腾讯用来确认你身份的凭证,

在哪里申请呢?腾讯云的首页,“云产品”--“管理工具”--“云API秘钥”,

没有使用过的话,是不会在你的控制台上显示的。

坑点三:由于腾讯云API文档的不合理,导致生成的代码有一些坑,

具体坑信息我已在代码里通过注释的方式解释了

代码语言:javascript
复制
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.vpc.v20170312 import vpc_client, models

#查询弹性IP
def findIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DescribeAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.DescribeAddresses(req)
        #eip=resp.to_json_string()[34:61]
        eip=resp.to_json_string()[48:60]
        print(eip)   #打印结果:"AddressId": "eip-ilzg91oy"
        return eip
    except TencentCloudSDKException as err:
        print(err)

#解除绑定IP
def unbindingIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.DisassociateAddressRequest()
        eip = findIp()
        params = eip
        #req.from_json_string(params)
        req.AddressId=params   #这里修改了一下,官网生成的是上一行代码
        resp = client.DisassociateAddress(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)
#释放IP
def releaseIp():
    try:
        cred = credential.Credential("SecretId ", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)

        req = models.DescribeAddressesRequest()
        eip = findIp()
        list=[]
        list.append(eip)
        params = list
        req.AddressIds=params  #这里修改了一下,要求传数组
        resp = client.ReleaseAddresses(req)
        print(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#创建IP
def newIp():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)
        req = models.AllocateAddressesRequest()
        params = '{}'
        req.from_json_string(params)
        resp = client.AllocateAddresses(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)

#绑定弹性IP
def binding():
    try:
        cred = credential.Credential("SecretId", "SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "vpc.ap-chongqing.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = vpc_client.VpcClient(cred, "ap-chongqing", clientProfile)

        req = models.AssociateAddressRequest()
        #params = '{"ins-61lwor90"}'
        #req.from_json_string(params)
        #这是只传AddressId的报错:MissingAssociateEntity message:You need to specify the entity ID `"InstanceId"` or `"NetworkInterfaceId"` to associate this address. requestId:81702256-e75f-458f-afde-e87a69554f83
        #所以至少要传两个值
        req.InstanceId = "ins-61cwor70"
        eip = findIp()
        req.AddressId=eip
        resp = client.AssociateAddress(req)
        print(resp.to_json_string())

    except TencentCloudSDKException as err:
        print(err)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云 API
云 API 是腾讯云开放生态的基石。通过云 API,只需少量的代码即可快速操作云产品;在熟练的情况下,使用云 API 完成一些频繁调用的功能可以极大提高效率;除此之外,通过 API 可以组合功能,实现更高级的功能,易于自动化, 易于远程调用, 兼容性强,对系统要求低。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档