yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/cam_user.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  
    20  	"yunion.io/x/log"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SUser struct {
    28  	multicloud.SBaseClouduser
    29  	client *SQcloudClient
    30  
    31  	Uin          int64
    32  	Name         string
    33  	Uid          int64
    34  	Remark       string
    35  	ConsoleLogin int
    36  	CountryCode  string
    37  	Email        string
    38  }
    39  
    40  func (user *SUser) GetGlobalId() string {
    41  	return fmt.Sprintf("%d", user.Uin)
    42  }
    43  
    44  func (self *SUser) GetEmailAddr() string {
    45  	return self.Email
    46  }
    47  
    48  func (self *SUser) GetInviteUrl() string {
    49  	return ""
    50  }
    51  
    52  func (user *SUser) GetISystemCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
    53  	policies := []SPolicy{}
    54  	offset := 1
    55  	for {
    56  		part, total, err := user.client.ListAttachedUserPolicies(user.GetGlobalId(), offset, 50)
    57  		if err != nil {
    58  			return nil, errors.Wrap(err, "GetClouduserPolicy")
    59  		}
    60  		policies = append(policies, part...)
    61  		if len(policies) >= total {
    62  			break
    63  		}
    64  		offset += 1
    65  	}
    66  	ret := []cloudprovider.ICloudpolicy{}
    67  	for i := range policies {
    68  		if policies[i].PolicyType == "QCS" || policies[i].PolicyType == "" {
    69  			policies[i].client = user.client
    70  			ret = append(ret, &policies[i])
    71  		}
    72  	}
    73  	return ret, nil
    74  }
    75  
    76  func (user *SUser) GetICustomCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
    77  	policies := []SPolicy{}
    78  	offset := 1
    79  	for {
    80  		part, total, err := user.client.ListAttachedUserPolicies(user.GetGlobalId(), offset, 50)
    81  		if err != nil {
    82  			return nil, errors.Wrap(err, "GetClouduserPolicy")
    83  		}
    84  		policies = append(policies, part...)
    85  		if len(policies) >= total {
    86  			break
    87  		}
    88  		offset += 1
    89  	}
    90  	ret := []cloudprovider.ICloudpolicy{}
    91  	for i := range policies {
    92  		if policies[i].PolicyType == "User" {
    93  			policies[i].client = user.client
    94  			ret = append(ret, &policies[i])
    95  		}
    96  	}
    97  	return ret, nil
    98  }
    99  
   100  func (user *SUser) AttachSystemPolicy(policyId string) error {
   101  	return user.client.AttachUserPolicy(user.GetGlobalId(), policyId)
   102  }
   103  
   104  func (user *SUser) AttachCustomPolicy(policyId string) error {
   105  	return user.client.AttachUserPolicy(user.GetGlobalId(), policyId)
   106  }
   107  
   108  func (user *SUser) DetachSystemPolicy(policyId string) error {
   109  	return user.client.DetachUserPolicy(user.GetGlobalId(), policyId)
   110  }
   111  
   112  func (user *SUser) DetachCustomPolicy(policyId string) error {
   113  	return user.client.DetachUserPolicy(user.GetGlobalId(), policyId)
   114  }
   115  
   116  func (user *SUser) IsConsoleLogin() bool {
   117  	return user.ConsoleLogin == 1
   118  }
   119  
   120  func (user *SUser) Delete() error {
   121  	return user.client.DeleteUser(user.Name)
   122  }
   123  
   124  func (user *SUser) ResetPassword(password string) error {
   125  	return user.client.UpdateUser(user.Name, password)
   126  }
   127  
   128  func (user *SUser) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
   129  	ret := []cloudprovider.ICloudgroup{}
   130  	offset := 1
   131  	for {
   132  		part, total, err := user.client.ListGroupsForUser(int(user.Uin), offset, 100)
   133  		if err != nil {
   134  			return nil, errors.Wrapf(err, "ListGroupsForUser")
   135  		}
   136  		for i := range part {
   137  			part[i].client = user.client
   138  			ret = append(ret, &part[i])
   139  		}
   140  		if len(ret) >= total {
   141  			break
   142  		}
   143  		offset += 1
   144  	}
   145  	return ret, nil
   146  }
   147  
   148  func (user *SUser) GetName() string {
   149  	return user.Name
   150  }
   151  
   152  func (self *SQcloudClient) DeleteUser(name string) error {
   153  	params := map[string]string{
   154  		"Name":  name,
   155  		"Force": "1",
   156  	}
   157  	_, err := self.camRequest("DeleteUser", params)
   158  	return err
   159  }
   160  
   161  func (self *SQcloudClient) ListUsers() ([]SUser, error) {
   162  	resp, err := self.camRequest("ListUsers", nil)
   163  	if err != nil {
   164  		return nil, errors.Wrap(err, "camRequest.ListUsers")
   165  	}
   166  	users := []SUser{}
   167  	err = resp.Unmarshal(&users, "Data")
   168  	if err != nil {
   169  		return nil, errors.Wrap(err, "resp.Unmarshal")
   170  	}
   171  	return users, nil
   172  }
   173  
   174  func (self *SQcloudClient) ListGroupsForUser(uin int, offset, limit int) ([]SGroup, 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  		"SubUin": fmt.Sprintf("%d", uin),
   183  		"Page":   fmt.Sprintf("%d", offset),
   184  		"Rp":     fmt.Sprintf("%d", limit),
   185  	}
   186  	resp, err := self.camRequest("ListGroupsForUser", params)
   187  	if err != nil {
   188  		return nil, 0, errors.Wrap(err, "camRequest.ListGroupsForUser")
   189  	}
   190  	groups := []SGroup{}
   191  	err = resp.Unmarshal(&groups, "GroupInfo")
   192  	if err != nil {
   193  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
   194  	}
   195  	total, _ := resp.Float("TotalNum")
   196  	return groups, int(total), nil
   197  }
   198  
   199  func (self *SQcloudClient) GetICloudusers() ([]cloudprovider.IClouduser, error) {
   200  	users, err := self.ListUsers()
   201  	if err != nil {
   202  		return nil, errors.Wrap(err, "ListUsers")
   203  	}
   204  
   205  	ret := []cloudprovider.IClouduser{}
   206  	for i := range users {
   207  		users[i].client = self
   208  		ret = append(ret, &users[i])
   209  	}
   210  	collaborators := []SUser{}
   211  	for {
   212  		part, total, err := self.ListCollaborators(len(collaborators), 50)
   213  		if err != nil {
   214  			return nil, errors.Wrapf(err, "ListCollaborators")
   215  		}
   216  		collaborators = append(collaborators, part...)
   217  		if len(collaborators) >= total {
   218  			break
   219  		}
   220  	}
   221  	for i := range collaborators {
   222  		collaborators[i].client = self
   223  		ret = append(ret, &collaborators[i])
   224  	}
   225  	return ret, nil
   226  }
   227  
   228  func (self *SQcloudClient) GetIClouduserByName(name string) (cloudprovider.IClouduser, error) {
   229  	return self.GetUser(name)
   230  }
   231  
   232  func (self *SQcloudClient) GetUser(name string) (*SUser, error) {
   233  	params := map[string]string{
   234  		"Name": name,
   235  	}
   236  	resp, err := self.camRequest("GetUser", params)
   237  	if err != nil {
   238  		return nil, errors.Wrap(err, "camRequest.GetUser")
   239  	}
   240  	user := &SUser{client: self}
   241  	err = resp.Unmarshal(user)
   242  	if err != nil {
   243  		return nil, errors.Wrap(err, "resp.Unmarshal")
   244  	}
   245  	return user, nil
   246  }
   247  
   248  func (self *SQcloudClient) CreateIClouduser(conf *cloudprovider.SClouduserCreateConfig) (cloudprovider.IClouduser, error) {
   249  	user, err := self.AddUser(conf.Name, conf.Password, conf.Desc, conf.IsConsoleLogin)
   250  	if err != nil {
   251  		return nil, errors.Wrap(err, "CreateClouduser")
   252  	}
   253  	for _, policyId := range conf.ExternalPolicyIds {
   254  		err = user.client.AttachUserPolicy(fmt.Sprintf("%d", user.Uin), policyId)
   255  		if err != nil {
   256  			log.Errorf("attach policy %s for user %s error: %v", policyId, conf.Name, err)
   257  		}
   258  	}
   259  	return user, nil
   260  }
   261  
   262  func (self *SQcloudClient) AddUser(name, password, desc string, consoleLogin bool) (*SUser, error) {
   263  	params := map[string]string{
   264  		"Name":         name,
   265  		"Remark":       desc,
   266  		"ConsoleLogin": "0",
   267  	}
   268  	if len(password) > 0 {
   269  		params["Password"] = password
   270  	}
   271  	if consoleLogin {
   272  		params["ConsoleLogin"] = "1"
   273  	}
   274  	resp, err := self.camRequest("AddUser", params)
   275  	if err != nil {
   276  		return nil, errors.Wrap(err, "camRequest.AddUser")
   277  	}
   278  	user := &SUser{client: self}
   279  	err = resp.Unmarshal(user)
   280  	if err != nil {
   281  		return nil, errors.Wrap(err, "resp.Unmarshal")
   282  	}
   283  	return user, nil
   284  }
   285  
   286  func (self *SQcloudClient) UpdateUser(name, password string) error {
   287  	params := map[string]string{
   288  		"Name":         name,
   289  		"ConsoleLogin": "1",
   290  		"Password":     password,
   291  	}
   292  	_, err := self.camRequest("UpdateUser", params)
   293  	if err != nil {
   294  		return errors.Wrap(err, "UpdateUser")
   295  	}
   296  	return nil
   297  }