Skip to content

Commit

Permalink
Add deletion_protection field to workflow resource (GoogleCloudPlatfo…
Browse files Browse the repository at this point in the history
  • Loading branch information
BENY4M1N authored and amanMahendroo committed Dec 17, 2024
1 parent debc963 commit 118183d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mmv1/products/workflows/Workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async:
custom_code:
extra_schema_entry: 'templates/terraform/extra_schema_entry/workflow.tmpl'
encoder: 'templates/terraform/encoders/workflow.go.tmpl'
pre_delete: 'templates/terraform/pre_delete/workflows_workflow.go.tmpl'
schema_version: 1
state_upgraders: true
examples:
Expand All @@ -60,6 +61,19 @@ examples:
name: 'workflow'
account_id: 'my-account'
exclude_import_test: true
ignore_read_extra:
- 'deletion_protection'
virtual_fields:
- name: 'deletion_protection'
description: |
Whether Terraform will be prevented from destroying the workflow. Defaults to true.
When a`terraform destroy` or `terraform apply` would delete the workflow,
the command will fail if this field is not set to false in Terraform state.
When the field is set to true or unset in Terraform state, a `terraform apply`
or `terraform destroy` that would delete the workflow will fail.
When the field is set to false, deleting the workflow is allowed.
type: Boolean
default_value: true
parameters:
- name: 'region'
type: String
Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/terraform/examples/workflow_basic.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ resource "google_workflows_workflow" "{{$.PrimaryResourceId}}" {
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
deletion_protection = false
source_contents = <<-EOF
# This is a sample workflow. You can replace it with your source code.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if d.Get("deletion_protection").(bool) {
return fmt.Errorf("cannot destroy workflow without setting deletion_protection=false and running `terraform apply`")
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ resource "google_workflows_workflow" "example" {
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
deletion_protection = false
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand Down Expand Up @@ -84,6 +85,126 @@ resource "google_workflows_workflow" "example" {
user_env_vars = {
url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
}
deletion_protection = false
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
# This workflow does the following:
# - reads current time and date information from an external API and stores
# the response in CurrentDateTime variable
# - retrieves a list of Wikipedia articles related to the day of the week
# from CurrentDateTime
# - returns the list of articles as an output of the workflow
# FYI, In terraform you need to escape the $$ or it will cause errors.

- getCurrentTime:
call: http.get
args:
url: $${sys.get_env("url")}
result: CurrentDateTime
- readWikipedia:
call: http.get
args:
url: https:/fi.wikipedia.org/w/api.php
query:
action: opensearch
search: $${CurrentDateTime.body.dayOfTheWeek}
result: WikiResult
- returnOutput:
return: $${WikiResult.body[1]}
EOF
}
`, name)
}

func TestAccWorkflowsWorkflow_UpdateDeletionProtectionFalseToTrue(t *testing.T) {
// Custom test written to test diffs
t.Parallel()

workflowName := fmt.Sprintf("tf-test-acc-workflow-%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkflowsWorkflow_Basic_DeletionProtectionFalse(workflowName),
},
{
Config: testAccWorkflowsWorkflow_Basic_DeletionProtectionTrue(workflowName),
},
{
Config: testAccWorkflowsWorkflow_Basic_DeletionProtectionFalse(workflowName),
},
},
})
}

func TestAccWorkflowsWorkflow_UpdateDeletionProtectionTrueToFalse(t *testing.T) {
// Custom test written to test diffs
t.Parallel()

workflowName := fmt.Sprintf("tf-test-acc-workflow-%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckWorkflowsWorkflowDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccWorkflowsWorkflow_Basic_DeletionProtectionTrue(workflowName),
},
{
Config: testAccWorkflowsWorkflow_Basic_DeletionProtectionFalse(workflowName),
},
},
})
}

func testAccWorkflowsWorkflow_Basic_DeletionProtectionFalse(name string) string {
return fmt.Sprintf(`
resource "google_workflows_workflow" "example" {
name = "%s"
region = "us-central1"
deletion_protection = false
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
# This workflow does the following:
# - reads current time and date information from an external API and stores
# the response in CurrentDateTime variable
# - retrieves a list of Wikipedia articles related to the day of the week
# from CurrentDateTime
# - returns the list of articles as an output of the workflow
# FYI, In terraform you need to escape the $$ or it will cause errors.

- getCurrentTime:
call: http.get
args:
url: $${sys.get_env("url")}
result: CurrentDateTime
- readWikipedia:
call: http.get
args:
url: https://en.wikipedia.org/w/api.php
query:
action: opensearch
search: $${CurrentDateTime.body.dayOfTheWeek}
result: WikiResult
- returnOutput:
return: $${WikiResult.body[1]}
EOF
}
`, name)
}

func testAccWorkflowsWorkflow_Basic_DeletionProtectionTrue(name string) string {
return fmt.Sprintf(`
resource "google_workflows_workflow" "example" {
name = "%s"
region = "us-central1"
deletion_protection = true
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
#
Expand Down Expand Up @@ -188,6 +309,7 @@ resource "google_workflows_workflow" "example" {
name = "%s"
region = "us-central1"
description = "Magic"
deletion_protection = false
crypto_key_name = "%s"
source_contents = <<-EOF
# This is a sample workflow, feel free to replace it with your source code
Expand Down

0 comments on commit 118183d

Please sign in to comment.