package com.fs.course.service;

import com.fs.config.cloud.CloudHostProper;
import com.hc.openapi.tool.fastjson.JSON;
import com.volcengine.model.request.AssumeRoleRequest;
import com.volcengine.model.response.AssumeRoleResponse;
import com.volcengine.service.sts.ISTSService;
import com.volcengine.service.sts.impl.STSServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class HsyAssumeRoleService {
    @Autowired
    private CloudHostProper cloudHostProper;

    @Value("${hsy.role_access_key:''}")
    private String role_access_key;

    @Value("${hsy.role_secret_key:''}")
    private String role_secret_key;

    @Value("${hsy.role_trn:''}")
    private String role_trn;
    public AssumeRoleResponse getRoleResponse() throws Exception {
        ISTSService stsService = STSServiceImpl.getInstance();

        stsService.setAccessKey(role_access_key);
        stsService.setSecretKey(role_secret_key);

//        stsService.setAccessKey("AKLTZTc4YTE4ZjI2OWViNDNjZGI2NjhiYTI5Njc5ZjA1Mzk");
//        stsService.setSecretKey("WXpjelpUYzFOakF5TUdObE5EZGtNR0ZsWXpKaU1tTmtZakk1WXpObE4yRQ==");

        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRoleSessionName(cloudHostProper.spaceName);
        request.setDurationSeconds(900);
        request.setRoleTrn(role_trn);

        AssumeRoleResponse resp = stsService.assumeRole(request);
        return resp;
    }

    public static void main(String[] args) throws Exception {
        ISTSService stsService = STSServiceImpl.getInstance();

        stsService.setAccessKey("AKLTNmMwNjJkNDFhYTVjNDIzYzhhNzEyZmZmZTlmYzBhNGM");
        stsService.setSecretKey("T0RaaFl6UmhZV1V4WXpKbU5EWTBNMkZpT0RNNU9UY3daak0wTjJFd09XUQ==");

        AssumeRoleRequest request = new AssumeRoleRequest();
        request.setRoleSessionName("just_for_test");
        request.setDurationSeconds(900);
        request.setRoleTrn("trn:iam::2114522511:role/hylj");
        AssumeRoleResponse resp = stsService.assumeRole(request);
        System.out.println(JSON.toJSONString(resp));

    }
}
