yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/elasticache_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 aws
    16  
    17  import (
    18  	"github.com/aws/aws-sdk-go/service/elasticache"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  func (region *SRegion) DescribeUsers(engine string) ([]*elasticache.User, error) {
    28  	ecClient, err := region.getAwsElasticacheClient()
    29  	if err != nil {
    30  		return nil, errors.Wrap(err, "client.getAwsElasticacheClient")
    31  	}
    32  
    33  	input := elasticache.DescribeUsersInput{}
    34  	if len(engine) > 0 {
    35  		input.Engine = &engine
    36  	}
    37  	marker := ""
    38  	maxrecords := (int64)(50)
    39  	input.MaxRecords = &maxrecords
    40  
    41  	users := []*elasticache.User{}
    42  	for {
    43  		if len(marker) >= 0 {
    44  			input.Marker = &marker
    45  		}
    46  		out, err := ecClient.DescribeUsers(&input)
    47  		if err != nil {
    48  			return nil, errors.Wrap(err, "ecClient.DescribeCacheClusters")
    49  		}
    50  		users = append(users, out.Users...)
    51  
    52  		if out.Marker != nil && len(*out.Marker) > 0 {
    53  			marker = *out.Marker
    54  		} else {
    55  			break
    56  		}
    57  	}
    58  
    59  	return users, nil
    60  }
    61  
    62  type SElasticacheUser struct {
    63  	multicloud.SElasticcacheAccountBase
    64  	AwsTags
    65  	region *SRegion
    66  	user   *elasticache.User
    67  }
    68  
    69  func (self *SElasticacheUser) GetId() string {
    70  	return *self.user.UserId
    71  }
    72  
    73  func (self *SElasticacheUser) GetName() string {
    74  	return *self.user.UserName
    75  }
    76  
    77  func (self *SElasticacheUser) GetGlobalId() string {
    78  	return self.GetId()
    79  }
    80  
    81  func (self *SElasticacheUser) GetStatus() string {
    82  	if self.user == nil || self.user.Status == nil {
    83  		return ""
    84  	}
    85  	//  "active", "modifying" or "deleting"
    86  	switch *self.user.Status {
    87  	case "active":
    88  		return api.ELASTIC_CACHE_ACCOUNT_STATUS_AVAILABLE
    89  	case "modifying":
    90  		return api.ELASTIC_CACHE_ACCOUNT_STATUS_MODIFYING
    91  	case "deleting":
    92  		return api.ELASTIC_CACHE_ACCOUNT_STATUS_DELETING
    93  	default:
    94  		return ""
    95  	}
    96  }
    97  
    98  func (self *SElasticacheUser) Refresh() error {
    99  	users, err := self.region.DescribeUsers("")
   100  	if err != nil {
   101  		return errors.Wrap(err, "region.DescribeUsers")
   102  	}
   103  	for i := range users {
   104  		if users[i] != nil && users[i].UserId != nil && *users[i].UserId == self.GetId() {
   105  			self.user = users[i]
   106  		}
   107  	}
   108  	return nil
   109  }
   110  
   111  func (self *SElasticacheUser) GetAccountType() string {
   112  	return ""
   113  }
   114  
   115  func (self *SElasticacheUser) GetAccountPrivilege() string {
   116  	if self.user != nil && self.user.AccessString != nil {
   117  		return *self.user.AccessString
   118  	}
   119  	return ""
   120  }
   121  
   122  func (self *SElasticacheUser) Delete() error {
   123  	return cloudprovider.ErrNotSupported
   124  }
   125  
   126  func (self *SElasticacheUser) ResetPassword(input cloudprovider.SCloudElasticCacheAccountResetPasswordInput) error {
   127  	return cloudprovider.ErrNotSupported
   128  }
   129  
   130  func (self *SElasticacheUser) UpdateAccount(input cloudprovider.SCloudElasticCacheAccountUpdateInput) error {
   131  	return cloudprovider.ErrNotSupported
   132  }