refactor: introduce generic Resource[C] pattern for GCP resources#983
Merged
james00012 merged 1 commit intomasterfrom Dec 31, 2025
Merged
refactor: introduce generic Resource[C] pattern for GCP resources#983james00012 merged 1 commit intomasterfrom
james00012 merged 1 commit intomasterfrom
Conversation
168d468 to
1c81d9d
Compare
d2ccd14 to
e8a7a2a
Compare
denis256
reviewed
Dec 30, 2025
gcp/gcp.go
Outdated
|
|
||
| // Prepare context for the resource | ||
| if err := resourceType.PrepareContext(context.Background(), resourceConfig); err != nil { | ||
| if err := resourceType.PrepareContext(context.Background()); err != nil { |
Member
There was a problem hiding this comment.
I suspect that resource timeouts will be lost wit this since result from GetAndSetResourceConfig() is lost
Contributor
Author
There was a problem hiding this comment.
Great find. Thanks for the thorought review. It should be ready for another review.
8a6b2a8 to
69252b0
Compare
This PR introduces a generic Resource[C] pattern to reduce boilerplate when implementing cloud resource types. Starting with GCP as a proof of concept before migrating AWS resources. ## Changes ### New `resource/` package - `Resource[C]` - Generic struct for all nukeable resources - `Scope` - Cloud provider-specific scope (region/projectID) - `SimpleBatchDeleter` - Concurrent deletion with semaphore - `SequentialDeleter` - Sequential deletion for rate-limited APIs - `MultiStepDeleter` - Multi-step deletion (e.g., empty then delete) ### GCP changes - `GcpResourceAdapter[C]` - Minimal adapter for GcpResource interface - `GcpResource.Nuke(ctx, identifiers)` - Context passed directly - `NewGCSBuckets()` - Constructor using generic pattern - Extracted `nukeResource()` helper for proper defer scope - Added `IsNukable` filtering before nuking - Uses `util.Split` instead of duplicate `splitIntoBatches` ### Improvements - `report.Record` in batch deleters for consistent reporting - `errors.Is()` and `%w` for proper error handling - Type aliases in `gcp/types.go` to avoid import cycles - Comprehensive tests in `resource/resource_test.go`
68000b4 to
21806af
Compare
denis256
approved these changes
Dec 30, 2025
james00012
added a commit
that referenced
this pull request
Dec 31, 2025
Migrates AWS resources to use the same generic Resource[C] pattern introduced for GCP in #983. This is the first batch of 10 resources. Changes: - Add AwsResourceAdapter[C] to wrap generic resources for AWS interface - Update AwsResource.Nuke() signature to accept context.Context - Migrate 10 simple resources to generic pattern: - AccessAnalyzer - CloudWatchDashboards - CloudWatchLogGroups - EC2KeyPairs - EC2PlacementGroups - EventBridgeSchedule - KinesisFirehose - KinesisStreams - LaunchConfigs - SesEmailTemplates Benefits: - Consolidates _types.go + .go files into single .go file - Uses factory functions (NewXxx()) instead of struct literals - Context passed directly to Nuke() instead of stored in struct - Consistent pattern between AWS and GCP resources Remaining 111 AWS resources can be migrated incrementally.
3 tasks
james00012
added a commit
that referenced
this pull request
Dec 31, 2025
Migrates AWS resources to use the same generic Resource[C] pattern introduced for GCP in #983. This is the first batch of 10 resources. Changes: - Add AwsResourceAdapter[C] to wrap generic resources for AWS interface - Update AwsResource.Nuke() signature to accept context.Context - Migrate 10 simple resources to generic pattern: - AccessAnalyzer - CloudWatchDashboards - CloudWatchLogGroups - EC2KeyPairs - EC2PlacementGroups - EventBridgeSchedule - KinesisFirehose - KinesisStreams - LaunchConfigs - SesEmailTemplates Benefits: - Consolidates _types.go + .go files into single .go file - Uses factory functions (NewXxx()) instead of struct literals - Context passed directly to Nuke() instead of stored in struct - Consistent pattern between AWS and GCP resources Remaining 111 AWS resources can be migrated incrementally.
james00012
added a commit
that referenced
this pull request
Dec 31, 2025
* feat: introduce generic Resource[C] pattern for AWS (first batch) Migrates AWS resources to use the same generic Resource[C] pattern introduced for GCP in #983. This is the first batch of 10 resources. Changes: - Add AwsResourceAdapter[C] to wrap generic resources for AWS interface - Update AwsResource.Nuke() signature to accept context.Context - Migrate 10 simple resources to generic pattern: - AccessAnalyzer - CloudWatchDashboards - CloudWatchLogGroups - EC2KeyPairs - EC2PlacementGroups - EventBridgeSchedule - KinesisFirehose - KinesisStreams - LaunchConfigs - SesEmailTemplates Benefits: - Consolidates _types.go + .go files into single .go file - Uses factory functions (NewXxx()) instead of struct literals - Context passed directly to Nuke() instead of stored in struct - Consistent pattern between AWS and GCP resources Remaining 111 AWS resources can be migrated incrementally. * refactor: improve generic Resource[C] pattern for testability and correctness Changes: - Use interface types instead of concrete clients for mockability - Add BulkDeleter helper for APIs with batch delete support - Fix missing pagination in KinesisFirehose, LaunchConfigs, SesEmailTemplates - Fix logging levels: errors use Errorf, deletion start uses Infof - Add unit tests for list, filter, and delete functions * refactor: remove duplicate delete logs from individual functions The batch_deleter already logs "[OK] Deleted X" for each item, so individual delete functions don't need to log again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a generic
Resource[C]pattern to standardize how cloud resources are defined and managed. Starting with GCP resources as a proof-of-concept, this pattern will eventually be applied to all 100+ AWS resources.Why this refactor?
Current state: Each resource type (EC2, S3, GCS Buckets, etc.) has its own struct with duplicated boilerplate:
identifiers []stringfieldnukables map[string]errorfieldResourceIdentifiers(),IsNukable(),MaxBatchSize()methodsNew approach: A single generic
Resource[C]struct handles all common behavior. Resource implementations only need to provide:What changed
New
resource/package:resource.go- CoreResource[C]generic struct with shared behaviorbatch_deleter.go- Reusable deletion strategies (SimpleBatchDeleter,SequentialDeleter,MultiStepDeleter)resource_test.go- Unit testsGCP migration:
gcp/resources/adapter.go- BridgesResource[C]toGcpResourceinterfacegcp/resources/types.go-GcpResourceinterface and container typesgcp/resources/gcs_bucket.go- Rewritten using generic patterngcs_bucket_types.goandbase_resource.go(no longer needed)Benefits
Backward compatibility
gcp.GcpResourceinterface preserved (via type aliases)Next steps
After this PR is validated, apply the same pattern to AWS resources incrementally, starting with simpler resources like
AccessAnalyzer.Test plan
go build ./...compiles successfullygo test ./resource/...passesgo test ./gcp/...passescloud-nuke gcp --dry-runwith real GCP project