// This file is auto-generated, don't edit it. Thanks. package albb import ( "encoding/json" "fmt" openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" openapiutil "github.com/alibabacloud-go/openapi-util/service" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" "strings" "time" ) //查询实例的详细信息列表 func ModifyInstanceAutoReleaseTime (InstanceId,AutoReleaseTime string) (_err error) { fmt.Println("ModifyInstanceAutoReleaseTime:",InstanceId,AutoReleaseTime ) client, _err := CreateClient() if _err != nil { return _err } params := &openapi.Params{ // 接口名称 Action: tea.String("ModifyInstanceAutoReleaseTime"), // 接口版本 Version: tea.String("2014-05-26"), // 接口协议 Protocol: tea.String("HTTPS"), // 接口 HTTP 方法 Method: tea.String("POST"), AuthType: tea.String("AK"), Style: tea.String("RPC"), // 接口 PATH Pathname: tea.String("/"), // 接口请求体内容格式 ReqBodyType: tea.String("json"), // 接口响应体内容格式 BodyType: tea.String("json"), } println("AutoReleaseTime:",AutoReleaseTime) // query params queries := map[string]interface{}{} queries["RegionId"] = tea.String("cn-hangzhou") queries["InstanceId"] = tea.String(InstanceId) queries["AutoReleaseTime"] = tea.String(AutoReleaseTime) // runtime options runtime := &util.RuntimeOptions{} request := &openapi.OpenApiRequest{ Query: openapiutil.Query(queries), } // 复制代码运行请自行打印 API 的返回值 // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。 _result, _err := client.CallApi(params, request, runtime) if _err != nil { return _err } data, _ := json.Marshal(_result) fmt.Println(string(data)) //fmt.Println("ccc",_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"]) return _err } //查询实例的详细信息列表 func DescribeInstances () (_err error) { client, _err := CreateClient() if _err != nil { return _err } params := &openapi.Params{ // 接口名称 Action: tea.String("DescribeInstances"), // 接口版本 Version: tea.String("2014-05-26"), // 接口协议 Protocol: tea.String("HTTPS"), // 接口 HTTP 方法 Method: tea.String("POST"), AuthType: tea.String("AK"), Style: tea.String("RPC"), // 接口 PATH Pathname: tea.String("/"), // 接口请求体内容格式 ReqBodyType: tea.String("json"), // 接口响应体内容格式 BodyType: tea.String("json"), } // query params queries := map[string]interface{}{} queries["RegionId"] = tea.String("cn-hangzhou") // runtime options runtime := &util.RuntimeOptions{} request := &openapi.OpenApiRequest{ Query: openapiutil.Query(queries), } // 复制代码运行请自行打印 API 的返回值 // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。 _result, _err := client.CallApi(params, request, runtime) if _err != nil { return _err } data, _ := json.Marshal(_result) fmt.Println(string(data)) //fmt.Println("ccc",_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"]) fmt.Println("len",len(_result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"].([]interface {}))) find_ecs := false for _,v := range _result["body"].(map[string]interface{})["Instances"].(map[string]interface{})["Instance"].([]interface {}){ fmt.Println("InstanceName:",v.(map[string]interface{})["InstanceName"],"InstanceId:",v.(map[string]interface{})["InstanceId"]) // 使用 strings.Contains 检查字符串是否包含子字符串 if strings.Contains(v.(map[string]interface{})["InstanceName"].(string), "ecs-verify") { find_ecs = true now := time.Now() oneHourLater := now.Add(time.Minute * 58) //oneHourLater = oneHourLater.Add(-time.Hour * 8) AutoReleaseTime := oneHourLater.Format(time.RFC3339) fmt.Println("延期:", v.(map[string]interface{})["InstanceName"].(string),v.(map[string]interface{})["InstanceId"].(string),AutoReleaseTime) ModifyInstanceAutoReleaseTime(v.(map[string]interface{})["InstanceId"].(string),AutoReleaseTime) } } // 判断是否要创建 if !find_ecs { err := RunInstances() if err != nil { panic(any(err)) } } return _err } /* { "body": { "Instances": { "Instance": [{ "AdditionalInfo": {}, "AutoReleaseTime": "", "ClusterId": "", "Cpu": 2, "CpuOptions": { "CoreCount": 1, "Numa": "ON", "ThreadsPerCore": 2 }, "CreationTime": "2024-11-23T03:56Z", "CreditSpecification": "", "DedicatedHostAttribute": { "DedicatedHostClusterId": "", "DedicatedHostId": "", "DedicatedHostName": "" }, "DedicatedInstanceAttribute": { "Affinity": "", "Tenancy": "" }, "DeletionProtection": false, "DeploymentSetId": "", "Description": "", "DeviceAvailable": true, "EcsCapacityReservationAttr": { "CapacityReservationId": "", "CapacityReservationPreference": "" }, "EipAddress": { "AllocationId": "", "InternetChargeType": "", "IpAddress": "" }, "ExpiredTime": "2025-12-07T16:00Z", "GPUAmount": 0, "GPUSpec": "", "HibernationOptions": { "Configured": false }, "HostName": "iZbp1as4p3kp4bip8dgrveZ", "ImageId": "centos_7_9_x64_20G_alibase_20240628.vhd", "ImageOptions": {}, "InnerIpAddress": { "IpAddress": [] }, "InstanceChargeType": "PrePaid", "InstanceId": "i-bp1as4p3kp4bip8dgrve", "InstanceName": "冷链服务器", "InstanceNetworkType": "vpc", "InstanceType": "ecs.u1-c1m2.large", "InstanceTypeFamily": "ecs.u1", "InternetChargeType": "PayByBandwidth", "InternetMaxBandwidthIn": 1000, "InternetMaxBandwidthOut": 5, "IoOptimized": true, "Memory": 4096, "MetadataOptions": { "HttpEndpoint": "", "HttpTokens": "" }, "NetworkInterfaces": { "NetworkInterface": [{ "MacAddress": "00:16:3e:01:0f:62", "NetworkInterfaceId": "eni-bp156zuduohi3pw9j7th", "PrimaryIpAddress": "172.20.11.83", "PrivateIpSets": { "PrivateIpSet": [{ "Primary": true, "PrivateIpAddress": "172.20.11.83" }] }, "Type": "Primary" }] }, "OSName": "CentOS 7.9 64位", "OSNameEn": "CentOS 7.9 64 bit", "OSType": "linux", "OperationLocks": { "LockReason": [] }, "PrivateDnsNameOptions": {}, "PublicIpAddress": { "IpAddress": ["47.97.103.163"] }, "Recyclable": false, "RegionId": "cn-hangzhou", "ResourceGroupId": "", "SaleCycle": "", "SecurityGroupIds": { "SecurityGroupId": ["sg-bp156zuduohi3pw66zbm"] }, "SerialNumber": "7c66d46a-ddef-451d-b542-c518b819d54e", "SpotPriceLimit": 0.0, "SpotStrategy": "NoSpot", "StartTime": "2024-12-22T11:47Z", "Status": "Running", "StoppedMode": "Not-applicable", "VlanId": "", "VpcAttributes": { "NatIpAddress": "", "PrivateIpAddress": { "IpAddress": ["172.20.11.83"] }, "VSwitchId": "vsw-bp11bjxe8djsv3c254bur", "VpcId": "vpc-bp14wz0vpzqhssnuezw1j" }, "ZoneId": "cn-hangzhou-h" }] }, "NextToken": "", "PageNumber": 1, "PageSize": 10, "RequestId": "A3F8B366-6F44-59BD-AE01-805090689456", "TotalCount": 1 }, "headers": { "access-control-allow-origin": "*", "access-control-expose-headers": "*", "connection": "keep-alive", "content-type": "application/json;charset=utf-8", "date": "Thu, 20 Feb 2025 01:04:22 GMT", "keep-alive": "timeout=25", "vary": "Accept-Encoding", "x-acs-request-id": "A3F8B366-6F44-59BD-AE01-805090689456", "x-acs-trace-id": "c3aef4e6346f739c859a7d7a93723893" }, "statusCode": 200 } */