feat: extract tag helper

This commit is contained in:
Julian Sommer
2026-04-01 15:27:43 +10:00
parent f5ec8c3bae
commit 91e4efb0ef
8 changed files with 30 additions and 89 deletions
+1 -11
View File
@@ -94,20 +94,10 @@ func (s *EgressOnlyIGWServiceImpl) CreateEgressOnlyInternetGateway(input *ec2.Cr
EgressOnlyInternetGatewayId: eigwID,
VpcId: *input.VpcId,
State: "attached",
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "egress-only-internet-gateway"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "egress-only-internet-gateway" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal Egress-only IGW record: %w", err)
+1 -12
View File
@@ -93,21 +93,10 @@ func (s *EIPServiceImpl) AllocateAddress(input *ec2.AllocateAddressInput, accoun
PublicIp: publicIP,
PoolName: poolName,
State: "allocated",
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "elastic-ip"),
CreatedAt: time.Now(),
}
// Store tags from input.
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "elastic-ip" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal EIP record: %w", err)
+1 -11
View File
@@ -91,20 +91,10 @@ func (s *IGWServiceImpl) createIGW(input *ec2.CreateInternetGatewayInput, accoun
record := IGWRecord{
InternetGatewayId: igwID,
State: "available",
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "internet-gateway"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "internet-gateway" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal IGW record: %w", err)
+1 -11
View File
@@ -268,23 +268,13 @@ func (s *SnapshotServiceImpl) CreateSnapshot(input *ec2.CreateSnapshotInput, acc
Encrypted: volumeConfig.VolumeMetadata.IsEncrypted,
OwnerID: accountID,
AvailabilityZone: volumeConfig.VolumeMetadata.AvailabilityZone,
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "snapshot"),
}
if input.Description != nil {
snapshotCfg.Description = *input.Description
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "snapshot" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
snapshotCfg.Tags[*tag.Key] = *tag.Value
}
}
}
}
// Track the volume→snapshot dependency in KV before persisting to S3.
// This ensures we never have an untracked snapshot in S3.
if err := s.addSnapshotRef(volumeID, snapshotID); err != nil {
+1 -11
View File
@@ -100,20 +100,10 @@ func (s *VPCServiceImpl) CreateNetworkInterface(input *ec2.CreateNetworkInterfac
Description: description,
Status: "available",
SecurityGroupIds: sgIds,
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "network-interface"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "network-interface" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal ENI record: %w", err)
+1 -11
View File
@@ -112,20 +112,10 @@ func (s *VPCServiceImpl) CreateSecurityGroup(input *ec2.CreateSecurityGroupInput
VpcId: vpcId,
IngressRules: []SGRule{},
EgressRules: defaultEgress,
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "security-group"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "security-group" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal security group record: %w", err)
+2 -22
View File
@@ -220,20 +220,10 @@ func (s *VPCServiceImpl) CreateVpc(input *ec2.CreateVpcInput, accountID string)
VNI: vni,
EnableDnsSupport: true, // AWS default
EnableDnsHostnames: false, // AWS default
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "vpc"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "vpc" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal VPC record: %w", err)
@@ -494,20 +484,10 @@ func (s *VPCServiceImpl) CreateSubnet(input *ec2.CreateSubnetInput, accountID st
AvailabilityZone: az,
State: "available",
IsDefault: false,
Tags: make(map[string]string),
Tags: utils.ExtractTags(input.TagSpecifications, "subnet"),
CreatedAt: time.Now(),
}
for _, tagSpec := range input.TagSpecifications {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == "subnet" {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
record.Tags[*tag.Key] = *tag.Value
}
}
}
}
data, err := json.Marshal(record)
if err != nil {
return nil, fmt.Errorf("failed to marshal subnet record: %w", err)
+22
View File
@@ -0,0 +1,22 @@
package utils
import (
"github.com/aws/aws-sdk-go/service/ec2"
)
// ExtractTags returns a map of tags from the TagSpecification that matches
// the given resourceType. If no specification matches, the returned map is
// empty (never nil).
func ExtractTags(tagSpecs []*ec2.TagSpecification, resourceType string) map[string]string {
tags := make(map[string]string)
for _, tagSpec := range tagSpecs {
if tagSpec.ResourceType != nil && *tagSpec.ResourceType == resourceType {
for _, tag := range tagSpec.Tags {
if tag.Key != nil && tag.Value != nil {
tags[*tag.Key] = *tag.Value
}
}
}
}
return tags
}