Skip to content

Commit

Permalink
Merge pull request #277 from ioito/hotfix/qx-elastic-cache-tags
Browse files Browse the repository at this point in the history
fix(aws): redis tags
  • Loading branch information
ioito authored May 4, 2023
2 parents 2fa935f + 495cd0b commit bc1e1d0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
67 changes: 65 additions & 2 deletions pkg/multicloud/aws/elasticache_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package aws

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 ""
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/multicloud/aws/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit bc1e1d0

Please sign in to comment.