chore: tags cleanup

This commit is contained in:
Julian Sommer
2026-06-05 09:31:39 +10:00
parent af690b3299
commit 70e9746e06
6 changed files with 32 additions and 55 deletions
+1 -10
View File
@@ -181,16 +181,7 @@ func snapshotConfigToEC2(cfg *SnapshotConfig) *ec2.Snapshot {
OwnerId: aws.String(cfg.OwnerID),
}
if len(cfg.Tags) > 0 {
tags := make([]*ec2.Tag, 0, len(cfg.Tags))
for key, value := range cfg.Tags {
tags = append(tags, &ec2.Tag{
Key: aws.String(key),
Value: aws.String(value),
})
}
snapshot.Tags = tags
}
snapshot.Tags = utils.MapToEC2Tags(cfg.Tags)
return snapshot
}
+15 -18
View File
@@ -1066,12 +1066,7 @@ func (s *ELBv2ServiceImpl) CreateLoadBalancer(input *elbv2.CreateLoadBalancerInp
}
}
tags := make(map[string]string)
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
tags[*tag.Key] = *tag.Value
}
}
tags := tagsFromSDK(input.Tags)
// Create ENIs in each subnet (when VPC service is available)
var eniIDs []string
@@ -1450,12 +1445,7 @@ func (s *ELBv2ServiceImpl) CreateTargetGroup(input *elbv2.CreateTargetGroupInput
tgID := utils.GenerateResourceID("tg")
tgArn := buildTGArn(s.region, accountID, name, tgID)
tags := make(map[string]string)
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
tags[*tag.Key] = *tag.Value
}
}
tags := tagsFromSDK(input.Tags)
record := &TargetGroupRecord{
TargetGroupArn: tgArn,
@@ -2078,12 +2068,7 @@ func (s *ELBv2ServiceImpl) CreateListener(input *elbv2.CreateListenerInput, acco
actions = append(actions, action)
}
tags := make(map[string]string)
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
tags[*tag.Key] = *tag.Value
}
}
tags := tagsFromSDK(input.Tags)
record := &ListenerRecord{
ListenerArn: listenerArn,
@@ -2464,6 +2449,18 @@ func (s *ELBv2ServiceImpl) DescribeTags(input *elbv2.DescribeTagsInput, accountI
}, nil
}
// tagsFromSDK converts an SDK Tag slice into a tag map, skipping entries with a
// nil key or value. Returns a non-nil (possibly empty) map for storage.
func tagsFromSDK(tags []*elbv2.Tag) map[string]string {
m := make(map[string]string, len(tags))
for _, tag := range tags {
if tag.Key != nil && tag.Value != nil {
m[*tag.Key] = *tag.Value
}
}
return m
}
// tagsMapToSDK converts a tag map into the SDK Tag slice with deterministic
// (sorted-by-key) ordering. Returns nil for empty/nil input so the response
// matches AWS for untagged resources.
@@ -86,12 +86,7 @@ func (s *ELBv2ServiceImpl) CreateRule(input *elbv2.CreateRuleInput, accountID st
return nil, errors.New(awserrors.ErrorServerInternal)
}
tags := make(map[string]string)
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
tags[*tag.Key] = *tag.Value
}
}
tags := tagsFromSDK(input.Tags)
record := &RuleRecord{
RuleArn: ruleArn,
+1 -7
View File
@@ -42,13 +42,7 @@ func (s *IAMServiceImpl) CreateInstanceProfile(accountID string, input *iam.Crea
ARN: fmt.Sprintf("arn:aws:iam::%s:instance-profile%s%s", accountID, path, profileName),
Path: path,
CreatedAt: time.Now().UTC().Format(time.RFC3339),
Tags: []Tag{},
}
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
profile.Tags = append(profile.Tags, Tag{Key: *tag.Key, Value: *tag.Value})
}
Tags: copyTags(input.Tags),
}
data, err := json.Marshal(profile)
+1 -7
View File
@@ -69,13 +69,7 @@ func (s *IAMServiceImpl) CreateRole(accountID string, input *iam.CreateRoleInput
MaxSessionDuration: maxSession,
CreatedAt: time.Now().UTC().Format(time.RFC3339),
AttachedPolicies: []string{},
Tags: []Tag{},
}
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
role.Tags = append(role.Tags, Tag{Key: *tag.Key, Value: *tag.Value})
}
Tags: copyTags(input.Tags),
}
data, err := json.Marshal(role)
+13 -7
View File
@@ -207,6 +207,18 @@ func getOrCreateBucket(js nats.JetStreamContext, name string, history uint8, rep
return kv, nil
}
// copyTags converts SDK IAM tags into the stored Tag slice, skipping entries
// with a nil key or value. Returns a non-nil (possibly empty) slice.
func copyTags(tags []*iam.Tag) []Tag {
out := make([]Tag, 0, len(tags))
for _, tag := range tags {
if tag.Key != nil && tag.Value != nil {
out = append(out, Tag{Key: *tag.Key, Value: *tag.Value})
}
}
return out
}
// ---------------------------------------------------------------------------
// User CRUD
// ---------------------------------------------------------------------------
@@ -239,16 +251,10 @@ func (s *IAMServiceImpl) CreateUser(accountID string, input *iam.CreateUserInput
Path: path,
CreatedAt: time.Now().UTC().Format(time.RFC3339),
AccessKeys: []string{},
Tags: []Tag{},
Tags: copyTags(input.Tags),
AttachedPolicies: []string{},
}
for _, tag := range input.Tags {
if tag.Key != nil && tag.Value != nil {
user.Tags = append(user.Tags, Tag{Key: *tag.Key, Value: *tag.Value})
}
}
data, err := json.Marshal(user)
if err != nil {
return nil, fmt.Errorf("marshal user: %w", err)