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

feat: add snowflake_grant_application_role resource #2690

Merged
merged 22 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e778c23
add application role
sfc-gh-swinkler Apr 10, 2024
b5d4406
create app using tf
sfc-gh-swinkler Apr 11, 2024
f3f9cb6
fix tests
sfc-gh-swinkler Apr 13, 2024
1b44738
Merge branch 'main' into grant-app-role
sfc-gh-swinkler Apr 13, 2024
06e5599
randomize name
sfc-gh-swinkler Apr 13, 2024
a8e5f6f
fix id
sfc-gh-swinkler Apr 16, 2024
40fb643
Merge branch 'main' into grant-app-role
sfc-gh-swinkler Apr 16, 2024
ebcce67
Merge branch 'main' into grant-app-role
sfc-gh-swinkler Apr 18, 2024
76365d5
Merge branch 'main' into grant-app-role
sfc-gh-swinkler Apr 22, 2024
824b56c
Fix linter issues and tests
sfc-gh-jmichalak May 8, 2024
db8d665
Merge remote-tracking branch 'origin/main' into grant-app-role
sfc-gh-jmichalak May 8, 2024
93a1e2f
Restore manifest2
sfc-gh-jmichalak May 8, 2024
10917e5
Fix tests
sfc-gh-jmichalak May 8, 2024
94833d7
Merge remote-tracking branch 'origin/main' into grant-app-role
sfc-gh-jmichalak May 8, 2024
5dc9593
Remove custom provider
sfc-gh-jmichalak May 9, 2024
739686a
Test app creation order
sfc-gh-jmichalak May 9, 2024
8a58368
Merge remote-tracking branch 'origin/main' into grant-app-role
sfc-gh-jmichalak May 15, 2024
7cbd64d
Fixes
sfc-gh-jmichalak May 15, 2024
bc8cf63
Improve acc tests
sfc-gh-jmichalak May 15, 2024
86bb80a
Merge remote-tracking branch 'origin/main' into grant-app-role
sfc-gh-jmichalak May 15, 2024
a978053
Fixes
sfc-gh-jmichalak May 16, 2024
6321eee
Merge remote-tracking branch 'origin/main' into grant-app-role
sfc-gh-jmichalak May 16, 2024
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
66 changes: 66 additions & 0 deletions docs/resources/grant_application_role.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
page_title: "snowflake_grant_application_role Resource - terraform-provider-snowflake"
subcategory: ""
description: |-

---

# snowflake_grant_application_role (Resource)



## Example Usage

```terraform
locals {
application_role_identifier = "\"my_appplication\".\"app_role_1\""
}

##################################
### grant application role to account role
##################################


resource "snowflake_role" "role" {
name = "my_role"
}

resource "snowflake_grant_application_role" "g" {
application_role_name = local.application_role_identifier
parent_account_role_name = snowflake_role.role.name
}

##################################
### grant application role to application
##################################

resource "snowflake_grant_application_role" "g" {
application_role_name = local.application_role_identifier
application_name = "my_second_application"
}
```

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

### Required

- `application_role_name` (String) Specifies the identifier for the application role to grant.

### Optional

- `application_name` (String) The fully qualified name of the application on which application role will be granted.
- `parent_account_role_name` (String) The fully qualified name of the account role on which application role will be granted.

### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# format is application_role_name (string) | object_type (ACCOUNT_ROLE|APPLICATION) | grantee_name (string)
terraform import "\"my_application\".\"app_role_1\"|ACCOUNT_ROLE|\"my_role\""
```
2 changes: 2 additions & 0 deletions examples/resources/snowflake_grant_application_role/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# format is application_role_name (string) | object_type (ACCOUNT_ROLE|APPLICATION) | grantee_name (string)
terraform import "\"my_application\".\"app_role_1\"|ACCOUNT_ROLE|\"my_role\""
26 changes: 26 additions & 0 deletions examples/resources/snowflake_grant_application_role/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
locals {
application_role_identifier = "\"my_appplication\".\"app_role_1\""
}

##################################
### grant application role to account role
##################################


resource "snowflake_role" "role" {
name = "my_role"
}

resource "snowflake_grant_application_role" "g" {
application_role_name = local.application_role_identifier
parent_account_role_name = snowflake_role.role.name
}

##################################
### grant application role to application
##################################

resource "snowflake_grant_application_role" "g" {
application_role_name = local.application_role_identifier
application_name = "my_second_application"
}
32 changes: 32 additions & 0 deletions pkg/acceptance/check_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand Down Expand Up @@ -363,3 +364,34 @@ func CheckUserPasswordPolicyAttachmentDestroy(t *testing.T) func(*terraform.Stat
return nil
}
}

func TestAccCheckGrantApplicationRoleDestroy(s *terraform.State) error {
client := TestAccProvider.Meta().(*provider.Context).Client
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_grant_application_role" {
continue
}
ctx := context.Background()
id := rs.Primary.ID
ids := strings.Split(id, "|")
applicationRoleName := ids[0]
objectType := ids[1]
parentRoleName := ids[2]
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
Of: &sdk.ShowGrantsOf{
ApplicationRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(applicationRoleName),
},
})
if err != nil {
continue
}
for _, grant := range grants {
if grant.GrantedTo == sdk.ObjectType(objectType) {
if grant.GranteeName.FullyQualifiedName() == parentRoleName {
return fmt.Errorf("application role grant %v still exists", grant)
}
}
}
}
return nil
}
1 change: 0 additions & 1 deletion pkg/acceptance/helpers/application_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (c *ApplicationClient) client() sdk.Applications {
func (c *ApplicationClient) CreateApplication(t *testing.T, packageId sdk.AccountObjectIdentifier, version string) (*sdk.Application, func()) {
t.Helper()
ctx := context.Background()

id := c.ids.RandomAccountObjectIdentifier()
err := c.client().Create(ctx, sdk.NewCreateApplicationRequest(id, packageId).WithVersion(sdk.NewApplicationVersionRequest().WithVersionAndPatch(sdk.NewVersionAndPatchRequest(version, nil))))
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func getResources() map[string]*schema.Resource {
"snowflake_file_format": resources.FileFormat(),
"snowflake_function": resources.Function(),
"snowflake_grant_account_role": resources.GrantAccountRole(),
"snowflake_grant_application_role": resources.GrantApplicationRole(),
"snowflake_grant_database_role": resources.GrantDatabaseRole(),
"snowflake_grant_ownership": resources.GrantOwnership(),
"snowflake_grant_privileges_to_role": resources.GrantPrivilegesToRole(),
Expand Down
Loading
Loading