Skip to content

feat(gcp): add Cloud SQL instance resource#1066

Open
Amit2465 wants to merge 1 commit intogruntwork-io:masterfrom
Amit2465:ay/gcp-cloudsql
Open

feat(gcp): add Cloud SQL instance resource#1066
Amit2465 wants to merge 1 commit intogruntwork-io:masterfrom
Amit2465:ay/gcp-cloudsql

Conversation

@Amit2465
Copy link
Contributor

Description

Add GCP Cloud SQL instance (cloud-sql-instance) as a new supported cloud-nuke resource.

How it works

List: Paginates through all Cloud SQL instances in the project. Instances in transient states (PENDING_CREATE, PENDING_DELETE, MAINTENANCE, ONLINE_MAINTENANCE, REPAIRING), instances with deletion protection enabled, and externally managed instances (EXTERNAL backend / ON_PREMISES_INSTANCE type) are skipped. Read replicas and read pool nodes are returned before primary instances to ensure correct deletion order, the Cloud SQL API rejects deletion of a primary that still has active replicas.

Delete: Sends a delete request for each instance and polls the returned long-running operation until it reaches DONE. Already-deleted instances (404) are handled gracefully.

Identifier format: Composite — {project}/{instance}

Test output

=== RUN   TestCloudSQLInstances_ResourceName
--- PASS: TestCloudSQLInstances_ResourceName (0.00s)
=== RUN   TestCloudSQLInstances_MaxBatchSize
--- PASS: TestCloudSQLInstances_MaxBatchSize (0.00s)
PASS
ok  	github.com/gruntwork-io/cloud-nuke/gcp/resources	0.029s

Pre-commit checks:

  • gofmt — no issues
  • golangci-lint run ./... — 0 issues
  • go build ./... — clean
  • go vet ./... — clean

Manually verified against a real GCP project:

  • inspect-gcp correctly lists Cloud SQL instances
  • Deletion completes successfully with operation polling

TODOs

Read the Gruntwork contribution guidelines.

  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • Include release notes. If this PR is backward incompatible, include a migration guide.
  • Attention Grunts - if this PR adds support for a new resource, ensure the nuke_sandbox and nuke_phxdevops jobs in .circleci/config.yml have been updated with appropriate exclusions (either directly in the job or via the .circleci/nuke_config.yml file) to prevent nuking IAM roles, groups, resources, etc that are important for the test accounts. (Note: nuke_sandbox and nuke_phxdevops only run aws, so no exclusions are needed for this new GCP resource.)

Release Notes (draft)

Added GCP Cloud SQL instance (cloud-sql-instance) as a new supported resource type.

Migration Guide

This PR introduces cloud-sql-instance as a new GCP resource type. Since cloud-nuke automatically includes all registered resource types, Cloud SQL instances will be nuked by default if no config file is provided.
To opt out, add the following to your cloud-nuke.yml config file:

GcpCloudSQLInstance:
  exclude:
    names_regex:
      - ".*"

@Amit2465 Amit2465 requested a review from denis256 as a code owner March 15, 2026 11:38
@james00012
Copy link
Contributor

Looks good overall. A few items:

  1. Global vs regional registration — Cloud SQL instances are regional but registered in getRegisteredGlobalResources(). Is this intentional? Could instances in some regions get missed?
  2. Missing t.Parallel() in both tests in cloudsql_test.go.
  3. Use go-commons/errors — listCloudSQLInstances, deleteCloudSQLInstance, and waitForCloudSQLOperation use fmt.Errorf. Project convention is errors.WithStackTrace().
  4. Replica ordering test: The replicas-before-primaries ordering is a correctness invariant for deletion. Worth adding a unit test for it.

Add support for nuking GCP Cloud SQL instances using the generic
resource pattern. Instances are discovered via paginated list and
deleted sequentially with long-running operation polling.

Safety:
- Skip instances in transient states (PENDING_CREATE, PENDING_DELETE,
  MAINTENANCE, ONLINE_MAINTENANCE, REPAIRING)
- Skip deletion-protected instances with a warning
- Skip externally managed instances (EXTERNAL backend /
  ON_PREMISES_INSTANCE type)
- Delete read replicas and read pool nodes before primary instances
  to satisfy API ordering constraints
@Amit2465
Copy link
Contributor Author

Thanks for the review, all fixed.

  • Global registration is intentionalInstances.List is project-scoped and returns all
    instances across all regions in one call, same as all other GCP resources here.
  • Wrapped all fmt.Errorf returns with goerrors.WithStackTrace (aliased to keep stdlib
    errors available for errors.As).
  • Added t.Parallel() to all tests and introduced a CloudSQLInstancesAPI interface for
    mocking to add TestCloudSQLInstances_ReplicaOrdering verifying replicas always precede
    primaries. this is correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants