From 70e9746e06a6b20e1c82c649df7cfb6f4d0090ee Mon Sep 17 00:00:00 2001 From: Julian Sommer <115516918+juliansommer@users.noreply.github.com> Date: Fri, 5 Jun 2026 09:31:39 +1000 Subject: [PATCH] chore: tags cleanup --- .../handlers/ec2/snapshot/service_impl.go | 11 +------ spinifex/handlers/elbv2/service_impl.go | 33 +++++++++---------- spinifex/handlers/elbv2/service_impl_rules.go | 7 +--- spinifex/handlers/iam/instance_profiles.go | 8 +---- spinifex/handlers/iam/roles.go | 8 +---- spinifex/handlers/iam/service_impl.go | 20 +++++++---- 6 files changed, 32 insertions(+), 55 deletions(-) diff --git a/spinifex/handlers/ec2/snapshot/service_impl.go b/spinifex/handlers/ec2/snapshot/service_impl.go index 48faa14c6..dad9bddad 100644 --- a/spinifex/handlers/ec2/snapshot/service_impl.go +++ b/spinifex/handlers/ec2/snapshot/service_impl.go @@ -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 } diff --git a/spinifex/handlers/elbv2/service_impl.go b/spinifex/handlers/elbv2/service_impl.go index f4b091f79..21471cadf 100644 --- a/spinifex/handlers/elbv2/service_impl.go +++ b/spinifex/handlers/elbv2/service_impl.go @@ -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. diff --git a/spinifex/handlers/elbv2/service_impl_rules.go b/spinifex/handlers/elbv2/service_impl_rules.go index 56ec4e0da..c1818a868 100644 --- a/spinifex/handlers/elbv2/service_impl_rules.go +++ b/spinifex/handlers/elbv2/service_impl_rules.go @@ -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, diff --git a/spinifex/handlers/iam/instance_profiles.go b/spinifex/handlers/iam/instance_profiles.go index a4b86ead1..12a445d7f 100644 --- a/spinifex/handlers/iam/instance_profiles.go +++ b/spinifex/handlers/iam/instance_profiles.go @@ -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) diff --git a/spinifex/handlers/iam/roles.go b/spinifex/handlers/iam/roles.go index 83bed719a..63601a690 100644 --- a/spinifex/handlers/iam/roles.go +++ b/spinifex/handlers/iam/roles.go @@ -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) diff --git a/spinifex/handlers/iam/service_impl.go b/spinifex/handlers/iam/service_impl.go index f205e2f42..133b63214 100644 --- a/spinifex/handlers/iam/service_impl.go +++ b/spinifex/handlers/iam/service_impl.go @@ -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)