-
Notifications
You must be signed in to change notification settings - Fork 418
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: datasource database role #2731
Merged
sfc-gh-jcieslak
merged 11 commits into
Snowflake-Labs:main
from
mlorek:feat-data-database-role
May 15, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2f02055
feat - datasource database role
mlorek 2a161b4
docs and examples
mlorek 018a8f1
Merge branch 'main' into feat-data-database-role
mlorek 351668e
Merge branch 'main' into feat-data-database-role
mlorek 9ee24d2
Merge branch 'main' into feat-data-database-role
mlorek 0c63f06
Merge branch 'Snowflake-Labs:main' into feat-data-database-role
mlorek c223343
review fixes
mlorek 0cbcc7c
upd docs
mlorek 29228c2
Merge branch 'main' into feat-data-database-role
mlorek e9108ff
doc fix
mlorek 7623db6
Merge branch 'main' into feat-data-database-role
mlorek 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
page_title: "snowflake_database_role Data Source - terraform-provider-snowflake" | ||
subcategory: "" | ||
description: |- | ||
|
||
--- | ||
|
||
# snowflake_database_role (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "snowflake_database_role" "db_role" { | ||
database = "MYDB" | ||
name = "DBROLE" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `database` (String) The database from which to return the database role from. | ||
- `name` (String) Database role name. | ||
|
||
### Read-Only | ||
|
||
- `comment` (String) The comment on the role | ||
- `id` (String) The ID of this resource. | ||
- `owner` (String) The owner of the role |
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,4 @@ | ||
data "snowflake_database_role" "db_role" { | ||
database = "MYDB" | ||
name = "DBROLE" | ||
} |
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,71 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var databaseRoleSchema = map[string]*schema.Schema{ | ||
"database": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The database from which to return the database role from.", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Database role name.", | ||
}, | ||
"comment": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The comment on the role", | ||
}, | ||
"owner": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The owner of the role", | ||
}, | ||
} | ||
|
||
// DatabaseRole Snowflake Database Role resource. | ||
func DatabaseRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: ReadDatabaseRole, | ||
Schema: databaseRoleSchema, | ||
} | ||
} | ||
|
||
// ReadDatabaseRole Reads the database role metadata information. | ||
func ReadDatabaseRole(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*provider.Context).Client | ||
|
||
databaseName := d.Get("database").(string) | ||
roleName := d.Get("name").(string) | ||
|
||
ctx := context.Background() | ||
dbObjId := sdk.NewDatabaseObjectIdentifier(databaseName, roleName) | ||
databaseRole, err := client.DatabaseRoles.ShowByID(ctx, dbObjId) | ||
if err != nil { | ||
log.Printf("[DEBUG] unable to show database role %s in db (%s)", roleName, databaseName) | ||
d.SetId("") | ||
return err | ||
} | ||
|
||
err = d.Set("comment", databaseRole.Comment) | ||
if err != nil { | ||
return err | ||
} | ||
err = d.Set("owner", databaseRole.Owner) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("database_role_read") | ||
return nil | ||
} |
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,79 @@ | ||
package datasources_test | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestAcc_DatabaseRole(t *testing.T) { | ||
dbName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
dbRoleName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.RequireAbove(tfversion.Version1_5_0), | ||
}, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: databaseRole(dbName, dbRoleName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.snowflake_database_role.db_role", "name"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_database_role.db_role", "comment"), | ||
resource.TestCheckResourceAttrSet("data.snowflake_database_role.db_role", "owner"), | ||
), | ||
}, | ||
{ | ||
Config: databaseRoleEmpty(dbName), | ||
ExpectError: regexp.MustCompile("Error: object does not exist"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func databaseRole(dbName, dbRoleName string) string { | ||
return fmt.Sprintf(` | ||
resource snowflake_database "test_db" { | ||
name = "%v" | ||
} | ||
|
||
resource snowflake_database_role "test_role" { | ||
name = "%v" | ||
comment = "test" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix formatting of Terraform config. |
||
database = snowflake_database.test_db.name | ||
} | ||
|
||
data snowflake_database_role "db_role" { | ||
database = snowflake_database.test_db.name | ||
name = snowflake_database_role.test_role.name | ||
depends_on = [ | ||
snowflake_database_role.test_role, | ||
] | ||
} | ||
`, dbName, dbRoleName) | ||
} | ||
|
||
func databaseRoleEmpty(dbName string) string { | ||
return fmt.Sprintf(` | ||
resource snowflake_database "test_db" { | ||
name = "%v" | ||
} | ||
|
||
data snowflake_database_role "db_role" { | ||
database = snowflake_database.test_db.name | ||
name = "dummy_missing" | ||
depends_on = [ | ||
snowflake_database.test_db, | ||
] | ||
} | ||
`, dbName) | ||
} |
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.
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.
docs have to be generated for this one, so just run
make pre-push
and the documentation should be generated. If any error pops up, refer to the contribution guidelines.