|
@@ -0,0 +1,145 @@
|
|
|
+
|
|
|
+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
|
|
|
+ """
|
|
|
+
|
|
|
+
|
|
|
+ config = open_api_models.Config(
|
|
|
+
|
|
|
+ access_key_id=os.environ[access_key_id],
|
|
|
+
|
|
|
+ access_key_secret=os.environ[access_key_secret]
|
|
|
+ )
|
|
|
+
|
|
|
+ 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:
|
|
|
+
|
|
|
+ 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):
|
|
|
+
|
|
|
+
|
|
|
+ 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:
|
|
|
+
|
|
|
+ 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):
|
|
|
+ dns_client()
|
|
|
+ update_dnsslbweight_request = alidns_20150109_models.UpdateDNSSLBWeightRequest(
|
|
|
+ lang=lang,
|
|
|
+ record_id=record,
|
|
|
+ weight=backup_weight
|
|
|
+ )
|
|
|
+ runtime = util_models.RuntimeOptions()
|
|
|
+ try:
|
|
|
+
|
|
|
+ dns_client.update_dnsslbweight_with_options(update_dnsslbweight_request, runtime)
|
|
|
+ except Exception as error:
|
|
|
+ exit(error)
|
|
|
+
|
|
|
+
|