Skip to content

Commit 37a9279

Browse files
authored
refactor: migrate AWS resources to generic Resource[C] pattern (#992)
* refactor: migrate AWS resources to generic Resource[C] pattern This refactoring introduces a type-safe generic resource pattern that simplifies AWS resource implementations and reduces code duplication. Key changes: - Introduce Resource[C] generic type with type-safe client handling - Add WrapAwsInitClient helper for AWS config initialization - Add batch deleters: SimpleBatchDeleter, SequentialDeleter, BulkDeleter, MultiStepDeleter, DeleteThenWait - Merge all *_types.go files into main resource files (55 files deleted) - Remove base_resource.go, common.go, globals.go (consolidated into adapter.go) - Update golangci-lint config for v2 compatibility - Fix context key type for SA1029 compliance - Add pagination support across all resources Net reduction: ~5,500 lines of code across 280 files changed. * refactor: add DefaultBatchSize constant and DefaultOnly support for EC2 resources - Add DefaultBatchSize=50 constant, standardize batch sizes across resources - Add NewEC2AwsResource helper for EC2 resources with DefaultOnly support - Expand awsDefaults command to delete VPC dependencies (endpoints, NAT gateways, ENIs, IGWs, subnets) before VPCs - Update EC2Endpoint, NatGateway, InternetGateway, NetworkInterface config types to EC2ResourceType - Remove unused nukeInternetGateway and nukeVpcEndpoint functions - Fix tests to use DefaultBatchSize constant * test: skip AWS-dependent tests when credentials unavailable
1 parent c450ebc commit 37a9279

File tree

329 files changed

+15834
-22700
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

329 files changed

+15834
-22700
lines changed

.golangci.yml

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version: "2"
2+
13
run:
24
timeout: 10m
35
tests: true
@@ -6,85 +8,68 @@ run:
68

79
linters:
810
enable:
9-
# Default linters (these come with golangci-lint)
10-
- errcheck # Checking for unchecked errors - critical for nil safety
11-
- gosimple # Simplify code
12-
- govet # Reports suspicious constructs including nil issues
13-
- ineffassign # Detects when assignments to existing variables are not used
14-
- staticcheck # Comprehensive static analysis including nil checks (SA5011)
15-
- typecheck # Like the front-end of a Go compiler, parses and type-checks
16-
- unused # Checks for unused constants, variables, functions and types
17-
18-
# Critical nil safety linters
19-
- nilnil # Checks that there is no simultaneous return of nil error and an invalid value
20-
- nilerr # Finds the code that returns nil even if it checks that the error is not nil
21-
11+
# Default linters
12+
- errcheck # Checking for unchecked errors
13+
- govet # Reports suspicious constructs
14+
- ineffassign # Detects unused assignments
15+
- staticcheck # Comprehensive static analysis
16+
- unused # Checks for unused code
17+
2218
# Code quality linters
23-
- bodyclose # Checks whether HTTP response body is closed successfully
24-
- durationcheck # Check for two common problems with time.Duration
25-
- errorlint # Find code that will cause problems with error handling
26-
- copyloopvar # Checks for pointers to enclosing loop variables (replaces exportloopref)
27-
- noctx # Finds sending http request without context.Context
28-
19+
- bodyclose # Checks HTTP response body is closed
20+
- durationcheck # Check time.Duration issues
21+
- errorlint # Error handling problems
22+
- copyloopvar # Checks loop variable captures
23+
- noctx # HTTP requests without context
24+
- nilerr # Returns nil even when error is not nil
25+
2926
# Style linters
30-
- gofmt # Checks whether code was gofmt-ed
31-
- goimports # Check import statements are formatted
32-
- misspell # Finds commonly misspelled English words in comments
33-
- whitespace # Detection of leading and trailing whitespace
27+
- misspell # Finds misspelled words
28+
- whitespace # Leading/trailing whitespace
3429

3530
disable:
36-
- depguard # Too restrictive for dependencies
37-
- exhaustive # Too strict for switch statements
38-
- gochecknoglobals # We use some globals
39-
- lll # Line length limit is too strict
40-
- wsl # Whitespace linter is too opinionated
31+
- depguard
32+
- exhaustive
33+
- gochecknoglobals
34+
- lll
35+
- wsl
36+
37+
formatters:
38+
enable:
39+
- gofmt
40+
- goimports
4141

4242
linters-settings:
4343
errcheck:
4444
check-type-assertions: true
4545
check-blank: true
46-
46+
4747
govet:
4848
enable:
49-
- shadow # Check for shadowed variables
50-
49+
- shadow
50+
5151
staticcheck:
52-
checks: ["all", "-ST1000", "-ST1003"] # Disable some stylistic checks
53-
54-
nilnil:
55-
checked-types:
56-
- ptr
57-
- func
58-
- iface
59-
- map
60-
- chan
52+
checks: ["all", "-ST1000", "-ST1003", "-QF1008", "-SA1029", "-SA9003"]
6153

6254
issues:
63-
# Only show issues on new/changed lines
64-
# This makes adoption easier for existing codebases
6555
new: true
66-
67-
# Exclude directories from all linters
56+
6857
exclude-dirs:
6958
- vendor
7059
- third_party
7160
- generated
72-
61+
7362
exclude-rules:
74-
# Exclude test files from some linters
7563
- path: _test\.go
7664
linters:
7765
- errcheck
7866
- noctx
79-
80-
# Exclude generated files
67+
- staticcheck
68+
8169
- path: "generated"
8270
linters:
8371
- staticcheck
8472
- govet
85-
86-
# Maximum issues count per one linter
73+
8774
max-issues-per-linter: 0
88-
89-
# Maximum count of issues with the same text
90-
max-same-issues: 0
75+
max-same-issues: 0

aws/aws_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package aws
22

33
import (
4-
"github.com/gruntwork-io/cloud-nuke/telemetry"
5-
"github.com/gruntwork-io/cloud-nuke/util"
4+
"os"
65
"testing"
76

7+
"github.com/gruntwork-io/cloud-nuke/telemetry"
8+
"github.com/gruntwork-io/cloud-nuke/util"
89
"github.com/stretchr/testify/assert"
910
)
1011

12+
// hasAWSCredentials checks if AWS credentials are available via environment variables.
13+
// This is a fast check that doesn't require calling AWS APIs.
14+
func hasAWSCredentials() bool {
15+
// Check for standard AWS credential environment variables
16+
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
17+
return true
18+
}
19+
// Check for AWS profile
20+
if os.Getenv("AWS_PROFILE") != "" {
21+
return true
22+
}
23+
// Check for web identity token (used in EKS/CI)
24+
if os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE") != "" {
25+
return true
26+
}
27+
return false
28+
}
29+
1130
func TestSplit(t *testing.T) {
1231
telemetry.InitTelemetry("cloud-nuke", "")
1332
t.Parallel()
@@ -34,7 +53,15 @@ func TestGetTargetRegions(t *testing.T) {
3453
telemetry.InitTelemetry("cloud-nuke", "")
3554
t.Parallel()
3655

37-
actualEnabledRegions, _ := GetEnabledRegions()
56+
// Skip if AWS credentials are not available (fast check via env vars)
57+
if !hasAWSCredentials() {
58+
t.Skip("Skipping test: AWS credentials not available")
59+
}
60+
61+
actualEnabledRegions, err := GetEnabledRegions()
62+
if err != nil {
63+
t.Skipf("Skipping test: failed to get enabled regions: %v", err)
64+
}
3865
assert.Greater(t, len(actualEnabledRegions), 0)
3966

4067
type test struct {

aws/query_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws
22

33
import (
4+
"os"
45
"testing"
56
"time"
67

@@ -12,8 +13,33 @@ import (
1213
"github.com/stretchr/testify/require"
1314
)
1415

16+
// hasAWSCredentialsForQuery checks if AWS credentials are available via environment variables.
17+
// This is a fast check that doesn't require calling AWS APIs.
18+
func hasAWSCredentialsForQuery() bool {
19+
// Check for standard AWS credential environment variables
20+
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
21+
return true
22+
}
23+
// Check for AWS profile
24+
if os.Getenv("AWS_PROFILE") != "" {
25+
return true
26+
}
27+
// Check for web identity token (used in EKS/CI)
28+
if os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE") != "" {
29+
return true
30+
}
31+
return false
32+
}
33+
1534
func TestNewQueryAcceptsValidExcludeAfterEntries(t *testing.T) {
1635
telemetry.InitTelemetry("cloud-nuke", "")
36+
37+
// Skip if AWS credentials are not available (fast check via env vars)
38+
// NewQuery validates regions against AWS, which requires credentials
39+
if !hasAWSCredentialsForQuery() {
40+
t.Skip("Skipping test: AWS credentials not available")
41+
}
42+
1743
type TestCase struct {
1844
Name string
1945
Regions []string

0 commit comments

Comments
 (0)