utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import json
  5. from http.client import responses
  6. import conf
  7. from typing import List
  8. from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
  9. from alibabacloud_tea_openapi import models as open_api_models
  10. from alibabacloud_alidns20150109 import models as alidns_20150109_models
  11. from alibabacloud_tea_util import models as util_models
  12. from alibabacloud_tea_util.client import Client as UtilClient
  13. from alibabacloud_cms20190101.client import Client as Cms20190101Client
  14. from alibabacloud_tea_openapi import models as open_api_models
  15. from alibabacloud_cms20190101 import models as cms_20190101_models
  16. from alibabacloud_tea_util import models as util_models
  17. from alibabacloud_tea_util.client import Client as UtilClient
  18. def update_dns_weights(bandwidth):
  19. if bandwidth > 1800:
  20. alb_weight = round(1800 / bandwidth * 100)
  21. backup_weight = (100 - alb_weight) // 2
  22. print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}")
  23. return backup_weight
  24. else:
  25. alb_weight = 0
  26. backup_weight = 0
  27. print(f"alb_weight: {alb_weight}, backup_weight: {backup_weight}")
  28. return backup_weight
  29. def client(access_key_id, access_key_secret, endpoint) -> Cms20190101Client:
  30. """
  31. 使用AK&SK初始化账号Client
  32. @return: Client
  33. @throws Exception
  34. """
  35. # 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  36. # 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
  37. config = open_api_models.Config(
  38. # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
  39. access_key_id=os.environ[access_key_id],
  40. # 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
  41. access_key_secret=os.environ[access_key_secret]
  42. )
  43. # Endpoint 请参考 https://api.aliyun.com/product/Cms
  44. config.endpoint = endpoint
  45. return Cms20190101Client(config)
  46. def DescribeMetricList(metrics_client, namespace, metric_name, period, dimensions, start_time, end_time):
  47. metrics_client()
  48. describe_metric_list_request = cms_20190101_models.DescribeMetricListRequest(
  49. namespace=namespace,
  50. metric_name=metric_name,
  51. period=period,
  52. dimensions=dimensions,
  53. start_time=start_time,
  54. end_time=end_time
  55. )
  56. runtime = util_models.RuntimeOptions()
  57. try:
  58. # 复制代码运行请自行打印 API 的返回值
  59. response = metrics_client.describe_metric_list_with_options(describe_metric_list_request, runtime)
  60. datapoints = json.loads(response["Datapoints"])
  61. if datapoints:
  62. value_in_bits = datapoints[0]["Value"]
  63. value_in_megabits = value_in_bits / 1000000
  64. print(f"Value in Megabits: {value_in_megabits:.2f} Mb")
  65. return value_in_megabits
  66. except Exception as error:
  67. exit(error)
  68. def get_records(response):
  69. # 提取longvideoapi的Value和RecordId
  70. # longvideoapi_records = [
  71. records = [
  72. {
  73. "Value": record["Value"],
  74. "RecordId": record["RecordId"]
  75. }
  76. for record in response["DomainRecords"]["Record"]
  77. if record["RR"] == "longvideoapi"
  78. ]
  79. return records
  80. def DescribeDomainRecords(dns_client, lang, domain_name):
  81. dns_client()
  82. describe_domain_records_request = alidns_20150109_models.DescribeDomainRecordsRequest(
  83. lang=lang,
  84. domain_name=domain_name,
  85. page_size=500,
  86. page_number=1
  87. )
  88. runtime = util_models.RuntimeOptions()
  89. try:
  90. # 复制代码运行请自行打印 API 的返回值
  91. response=dns_client.describe_domain_records_with_options(describe_domain_records_request, runtime)
  92. # record_id = response["DomainRecords"]["Record"][0]["RecordId"]
  93. # print(f"RecordId: {record_id}")
  94. # return record_id
  95. # longvideoapi_records = [
  96. # {
  97. # "Value": record["Value"],
  98. # "RecordId": record["RecordId"]
  99. # }
  100. # for record in response["DomainRecords"]["Record"]
  101. # if record["RR"] == "longvideoapi"
  102. # ]
  103. #
  104. # # 打印结果
  105. # for record in longvideoapi_records:
  106. # print(f"Value: {record['Value']}, RecordId: {record['RecordId']}")
  107. # 调用函数并获取结果
  108. result = get_records(response)
  109. # 打印结果
  110. for record in result:
  111. print(record)
  112. except Exception as error:
  113. exit(error)
  114. def update_dnsslbweight(dns_client, lang, record, backup_weight):
  115. dns_client()
  116. update_dnsslbweight_request = alidns_20150109_models.UpdateDNSSLBWeightRequest(
  117. lang=lang,
  118. record_id=record,
  119. weight=backup_weight
  120. )
  121. runtime = util_models.RuntimeOptions()
  122. try:
  123. # 复制代码运行请自行打印 API 的返回值
  124. dns_client.update_dnsslbweight_with_options(update_dnsslbweight_request, runtime)
  125. except Exception as error:
  126. exit(error)