运维使用脚本,批量获取实例下阿里云服务器续费价格,按年计算。
# 阿里云 python sdk 安装
pip install alibabacloud_ecs20140526==4.0.2
脚本如下:
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from typing import List
import csv
from alibabacloud_ecs20140526.client import Client as Ecs20140526Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ecs20140526 import models as ecs_20140526_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
InstanceData = []
access_key_id = ""access_key_secret =""
class Sample(object):
def __init__(self):
self.client = Sample.create_client(access_key_id, access_key_secret)
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> Ecs20140526Client:
"""
使用 AK&SK 初始化账号 Client
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret
)
# Endpoint 请参考 https://api.aliyun.com/product/Ecs
config.endpoint = f'ecs.cn-beijing.aliyuncs.com'
return Ecs20140526Client(config)
def DescribeRenewalPriceRequest(self, resource_id) -> dict:
runtime = util_models.RuntimeOptions()
describe_renewal_price_request = ecs_20140526_models.DescribeRenewalPriceRequest(
region_id='cn-beijing',
resource_type='instance',
resource_id=resource_id,
price_unit='Year',
period=1
)
try:
# 复制代码运行请自行打印 API 的返回值
data = self.client.describe_renewal_price_with_options(describe_renewal_price_request, runtime)
return data.to_map()
except Exception as error:
return dict
def DescribeInstancesRequest(self,page_number):
describe_instances_request = ecs_20140526_models.DescribeInstancesRequest(
region_id='cn-beijing',
page_size=50,
page_number=page_number,
)
runtime = util_models.RuntimeOptions()
try:
# 复制代码运行请自行打印 API 的返回值
data = self.client.describe_instances_with_options(describe_instances_request, runtime).to_map()
# print(data)
for Instance in data['body']['Instances']['Instance']:
HostName = Instance['HostName']
if HostName == "redis-kafka-1":
print(Instance)
InstanceId = Instance['InstanceId']
price = self.DescribeRenewalPriceRequest(resource_id=InstanceId)
TradePrice = price['body']['PriceInfo']['Price']['TradePrice']
print(HostName)
InstanceData.append([HostName, InstanceId, TradePrice])
except Exception as error:
print(error)
def main(self):
describe_instances_request = ecs_20140526_models.DescribeInstancesRequest(
region_id='cn-beijing',
page_size=1,
)
runtime = util_models.RuntimeOptions()
data = self.client.describe_instances_with_options(describe_instances_request, runtime).to_map()
TotalCount=data['body']['TotalCount']
num_pages = (TotalCount + 50 - 1) // 50
for page in range(1,num_pages+1):
self.DescribeInstancesRequest(page)
WriteCsvFile()
def WriteCsvFile():
with open("Aliyun.csv", 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerows(InstanceData)
print("CSV 文件写入完成!")
if __name__ == '__main__':
app = Sample()
app.main()
注意替换 access_key_id access_key_secret
price_unit=’Year’ 单位为年 period 为 1 也就是按照续费 1 年计算
region_id 当前设置为北京,可根据自身需要修改