|
@@ -0,0 +1,145 @@
|
|
|
+# -*- 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):
|
|
|
+ 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):
|
|
|
+ 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):
|
|
|
+ # 提取longvideoapi的Value和RecordId
|
|
|
+ # longvideoapi_records = [
|
|
|
+ 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):
|
|
|
+ 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:
|
|
|
+ # 复制代码运行请自行打印 API 的返回值
|
|
|
+ response=dns_client.describe_domain_records_with_options(describe_domain_records_request, runtime)
|
|
|
+ # record_id = response["DomainRecords"]["Record"][0]["RecordId"]
|
|
|
+ # print(f"RecordId: {record_id}")
|
|
|
+ # return record_id
|
|
|
+ # longvideoapi_records = [
|
|
|
+ # {
|
|
|
+ # "Value": record["Value"],
|
|
|
+ # "RecordId": record["RecordId"]
|
|
|
+ # }
|
|
|
+ # for record in response["DomainRecords"]["Record"]
|
|
|
+ # if record["RR"] == "longvideoapi"
|
|
|
+ # ]
|
|
|
+ #
|
|
|
+ # # 打印结果
|
|
|
+ # for record in longvideoapi_records:
|
|
|
+ # print(f"Value: {record['Value']}, RecordId: {record['RecordId']}")
|
|
|
+ # 调用函数并获取结果
|
|
|
+ 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):
|
|
|
+ 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)
|
|
|
+
|
|
|
+
|