Skip to content

Commit 949f6b0

Browse files
containerattached: Add deletion policy. (#7134) (#13551)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent b745fbd commit 949f6b0

5 files changed

+117
-124
lines changed

.changelog/7134.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
containerattached: Add a deletion policy enum field.
3+
```

google/resource_container_attached_cluster.go

+25
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ the Workload Identity Pool.`,
308308
},
309309
},
310310
},
311+
"deletion_policy": {
312+
Type: schema.TypeString,
313+
Optional: true,
314+
Default: "DELETE",
315+
Description: `Policy to determine what flags to send on delete.`,
316+
},
311317
"project": {
312318
Type: schema.TypeString,
313319
Optional: true,
@@ -478,6 +484,12 @@ func resourceContainerAttachedClusterRead(d *schema.ResourceData, meta interface
478484
return handleNotFoundError(err, d, fmt.Sprintf("ContainerAttachedCluster %q", d.Id()))
479485
}
480486

487+
// Explicitly set virtual fields to default values if unset
488+
if _, ok := d.GetOkExists("deletion_policy"); !ok {
489+
if err := d.Set("deletion_policy", "DELETE"); err != nil {
490+
return fmt.Errorf("Error setting deletion_policy: %s", err)
491+
}
492+
}
481493
if err := d.Set("project", project); err != nil {
482494
return fmt.Errorf("Error reading Cluster: %s", err)
483495
}
@@ -722,6 +734,14 @@ func resourceContainerAttachedClusterDelete(d *schema.ResourceData, meta interfa
722734
}
723735

724736
var obj map[string]interface{}
737+
if v, ok := d.GetOk("deletion_policy"); ok {
738+
if v == "DELETE_IGNORE_ERRORS" {
739+
url, err = addQueryParams(url, map[string]string{"ignore_errors": "true"})
740+
if err != nil {
741+
return err
742+
}
743+
}
744+
}
725745
log.Printf("[DEBUG] Deleting Cluster %q", d.Id())
726746

727747
// err == nil indicates that the billing_project value was found
@@ -763,6 +783,11 @@ func resourceContainerAttachedClusterImport(d *schema.ResourceData, meta interfa
763783
}
764784
d.SetId(id)
765785

786+
// Explicitly set virtual fields to default values on import
787+
if err := d.Set("deletion_policy", "DELETE"); err != nil {
788+
return nil, fmt.Errorf("Error setting deletion_policy: %s", err)
789+
}
790+
766791
return []*schema.ResourceData{d}, nil
767792
}
768793

google/resource_container_attached_cluster_generated_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,60 @@ resource "google_container_attached_cluster" "primary" {
144144
`, context)
145145
}
146146

147+
func TestAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(t *testing.T) {
148+
t.Parallel()
149+
150+
context := map[string]interface{}{
151+
"random_suffix": randString(t, 10),
152+
}
153+
154+
vcrTest(t, resource.TestCase{
155+
PreCheck: func() { testAccPreCheck(t) },
156+
Providers: testAccProviders,
157+
CheckDestroy: testAccCheckContainerAttachedClusterDestroyProducer(t),
158+
Steps: []resource.TestStep{
159+
{
160+
Config: testAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(context),
161+
},
162+
{
163+
ResourceName: "google_container_attached_cluster.primary",
164+
ImportState: true,
165+
ImportStateVerify: true,
166+
ImportStateVerifyIgnore: []string{"location", "deletion_policy"},
167+
},
168+
},
169+
})
170+
}
171+
172+
func testAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(context map[string]interface{}) string {
173+
return Nprintf(`
174+
data "google_project" "project" {
175+
}
176+
177+
data "google_container_attached_versions" "versions" {
178+
location = "us-west1"
179+
project = data.google_project.project.project_id
180+
}
181+
182+
resource "google_container_attached_cluster" "primary" {
183+
name = "basic%{random_suffix}"
184+
location = "us-west1"
185+
project = data.google_project.project.project_id
186+
description = "Test cluster"
187+
distribution = "aks"
188+
oidc_config {
189+
issuer_url = "https://oidc.issuer.url"
190+
}
191+
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
192+
fleet {
193+
project = "projects/${data.google_project.project.number}"
194+
}
195+
196+
deletion_policy = "DELETE_IGNORE_ERRORS"
197+
}
198+
`, context)
199+
}
200+
147201
func testAccCheckContainerAttachedClusterDestroyProducer(t *testing.T) func(s *terraform.State) error {
148202
return func(s *terraform.State) error {
149203
for name, rs := range s.RootModule().Resources {

google/resource_container_attached_cluster_sweeper_test.go

-124
This file was deleted.

website/docs/r/container_attached_cluster.html.markdown

+35
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,40 @@ resource "google_container_attached_cluster" "primary" {
111111
}
112112
}
113113
```
114+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
115+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=container_attached_cluster_ignore_errors&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
116+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
117+
</a>
118+
</div>
119+
## Example Usage - Container Attached Cluster Ignore Errors
120+
121+
122+
```hcl
123+
data "google_project" "project" {
124+
}
125+
126+
data "google_container_attached_versions" "versions" {
127+
location = "us-west1"
128+
project = data.google_project.project.project_id
129+
}
130+
131+
resource "google_container_attached_cluster" "primary" {
132+
name = "basic"
133+
location = "us-west1"
134+
project = data.google_project.project.project_id
135+
description = "Test cluster"
136+
distribution = "aks"
137+
oidc_config {
138+
issuer_url = "https://oidc.issuer.url"
139+
}
140+
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
141+
fleet {
142+
project = "projects/${data.google_project.project.number}"
143+
}
144+
145+
deletion_policy = "DELETE_IGNORE_ERRORS"
146+
}
147+
```
114148

115149
## Argument Reference
116150

@@ -209,6 +243,7 @@ The following arguments are supported:
209243
* `project` - (Optional) The ID of the project in which the resource belongs.
210244
If it is not provided, the provider project is used.
211245

246+
* `deletion_policy` - (Optional) Policy to determine what flags to send on delete.
212247

213248
<a name="nested_logging_config"></a>The `logging_config` block supports:
214249

0 commit comments

Comments
 (0)