package com.muyu.car.instance;

import com.alibaba.fastjson.JSON;
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
import com.aliyun.tea.TeaException;
import com.muyu.car.domain.example.ExampleInformation;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * 启动后查询实例信息
 * @Author：蓬叁
 * @Package：com.muyu.car.instance
 * @Project：cloud-server-8
 * @name：QueryInstance
 * @Date：2024/9/29 下午8:58
 */
@Log4j2
public class QueryInstance {

    public static List<ExampleInformation> queryInstance(List<String> instanceIds) throws Exception {
        com.aliyun.ecs20140526.Client client = CreateClient.createClient();

        com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
                .setInstanceIds(JSON.toJSONString(instanceIds))
                .setRegionId("cn-hangzhou");

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        try {
            // 复制代码运行请自行打印 API 的返回值
            DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
            DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
            ArrayList<ExampleInformation> exampleInformations = new ArrayList<>();
            for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : body.getInstances().getInstance()){
                ExampleInformation exampleInformation = new ExampleInformation();
                exampleInformation.setInstanceId(instance.getInstanceId());
                log.info("实例ID:{}",exampleInformation.getInstanceId());
                exampleInformation.setStatus(instance.getStatus());
                log.info("实例状态:{}",exampleInformation.getStatus());
                exampleInformation.setIpAddress(String.valueOf(instance.getPublicIpAddress().getIpAddress()));
                log.info("实例IP:{}",exampleInformation.getIpAddress());
                exampleInformations.add(exampleInformation);
            }
            log.info("实例信息:{}",exampleInformations);
            return exampleInformations;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

}
