yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/cam_policy.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SPolicy struct {
    28  	client *SQcloudClient
    29  
    30  	PolicyId       int64
    31  	PolicyName     string
    32  	AddTime        time.Time
    33  	CreateMode     string
    34  	PolicyType     string
    35  	Description    string
    36  	PolicyDocument string
    37  }
    38  
    39  func (self *SPolicy) GetGlobalId() string {
    40  	return fmt.Sprintf("%d", self.PolicyId)
    41  }
    42  
    43  func (self *SPolicy) GetName() string {
    44  	return self.PolicyName
    45  }
    46  
    47  func (self *SPolicy) UpdateDocument(document *jsonutils.JSONDict) error {
    48  	return self.client.UpdatePolicy(int(self.PolicyId), document.String(), "")
    49  }
    50  
    51  func (self *SPolicy) GetDocument() (*jsonutils.JSONDict, error) {
    52  	if len(self.PolicyDocument) == 0 {
    53  		err := self.Refresh()
    54  		if err != nil {
    55  			return nil, errors.Wrapf(err, "Refesh")
    56  		}
    57  	}
    58  	jsonObj, err := jsonutils.Parse([]byte(self.PolicyDocument))
    59  	if err != nil {
    60  		return nil, errors.Wrapf(err, "jsonutils.Parse")
    61  	}
    62  	return jsonObj.(*jsonutils.JSONDict), nil
    63  }
    64  
    65  func (self *SPolicy) Delete() error {
    66  	return self.client.DeletePolicy([]int{int(self.PolicyId)})
    67  }
    68  
    69  func (self *SPolicy) Refresh() error {
    70  	p, err := self.client.GetPolicy(self.GetGlobalId())
    71  	if err != nil {
    72  		return errors.Wrapf(err, "GetPolicy(%s)", self.GetGlobalId())
    73  	}
    74  	// p.PolicyId一般为0,若jsonutils.Update会有问题
    75  	self.Description = p.Description
    76  	self.PolicyDocument = p.PolicyDocument
    77  	return nil
    78  }
    79  
    80  func (self *SPolicy) GetDescription() string {
    81  	if len(self.Description) == 0 {
    82  		err := self.Refresh()
    83  		if err != nil {
    84  			return ""
    85  		}
    86  	}
    87  	return self.Description
    88  }
    89  
    90  func (self *SQcloudClient) GetISystemCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
    91  	ret := []cloudprovider.ICloudpolicy{}
    92  	offset := 1
    93  	for {
    94  		part, total, err := self.ListPolicies("", "QCS", offset, 50)
    95  		if err != nil {
    96  			return nil, errors.Wrap(err, "ListPolicies")
    97  		}
    98  		for i := range part {
    99  			part[i].client = self
   100  			ret = append(ret, &part[i])
   101  		}
   102  		if len(ret) >= total {
   103  			break
   104  		}
   105  		offset += 1
   106  	}
   107  	return ret, nil
   108  }
   109  
   110  func (self *SQcloudClient) GetICustomCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
   111  	ret := []cloudprovider.ICloudpolicy{}
   112  	offset := 1
   113  	for {
   114  		part, total, err := self.ListPolicies("", "Local", offset, 50)
   115  		if err != nil {
   116  			return nil, errors.Wrap(err, "ListPolicies")
   117  		}
   118  		for i := range part {
   119  			part[i].client = self
   120  			ret = append(ret, &part[i])
   121  		}
   122  		if len(ret) >= total {
   123  			break
   124  		}
   125  		offset += 1
   126  	}
   127  	return ret, nil
   128  }
   129  
   130  func (self *SQcloudClient) ListAttachedUserPolicies(uin string, offset int, limit int) ([]SPolicy, int, error) {
   131  	if offset < 1 {
   132  		offset = 1
   133  	}
   134  	if limit <= 0 || limit > 50 {
   135  		limit = 50
   136  	}
   137  	params := map[string]string{
   138  		"TargetUin": uin,
   139  		"Page":      fmt.Sprintf("%d", offset),
   140  		"Rp":        fmt.Sprintf("%d", limit),
   141  	}
   142  	resp, err := self.camRequest("ListAttachedUserPolicies", params)
   143  	if err != nil {
   144  		return nil, 0, errors.Wrap(err, "camRequest.ListAttachedUserPolicies")
   145  	}
   146  	policies := []SPolicy{}
   147  	err = resp.Unmarshal(&policies, "List")
   148  	if err != nil {
   149  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
   150  	}
   151  	total, _ := resp.Float("TotalNum")
   152  	return policies, int(total), nil
   153  }
   154  
   155  func (self *SQcloudClient) AttachUserPolicy(uin, policyId string) error {
   156  	params := map[string]string{
   157  		"AttachUin": uin,
   158  		"PolicyId":  policyId,
   159  	}
   160  	_, err := self.camRequest("AttachUserPolicy", params)
   161  	return err
   162  }
   163  
   164  func (self *SQcloudClient) DetachUserPolicy(uin, policyId string) error {
   165  	params := map[string]string{
   166  		"DetachUin": uin,
   167  		"PolicyId":  policyId,
   168  	}
   169  	_, err := self.camRequest("DetachUserPolicy", params)
   170  	return err
   171  }
   172  
   173  // https://cloud.tencent.com/document/api/598/34570
   174  func (self *SQcloudClient) ListPolicies(keyword, scope string, offset int, limit int) ([]SPolicy, int, error) {
   175  	if offset < 1 {
   176  		offset = 1
   177  	}
   178  	if limit <= 0 || limit > 50 {
   179  		limit = 50
   180  	}
   181  	params := map[string]string{
   182  		"Page": fmt.Sprintf("%d", offset),
   183  		"Rp":   fmt.Sprintf("%d", limit),
   184  	}
   185  	if len(scope) > 0 {
   186  		params["Scope"] = scope
   187  	}
   188  	if len(keyword) > 0 {
   189  		params["Keyword"] = keyword
   190  	}
   191  	resp, err := self.camRequest("ListPolicies", params)
   192  	if err != nil {
   193  		return nil, 0, errors.Wrap(err, "camRequest.ListPolicies")
   194  	}
   195  	policies := []SPolicy{}
   196  	err = resp.Unmarshal(&policies, "List")
   197  	if err != nil {
   198  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
   199  	}
   200  	total, _ := resp.Float("TotalNum")
   201  	return policies, int(total), nil
   202  }
   203  
   204  func (self *SQcloudClient) GetPolicy(policyId string) (*SPolicy, error) {
   205  	params := map[string]string{
   206  		"PolicyId": policyId,
   207  	}
   208  	resp, err := self.camRequest("GetPolicy", params)
   209  	if err != nil {
   210  		return nil, errors.Wrap(err, "GetPolicy")
   211  	}
   212  	policy := SPolicy{}
   213  	err = resp.Unmarshal(&policy)
   214  	if err != nil {
   215  		return nil, errors.Wrap(err, "resp.Unmarshal")
   216  	}
   217  	return &policy, nil
   218  }
   219  
   220  func (self *SQcloudClient) DeletePolicy(policyIds []int) error {
   221  	params := map[string]string{}
   222  	for i, policyId := range policyIds {
   223  		params[fmt.Sprintf("PolicyId.%d", i)] = fmt.Sprintf("%d", policyId)
   224  	}
   225  	_, err := self.camRequest("DeletePolicy", params)
   226  	return err
   227  }
   228  
   229  func (self *SQcloudClient) GetPolicyByName(name string) (*SPolicy, error) {
   230  	policies := []SPolicy{}
   231  	offset := 1
   232  	for {
   233  		part, total, err := self.ListPolicies(name, "", offset, 50)
   234  		if err != nil {
   235  			return nil, errors.Wrapf(err, "ListPolicies")
   236  		}
   237  		policies = append(policies, part...)
   238  		if len(policies) >= total {
   239  			break
   240  		}
   241  		offset += 1
   242  	}
   243  	for i := range policies {
   244  		if policies[i].PolicyName == name {
   245  			policies[i].client = self
   246  			return &policies[i], nil
   247  		}
   248  	}
   249  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, "GetPolicyByName(%s)", name)
   250  }
   251  
   252  func (self *SQcloudClient) CreatePolicy(name, document, desc string) (*SPolicy, error) {
   253  	params := map[string]string{
   254  		"PolicyName":     name,
   255  		"PolicyDocument": document,
   256  		"Description":    desc,
   257  	}
   258  	_, err := self.camRequest("CreatePolicy", params)
   259  	if err != nil {
   260  		return nil, errors.Wrapf(err, "CreatePolicy")
   261  	}
   262  	return self.GetPolicyByName(name)
   263  }
   264  
   265  func (self *SQcloudClient) CreateICloudpolicy(opts *cloudprovider.SCloudpolicyCreateOptions) (cloudprovider.ICloudpolicy, error) {
   266  	policy, err := self.CreatePolicy(opts.Name, opts.Document.String(), opts.Desc)
   267  	if err != nil {
   268  		return nil, errors.Wrapf(err, "CreatePolicy")
   269  	}
   270  	return policy, nil
   271  }
   272  
   273  func (self *SQcloudClient) UpdatePolicy(policyId int, document, desc string) error {
   274  	params := map[string]string{
   275  		"PolicyId": fmt.Sprintf("%d", policyId),
   276  	}
   277  	if len(document) > 0 {
   278  		params["PolicyDocument"] = document
   279  	}
   280  	if len(desc) > 0 {
   281  		params["Description"] = desc
   282  	}
   283  	_, err := self.camRequest("UpdatePolicy", params)
   284  	return err
   285  }