Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Aug 27, 2024
1 parent 6ca0e04 commit 9f55e32
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/database_roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: |-
---

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


Expand Down
1 change: 1 addition & 0 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,4 @@ func (w *WarehouseDatasourceShowOutputAssert) IsEmpty() {
- handle attribute types in resource assertions (currently strings only; TODO left in `assert/resourceassert/gen/model.go`)
- distinguish between different enum types (TODO left in `assert/resourceshowoutputassert/gen/templates.go`)
- support the rest of attribute types in config model builders (TODO left in `config/model/gen/model.go`)
- Omit computed fields in the model (like FullyQualifiedName), because it doesn't make sense to set them
1 change: 0 additions & 1 deletion pkg/internal/genhelpers/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func generateAndSaveForAllObjects[T ObjectNameProvider, M GenerationModel](objec
}
filename := filenameProvider(s, model)
if err := WriteCodeToFile(&buffer, filename); err != nil {
log.Println("Failed to save: ", buffer.String())
errs = append(errs, fmt.Errorf("saving output for object %s to file %s failed with err: %w", s.ObjectName(), filename, err))
continue
}
Expand Down
32 changes: 23 additions & 9 deletions pkg/resources/database_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var databaseRoleSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Specifies the identifier for the database role.",
DiffSuppressFunc: suppressIdentifierQuoting,
},
Expand Down Expand Up @@ -99,15 +98,18 @@ func ReadDatabaseRole(ctx context.Context, d *schema.ResourceData, meta any) dia
}

databaseRole, err := client.DatabaseRoles.ShowByID(ctx, id)
if err != nil && errors.Is(err, sdk.ErrObjectNotFound) {
d.SetId("")
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Database role not found; marking it as removed",
Detail: fmt.Sprintf("Database role name: %s, err: %s", id.FullyQualifiedName(), err),
},
if err != nil {
if errors.Is(err, sdk.ErrObjectNotFound) {
d.SetId("")
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Database role not found; marking it as removed",
Detail: fmt.Sprintf("Database role name: %s, err: %s", id.FullyQualifiedName(), err),
},
}
}
return diag.FromErr(err)
}

if err := d.Set("comment", databaseRole.Comment); err != nil {
Expand Down Expand Up @@ -155,6 +157,18 @@ func UpdateDatabaseRole(ctx context.Context, d *schema.ResourceData, meta any) d
return diag.FromErr(err)
}

if d.HasChange("name") {
newId := sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), d.Get("name").(string))

err = client.DatabaseRoles.Alter(ctx, sdk.NewAlterDatabaseRoleRequest(id).WithRename(newId))
if err != nil {
return diag.FromErr(err)
}

d.SetId(helpers.EncodeResourceIdentifier(newId))
id = newId
}

if d.HasChange("comment") {
newComment := d.Get("comment").(string)
err := client.DatabaseRoles.Alter(ctx, sdk.NewAlterDatabaseRoleRequest(id).WithSet(*sdk.NewDatabaseRoleSetRequest(newComment)))
Expand Down
23 changes: 23 additions & 0 deletions pkg/resources/database_role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

func TestAcc_DatabaseRole(t *testing.T) {
id := acc.TestClient().Ids.RandomDatabaseObjectIdentifier()
newId := acc.TestClient().Ids.RandomDatabaseObjectIdentifier()
comment := random.Comment()
databaseRoleModel := model.DatabaseRole("test", id.DatabaseName(), id.Name())
databaseRoleModelWithComment := model.DatabaseRole("test", id.DatabaseName(), id.Name()).WithComment(comment)
Expand Down Expand Up @@ -122,6 +123,28 @@ func TestAcc_DatabaseRole(t *testing.T) {
HasComment(""),
),
},
// rename
{
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("snowflake_database_role.test", plancheck.ResourceActionUpdate),
},
},
Config: config.FromModel(t, databaseRoleModel.WithName(newId.Name())),
Check: assert.AssertThat(t,
resourceassert.DatabaseRoleResource(t, "snowflake_database_role.test").
HasNameString(newId.Name()).
HasDatabaseString(newId.DatabaseName()).
HasCommentString("").
HasFullyQualifiedNameString(newId.FullyQualifiedName()),
resourceshowoutputassert.DatabaseRoleShowOutput(t, "snowflake_database_role.test").
HasName(newId.Name()).
HasComment(""),
objectassert.DatabaseRole(t, newId).
HasName(newId.Name()).
HasComment(""),
),
},
},
})
}
Expand Down
16 changes: 5 additions & 11 deletions pkg/sdk/database_role_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,18 @@ func (s *CreateDatabaseRoleRequest) toOpts() *createDatabaseRoleOptions {

func (s *AlterDatabaseRoleRequest) toOpts() *alterDatabaseRoleOptions {
opts := alterDatabaseRoleOptions{
IfExists: Bool(s.ifExists),
name: s.name,
}
if s.rename != nil {
opts.Rename = s.rename
IfExists: Bool(s.ifExists),
name: s.name,
Rename: s.rename,
SetTags: s.setTags,
UnsetTags: s.unsetTags,
}
if s.set != nil {
opts.Set = &DatabaseRoleSet{s.set.comment}
}
if s.unset != nil {
opts.Unset = &DatabaseRoleUnset{true}
}
if s.setTags != nil {
opts.SetTags = s.setTags
}
if s.unsetTags != nil {
opts.UnsetTags = s.unsetTags
}
return &opts
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/sdk/testint/database_role_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

// TODO: add tests for new show filtering option and set/unset tags

func TestInt_DatabaseRoles(t *testing.T) {
client := testClient(t)
ctx := testContext(t)
Expand Down
24 changes: 24 additions & 0 deletions templates/data-sources/database_roles.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
subcategory: ""
description: |-
{{ if gt (len (split .Description "<deprecation>")) 1 -}}
{{ index (split .Description "<deprecation>") 1 | plainmarkdown | trimspace | prefixlines " " }}
{{- else -}}
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
{{- end }}
---

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

# {{.Name}} ({{.Type}})

{{ .Description | trimspace }}

{{ if .HasExample -}}
## Example Usage

{{ tffile (printf "examples/data-sources/%s/data-source.tf" .Name)}}
{{- end }}

{{ .SchemaMarkdown | trimspace }}

0 comments on commit 9f55e32

Please sign in to comment.