package com.brframework.commonoss.core.sts;

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

/**
 * @author xu
 * @date 2019/8/21 16:42
 */
public class AliYunSTSPolicyStatementBuilder {

    JSONObject statement = new JSONObject();

    private AliYunSTSPolicyStatementBuilder(){}

    public static AliYunSTSPolicyStatementBuilder builder(){
        return new AliYunSTSPolicyStatementBuilder();
    }

    /**
     * 影响的操作
     * oss:*  所有权限
     * oss:GetObject   读
     * oss:PutObject   写
     * oss:DeleteObject 删
     * oss:ListParts  列举
     * oss:AbortMultipartUpload
     * oss:ListObjects 列举
     * @param actions oss:*
     * @return
     */
    public AliYunSTSPolicyStatementBuilder action(String ... actions){
        statement.put("Action", new JSONArray(Lists.newArrayList(actions)));
        return this;
    }

    /**
     * 影响的方式
     * @param effect Allow
     * @return
     */
    public AliYunSTSPolicyStatementBuilder effect(String effect){
        statement.put("Effect", effect);
        return this;
    }

    /**
     * 影响的目录
     * acs:oss:*:*:app-base-oss/user1/*
     * acs:oss:*:*:app-base-oss
     * acs:oss:*:*:*     所有bucket，所有目录
     * @param resources acs:oss:*:*:*
     * @return
     */
    public AliYunSTSPolicyStatementBuilder resource(String ... resources){
        statement.put("Resource", new JSONArray(Lists.newArrayList(resources)));
        return this;
    }

    public JSONObject build(){
        return statement;
    }
}
