yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/clouduser.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 huawei
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/huawei/client/modules"
    25  )
    26  
    27  type SLink struct {
    28  	Next     string
    29  	Previous string
    30  	Self     string
    31  }
    32  
    33  type SClouduser struct {
    34  	client *SHuaweiClient
    35  
    36  	Description       string
    37  	DomainId          string
    38  	Enabled           bool
    39  	ForceResetPwd     bool
    40  	Id                string
    41  	LastProjectId     string
    42  	Links             SLink
    43  	Name              string
    44  	PasswordExpiresAt string
    45  	PwdStatus         bool
    46  }
    47  
    48  func (user *SClouduser) GetGlobalId() string {
    49  	return user.Id
    50  }
    51  
    52  func (user *SClouduser) GetName() string {
    53  	return user.Name
    54  }
    55  
    56  func (user *SClouduser) GetEmailAddr() string {
    57  	return ""
    58  }
    59  
    60  func (user *SClouduser) GetInviteUrl() string {
    61  	return ""
    62  }
    63  
    64  func (user *SClouduser) GetISystemCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
    65  	return []cloudprovider.ICloudpolicy{}, nil
    66  }
    67  
    68  func (user *SClouduser) GetICustomCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
    69  	return []cloudprovider.ICloudpolicy{}, nil
    70  }
    71  
    72  func (user *SClouduser) AttachSystemPolicy(policyType string) error {
    73  	return cloudprovider.ErrNotSupported
    74  }
    75  
    76  func (user *SClouduser) AttachCustomPolicy(policyType string) error {
    77  	return cloudprovider.ErrNotSupported
    78  }
    79  
    80  func (user *SClouduser) DetachSystemPolicy(policyId string) error {
    81  	return cloudprovider.ErrNotSupported
    82  }
    83  
    84  func (user *SClouduser) DetachCustomPolicy(policyId string) error {
    85  	return cloudprovider.ErrNotSupported
    86  }
    87  
    88  func (user *SClouduser) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
    89  	groups, err := user.client.ListUserGroups(user.Id)
    90  	if err != nil {
    91  		return nil, errors.Wrap(err, "Users.ListGroups")
    92  	}
    93  	ret := []cloudprovider.ICloudgroup{}
    94  	for i := range groups {
    95  		groups[i].client = user.client
    96  		ret = append(ret, &groups[i])
    97  	}
    98  	return ret, nil
    99  }
   100  
   101  func (user *SClouduser) Delete() error {
   102  	return user.client.DeleteClouduser(user.Id)
   103  }
   104  
   105  func (user *SClouduser) IsConsoleLogin() bool {
   106  	return user.Enabled == true
   107  }
   108  
   109  func (user *SClouduser) ResetPassword(password string) error {
   110  	return user.client.ResetClouduserPassword(user.Id, password)
   111  }
   112  
   113  func (self *SHuaweiClient) DeleteClouduser(id string) error {
   114  	client, err := self.newGeneralAPIClient()
   115  	if err != nil {
   116  		return errors.Wrap(err, "newGeneralAPIClient")
   117  	}
   118  	_, err = client.Users.Delete(id)
   119  	return err
   120  }
   121  
   122  func (self *SHuaweiClient) ListUserGroups(userId string) ([]SCloudgroup, error) {
   123  	client, err := self.newGeneralAPIClient()
   124  	if err != nil {
   125  		return nil, errors.Wrap(err, "newGeneralAPIClient")
   126  	}
   127  	result, err := client.Users.ListGroups(userId)
   128  	if err != nil {
   129  		return nil, errors.Wrap(err, "Users.ListGroups")
   130  	}
   131  	groups := []SCloudgroup{}
   132  	err = jsonutils.Update(&groups, result.Data)
   133  	if err != nil {
   134  		return nil, errors.Wrap(err, "jsonutils.Update")
   135  	}
   136  	return groups, nil
   137  }
   138  
   139  func (self *SHuaweiClient) GetCloudusers(name string) ([]SClouduser, error) {
   140  	params := map[string]string{}
   141  	if len(name) > 0 {
   142  		params["name"] = name
   143  	}
   144  	users := []SClouduser{}
   145  	client, err := self.newGeneralAPIClient()
   146  	if err != nil {
   147  		return nil, errors.Wrap(err, "newGeneralAPIClient")
   148  	}
   149  	err = doListAllWithOffset(client.Users.List, params, &users)
   150  	if err != nil {
   151  		return nil, errors.Wrap(err, "doListAllWithOffset")
   152  	}
   153  	return users, nil
   154  }
   155  
   156  func (self *SHuaweiClient) GetICloudusers() ([]cloudprovider.IClouduser, error) {
   157  	users, err := self.GetCloudusers("")
   158  	if err != nil {
   159  		return nil, errors.Wrap(err, "GetCloudusers")
   160  	}
   161  	iUsers := []cloudprovider.IClouduser{}
   162  	for i := range users {
   163  		if users[i].Name != self.ownerName {
   164  			users[i].client = self
   165  			iUsers = append(iUsers, &users[i])
   166  		}
   167  	}
   168  	return iUsers, nil
   169  }
   170  
   171  func (self *SHuaweiClient) GetIClouduserByName(name string) (cloudprovider.IClouduser, error) {
   172  	users, err := self.GetCloudusers(name)
   173  	if err != nil {
   174  		return nil, errors.Wrapf(err, "GetCloudusers(%s)", name)
   175  	}
   176  	if len(users) == 0 {
   177  		return nil, cloudprovider.ErrNotFound
   178  	}
   179  	if len(users) > 1 {
   180  		return nil, cloudprovider.ErrDuplicateId
   181  	}
   182  	users[0].client = self
   183  	return &users[0], nil
   184  }
   185  
   186  func (self *SHuaweiClient) CreateIClouduser(conf *cloudprovider.SClouduserCreateConfig) (cloudprovider.IClouduser, error) {
   187  	return self.CreateClouduser(conf.Name, conf.Password, conf.Desc)
   188  }
   189  
   190  func (self *SHuaweiClient) CreateClouduser(name, password, desc string) (*SClouduser, error) {
   191  	params := map[string]string{
   192  		"name":      name,
   193  		"domain_id": self.ownerId,
   194  	}
   195  	if len(password) > 0 {
   196  		params["password"] = password
   197  	}
   198  	if len(desc) > 0 {
   199  		params["description"] = desc
   200  	}
   201  	client, err := self.newGeneralAPIClient()
   202  	if err != nil {
   203  		return nil, errors.Wrap(err, "newGeneralAPIClient")
   204  	}
   205  	user := SClouduser{client: self}
   206  	err = DoCreate(client.Users.Create, jsonutils.Marshal(map[string]interface{}{"user": params}), &user)
   207  	if err != nil {
   208  		ce, ok := err.(*modules.HuaweiClientError)
   209  		if ok && len(ce.Errorcode) > 0 && ce.Errorcode[0] == "1101" {
   210  			return nil, errors.Wrap(err, `IAM user name. The length is between 5 and 32. The first digit is not a number. Special characters can only contain the '_' '-' or ' '`) //https://support.huaweicloud.com/api-iam/iam_08_0015.html
   211  		}
   212  		return nil, errors.Wrap(err, "DoCreate")
   213  	}
   214  	return &user, nil
   215  }
   216  
   217  func (self *SHuaweiClient) ResetClouduserPassword(id, password string) error {
   218  	client, err := self.newGeneralAPIClient()
   219  	if err != nil {
   220  		return errors.Wrap(err, "newGeneralAPIClient")
   221  	}
   222  	return client.Users.ResetPassword(id, password)
   223  }
   224  
   225  type SAccessKey struct {
   226  	client *SHuaweiClient
   227  
   228  	AccessKey   string    `json:"access"`
   229  	Secret      string    `json:"secret"`
   230  	Description string    `json:"description"`
   231  	Status      string    `json:"status"`
   232  	CreatedAt   time.Time `json:"create_time"`
   233  }
   234  
   235  func (self *SHuaweiClient) GetAKSK(id string) ([]cloudprovider.SAccessKey, error) {
   236  	obj, err := self.getAKSKList(id)
   237  	if err != nil {
   238  		return nil, errors.Wrap(err, "SHuaweiClient.getAKSKList")
   239  	}
   240  	aks := make([]SAccessKey, 0)
   241  	obj.Unmarshal(&aks, "credentials")
   242  	res := make([]cloudprovider.SAccessKey, len(aks))
   243  	for i := 0; i < len(aks); i++ {
   244  		res[i].Name = aks[i].Description
   245  		res[i].AccessKey = aks[i].AccessKey
   246  		res[i].Secret = aks[i].Secret
   247  		res[i].Status = aks[i].Status
   248  		res[i].CreatedAt = aks[i].CreatedAt
   249  	}
   250  	return res, nil
   251  }
   252  
   253  func (self *SHuaweiClient) CreateAKSK(id, name string) (*cloudprovider.SAccessKey, error) {
   254  	params := map[string]interface{}{
   255  		"credential": map[string]interface{}{
   256  			"user_id":     id,
   257  			"description": name,
   258  		},
   259  	}
   260  	obj, err := self.createAKSK(params)
   261  	if err != nil {
   262  		return nil, errors.Wrap(err, "SHuaweiClient.createAKSK")
   263  	}
   264  	ak := SAccessKey{}
   265  	obj.Unmarshal(&ak, "credential")
   266  	res := cloudprovider.SAccessKey{
   267  		Name:      ak.Description,
   268  		AccessKey: ak.AccessKey,
   269  		Secret:    ak.Secret,
   270  	}
   271  	return &res, nil
   272  }
   273  
   274  func (self *SHuaweiClient) DeleteAKSK(accessKey string) error {
   275  	_, err := self.deleteAKSK(accessKey)
   276  	return err
   277  }
   278  
   279  func (user *SClouduser) DeleteAccessKey(accessKey string) error {
   280  	return user.client.DeleteAKSK(accessKey)
   281  }
   282  
   283  func (user *SClouduser) CreateAccessKey(name string) (*cloudprovider.SAccessKey, error) {
   284  	return user.client.CreateAKSK(user.Id, name)
   285  }
   286  
   287  func (user *SClouduser) GetAccessKeys() ([]cloudprovider.SAccessKey, error) {
   288  	return user.client.GetAKSK(user.Id)
   289  }