github.com/qiniu/go-sdk/v7@v7.27.0/iam/apis/create_policy/api.go (about)

     1  // THIS FILE IS GENERATED BY api-generator, DO NOT EDIT DIRECTLY!
     2  
     3  // 新建授权策略
     4  package create_policy
     5  
     6  import (
     7  	"encoding/json"
     8  
     9  	credentials "github.com/qiniu/go-sdk/v7/storagev2/credentials"
    10  	errors "github.com/qiniu/go-sdk/v7/storagev2/errors"
    11  )
    12  
    13  // 调用 API 所用的请求
    14  type Request struct {
    15  	Credentials credentials.CredentialsProvider // 鉴权参数,用于生成鉴权凭证,如果为空,则使用 HTTPClientOptions 中的 CredentialsProvider
    16  	Alias       string                          // 授权策略别名,由 `A-Za-z0-9` 组成
    17  	EditType    int64                           // 1:是通过自定义 JSON 编辑的策略 2:是通过 UI 编辑的策略
    18  	Description string                          // 授权策略描述
    19  	Statement   CreateStatements                // 授权策略规则集合
    20  }
    21  
    22  // 授权策略规则的操作集合
    23  type CreateActions = []string
    24  
    25  // 授权策略规则的资源集合
    26  type CreateResources = []string
    27  
    28  // 授权策略规则
    29  type CreateStatement struct {
    30  	Actions   CreateActions   // 授权策略规则的操作集合,action 查询参考 action 接口,格式为 service/action_alias
    31  	Resources CreateResources // 授权策略规则的资源集合,格式为 qrn:product:region:uid:[resource-type/]resource-id ;可以简写为 qrn:product:::resource-id
    32  	Effect    string          // 授权策略规则的生效类型,允许访问或拒绝访问
    33  }
    34  type jsonCreateStatement struct {
    35  	Actions   CreateActions   `json:"action"`   // 授权策略规则的操作集合,action 查询参考 action 接口,格式为 service/action_alias
    36  	Resources CreateResources `json:"resource"` // 授权策略规则的资源集合,格式为 qrn:product:region:uid:[resource-type/]resource-id ;可以简写为 qrn:product:::resource-id
    37  	Effect    string          `json:"effect"`   // 授权策略规则的生效类型,允许访问或拒绝访问
    38  }
    39  
    40  func (j *CreateStatement) MarshalJSON() ([]byte, error) {
    41  	if err := j.validate(); err != nil {
    42  		return nil, err
    43  	}
    44  	return json.Marshal(&jsonCreateStatement{Actions: j.Actions, Resources: j.Resources, Effect: j.Effect})
    45  }
    46  
    47  func (j *CreateStatement) UnmarshalJSON(data []byte) error {
    48  	var nj jsonCreateStatement
    49  	if err := json.Unmarshal(data, &nj); err != nil {
    50  		return err
    51  	}
    52  	j.Actions = nj.Actions
    53  	j.Resources = nj.Resources
    54  	j.Effect = nj.Effect
    55  	return nil
    56  }
    57  
    58  func (j *CreateStatement) validate() error {
    59  	if len(j.Actions) == 0 {
    60  		return errors.MissingRequiredFieldError{Name: "Actions"}
    61  	}
    62  	if len(j.Resources) == 0 {
    63  		return errors.MissingRequiredFieldError{Name: "Resources"}
    64  	}
    65  	if j.Effect == "" {
    66  		return errors.MissingRequiredFieldError{Name: "Effect"}
    67  	}
    68  	return nil
    69  }
    70  
    71  // 授权策略规则集合
    72  type CreateStatements = []CreateStatement
    73  
    74  // 创建授权策略参数
    75  type (
    76  	CreatePolicyParam = Request
    77  	jsonRequest       struct {
    78  		Alias       string           `json:"alias,omitempty"`       // 授权策略别名,由 `A-Za-z0-9` 组成
    79  		EditType    int64            `json:"edit_type,omitempty"`   // 1:是通过自定义 JSON 编辑的策略 2:是通过 UI 编辑的策略
    80  		Description string           `json:"description,omitempty"` // 授权策略描述
    81  		Statement   CreateStatements `json:"statement"`             // 授权策略规则集合
    82  	}
    83  )
    84  
    85  func (j *Request) MarshalJSON() ([]byte, error) {
    86  	if err := j.validate(); err != nil {
    87  		return nil, err
    88  	}
    89  	return json.Marshal(&jsonRequest{Alias: j.Alias, EditType: j.EditType, Description: j.Description, Statement: j.Statement})
    90  }
    91  
    92  func (j *Request) UnmarshalJSON(data []byte) error {
    93  	var nj jsonRequest
    94  	if err := json.Unmarshal(data, &nj); err != nil {
    95  		return err
    96  	}
    97  	j.Alias = nj.Alias
    98  	j.EditType = nj.EditType
    99  	j.Description = nj.Description
   100  	j.Statement = nj.Statement
   101  	return nil
   102  }
   103  
   104  func (j *Request) validate() error {
   105  	if len(j.Statement) == 0 {
   106  		return errors.MissingRequiredFieldError{Name: "Statement"}
   107  	}
   108  	for _, value := range j.Statement {
   109  		if err := value.validate(); err != nil {
   110  			return err
   111  		}
   112  	}
   113  	return nil
   114  }
   115  
   116  // 获取 API 所用的响应
   117  type Response struct {
   118  	Data CreatedPolicyData // 授权策略信息
   119  }
   120  
   121  // 授权策略规则的操作集合
   122  type CreatedActions = []string
   123  
   124  // 授权策略规则的资源集合
   125  type CreatedResources = []string
   126  
   127  // 授权策略规则
   128  type CreatedStatement struct {
   129  	Actions   CreatedActions   // 授权策略规则的操作集合
   130  	Resources CreatedResources // 授权策略规则的资源集合
   131  	Effect    string           // 授权策略规则的生效类型,允许访问或拒绝访问
   132  }
   133  type jsonCreatedStatement struct {
   134  	Actions   CreatedActions   `json:"action"`   // 授权策略规则的操作集合
   135  	Resources CreatedResources `json:"resource"` // 授权策略规则的资源集合
   136  	Effect    string           `json:"effect"`   // 授权策略规则的生效类型,允许访问或拒绝访问
   137  }
   138  
   139  func (j *CreatedStatement) MarshalJSON() ([]byte, error) {
   140  	if err := j.validate(); err != nil {
   141  		return nil, err
   142  	}
   143  	return json.Marshal(&jsonCreatedStatement{Actions: j.Actions, Resources: j.Resources, Effect: j.Effect})
   144  }
   145  
   146  func (j *CreatedStatement) UnmarshalJSON(data []byte) error {
   147  	var nj jsonCreatedStatement
   148  	if err := json.Unmarshal(data, &nj); err != nil {
   149  		return err
   150  	}
   151  	j.Actions = nj.Actions
   152  	j.Resources = nj.Resources
   153  	j.Effect = nj.Effect
   154  	return nil
   155  }
   156  
   157  func (j *CreatedStatement) validate() error {
   158  	if len(j.Actions) == 0 {
   159  		return errors.MissingRequiredFieldError{Name: "Actions"}
   160  	}
   161  	if len(j.Resources) == 0 {
   162  		return errors.MissingRequiredFieldError{Name: "Resources"}
   163  	}
   164  	if j.Effect == "" {
   165  		return errors.MissingRequiredFieldError{Name: "Effect"}
   166  	}
   167  	return nil
   168  }
   169  
   170  // 授权策略规则集合
   171  type CreatedStatements = []CreatedStatement
   172  
   173  // 授权策略信息
   174  type Data struct {
   175  	Id          string            // 记录 ID
   176  	RootUid     int64             // 根用户 uid
   177  	Alias       string            // 授权策略别名
   178  	Description string            // 授权策略描述
   179  	Enabled     bool              // 授权策略是否启用
   180  	CreatedAt   string            // 授权策略创建时间
   181  	UpdatedAt   string            // 授权策略上次更新时间
   182  	Statement   CreatedStatements // 授权策略规则集合
   183  }
   184  
   185  // 返回的授权策略信息
   186  type (
   187  	CreatedPolicyData = Data
   188  	jsonData          struct {
   189  		Id          string            `json:"id"`          // 记录 ID
   190  		RootUid     int64             `json:"root_uid"`    // 根用户 uid
   191  		Alias       string            `json:"alias"`       // 授权策略别名
   192  		Description string            `json:"description"` // 授权策略描述
   193  		Enabled     bool              `json:"enabled"`     // 授权策略是否启用
   194  		CreatedAt   string            `json:"created_at"`  // 授权策略创建时间
   195  		UpdatedAt   string            `json:"updated_at"`  // 授权策略上次更新时间
   196  		Statement   CreatedStatements `json:"statement"`   // 授权策略规则集合
   197  	}
   198  )
   199  
   200  func (j *Data) MarshalJSON() ([]byte, error) {
   201  	if err := j.validate(); err != nil {
   202  		return nil, err
   203  	}
   204  	return json.Marshal(&jsonData{Id: j.Id, RootUid: j.RootUid, Alias: j.Alias, Description: j.Description, Enabled: j.Enabled, CreatedAt: j.CreatedAt, UpdatedAt: j.UpdatedAt, Statement: j.Statement})
   205  }
   206  
   207  func (j *Data) UnmarshalJSON(data []byte) error {
   208  	var nj jsonData
   209  	if err := json.Unmarshal(data, &nj); err != nil {
   210  		return err
   211  	}
   212  	j.Id = nj.Id
   213  	j.RootUid = nj.RootUid
   214  	j.Alias = nj.Alias
   215  	j.Description = nj.Description
   216  	j.Enabled = nj.Enabled
   217  	j.CreatedAt = nj.CreatedAt
   218  	j.UpdatedAt = nj.UpdatedAt
   219  	j.Statement = nj.Statement
   220  	return nil
   221  }
   222  
   223  func (j *Data) validate() error {
   224  	if j.Id == "" {
   225  		return errors.MissingRequiredFieldError{Name: "Id"}
   226  	}
   227  	if j.RootUid == 0 {
   228  		return errors.MissingRequiredFieldError{Name: "RootUid"}
   229  	}
   230  	if j.Alias == "" {
   231  		return errors.MissingRequiredFieldError{Name: "Alias"}
   232  	}
   233  	if j.Description == "" {
   234  		return errors.MissingRequiredFieldError{Name: "Description"}
   235  	}
   236  	if j.CreatedAt == "" {
   237  		return errors.MissingRequiredFieldError{Name: "CreatedAt"}
   238  	}
   239  	if j.UpdatedAt == "" {
   240  		return errors.MissingRequiredFieldError{Name: "UpdatedAt"}
   241  	}
   242  	if len(j.Statement) == 0 {
   243  		return errors.MissingRequiredFieldError{Name: "Statement"}
   244  	}
   245  	for _, value := range j.Statement {
   246  		if err := value.validate(); err != nil {
   247  			return err
   248  		}
   249  	}
   250  	return nil
   251  }
   252  
   253  // 返回的授权策略响应
   254  type (
   255  	CreatedPolicyResp = Response
   256  	jsonResponse      struct {
   257  		Data CreatedPolicyData `json:"data"` // 授权策略信息
   258  	}
   259  )
   260  
   261  func (j *Response) MarshalJSON() ([]byte, error) {
   262  	if err := j.validate(); err != nil {
   263  		return nil, err
   264  	}
   265  	return json.Marshal(&jsonResponse{Data: j.Data})
   266  }
   267  
   268  func (j *Response) UnmarshalJSON(data []byte) error {
   269  	var nj jsonResponse
   270  	if err := json.Unmarshal(data, &nj); err != nil {
   271  		return err
   272  	}
   273  	j.Data = nj.Data
   274  	return nil
   275  }
   276  
   277  func (j *Response) validate() error {
   278  	if err := j.Data.validate(); err != nil {
   279  		return err
   280  	}
   281  	return nil
   282  }