# -*- coding: utf-8 -*- import os import sys import json from http.client import responses import conf from typing import List from alibabacloud_alidns20150109.client import Client as Alidns20150109Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_alidns20150109 import models as alidns_20150109_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_util.client import Client as UtilClient from alibabacloud_cms20190101.client import Client as Cms20190101Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_cms20190101 import models as cms_20190101_models from alibabacloud_tea_util import models as util_models from alibabacloud_tea_util.client import Client as UtilClient def update_dns_weights(bandwidth): ''' @param bandwidth: 带宽值 @return: ''' if bandwidth > 1800: alb_weight = round(1800 / bandwidth * 100) backup_weight = (100 - alb_weight) // 2 print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}") return backup_weight else: alb_weight = 0 backup_weight = 0 print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}") return backup_weight def client(access_key_id, access_key_secret, endpoint) -> Cms20190101Client: """ 使用AK&SK初始化账号Client @return: Client @throws Exception """ # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 # 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。 config = open_api_models.Config( # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。, access_key_id=os.environ[access_key_id], # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。, access_key_secret=os.environ[access_key_secret] ) # Endpoint 请参考 https://api.aliyun.com/product/Cms config.endpoint = endpoint return Cms20190101Client(config) def DescribeMetricList(metrics_client, namespace, metric_name, period, dimensions, start_time, end_time): ''' @param metrics_client: 云监控客户端 @param namespace: 云产品的数据命名空间 共享带宽 @param metric_name: 云产品的监控项名称 入流量 @param period: 监控数据的统计周期 秒 @param dimensions: 指定资源的监控维度 共享带宽 @param start_time: 监控开始时间 @param end_time: 监控结束时间 @return: ''' metrics_client() describe_metric_list_request = cms_20190101_models.DescribeMetricListRequest( namespace=namespace, metric_name=metric_name, period=period, dimensions=dimensions, start_time=start_time, end_time=end_time ) runtime = util_models.RuntimeOptions() try: # 复制代码运行请自行打印 API 的返回值 response = metrics_client.describe_metric_list_with_options(describe_metric_list_request, runtime) datapoints = json.loads(response["Datapoints"]) if datapoints: value_in_bits = datapoints[0]["Value"] value_in_megabits = value_in_bits / 1000000 print(f"Value in Megabits: {value_in_megabits:.2f} Mb") return value_in_megabits except Exception as error: exit(error) def get_records(response): ''' @param response: @return: ''' records = [ { "Value": record["Value"], "RecordId": record["RecordId"] } for record in response["DomainRecords"]["Record"] if record["RR"] == "longvideoapi" ] return records def DescribeDomainRecords(dns_client, lang, domain_name): ''' @param dns_client: dns客户端 @param lang: 语言 @param domain_name: 解析记录的ID 主域名 @return: ''' dns_client() describe_domain_records_request = alidns_20150109_models.DescribeDomainRecordsRequest( lang=lang, domain_name=domain_name, page_size=500, page_number=1 ) runtime = util_models.RuntimeOptions() try: response=dns_client.describe_domain_records_with_options(describe_domain_records_request, runtime) result = get_records(response) for record in result: print(record) except Exception as error: exit(error) def update_dnsslbweight(dns_client, lang, record, backup_weight): ''' @param dns_client: dns客户端 @param lang: 语言 @param record: 解析记录ID @param backup_weight: 权重 @return: ''' dns_client() update_dnsslbweight_request = alidns_20150109_models.UpdateDNSSLBWeightRequest( lang=lang, record_id=record, weight=backup_weight ) runtime = util_models.RuntimeOptions() try: # 复制代码运行请自行打印 API 的返回值 dns_client.update_dnsslbweight_with_options(update_dnsslbweight_request, runtime) except Exception as error: exit(error)