Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force_destroy field to google_storage_managed_folder #11303

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions mmv1/products/storage/ManagedFolder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ kind: 'storage#managedFolder'
base_url: 'b/{{bucket}}/managedFolders'
self_link: 'b/{{bucket}}/managedFolders/{{%name}}'
id_format: '{{bucket}}/{{name}}'
delete_url: 'b/{{bucket}}/managedFolders/{{%name}}'
delete_url: 'b/{{bucket}}/managedFolders/{{%name}}?allowNonEmpty={{force_destroy}}'
has_self_link: true
immutable: true
skip_sweeper: true # Skipping sweeper since this is a child resource.
skip_sweeper: true # Skipping sweeper since this is a child resource.
description: |
A Google Cloud Storage Managed Folder.

Expand All @@ -41,30 +40,45 @@ references: !ruby/object:Api::Resource::ReferenceLinks
import_format:
- '{{bucket}}/managedFolders/{{%name}}'
- '{{bucket}}/{{%name}}'
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_update: templates/terraform/custom_update/storage_managed_folder.go.erb
examples:
- !ruby/object:Provider::Terraform::Examples
name: 'storage_managed_folder_basic'
primary_resource_id: 'folder'
vars:
bucket_name: 'my-bucket'
ignore_read_extra:
- 'force_destroy'
parameters:
- !ruby/object:Api::Type::ResourceRef
name: 'bucket'
resource: 'Bucket'
imports: 'name'
description: 'The name of the bucket that contains the managed folder.'
required: true
immutable: true
- !ruby/object:Api::Type::String
name: 'name'
description: |
The name of the managed folder expressed as a path. Must include
trailing '/'. For example, `example_dir/example_dir2/`.
required: true
immutable: true
# The API returns values with trailing slashes, even if not
# provided. Enforcing trailing slashes prevents diffs and ensures
# consistent output.
validation: !ruby/object:Provider::Terraform::Validation
regex: '/$'
virtual_fields:
- !ruby/object:Api::Type::Boolean
name: 'force_destroy'
default_value: false
description: |
Allows the deletion of a managed folder even if contains
objects. If a non-empty managed folder is deleted, any objects
within the folder will remain in a simulated folder with the
same name.
properties:
- !ruby/object:Api::Type::String
name: createTime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_ = config

// we can only get here if force_destroy was updated
SarahFrench marked this conversation as resolved.
Show resolved Hide resolved
if d.Get("force_destroy") != nil {
if err := d.Set("force_destroy", d.Get("force_destroy")); err != nil {
return fmt.Errorf("Error updating force_destroy: %s", err)
}
}

// all other fields are immutable, don't do anything else
return nil
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ resource "google_storage_bucket" "bucket" {
}

resource "google_storage_managed_folder" "<%= ctx[:primary_resource_id] %>" {
bucket = google_storage_bucket.bucket.name
name = "managed/folder/name/"
bucket = google_storage_bucket.bucket.name
name = "managed/folder/name/"
force_destroy = true
SarahFrench marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package storage_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

func TestAccStorageManagedFolder_storageManagedFolderUpdate(t *testing.T) {
t.Parallel()
bucketName := fmt.Sprintf("tf-test-managed-folder-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccStorageManagedFolder_bucket(bucketName) + testAccStorageManagedFolder_managedFolder(false),
},
{
ResourceName: "google_storage_managed_folder.folder",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"bucket", "force_destroy"},
},
{
Config: testAccStorageManagedFolder_bucket(bucketName),
ExpectError: regexp.MustCompile(`Error 409: The managed folder you tried to delete is not empty.`),
},
{
Config: testAccStorageManagedFolder_bucket(bucketName) + testAccStorageManagedFolder_managedFolder(true),
},
{
ResourceName: "google_storage_managed_folder.folder",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"bucket", "force_destroy"},
},
{
Config: testAccStorageManagedFolder_bucket(bucketName),
},
},
})
}

func testAccStorageManagedFolder_bucket(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "object" {
name = "managed/folder/name/file.txt"
content = "This file will affect the folder being deleted if allowNonEmpty=false"
bucket = google_storage_bucket.bucket.name
}
`, bucketName)
}

func testAccStorageManagedFolder_managedFolder(forceDestroy bool) string {
return fmt.Sprintf(`
resource "google_storage_managed_folder" "folder" {
bucket = google_storage_bucket.bucket.name
name = "managed/folder/name/"
force_destroy = %t
}
`, forceDestroy)
}