diff --git a/docs/data-sources/database_roles.md b/docs/data-sources/database_roles.md index cbd5b30cfd..8298c3d9ad 100644 --- a/docs/data-sources/database_roles.md +++ b/docs/data-sources/database_roles.md @@ -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) diff --git a/pkg/acceptance/bettertestspoc/README.md b/pkg/acceptance/bettertestspoc/README.md index 047c9c90b4..0fe2f38f32 100644 --- a/pkg/acceptance/bettertestspoc/README.md +++ b/pkg/acceptance/bettertestspoc/README.md @@ -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 diff --git a/pkg/internal/genhelpers/generator.go b/pkg/internal/genhelpers/generator.go index ed2716735f..c3c4848290 100644 --- a/pkg/internal/genhelpers/generator.go +++ b/pkg/internal/genhelpers/generator.go @@ -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 } diff --git a/pkg/resources/database_role.go b/pkg/resources/database_role.go index 84d9a8ea44..c9dcea7488 100644 --- a/pkg/resources/database_role.go +++ b/pkg/resources/database_role.go @@ -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, }, @@ -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 { @@ -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))) diff --git a/pkg/resources/database_role_acceptance_test.go b/pkg/resources/database_role_acceptance_test.go index 8144ff5ad8..1d3552a86c 100644 --- a/pkg/resources/database_role_acceptance_test.go +++ b/pkg/resources/database_role_acceptance_test.go @@ -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) @@ -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(""), + ), + }, }, }) } diff --git a/pkg/sdk/database_role_impl.go b/pkg/sdk/database_role_impl.go index cad3c563bb..21d0bc7210 100644 --- a/pkg/sdk/database_role_impl.go +++ b/pkg/sdk/database_role_impl.go @@ -84,11 +84,11 @@ 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} @@ -96,12 +96,6 @@ func (s *AlterDatabaseRoleRequest) toOpts() *alterDatabaseRoleOptions { 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 } diff --git a/pkg/sdk/testint/database_role_integration_test.go b/pkg/sdk/testint/database_role_integration_test.go index 4add83296e..affca49e1f 100644 --- a/pkg/sdk/testint/database_role_integration_test.go +++ b/pkg/sdk/testint/database_role_integration_test.go @@ -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) diff --git a/templates/data-sources/database_roles.md.tmpl b/templates/data-sources/database_roles.md.tmpl new file mode 100644 index 0000000000..d3ff8d9c6c --- /dev/null +++ b/templates/data-sources/database_roles.md.tmpl @@ -0,0 +1,24 @@ +--- +page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}" +subcategory: "" +description: |- +{{ if gt (len (split .Description "")) 1 -}} +{{ index (split .Description "") 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 }}