-
Notifications
You must be signed in to change notification settings - Fork 427
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
feat: Unsafe execute v1 readiness #3266
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
14ba7e5
wip
sfc-gh-jcieslak 3989040
wip
sfc-gh-jcieslak 3341278
wip
sfc-gh-jcieslak 58861be
wip
sfc-gh-jcieslak 4efc5b6
wip
sfc-gh-jcieslak 74827d5
wip
sfc-gh-jcieslak 38631ba
wip
sfc-gh-jcieslak 5224266
Merge branch 'refs/heads/main' into unsafe-execute-v1-readiness
sfc-gh-jcieslak 558725a
changes after review
sfc-gh-jcieslak 246a529
Merge branch 'refs/heads/main' into unsafe-execute-v1-readiness
sfc-gh-jcieslak 38a94e1
changes after review
sfc-gh-jcieslak cca2351
Merge branch 'refs/heads/main' into unsafe-execute-v1-readiness
sfc-gh-jcieslak 0e681aa
changes after review
sfc-gh-jcieslak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "snowflake_execute Resource - terraform-provider-snowflake" | ||
subcategory: "" | ||
description: |- | ||
Resource allowing execution of ANY SQL statement. It may destroy resources if used incorrectly. It may behave incorrectly combined with other resources. Use at your own risk. | ||
--- | ||
|
||
# snowflake_execute (Resource) | ||
|
||
!> **Warning** This is a dangerous resource that allows executing **ANY** SQL statement. It may destroy resources if used incorrectly. It may behave incorrectly combined with other resources. Use at your own risk. | ||
|
||
~> **Note** It can be theoretically used to manage resource that are not supported by the provider. This is risky and may brake other resources if used incorrectly. | ||
|
||
~> **Note** Use `query` parameter with caution. It will fetch **ALL** the results returned by the query provided. Try to limit the number of results by writing query with filters. Query failure does not stop resource creation; it simply results in `query_results` being empty. | ||
|
||
Resource allowing execution of ANY SQL statement. It may destroy resources if used incorrectly. It may behave incorrectly combined with other resources. Use at your own risk. | ||
sfc-gh-asawicki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
################################## | ||
### simple use cases | ||
################################## | ||
|
||
# create and destroy resource | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# create and destroy resource using qualified name | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE \"abc\"" | ||
revert = "DROP DATABASE \"abc\"" | ||
} | ||
|
||
# with query | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "SHOW DATABASES LIKE '%ABC%'" | ||
} | ||
|
||
################################## | ||
### grants example | ||
################################## | ||
|
||
# grant and revoke privilege USAGE to ROLE on database | ||
resource "snowflake_execute" "test" { | ||
execute = "GRANT USAGE ON DATABASE ABC TO ROLE XYZ" | ||
revert = "REVOKE USAGE ON DATABASE ABC FROM ROLE XYZ" | ||
} | ||
|
||
# grant and revoke with for_each | ||
variable "database_grants" { | ||
type = list(object({ | ||
database_name = string | ||
role_id = string | ||
privileges = list(string) | ||
})) | ||
} | ||
|
||
resource "snowflake_execute" "test" { | ||
for_each = { for index, db_grant in var.database_grants : index => db_grant } | ||
execute = "GRANT ${join(",", each.value.privileges)} ON DATABASE ${each.value.database_name} TO ROLE ${each.value.role_id}" | ||
revert = "REVOKE ${join(",", each.value.privileges)} ON DATABASE ${each.value.database_name} FROM ROLE ${each.value.role_id}" | ||
} | ||
|
||
################################## | ||
### fixing bad configuration | ||
################################## | ||
|
||
# bad revert - simple | ||
# 1 - resource created with a bad revert; it is constructed, revert is not validated before destroy happens | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 2 - fix the revert first; resource won't be recreated | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# bad revert - complex (we assume that the problem is spotted after trying to change the execute) | ||
# 1 - resource created with a bad revert; it is constructed, revert is not validated before destroy happens | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 2 - try to create different database; it will fail on bad destroy | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE XYZ" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 3 - fix the revert first | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# 4 - create different database updating revert also | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE XYZ" | ||
revert = "DROP DATABASE XYZ" | ||
} | ||
|
||
# bad query | ||
# 1 - resource will be created; query_results will be empty | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "bad query" | ||
} | ||
|
||
# 2 - fix the query; query_results will be calculated; resource won't be recreated | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "SHOW DATABASES LIKE '%ABC%'" | ||
} | ||
``` | ||
-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources). | ||
<!-- TODO(SNOW-1634854): include an example showing both methods--> | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `execute` (String) SQL statement to execute. Forces recreation of resource on change. | ||
- `revert` (String) SQL statement to revert the execute statement. Invoked when resource is being destroyed. | ||
|
||
### Optional | ||
|
||
- `query` (String) Optional SQL statement to do a read. Invoked after creation and every time it is changed. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `query_results` (List of Map of String) List of key-value maps (text to text) retrieved after executing read query. Will be empty if the query results in an error. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
{{codefile "shell" "/Users/jcieslak/Documents/terraform-provider-snowflake/examples/resources/snowflake_execute/import.sh"}} | ||
sfc-gh-asawicki marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform import snowflake_execute.example '<random-uuid>' | ||
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
################################## | ||
### simple use cases | ||
################################## | ||
|
||
# create and destroy resource | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# create and destroy resource using qualified name | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE \"abc\"" | ||
revert = "DROP DATABASE \"abc\"" | ||
} | ||
|
||
# with query | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "SHOW DATABASES LIKE '%ABC%'" | ||
} | ||
|
||
################################## | ||
### grants example | ||
################################## | ||
|
||
# grant and revoke privilege USAGE to ROLE on database | ||
resource "snowflake_execute" "test" { | ||
execute = "GRANT USAGE ON DATABASE ABC TO ROLE XYZ" | ||
revert = "REVOKE USAGE ON DATABASE ABC FROM ROLE XYZ" | ||
} | ||
|
||
# grant and revoke with for_each | ||
variable "database_grants" { | ||
type = list(object({ | ||
database_name = string | ||
role_id = string | ||
privileges = list(string) | ||
})) | ||
} | ||
|
||
resource "snowflake_execute" "test" { | ||
for_each = { for index, db_grant in var.database_grants : index => db_grant } | ||
execute = "GRANT ${join(",", each.value.privileges)} ON DATABASE ${each.value.database_name} TO ROLE ${each.value.role_id}" | ||
revert = "REVOKE ${join(",", each.value.privileges)} ON DATABASE ${each.value.database_name} FROM ROLE ${each.value.role_id}" | ||
} | ||
|
||
################################## | ||
### fixing bad configuration | ||
sfc-gh-asawicki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
################################## | ||
|
||
# bad revert - simple | ||
# 1 - resource created with a bad revert; it is constructed, revert is not validated before destroy happens | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 2 - fix the revert first; resource won't be recreated | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# bad revert - complex (we assume that the problem is spotted after trying to change the execute) | ||
# 1 - resource created with a bad revert; it is constructed, revert is not validated before destroy happens | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 2 - try to create different database; it will fail on bad destroy | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE XYZ" | ||
revert = "SELECT 1" | ||
} | ||
|
||
# 3 - fix the revert first | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
} | ||
|
||
# 4 - create different database updating revert also | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE XYZ" | ||
revert = "DROP DATABASE XYZ" | ||
} | ||
|
||
# bad query | ||
# 1 - resource will be created; query_results will be empty | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "bad query" | ||
} | ||
|
||
# 2 - fix the query; query_results will be calculated; resource won't be recreated | ||
resource "snowflake_execute" "test" { | ||
execute = "CREATE DATABASE ABC" | ||
revert = "DROP DATABASE ABC" | ||
query = "SHOW DATABASES LIKE '%ABC%'" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally forgot about adding new resources here. Can I just add them or do I need to add the matching labels first? (e.g. for new functions and procedures)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that is a good question, and I couldn't find an answer for it. Maybe we can see after merging this one because I haven't added labels in the repo yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: we have SNOW-1831333 for this.