Skip to content

Commit 9853a58

Browse files
committed
refactor: add type-safe WrapGcpInitClient helper for GCP resources
- Add WrapGcpInitClient helper matching AWS's WrapAwsInitClient pattern - Add GCP constants (DefaultBatchSize, DefaultWaitTimeout) for consistency - Improve GCP adapter documentation to match AWS quality - Update gcs_bucket.go to use WrapGcpInitClient with proper error handling - Replace silent failures with panics for programming errors
1 parent 37a9279 commit 9853a58

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

gcp/resources/adapter.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@ package resources
22

33
import (
44
"context"
5+
"fmt"
6+
"time"
57

68
"github.com/gruntwork-io/cloud-nuke/resource"
79
)
810

11+
const (
12+
DefaultWaitTimeout = 5 * time.Minute
13+
DefaultBatchSize = 50
14+
)
15+
16+
// GcpInitClientFunc is the type-safe client initialization function signature.
17+
type GcpInitClientFunc[C any] func(r *resource.Resource[C], projectID string)
18+
19+
// WrapGcpInitClient converts a GcpInitClientFunc to the generic InitClient signature.
20+
// Panics on type assertion failure since that indicates a programming error.
21+
func WrapGcpInitClient[C any](fn GcpInitClientFunc[C]) func(r *resource.Resource[C], cfg any) {
22+
return func(r *resource.Resource[C], cfg any) {
23+
projectID, ok := cfg.(string)
24+
if !ok {
25+
panic(fmt.Sprintf("WrapGcpInitClient: expected string projectID, got %T", cfg))
26+
}
27+
fn(r, projectID)
28+
}
29+
}
30+
931
// GcpResourceAdapter wraps a generic Resource to satisfy the GcpResource interface.
10-
// It provides type-safe Init(string) for GCP's project ID initialization.
1132
type GcpResourceAdapter[C any] struct {
1233
*resource.Resource[C]
1334
}
@@ -27,5 +48,4 @@ func (g *GcpResourceAdapter[C]) Nuke(ctx context.Context, identifiers []string)
2748
return g.Resource.Nuke(ctx, identifiers)
2849
}
2950

30-
// Compile-time interface satisfaction check
3151
var _ GcpResource = (*GcpResourceAdapter[any])(nil)

gcp/resources/gcs_bucket.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@ import (
1818
func NewGCSBuckets() GcpResource {
1919
return NewGcpResource(&resource.Resource[*storage.Client]{
2020
ResourceTypeName: "gcs-bucket",
21-
BatchSize: 50,
22-
InitClient: func(r *resource.Resource[*storage.Client], cfg any) {
23-
projectID, ok := cfg.(string)
24-
if !ok {
25-
logging.Debugf("Invalid config type for GCS client: expected string")
26-
return
27-
}
21+
BatchSize: DefaultBatchSize,
22+
InitClient: WrapGcpInitClient(func(r *resource.Resource[*storage.Client], projectID string) {
2823
r.Scope.ProjectID = projectID
2924
client, err := storage.NewClient(context.Background())
3025
if err != nil {
31-
logging.Debugf("Failed to create GCS client: %v", err)
32-
return
26+
panic(fmt.Sprintf("failed to create GCS client: %v", err))
3327
}
3428
r.Client = client
35-
},
29+
}),
3630
ConfigGetter: func(c config.Config) config.ResourceType {
3731
return c.GCSBucket
3832
},

0 commit comments

Comments
 (0)