Skip to content

Commit

Permalink
consume dcl assured workloads issues fixes (#5082) (#9835)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Aug 17, 2021
1 parent 99e9711 commit 9622c40
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .changelog/5082.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
assuredworkloads: enhanced resource deletion so `google_assured_workloads_workload` can delete what it creates
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/hashicorp/terraform-provider-google
require (
cloud.google.com/go/bigtable v1.10.1
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87
github.com/apparentlymart/go-cidr v1.1.0
github.com/client9/misspell v0.3.4
github.com/davecgh/go-spew v1.1.1
Expand All @@ -26,5 +26,5 @@ require (
google.golang.org/api v0.48.0
google.golang.org/grpc v1.38.0
)

go 1.16
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-202107
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210728014810-286c21ee1916/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3 h1:hCgFUjbMh/nscHQ3Q8/GxFpTkbgytks+FoZt/e2qD3M=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87 h1:k1QG6P6FW079MKAzFsnNtzIXGHFaiSDZyWnPR5IBhqo=
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
Expand Down Expand Up @@ -1562,4 +1564,4 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
2 changes: 1 addition & 1 deletion google/resource_assured_workloads_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func AssuredWorkloadsWorkloadResourceSettingsSchema() *schema.Resource {
Optional: true,
ForceNew: true,
Description: ``,
ValidateFunc: validation.StringInSlice([]string{"RESOURCE_TYPE_UNSPECIFIED", "CONSUMER_PROJECT", "ENCRYPTION_KEYS_PROJECT", "KEYRING", ""}, false),
ValidateFunc: validation.StringInSlice([]string{"RESOURCE_TYPE_UNSPECIFIED", "CONSUMER_PROJECT", "ENCRYPTION_KEYS_PROJECT", "KEYRING", "CONSUMER_FOLDER", ""}, false),
},
},
}
Expand Down
86 changes: 0 additions & 86 deletions google/resource_assured_workloads_workload_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ import (
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
assuredworkloads "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"log"
"strconv"
"strings"
"testing"
"time"
)

func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
Expand All @@ -47,7 +43,6 @@ func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAssuredWorkloadsWorkload_BasicHandWritten(context),
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
},
{
ResourceName: "google_assured_workloads_workload.primary",
Expand All @@ -57,7 +52,6 @@ func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
},
{
Config: testAccAssuredWorkloadsWorkload_BasicHandWrittenUpdate0(context),
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
},
{
ResourceName: "google_assured_workloads_workload.primary",
Expand Down Expand Up @@ -85,7 +79,6 @@ func TestAccAssuredWorkloadsWorkload_FullHandWritten(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAssuredWorkloadsWorkload_FullHandWritten(context),
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
},
{
ResourceName: "google_assured_workloads_workload.primary",
Expand All @@ -97,85 +90,6 @@ func TestAccAssuredWorkloadsWorkload_FullHandWritten(t *testing.T) {
})
}

// deleteAssuredWorkloadProvisionedResources deletes the resources provisioned by
// assured workloads.. this is needed in order to delete the parent resource
func deleteAssuredWorkloadProvisionedResources(t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
timeout := *schema.DefaultTimeout(4 * time.Minute)
for _, rs := range s.RootModule().Resources {
if rs.Type != "google_assured_workloads_workload" {
continue
}
resourceAttributes := rs.Primary.Attributes
n, err := strconv.Atoi(resourceAttributes["resources.#"])
log.Printf("[DEBUG]found %v resources\n", n)
log.Println(resourceAttributes)
if err != nil {
return err
}

// first delete the projects
for i := 0; i < n; i++ {
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
if !strings.Contains(typee, "PROJECT") {
continue
}
resource_id := resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
log.Printf("[DEBUG] searching for project %s\n", resource_id)
err := retryTimeDuration(func() (reqErr error) {
_, reqErr = config.NewResourceManagerClient(config.userAgent).Projects.Get(resource_id).Do()
return reqErr
}, timeout)
if err != nil {
log.Printf("[DEBUG] did not find project %sn", resource_id)
continue
}
log.Printf("[DEBUG] found project %s\n", resource_id)

err = retryTimeDuration(func() error {
_, delErr := config.NewResourceManagerClient(config.userAgent).Projects.Delete(resource_id).Do()
return delErr
}, timeout)
if err != nil {
log.Printf("Error deleting project '%s': %s\n ", resource_id, err)
continue
}
log.Printf("[DEBUG] deleted project %s\n", resource_id)
}

// Then delete the folders
for i := 0; i < n; i++ {
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
if typee != "CONSUMER_FOLDER" {
continue
}
resource_id := "folders/" + resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
err := retryTimeDuration(func() error {
var reqErr error
_, reqErr = config.NewResourceManagerV2Client(config.userAgent).Folders.Get(resource_id).Do()
return reqErr
}, timeout)
log.Printf("[DEBUG] searching for folder %s\n", resource_id)
if err != nil {
log.Printf("[DEBUG] did not find folder %sn", resource_id)
continue
}
log.Printf("[DEBUG] found folder %s\n", resource_id)
err = retryTimeDuration(func() error {
_, reqErr := config.NewResourceManagerV2Client(config.userAgent).Folders.Delete(resource_id).Do()
return reqErr
}, timeout)
if err != nil {
return fmt.Errorf("Error deleting folder '%s': %s\n ", resource_id, err)
}
log.Printf("[DEBUG] deleted folder %s\n", resource_id)
}
}
return nil
}
}

func testAccAssuredWorkloadsWorkload_BasicHandWritten(context map[string]interface{}) string {
return Nprintf(`
resource "google_assured_workloads_workload" "primary" {
Expand Down
2 changes: 1 addition & 1 deletion google/resource_composer_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
composer "google.golang.org/api/composer/v1beta1"
"google.golang.org/api/composer/v1beta1"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/assured_workloads_workload.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ The `resource_settings` block supports:

* `resource_id` -
(Optional)
Resource identifier. For a project this represents project_id. If the project is already taken, the workload creation will fail.
Resource identifier. For a project this represents project_number. If the project is already taken, the workload creation will fail.

* `resource_type` -
(Optional)
Indicates the type of resource. This field should be specified to correspond the id to the right project type (CONSUMER_PROJECT or ENCRYPTION_KEYS_PROJECT) Possible values: RESOURCE_TYPE_UNSPECIFIED, CONSUMER_PROJECT, ENCRYPTION_KEYS_PROJECT, KEYRING
Indicates the type of resource. This field should be specified to correspond the id to the right project type (CONSUMER_PROJECT or ENCRYPTION_KEYS_PROJECT) Possible values: RESOURCE_TYPE_UNSPECIFIED, CONSUMER_PROJECT, ENCRYPTION_KEYS_PROJECT, KEYRING, CONSUMER_FOLDER

## Attributes Reference

Expand Down

0 comments on commit 9622c40

Please sign in to comment.