diff --git a/pkg/multicloud/aws/elasticache_instance.go b/pkg/multicloud/aws/elasticache_instance.go index f3452f026..09fe34ce7 100644 --- a/pkg/multicloud/aws/elasticache_instance.go +++ b/pkg/multicloud/aws/elasticache_instance.go @@ -15,6 +15,7 @@ package aws import ( + "fmt" "strconv" "strings" "time" @@ -432,6 +433,68 @@ func (self *SElasticache) GetPrivateIpAddr() string { return "" } +func (self *SElasticache) GetTags() (map[string]string, error) { + params := map[string]string{ + "ResourceName": *self.replicaGroup.ARN, + } + tags := AwsTags{} + err := self.region.redisRequest("ListTagsForResource", params, &tags) + if err != nil { + return nil, errors.Wrapf(err, "ListTagsForResource") + } + return tags.GetTags() +} + +func (self *SElasticache) SetTags(tags map[string]string, replace bool) error { + oldTags, err := self.GetTags() + if err != nil { + return errors.Wrapf(err, "GetTags") + } + added, removed := map[string]string{}, map[string]string{} + for k, v := range tags { + oldValue, ok := oldTags[k] + if !ok { + added[k] = v + } else if oldValue != v { + removed[k] = oldValue + added[k] = v + } + } + if replace { + for k, v := range oldTags { + newValue, ok := tags[k] + if !ok { + removed[k] = v + } else if v != newValue { + added[k] = newValue + removed[k] = v + } + } + } + if len(removed) > 0 { + params := map[string]string{ + "ResourceName": *self.replicaGroup.ARN, + } + i := 1 + for k := range tags { + params[fmt.Sprintf("TagKeys.member.%d", i)] = k + } + return self.region.redisRequest("RemoveTagsFromResource", params, nil) + } + if len(added) > 0 { + params := map[string]string{ + "ResourceName": *self.replicaGroup.ARN, + } + i := 1 + for k, v := range tags { + params[fmt.Sprintf("Tags.member.%d.Key", i)] = k + params[fmt.Sprintf("Tags.member.%d.Value", i)] = v + } + return self.region.redisRequest("AddTagsToResource", params, nil) + } + return nil +} + func (self *SElasticache) GetPrivateConnectPort() int { for _, nodeGroup := range self.replicaGroup.NodeGroups { if nodeGroup != nil && nodeGroup.PrimaryEndpoint != nil && nodeGroup.PrimaryEndpoint.Port != nil { @@ -464,7 +527,7 @@ func (self *SElasticache) GetMaintainStartTime() string { } splited := strings.Split(window, "-") - return splited[0] + return strings.Trim(splited[0], "") } func (self *SElasticache) GetMaintainEndTime() string { @@ -479,7 +542,7 @@ func (self *SElasticache) GetMaintainEndTime() string { splited := strings.Split(window, "-") if len(splited) == 2 { - return splited[1] + return strings.Trim(splited[1], "") } return "" } diff --git a/pkg/multicloud/aws/region.go b/pkg/multicloud/aws/region.go index 4720f73c7..720c260ef 100644 --- a/pkg/multicloud/aws/region.go +++ b/pkg/multicloud/aws/region.go @@ -138,6 +138,7 @@ const ( ROUTE53_SERVICE_NAME = "route53" ELASTICACHE_SERVICE_NAME = "elasticache" + ELASTICACHE_SERVICE_ID = "ElastiCache" ELB_SERVICE_NAME = "elasticloadbalancing" ELB_SERVICE_ID = "Elastic Load Balancing v2" @@ -257,6 +258,10 @@ func (self *SRegion) rdsRequest(apiName string, params map[string]string, retval return self.client.request(self.RegionId, RDS_SERVICE_NAME, RDS_SERVICE_ID, "2014-10-31", apiName, params, retval, true) } +func (self *SRegion) redisRequest(apiName string, params map[string]string, retval interface{}) error { + return self.client.request(self.RegionId, ELASTICACHE_SERVICE_NAME, ELASTICACHE_SERVICE_ID, "2015-02-02", apiName, params, retval, true) +} + func (self *SRegion) ec2Request(apiName string, params map[string]string, retval interface{}) error { return self.client.request(self.RegionId, EC2_SERVICE_NAME, EC2_SERVICE_ID, "2016-11-15", apiName, params, retval, true) }