Skip to content

Commit

Permalink
feat: Resource monitor v1 readiness part 2 (#3064)
Browse files Browse the repository at this point in the history
## Changes
- Refactor data source
- Update examples and documentation for resource and data source
- Update examples for database role

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [x] acceptance tests

## References
<!-- issues documentation links, etc  -->

* [SHOW RESOURCE
MONITORS](https://docs.snowflake.com/en/sql-reference/sql/show-resource-monitors)
  • Loading branch information
sfc-gh-jcieslak authored and sfc-gh-fbudzynski committed Sep 19, 2024
1 parent 0981bfc commit 7c7ef99
Show file tree
Hide file tree
Showing 18 changed files with 709 additions and 93 deletions.
26 changes: 26 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ describe deprecations or breaking changes and help you to change your configurat
across different versions.

## v0.95.0 ➞ v0.96.0

### *(breaking change)* resource_monitor resource
Removed fields:
- `set_for_account` (will be settable on account resource, right now, the preferred way is to set it through unsafe_execute resource)
- `warehouses` (can be set on warehouse resource, optionally through unsafe_execute resource only if the warehouse is not managed by Terraform)
- `suspend_triggers` (now, `suspend_trigger` should be used)
- `suspend_immediate_triggers` (now, `suspend_immediate_trigger` should be used)

### *(breaking change)* resource_monitor data source
Changes:
- New filtering option `like`
- Now, the output of `SHOW RESOURCE MONITORS` is now inside `resource_monitors.*.show_output`. Here's the list of currently available fields:
- `name`
- `credit_quota`
- `used_credits`
- `remaining_credits`
- `level`
- `frequency`
- `start_time`
- `end_time`
- `suspend_at`
- `suspend_immediate_at`
- `created_on`
- `owner`
- `comment`

### snowflake_row_access_policies data source changes
New filtering options:
- `in`
Expand Down
61 changes: 57 additions & 4 deletions docs/data-sources/database_roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,73 @@
page_title: "snowflake_database_roles Data Source - terraform-provider-snowflake"
subcategory: ""
description: |-
Datasource used to get details of filtered database roles. Filtering is aligned with the current possibilities for SHOW DATABASE ROLES https://docs.snowflake.com/en/sql-reference/sql/show-database-roles query (like and limit are supported). The results of SHOW is encapsulated in show_output collection.
---

!> **V1 release candidate** This data source was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the data source if needed. Any errors reported will be resolved with a higher priority. We encourage checking this data source out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0920--v0930) to use it.

# snowflake_database_roles (Data Source)


Datasource used to get details of filtered database roles. Filtering is aligned with the current possibilities for [SHOW DATABASE ROLES](https://docs.snowflake.com/en/sql-reference/sql/show-database-roles) query (`like` and `limit` are supported). The results of SHOW is encapsulated in show_output collection.

## Example Usage

```terraform
data "snowflake_database_roles" "db_roles" {
database = "MYDB"
# Simple usage
data "snowflake_database_roles" "simple" {
in_database = "database-name"
}
output "simple_output" {
value = data.snowflake_database_roles.simple.database_roles
}
# Filtering (like)
data "snowflake_database_roles" "like" {
in_database = "database-name"
like = "database_role-name"
}
output "like_output" {
value = data.snowflake_database_roles.like.database_roles
}
# Filtering (limit)
data "snowflake_database_roles" "limit" {
in_database = "database-name"
limit {
rows = 10
from = "prefix-"
}
}
output "limit_output" {
value = data.snowflake_database_roles.limit.database_roles
}
# Ensure the number of database roles is equal to at least one element (with the use of postcondition)
data "snowflake_database_roles" "assert_with_postcondition" {
in_database = "database-name"
like = "database_role-name-%"
lifecycle {
postcondition {
condition = length(self.database_roles) > 0
error_message = "there should be at least one database role"
}
}
}
# Ensure the number of database roles is equal to at exactly one element (with the use of check block)
check "database_role_check" {
data "snowflake_resource_monitors" "assert_with_check_block" {
in_database = "database-name"
like = "database_role-name"
}
assert {
condition = length(data.snowflake_database_roles.assert_with_check_block.database_roles) == 1
error_message = "Database roles filtered by '${data.snowflake_database_roles.assert_with_check_block.like}' returned ${length(data.snowflake_database_roles.assert_with_check_block.database_roles)} database roles where one was expected"
}
}
```

Expand Down
69 changes: 64 additions & 5 deletions docs/data-sources/resource_monitors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,93 @@
page_title: "snowflake_resource_monitors Data Source - terraform-provider-snowflake"
subcategory: ""
description: |-
Datasource used to get details of filtered resource monitors. Filtering is aligned with the current possibilities for SHOW RESOURCE MONITORS https://docs.snowflake.com/en/sql-reference/sql/show-resource-monitors query (like is supported). The results of SHOW is encapsulated in show_output collection.
---

# snowflake_resource_monitors (Data Source)
!> **V1 release candidate** This data source was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the data source if needed. Any errors reported will be resolved with a higher priority. We encourage checking this data source out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0950--v0960) to use it.

# snowflake_resource_monitors (Data Source)

Datasource used to get details of filtered resource monitors. Filtering is aligned with the current possibilities for [SHOW RESOURCE MONITORS](https://docs.snowflake.com/en/sql-reference/sql/show-resource-monitors) query (`like` is supported). The results of SHOW is encapsulated in show_output collection.

## Example Usage

```terraform
data "snowflake_resource_monitors" "current" {
# Simple usage
data "snowflake_resource_monitors" "simple" {
}
output "simple_output" {
value = data.snowflake_resource_monitors.simple.resource_monitors
}
# Filtering (like)
data "snowflake_resource_monitors" "like" {
like = "resource-monitor-name"
}
output "like_output" {
value = data.snowflake_resource_monitors.like.resource_monitors
}
# Ensure the number of resource monitors is equal to at least one element (with the use of postcondition)
data "snowflake_resource_monitors" "assert_with_postcondition" {
like = "resource-monitor-name-%"
lifecycle {
postcondition {
condition = length(self.resource_monitors) > 0
error_message = "there should be at least one resource monitor"
}
}
}
# Ensure the number of resource monitors is equal to at exactly one element (with the use of check block)
check "resource_monitor_check" {
data "snowflake_resource_monitors" "assert_with_check_block" {
like = "resource-monitor-name"
}
assert {
condition = length(data.snowflake_resource_monitors.assert_with_check_block.resource_monitors) == 1
error_message = "Resource monitors filtered by '${data.snowflake_resource_monitors.assert_with_check_block.like}' returned ${length(data.snowflake_resource_monitors.assert_with_check_block.resource_monitors)} resource monitors where one was expected"
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `like` (String) Filters the output with **case-insensitive** pattern, with support for SQL wildcard characters (`%` and `_`).

### Read-Only

- `id` (String) The ID of this resource.
- `resource_monitors` (List of Object) The resource monitors in the database (see [below for nested schema](#nestedatt--resource_monitors))
- `resource_monitors` (List of Object) Holds the aggregated output of all resource monitor details queries. (see [below for nested schema](#nestedatt--resource_monitors))

<a id="nestedatt--resource_monitors"></a>
### Nested Schema for `resource_monitors`

Read-Only:

- `show_output` (List of Object) (see [below for nested schema](#nestedobjatt--resource_monitors--show_output))

<a id="nestedobjatt--resource_monitors--show_output"></a>
### Nested Schema for `resource_monitors.show_output`

Read-Only:

- `comment` (String)
- `credit_quota` (String)
- `created_on` (String)
- `credit_quota` (Number)
- `end_time` (String)
- `frequency` (String)
- `level` (String)
- `name` (String)
- `owner` (String)
- `remaining_credits` (Number)
- `start_time` (String)
- `suspend_at` (Number)
- `suspend_immediate_at` (Number)
- `used_credits` (Number)
37 changes: 29 additions & 8 deletions docs/resources/resource_monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,50 @@ description: |-
---

!> **V1 release candidate** This resource was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the resource if needed. Any errors reported will be resolved with a higher priority. We encourage checking this resource out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0950--v0960) to use it.

~> **Note** For more details about resource monitor usage, please visit [this guide on Snowflake documentation page](https://docs.snowflake.com/en/user-guide/resource-monitors).

**! Warning !** Due to Snowflake limitations, the following actions are not supported:
- Cannot create resource monitors with only triggers set, any other attribute has to be set.
- Once a resource monitor has at least one trigger assigned, it cannot fully unset them (has to have at least one trigger, doesn't matter of which type). That's why when you unset all the triggers on a resource monitor, it will be automatically recreated.

# snowflake_resource_monitor (Resource)



## Example Usage

```terraform
resource "snowflake_resource_monitor" "monitor" {
name = "monitor"
// Note: Without credit quota and triggers specified in the configuration, the resource monitor is not performing any work.
// More on resource monitor usage: https://docs.snowflake.com/en/user-guide/resource-monitors.
resource "snowflake_resource_monitor" "minimal" {
name = "resource-monitor-name"
}
// Note: Resource monitors have to be attached to account or warehouse to be able to track credit usage.
resource "snowflake_resource_monitor" "minimal_working" {
name = "resource-monitor-name"
credit_quota = 100
suspend_trigger = 100
notify_users = ["USERONE", "USERTWO"]
}
resource "snowflake_resource_monitor" "complete" {
name = "resource-monitor-name"
credit_quota = 100
frequency = "DAILY"
start_timestamp = "2020-12-07 00:00"
end_timestamp = "2021-12-07 00:00"
start_timestamp = "2030-12-07 00:00"
end_timestamp = "2035-12-07 00:00"
notify_triggers = [40, 50]
suspend_triggers = 50
suspend_immediate_triggers = 90
notify_triggers = [40, 50]
suspend_trigger = 50
suspend_immediate_trigger = 90
notify_users = ["USERONE", "USERTWO"]
}
```

-> **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-->

Expand Down
59 changes: 56 additions & 3 deletions examples/data-sources/snowflake_database_roles/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
data "snowflake_database_roles" "db_roles" {
database = "MYDB"
}
# Simple usage
data "snowflake_database_roles" "simple" {
in_database = "database-name"
}

output "simple_output" {
value = data.snowflake_database_roles.simple.database_roles
}

# Filtering (like)
data "snowflake_database_roles" "like" {
in_database = "database-name"
like = "database_role-name"
}

output "like_output" {
value = data.snowflake_database_roles.like.database_roles
}

# Filtering (limit)
data "snowflake_database_roles" "limit" {
in_database = "database-name"
limit {
rows = 10
from = "prefix-"
}
}

output "limit_output" {
value = data.snowflake_database_roles.limit.database_roles
}

# Ensure the number of database roles is equal to at least one element (with the use of postcondition)
data "snowflake_database_roles" "assert_with_postcondition" {
in_database = "database-name"
like = "database_role-name-%"
lifecycle {
postcondition {
condition = length(self.database_roles) > 0
error_message = "there should be at least one database role"
}
}
}

# Ensure the number of database roles is equal to at exactly one element (with the use of check block)
check "database_role_check" {
data "snowflake_resource_monitors" "assert_with_check_block" {
in_database = "database-name"
like = "database_role-name"
}

assert {
condition = length(data.snowflake_database_roles.assert_with_check_block.database_roles) == 1
error_message = "Database roles filtered by '${data.snowflake_database_roles.assert_with_check_block.like}' returned ${length(data.snowflake_database_roles.assert_with_check_block.database_roles)} database roles where one was expected"
}
}
41 changes: 39 additions & 2 deletions examples/data-sources/snowflake_resource_monitors/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
data "snowflake_resource_monitors" "current" {
}
# Simple usage
data "snowflake_resource_monitors" "simple" {
}

output "simple_output" {
value = data.snowflake_resource_monitors.simple.resource_monitors
}

# Filtering (like)
data "snowflake_resource_monitors" "like" {
like = "resource-monitor-name"
}

output "like_output" {
value = data.snowflake_resource_monitors.like.resource_monitors
}

# Ensure the number of resource monitors is equal to at least one element (with the use of postcondition)
data "snowflake_resource_monitors" "assert_with_postcondition" {
like = "resource-monitor-name-%"
lifecycle {
postcondition {
condition = length(self.resource_monitors) > 0
error_message = "there should be at least one resource monitor"
}
}
}

# Ensure the number of resource monitors is equal to at exactly one element (with the use of check block)
check "resource_monitor_check" {
data "snowflake_resource_monitors" "assert_with_check_block" {
like = "resource-monitor-name"
}

assert {
condition = length(data.snowflake_resource_monitors.assert_with_check_block.resource_monitors) == 1
error_message = "Resource monitors filtered by '${data.snowflake_resource_monitors.assert_with_check_block.like}' returned ${length(data.snowflake_resource_monitors.assert_with_check_block.resource_monitors)} resource monitors where one was expected"
}
}
Loading

0 comments on commit 7c7ef99

Please sign in to comment.