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: datasource database role #2731

Merged
merged 11 commits into from
May 15, 2024
33 changes: 33 additions & 0 deletions docs/data-sources/database_role.md
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
4 changes: 4 additions & 0 deletions examples/data-sources/snowflake_database_role/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "snowflake_database_role" "db_role" {
database = "MYDB"
name = "DBROLE"
}
71 changes: 71 additions & 0 deletions pkg/datasources/database_role.go
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{
Copy link
Collaborator

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.

"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
}
79 changes: 79 additions & 0 deletions pkg/datasources/database_role_acceptance_test.go
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
1 change: 1 addition & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ func getDataSources() map[string]*schema.Resource {
"snowflake_current_account": datasources.CurrentAccount(),
"snowflake_current_role": datasources.CurrentRole(),
"snowflake_database": datasources.Database(),
"snowflake_database_role": datasources.DatabaseRole(),
"snowflake_database_roles": datasources.DatabaseRoles(),
"snowflake_databases": datasources.Databases(),
"snowflake_dynamic_tables": datasources.DynamicTables(),
Expand Down
Loading