DescribeInstances.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // This file is auto-generated, don't edit it. Thanks.
  2. package albb
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  7. openapiutil "github.com/alibabacloud-go/openapi-util/service"
  8. util "github.com/alibabacloud-go/tea-utils/v2/service"
  9. "github.com/alibabacloud-go/tea/tea"
  10. "strings"
  11. "time"
  12. )
  13. //查询实例的详细信息列表
  14. func ModifyInstanceAutoReleaseTime (InstanceId,AutoReleaseTime string) (_err error) {
  15. fmt.Println("ModifyInstanceAutoReleaseTime:",InstanceId,AutoReleaseTime )
  16. client, _err := CreateClient()
  17. if _err != nil {
  18. return _err
  19. }
  20. params := &openapi.Params{
  21. // 接口名称
  22. Action: tea.String("ModifyInstanceAutoReleaseTime"),
  23. // 接口版本
  24. Version: tea.String("2014-05-26"),
  25. // 接口协议
  26. Protocol: tea.String("HTTPS"),
  27. // 接口 HTTP 方法
  28. Method: tea.String("POST"),
  29. AuthType: tea.String("AK"),
  30. Style: tea.String("RPC"),
  31. // 接口 PATH
  32. Pathname: tea.String("/"),
  33. // 接口请求体内容格式
  34. ReqBodyType: tea.String("json"),
  35. // 接口响应体内容格式
  36. BodyType: tea.String("json"),
  37. }
  38. println("AutoReleaseTime:",AutoReleaseTime)
  39. // query params
  40. queries := map[string]interface{}{}
  41. queries["RegionId"] = tea.String("cn-hangzhou")
  42. queries["InstanceId"] = tea.String(InstanceId)
  43. queries["AutoReleaseTime"] = tea.String(AutoReleaseTime)
  44. // runtime options
  45. runtime := &util.RuntimeOptions{}
  46. request := &openapi.OpenApiRequest{
  47. Query: openapiutil.Query(queries),
  48. }
  49. // 复制代码运行请自行打印 API 的返回值
  50. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  51. _result, _err := client.CallApi(params, request, runtime)
  52. if _err != nil {
  53. return _err
  54. }
  55. data, _ := json.Marshal(_result)
  56. fmt.Println(string(data))
  57. //fmt.Println("ccc",_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"])
  58. return _err
  59. }
  60. //查询实例的详细信息列表
  61. func DescribeInstances () (_err error) {
  62. client, _err := CreateClient()
  63. if _err != nil {
  64. return _err
  65. }
  66. params := &openapi.Params{
  67. // 接口名称
  68. Action: tea.String("DescribeInstances"),
  69. // 接口版本
  70. Version: tea.String("2014-05-26"),
  71. // 接口协议
  72. Protocol: tea.String("HTTPS"),
  73. // 接口 HTTP 方法
  74. Method: tea.String("POST"),
  75. AuthType: tea.String("AK"),
  76. Style: tea.String("RPC"),
  77. // 接口 PATH
  78. Pathname: tea.String("/"),
  79. // 接口请求体内容格式
  80. ReqBodyType: tea.String("json"),
  81. // 接口响应体内容格式
  82. BodyType: tea.String("json"),
  83. }
  84. // query params
  85. queries := map[string]interface{}{}
  86. queries["RegionId"] = tea.String("cn-hangzhou")
  87. // runtime options
  88. runtime := &util.RuntimeOptions{}
  89. request := &openapi.OpenApiRequest{
  90. Query: openapiutil.Query(queries),
  91. }
  92. // 复制代码运行请自行打印 API 的返回值
  93. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  94. _result, _err := client.CallApi(params, request, runtime)
  95. if _err != nil {
  96. return _err
  97. }
  98. data, _ := json.Marshal(_result)
  99. fmt.Println(string(data))
  100. //fmt.Println("ccc",_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"])
  101. fmt.Println("len",len(_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"].([]interface {})))
  102. find_ecs := false
  103. for _,v := range _result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"].([]interface {}){
  104. fmt.Println("InstanceName:",v.(map[string]interface{})["InstanceName"],"InstanceId:",v.(map[string]interface{})["InstanceId"])
  105. // 使用 strings.Contains 检查字符串是否包含子字符串
  106. if strings.Contains(v.(map[string]interface{})["InstanceName"].(string), "ecs-verify") {
  107. find_ecs = true
  108. now := time.Now()
  109. oneHourLater := now.Add(time.Minute * 58)
  110. //oneHourLater = oneHourLater.Add(-time.Hour * 8)
  111. AutoReleaseTime := oneHourLater.Format(time.RFC3339)
  112. fmt.Println("延期:", v.(map[string]interface{})["InstanceName"].(string),v.(map[string]interface{})["InstanceId"].(string),AutoReleaseTime)
  113. ModifyInstanceAutoReleaseTime(v.(map[string]interface{})["InstanceId"].(string),AutoReleaseTime)
  114. }
  115. }
  116. // 判断是否要创建
  117. if !find_ecs {
  118. err := RunInstances()
  119. if err != nil {
  120. panic(any(err))
  121. }
  122. }
  123. return _err
  124. }
  125. /*
  126. {
  127. "body": {
  128. "Instances": {
  129. "Instance": [{
  130. "AdditionalInfo": {},
  131. "AutoReleaseTime": "",
  132. "ClusterId": "",
  133. "Cpu": 2,
  134. "CpuOptions": {
  135. "CoreCount": 1,
  136. "Numa": "ON",
  137. "ThreadsPerCore": 2
  138. },
  139. "CreationTime": "2024-11-23T03:56Z",
  140. "CreditSpecification": "",
  141. "DedicatedHostAttribute": {
  142. "DedicatedHostClusterId": "",
  143. "DedicatedHostId": "",
  144. "DedicatedHostName": ""
  145. },
  146. "DedicatedInstanceAttribute": {
  147. "Affinity": "",
  148. "Tenancy": ""
  149. },
  150. "DeletionProtection": false,
  151. "DeploymentSetId": "",
  152. "Description": "",
  153. "DeviceAvailable": true,
  154. "EcsCapacityReservationAttr": {
  155. "CapacityReservationId": "",
  156. "CapacityReservationPreference": ""
  157. },
  158. "EipAddress": {
  159. "AllocationId": "",
  160. "InternetChargeType": "",
  161. "IpAddress": ""
  162. },
  163. "ExpiredTime": "2025-12-07T16:00Z",
  164. "GPUAmount": 0,
  165. "GPUSpec": "",
  166. "HibernationOptions": {
  167. "Configured": false
  168. },
  169. "HostName": "iZbp1as4p3kp4bip8dgrveZ",
  170. "ImageId": "centos_7_9_x64_20G_alibase_20240628.vhd",
  171. "ImageOptions": {},
  172. "InnerIpAddress": {
  173. "IpAddress": []
  174. },
  175. "InstanceChargeType": "PrePaid",
  176. "InstanceId": "i-bp1as4p3kp4bip8dgrve",
  177. "InstanceName": "冷链服务器",
  178. "InstanceNetworkType": "vpc",
  179. "InstanceType": "ecs.u1-c1m2.large",
  180. "InstanceTypeFamily": "ecs.u1",
  181. "InternetChargeType": "PayByBandwidth",
  182. "InternetMaxBandwidthIn": 1000,
  183. "InternetMaxBandwidthOut": 5,
  184. "IoOptimized": true,
  185. "Memory": 4096,
  186. "MetadataOptions": {
  187. "HttpEndpoint": "",
  188. "HttpTokens": ""
  189. },
  190. "NetworkInterfaces": {
  191. "NetworkInterface": [{
  192. "MacAddress": "00:16:3e:01:0f:62",
  193. "NetworkInterfaceId": "eni-bp156zuduohi3pw9j7th",
  194. "PrimaryIpAddress": "172.20.11.83",
  195. "PrivateIpSets": {
  196. "PrivateIpSet": [{
  197. "Primary": true,
  198. "PrivateIpAddress": "172.20.11.83"
  199. }]
  200. },
  201. "Type": "Primary"
  202. }]
  203. },
  204. "OSName": "CentOS 7.9 64位",
  205. "OSNameEn": "CentOS 7.9 64 bit",
  206. "OSType": "linux",
  207. "OperationLocks": {
  208. "LockReason": []
  209. },
  210. "PrivateDnsNameOptions": {},
  211. "PublicIpAddress": {
  212. "IpAddress": ["47.97.103.163"]
  213. },
  214. "Recyclable": false,
  215. "RegionId": "cn-hangzhou",
  216. "ResourceGroupId": "",
  217. "SaleCycle": "",
  218. "SecurityGroupIds": {
  219. "SecurityGroupId": ["sg-bp156zuduohi3pw66zbm"]
  220. },
  221. "SerialNumber": "7c66d46a-ddef-451d-b542-c518b819d54e",
  222. "SpotPriceLimit": 0.0,
  223. "SpotStrategy": "NoSpot",
  224. "StartTime": "2024-12-22T11:47Z",
  225. "Status": "Running",
  226. "StoppedMode": "Not-applicable",
  227. "VlanId": "",
  228. "VpcAttributes": {
  229. "NatIpAddress": "",
  230. "PrivateIpAddress": {
  231. "IpAddress": ["172.20.11.83"]
  232. },
  233. "VSwitchId": "vsw-bp11bjxe8djsv3c254bur",
  234. "VpcId": "vpc-bp14wz0vpzqhssnuezw1j"
  235. },
  236. "ZoneId": "cn-hangzhou-h"
  237. }]
  238. },
  239. "NextToken": "",
  240. "PageNumber": 1,
  241. "PageSize": 10,
  242. "RequestId": "A3F8B366-6F44-59BD-AE01-805090689456",
  243. "TotalCount": 1
  244. },
  245. "headers": {
  246. "access-control-allow-origin": "*",
  247. "access-control-expose-headers": "*",
  248. "connection": "keep-alive",
  249. "content-type": "application/json;charset=utf-8",
  250. "date": "Thu, 20 Feb 2025 01:04:22 GMT",
  251. "keep-alive": "timeout=25",
  252. "vary": "Accept-Encoding",
  253. "x-acs-request-id": "A3F8B366-6F44-59BD-AE01-805090689456",
  254. "x-acs-trace-id": "c3aef4e6346f739c859a7d7a93723893"
  255. },
  256. "statusCode": 200
  257. }
  258. */