提供Discuz ,ECShop ,PHPCMS ,帝国CMS ,CSS教程 ,PHP教程 ,DedeCMS ,WordPress ,HTML教程等cms问题查询.
当前位置: 运维 > Linux > 怎样直接经由过程 API 举行续费查询和续费治理【Linux】,Linux

本篇文章给人人带来的内容是关于怎样直接经由过程 API 举行续费查询和续费治理,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

ECS 实例续费

本文重要触及以下症结功用:

依据逾期时刻查询云服务器

续费实例

查询云服务器自动续费时刻

设置云服务器自动续费时刻

关于包年包月的云服务器,生命周期非常重要。假如云服务器资本不能定时续费,将能够致使服务器被锁定以至被开释,从而影响营业持续性。API 协助您实时相识和搜检资本的到期时刻,并完成续费充值功用。

本篇需关注以下 API:

查询实例列表

续费实例

查询指定范围内到期的云服务器

查询实例列表的 API,经由过程过滤参数,您能够查询肯定时刻范围内到期的实例信息。经由过程设置过滤参数 ExpiredStartTime 和 ExpiredEndTime(时刻参数 依据 ISO8601 规范示意,并须要运用 UTC 时刻。 花样为:yyyy-MM-ddTHH:mmZ。) ,能够方便地查询该时刻范围内到期的实例列表。假如须要经由过程安全组举行过滤,只需加上安全组 ID 即可。

INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z'
INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z'
def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None,
                                 check_need_renew=True, security_group_id=None):
    request = DescribeInstancesRequest()
    if check_need_renew is True:
        request.set_Filter3Key("ExpiredStartTime")
        request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING)
        request.set_Filter4Key("ExpiredEndTime")
        request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING)
    if instance_id is not None:
        request.set_InstanceIds(json.dumps([instance_id]))
    if security_group_id:
        request.set_SecurityGroupId(security_group_id)
    request.set_PageNumber(page_number)
    request.set_PageSize(page_size)
    return _send_request(request)

续费云服务器

续费实例只支撑包年包月的服务器范例,不支撑按量付费的服务器,同时请求用户必需支撑账号的余额付出或信用付出。实行 API 的时刻将实行同步的扣费和定单生成。因而,实行 API 的时刻必需保证您的账号有足够的资金支撑自动扣费。

def _renew_instance_action(instance_id, period='1'):
    request = RenewInstanceRequest()
    request.set_Period(period)
    request.set_InstanceId(instance_id)
    response = _send_request(request)
    logging.info('renew %s ready, output is %s ', instance_id, response)

续费实例将会自动完成扣费。在完成续费后,您能够依据InstanceId查询实例的资本到期时刻。因为 API 为异步使命,查询资本到期时刻能够须要耽误 10 秒才会变化。

开启云服务器自动续费

为了削减您的资本到期保护本钱,针对包年包月的 ECS 实例,阿里云还推出了自动续费功用。 自动续费扣款日为服务器到期前第 9 天的 08:00:00。假如前一日实行自动扣费失利,将会继承下一日定时实行,直到完成扣费或许 9 天后到期资本锁定。您只须要保证本身的账号余额或许信用额度足够即可。

查询自动续费设置

您能够经由过程 OpenAPI 来查询和设置自动续费。该 API 仅支撑包年包月的实例,按量付费的实例实行将会报错。查询实例的自动续费状况支撑一次最多查询 100 个包年包月的实例,多个实例 ID 以逗号衔接。

DescribeInstanceAutoRenewAttribut 的入参为实例 ID.

InstanceId:支撑最多查询 100 个包年包月的实例,多个实例 ID 以逗号衔接。

python # check the instances is renew or not def describe_auto_renew(instance_ids, expected_auto_renew=True): describe_request = DescribeInstanceAutoRenewAttributeRequest() describe_request.set_InstanceId(instance_ids) response_detail = _send_request(request=describe_request) failed_instance_ids = '' if response_detail is not None: attributes = response_detail.get('InstanceRenewAttributes').get('InstanceRenewAttribute') if attributes: for item in attributes: auto_renew_status = item.get('AutoRenewEnabled') if auto_renew_status != expected_auto_renew: failed_instance_ids += item.get('InstanceId') + ',' describe_auto_renew('i-1111,i-2222')

返回内容以下:

{"InstanceRenewAttributes":{"InstanceRenewAttribute":
[{"Duration":0,"InstanceId":"i-1111","AutoRenewEnabled":false},
{"Duration":0,"InstanceId":"i-2222","AutoRenewEnabled":false}]},
"RequestId":"71FBB7A5-C793-4A0D-B17E-D6B426EA746A"}

假如设置自动续费,则返回的属性AutoRenewEnabled为 true,不然返回 false。

设置和作废云服务器的自动续费

设置自动续费有三个入参:

InstanceId: 支撑最多查询100个包年包月的实例,多个实例 ID 以逗号衔接。

Duration:支撑 1、2、3、6、12,单元为月。

AutoRenew:true/false, true为开启自动续费,false为作废自动续费。

python def setting_instance_auto_renew(instance_ids, auto_renew = True): logging.info('execute enable auto renew ' + instance_ids) request = ModifyInstanceAutoRenewAttributeRequest(); request.set_Duration(1); request.set_AutoRenew(auto_renew); request.set_InstanceId(instance_ids) _send_request(request)

实行胜利返回 Response 以下:

python {"RequestId":"7DAC9984-AAB4-43EF-8FC7-7D74C57BE46D"}

