Skip to content

Commit 935039d

Browse files
consume dcl assured workloads issues fixes (#5082) (#3533)
Signed-off-by: Modular Magician <[email protected]>
1 parent 9d835b2 commit 935039d

File tree

7 files changed

+12
-93
lines changed

7 files changed

+12
-93
lines changed

.changelog/5082.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
assuredworkloads: enhanced resource deletion so `google_assured_workloads_workload` can delete what it creates
3+
```

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module github.com/hashicorp/terraform-provider-google-beta
22
require (
33
cloud.google.com/go/bigtable v1.10.1
4-
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3
4+
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87
55
github.com/apparentlymart/go-cidr v1.1.0
66
github.com/client9/misspell v0.3.4
77
github.com/davecgh/go-spew v1.1.1
@@ -26,5 +26,5 @@ require (
2626
google.golang.org/api v0.48.0
2727
google.golang.org/grpc v1.38.0
2828
)
29-
29+
3030
go 1.16

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-202107
7575
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210728014810-286c21ee1916/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
7676
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3 h1:hCgFUjbMh/nscHQ3Q8/GxFpTkbgytks+FoZt/e2qD3M=
7777
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210803014808-0fdb1dc411b3/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
78+
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87 h1:k1QG6P6FW079MKAzFsnNtzIXGHFaiSDZyWnPR5IBhqo=
79+
github.com/GoogleCloudPlatform/declarative-resource-client-library v0.0.0-20210812005055-c5fc24943f87/go.mod h1:oEeBHikdF/NrnUy0ornVaY1OT+jGvTqm+LQS0+ZDKzU=
7880
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
7981
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
8082
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
@@ -1562,4 +1564,4 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
15621564
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
15631565
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
15641566
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
1565-
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
1567+
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=

google-beta/resource_assured_workloads_workload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func AssuredWorkloadsWorkloadResourceSettingsSchema() *schema.Resource {
171171
Optional: true,
172172
ForceNew: true,
173173
Description: ``,
174-
ValidateFunc: validation.StringInSlice([]string{"RESOURCE_TYPE_UNSPECIFIED", "CONSUMER_PROJECT", "ENCRYPTION_KEYS_PROJECT", "KEYRING", ""}, false),
174+
ValidateFunc: validation.StringInSlice([]string{"RESOURCE_TYPE_UNSPECIFIED", "CONSUMER_PROJECT", "ENCRYPTION_KEYS_PROJECT", "KEYRING", "CONSUMER_FOLDER", ""}, false),
175175
},
176176
},
177177
}

google-beta/resource_assured_workloads_workload_generated_test.go

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ import (
2121
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
2222
assuredworkloads "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/assuredworkloads/beta"
2323
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
24-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2524
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
26-
"log"
27-
"strconv"
2825
"strings"
2926
"testing"
30-
"time"
3127
)
3228

3329
func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
@@ -47,7 +43,6 @@ func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
4743
Steps: []resource.TestStep{
4844
{
4945
Config: testAccAssuredWorkloadsWorkload_BasicHandWritten(context),
50-
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
5146
},
5247
{
5348
ResourceName: "google_assured_workloads_workload.primary",
@@ -57,7 +52,6 @@ func TestAccAssuredWorkloadsWorkload_BasicHandWritten(t *testing.T) {
5752
},
5853
{
5954
Config: testAccAssuredWorkloadsWorkload_BasicHandWrittenUpdate0(context),
60-
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
6155
},
6256
{
6357
ResourceName: "google_assured_workloads_workload.primary",
@@ -85,7 +79,6 @@ func TestAccAssuredWorkloadsWorkload_FullHandWritten(t *testing.T) {
8579
Steps: []resource.TestStep{
8680
{
8781
Config: testAccAssuredWorkloadsWorkload_FullHandWritten(context),
88-
Check: resource.ComposeTestCheckFunc(deleteAssuredWorkloadProvisionedResources(t)),
8982
},
9083
{
9184
ResourceName: "google_assured_workloads_workload.primary",
@@ -97,85 +90,6 @@ func TestAccAssuredWorkloadsWorkload_FullHandWritten(t *testing.T) {
9790
})
9891
}
9992

100-
// deleteAssuredWorkloadProvisionedResources deletes the resources provisioned by
101-
// assured workloads.. this is needed in order to delete the parent resource
102-
func deleteAssuredWorkloadProvisionedResources(t *testing.T) resource.TestCheckFunc {
103-
return func(s *terraform.State) error {
104-
config := googleProviderConfig(t)
105-
timeout := *schema.DefaultTimeout(4 * time.Minute)
106-
for _, rs := range s.RootModule().Resources {
107-
if rs.Type != "google_assured_workloads_workload" {
108-
continue
109-
}
110-
resourceAttributes := rs.Primary.Attributes
111-
n, err := strconv.Atoi(resourceAttributes["resources.#"])
112-
log.Printf("[DEBUG]found %v resources\n", n)
113-
log.Println(resourceAttributes)
114-
if err != nil {
115-
return err
116-
}
117-
118-
// first delete the projects
119-
for i := 0; i < n; i++ {
120-
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
121-
if !strings.Contains(typee, "PROJECT") {
122-
continue
123-
}
124-
resource_id := resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
125-
log.Printf("[DEBUG] searching for project %s\n", resource_id)
126-
err := retryTimeDuration(func() (reqErr error) {
127-
_, reqErr = config.NewResourceManagerClient(config.userAgent).Projects.Get(resource_id).Do()
128-
return reqErr
129-
}, timeout)
130-
if err != nil {
131-
log.Printf("[DEBUG] did not find project %sn", resource_id)
132-
continue
133-
}
134-
log.Printf("[DEBUG] found project %s\n", resource_id)
135-
136-
err = retryTimeDuration(func() error {
137-
_, delErr := config.NewResourceManagerClient(config.userAgent).Projects.Delete(resource_id).Do()
138-
return delErr
139-
}, timeout)
140-
if err != nil {
141-
log.Printf("Error deleting project '%s': %s\n ", resource_id, err)
142-
continue
143-
}
144-
log.Printf("[DEBUG] deleted project %s\n", resource_id)
145-
}
146-
147-
// Then delete the folders
148-
for i := 0; i < n; i++ {
149-
typee := resourceAttributes[fmt.Sprintf("resources.%d.resource_type", i)]
150-
if typee != "CONSUMER_FOLDER" {
151-
continue
152-
}
153-
resource_id := "folders/" + resourceAttributes[fmt.Sprintf("resources.%d.resource_id", i)]
154-
err := retryTimeDuration(func() error {
155-
var reqErr error
156-
_, reqErr = config.NewResourceManagerV2Client(config.userAgent).Folders.Get(resource_id).Do()
157-
return reqErr
158-
}, timeout)
159-
log.Printf("[DEBUG] searching for folder %s\n", resource_id)
160-
if err != nil {
161-
log.Printf("[DEBUG] did not find folder %sn", resource_id)
162-
continue
163-
}
164-
log.Printf("[DEBUG] found folder %s\n", resource_id)
165-
err = retryTimeDuration(func() error {
166-
_, reqErr := config.NewResourceManagerV2Client(config.userAgent).Folders.Delete(resource_id).Do()
167-
return reqErr
168-
}, timeout)
169-
if err != nil {
170-
return fmt.Errorf("Error deleting folder '%s': %s\n ", resource_id, err)
171-
}
172-
log.Printf("[DEBUG] deleted folder %s\n", resource_id)
173-
}
174-
}
175-
return nil
176-
}
177-
}
178-
17993
func testAccAssuredWorkloadsWorkload_BasicHandWritten(context map[string]interface{}) string {
18094
return Nprintf(`
18195
resource "google_assured_workloads_workload" "primary" {

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

website/docs/r/assured_workloads_workload.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ The `resource_settings` block supports:
118118

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

123123
* `resource_type` -
124124
(Optional)
125-
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
125+
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
126126

127127
## Attributes Reference
128128

0 commit comments

Comments
 (0)