Skip to content

Commit

Permalink
containerattached: Add deletion policy. (#7134) (#13551)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 23, 2023
1 parent b745fbd commit 949f6b0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 124 deletions.
3 changes: 3 additions & 0 deletions .changelog/7134.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
containerattached: Add a deletion policy enum field.
```
25 changes: 25 additions & 0 deletions google/resource_container_attached_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ the Workload Identity Pool.`,
},
},
},
"deletion_policy": {
Type: schema.TypeString,
Optional: true,
Default: "DELETE",
Description: `Policy to determine what flags to send on delete.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -478,6 +484,12 @@ func resourceContainerAttachedClusterRead(d *schema.ResourceData, meta interface
return handleNotFoundError(err, d, fmt.Sprintf("ContainerAttachedCluster %q", d.Id()))
}

// Explicitly set virtual fields to default values if unset
if _, ok := d.GetOkExists("deletion_policy"); !ok {
if err := d.Set("deletion_policy", "DELETE"); err != nil {
return fmt.Errorf("Error setting deletion_policy: %s", err)
}
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
Expand Down Expand Up @@ -722,6 +734,14 @@ func resourceContainerAttachedClusterDelete(d *schema.ResourceData, meta interfa
}

var obj map[string]interface{}
if v, ok := d.GetOk("deletion_policy"); ok {
if v == "DELETE_IGNORE_ERRORS" {
url, err = addQueryParams(url, map[string]string{"ignore_errors": "true"})
if err != nil {
return err
}
}
}
log.Printf("[DEBUG] Deleting Cluster %q", d.Id())

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

// Explicitly set virtual fields to default values on import
if err := d.Set("deletion_policy", "DELETE"); err != nil {
return nil, fmt.Errorf("Error setting deletion_policy: %s", err)
}

return []*schema.ResourceData{d}, nil
}

Expand Down
54 changes: 54 additions & 0 deletions google/resource_container_attached_cluster_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,60 @@ resource "google_container_attached_cluster" "primary" {
`, context)
}

func TestAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerAttachedClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(context),
},
{
ResourceName: "google_container_attached_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "deletion_policy"},
},
},
})
}

func testAccContainerAttachedCluster_containerAttachedClusterIgnoreErrorsExample(context map[string]interface{}) string {
return Nprintf(`
data "google_project" "project" {
}
data "google_container_attached_versions" "versions" {
location = "us-west1"
project = data.google_project.project.project_id
}
resource "google_container_attached_cluster" "primary" {
name = "basic%{random_suffix}"
location = "us-west1"
project = data.google_project.project.project_id
description = "Test cluster"
distribution = "aks"
oidc_config {
issuer_url = "https://oidc.issuer.url"
}
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
fleet {
project = "projects/${data.google_project.project.number}"
}
deletion_policy = "DELETE_IGNORE_ERRORS"
}
`, context)
}

func testAccCheckContainerAttachedClusterDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
124 changes: 0 additions & 124 deletions google/resource_container_attached_cluster_sweeper_test.go

This file was deleted.

35 changes: 35 additions & 0 deletions website/docs/r/container_attached_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ resource "google_container_attached_cluster" "primary" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgit.luolix.top%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">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Container Attached Cluster Ignore Errors


```hcl
data "google_project" "project" {
}
data "google_container_attached_versions" "versions" {
location = "us-west1"
project = data.google_project.project.project_id
}
resource "google_container_attached_cluster" "primary" {
name = "basic"
location = "us-west1"
project = data.google_project.project.project_id
description = "Test cluster"
distribution = "aks"
oidc_config {
issuer_url = "https://oidc.issuer.url"
}
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
fleet {
project = "projects/${data.google_project.project.number}"
}
deletion_policy = "DELETE_IGNORE_ERRORS"
}
```

## Argument Reference

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

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

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

Expand Down

0 comments on commit 949f6b0

Please sign in to comment.