续费胜利后,您能够再实行一次查询。假如续费胜利将返回续费时长以及是不是开启自动续费。

python {"InstanceRenewAttributes":{"InstanceRenewAttribute":[{"Duration":1,"InstanceId":"i-1111","AutoRenewEnabled":true},{"Duration":1,"InstanceId":"i-2222","AutoRenewEnabled":true}]},"RequestId":"7F4D14B0-D0D2-48C7-B310-B1DF713D4331"}

完全的代码以下:

#  coding=utf-8
# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
# make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
import json
import logging
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.DescribeInstanceAutoRenewAttributeRequest import \
    DescribeInstanceAutoRenewAttributeRequest
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526.ModifyInstanceAutoRenewAttributeRequest import \
    ModifyInstanceAutoRenewAttributeRequest
from aliyunsdkecs.request.v20140526.RenewInstanceRequest import RenewInstanceRequest
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')
clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
# data format in UTC, only support passed the value for minute, seconds is not support.
INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z'
INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z'
def renew_job(page_size=100, page_number=1, check_need_renew=True, security_group_id=None):
    response = describe_need_renew_instance(page_size=page_size, page_number=page_number,
                                            check_need_renew=check_need_renew,
                                            security_group_id=security_group_id)
    response_list = response.get('Instances').get('Instance')
    logging.info("%s instances need to renew", str(response.get('TotalCount')))
    if response_list > 0:
        instance_ids = ''
        for item in response_list:
            instance_id = item.get('InstanceId')
            instance_ids += instance_id + ','
            renew_instance(instance_id=instance_id)
        logging.info("%s execute renew action ready", instance_ids)
def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None,
                                 check_need_renew=True, security_group_id=None):
    request = DescribeInstancesRequest()
    if check_need_renew is True:
        request.set_Filter3Key("ExpiredStartTime")
        request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING)
        request.set_Filter4Key("ExpiredEndTime")
        request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING)
    if instance_id is not None:
        request.set_InstanceIds(json.dumps([instance_id]))
    if security_group_id:
        request.set_SecurityGroupId(security_group_id)
    request.set_PageNumber(page_number)
    request.set_PageSize(page_size)
    return _send_request(request)
# check the instances is renew or not
def describe_instance_auto_renew_setting(instance_ids, expected_auto_renew=True):
    describe_request = DescribeInstanceAutoRenewAttributeRequest()
    describe_request.set_InstanceId(instance_ids)
    response_detail = _send_request(request=describe_request)
    failed_instance_ids = ''
    if response_detail is not None:
        attributes = response_detail.get('InstanceRenewAttributes').get('InstanceRenewAttribute')
        if attributes:
            for item in attributes:
                auto_renew_status = item.get('AutoRenewEnabled')
                if auto_renew_status != expected_auto_renew:
                    failed_instance_ids += item.get('InstanceId') + ','
    if len(failed_instance_ids) > 0:
        logging.error("instance %s auto renew not match expect %s.", failed_instance_ids,
                      expected_auto_renew)
def setting_instance_auto_renew(instance_ids, auto_renew=True):
    logging.info('execute enable auto renew ' + instance_ids)
    request = ModifyInstanceAutoRenewAttributeRequest();
    request.set_Duration(1);
    request.set_AutoRenew(auto_renew);
    request.set_InstanceId(instance_ids)
    _send_request(request)
    describe_instance_auto_renew_setting(instance_ids, auto_renew)
# if using the instance id can be found means the instance is not renew successfully.
def check_instance_need_renew(instance_id):
    response = describe_need_renew_instance(instance_id=instance_id)
    if response is not None:
        return response.get('TotalCount') == 1
    return False
# 续费一个实例一个月
def renew_instance(instance_id, period='1'):
    need_renew = check_instance_need_renew(instance_id)
    if need_renew:
        _renew_instance_action(instance_id=instance_id, period=period)
        # describe_need_renew_instance(instance_id=instance_id, check_need_renew=False)
def _renew_instance_action(instance_id, period='1'):
    request = RenewInstanceRequest()
    request.set_Period(period)
    request.set_InstanceId(instance_id)
    response = _send_request(request)
    logging.info('renew %s ready, output is %s ', instance_id, response)
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)
if __name__ == '__main__':
    logging.info("Renew ECS Instance by OpenApi!")
    # 查询在指定的时刻范围内是不是有须要续费的实例。
    describe_need_renew_instance()
    # 续费一个实例, 直接实行扣费
    renew_instance('i-1111')
    # 查询实例自动续费的状况
    # describe_instance_auto_renew_setting('i-1111,i-2222')
    # 设置实例自动续费
    # setting_instance_auto_renew('i-1111,i-2222')

以上就是怎样直接经由过程 API 举行续费查询和续费治理的细致内容,更多请关注ki4网别的相干文章!

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

分享到:
赞(0) 打赏

支付宝扫一扫打赏

微信扫一扫打赏

标签:

上一篇:

下一篇:

相关推荐

0 条评论关于"怎样直接经由过程 API 举行续费查询和续费治理【Linux】,Linux"

最新评论

    暂无留言哦~~

博客简介

看古风美女插画Cos小姐姐,素材合集图集打包下载:炫龙网,好看二次元插画应有尽有,唯美小姐姐等你来。

友情链接

他们同样是一群网虫,却不是每天泡在网上游走在淘宝和网游之间、刷着本来就快要透支的信用卡。他们或许没有踏出国门一步,但同学却不局限在一国一校,而是遍及全球!申请交换友链

服务热线:
 

 QQ在线交流

 旺旺在线