package com.brframework.commonoss.core.sts;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 构建STS Policy
 *
 * https://help.aliyun.com/document_detail/31921.html?spm=a2c4g.11186623.2.33.56a04947Fs3945
 *
 * 参考数据
 * {"Statement":[{"Action":["oss:*"],"Effect":"Allow","Resource":["acs:oss:*:*:*"]}],"Version":"1"}
 *
 * @author xu
 * @date 2019/8/21 16:36
 */
public class AliYunSTSPolicyBuilder {

    JSONObject policy = new JSONObject();

    private AliYunSTSPolicyBuilder(){}

    public static AliYunSTSPolicyBuilder builder(){
        //默认Version 1
        return new AliYunSTSPolicyBuilder().version("1");
    }

    public AliYunSTSPolicyBuilder statement(AliYunSTSPolicyStatementBuilder ... builders){

        List<JSONObject> statements = Lists.newArrayList();
        for (AliYunSTSPolicyStatementBuilder builder : builders) {
            statements.add(builder.build());
        }
        policy.put("Statement", statements);
        return this;
    }

    public AliYunSTSPolicyBuilder version(String version){
        policy.put("Version", version);
        return this;
    }

    public JSONObject build(){
        return policy;
    }


}
