Skip to content

Commit

Permalink
Add handwritten sweeper for google_data_loss_prevention_discovery_con…
Browse files Browse the repository at this point in the history
…fig (#11429) (#19115)

[upstream:b3b07233e34229211bda7d28209047af8e108d1e]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 13, 2024
1 parent 479c901 commit b04c602
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .changelog/11429.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package datalossprevention

import (
"context"
"log"
"strings"
"testing"

"github.com/hashicorp/terraform-provider-google/google/envvar"
"github.com/hashicorp/terraform-provider-google/google/sweeper"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
Expand All @@ -50,21 +33,17 @@ func testSweepDataLossPreventionDiscoveryConfig(region string) error {
return err
}

t := &testing.T{}
billingId := envvar.GetTestBillingAccountFromEnv(t)

// Setup variables to replace in list template
d := &tpgresource.ResourceDataMock{
FieldsInSchema: map[string]interface{}{
"project": config.Project,
"region": region,
"location": region,
"zone": "-",
"billing_account": billingId,
"project": config.Project,
"region": region,
"location": region,
"zone": "-",
},
}

listTemplate := strings.Split("https://dlp.googleapis.com/v2/{{parent}}/discoveryConfigs", "?")[0]
listTemplate := strings.Split("https://dlp.googleapis.com/v2/projects/{{project}}/locations/{{location}}/discoveryConfigs", "?")[0]
listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err)
Expand Down Expand Up @@ -92,23 +71,18 @@ func testSweepDataLossPreventionDiscoveryConfig(region string) error {
rl := resourceList.([]interface{})

log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName)
// Keep count of items that aren't sweepable for logging.
nonPrefixCount := 0
for _, ri := range rl {
obj := ri.(map[string]interface{})
if obj["name"] == nil {
log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName)
return nil
}

// Note that we do not check for a sweepable prefix here.
// We can have at most 1 DiscoveryConfig for a storage type in the same project/location, so ensure we delete everything.
name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string))
// Skip resources that shouldn't be sweeped
if !sweeper.IsSweepableTestResource(name) {
nonPrefixCount++
continue
}

deleteTemplate := "https://dlp.googleapis.com/v2/{{parent}}/discoveryConfigs/{{name}}"
deleteTemplate := "https://dlp.googleapis.com/v2/projects/{{project}}/locations/{{location}}/discoveryConfigs/{{name}}"
deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
Expand All @@ -130,10 +104,5 @@ func testSweepDataLossPreventionDiscoveryConfig(region string) error {
log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name)
}
}

if nonPrefixCount > 0 {
log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount)
}

return nil
}

0 comments on commit b04c602

Please sign in to comment.