package com.qmth.ops.aliyun.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.qmth.ops.aliyun.config.SlsConfig;

public class AssumeRoleClient {

    private static final String REGION_ID = "cn-hangzhou";

    public static AssumeRoleResponse get(SlsConfig config) {
        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, config.getAccessKey(), config.getAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);

        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRegionId(REGION_ID);
        request.setRoleArn(config.getRamAssumeRole());
        request.setRoleSessionName(config.getRamName());
        try {
            return client.getAcsResponse(request);
        } catch (Exception e) {
            throw new RuntimeException("assume role error", e);
        }
    }
}
