Skip to content

Commit

Permalink
fix(domain): workspace assignments should only care about managed res…
Browse files Browse the repository at this point in the history
…ources (#182)

# 📥 Pull Request

## ❓ What are you trying to address

- Currently `domain_workspace_assignments` resource enforce assignments
based only on TF configuration. It means if someone adds more resources
manually on the Fabric side, the next run of TF will remove it. Expected
behavior: Resource must only manage TF provided configuration.

This fix is related (not directly) to #174 and potentially to #183

---------

Co-authored-by: Pablo Zaidenvoren <[email protected]>
  • Loading branch information
DariuszPorowski and PabloZaiden authored Jan 10, 2025
1 parent 8265ac1 commit 3ca1c37
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/fixed-20250108-115142.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: fixed
body: |
`Provider produced inconsistent result after apply` error appears when using `fabric_domain_workspace_assignments` multiple times due to enforce assignments based only on TF configuration and ignoring the real state on the Fabric side.
Resource must only manage TF provided configuration and ignore any configuration provided outside TF.
time: 2025-01-08T11:51:42.1488752-08:00
custom:
Issue: "174"
2 changes: 1 addition & 1 deletion docs/resources/domain_workspace_assignments.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "fabric_domain_workspace_assignments" "example" {
### Required

- `domain_id` (String) The Domain ID. Changing this forces a new resource to be created.
- `workspace_ids` (Set of String) The list of Workspaces.
- `workspace_ids` (Set of String) The set of Workspace IDs.

### Optional

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ type resourceDomainWorkspaceAssignmentsModel struct {
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

func (to *resourceDomainWorkspaceAssignmentsModel) setWorkspaces(ctx context.Context, from []fabadmin.DomainWorkspace) diag.Diagnostics {
elements := []customtypes.UUID{}
func (to *resourceDomainWorkspaceAssignmentsModel) setWorkspaces(ctx context.Context, from []string) diag.Diagnostics {
elements := make([]customtypes.UUID, 0, len(from))

for _, entity := range from {
elements = append(elements, customtypes.NewUUIDPointerValue(entity.ID))
for _, element := range from {
elements = append(elements, customtypes.NewUUIDValue(element))
}

values, diags := types.SetValueFrom(ctx, customtypes.UUIDType{}, elements)
Expand Down Expand Up @@ -69,15 +69,14 @@ func (to *requestDeleteDomainWorkspaceAssignments) set(ctx context.Context, from
}

func getWorkspaceIDs(ctx context.Context, from resourceDomainWorkspaceAssignmentsModel) ([]string, diag.Diagnostics) {
values := []string{}

elements := make([]customtypes.UUID, 0, len(from.WorkspaceIDs.Elements()))

diags := from.WorkspaceIDs.ElementsAs(ctx, &elements, false)
if diags.HasError() {
if diags := from.WorkspaceIDs.ElementsAs(ctx, &elements, false); diags.HasError() {
return nil, diags
}

values := make([]string, 0, len(elements))

for _, element := range elements {
values = append(values, element.ValueString())
}
Expand Down
30 changes: 24 additions & 6 deletions internal/services/domain/resource_domain_workspace_assignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package domain
import (
"context"
"fmt"
"slices"

"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (r *resourceDomainWorkspaceAssignments) Schema(ctx context.Context, _ resou
},
},
"workspace_ids": schema.SetAttribute{
MarkdownDescription: "The list of Workspaces.",
MarkdownDescription: "The set of Workspace IDs.",
Required: true,
ElementType: customtypes.UUIDType{},
},
Expand Down Expand Up @@ -309,15 +310,13 @@ func (r *resourceDomainWorkspaceAssignments) Delete(ctx context.Context, req res
func (r *resourceDomainWorkspaceAssignments) diffWorkspaces(ctx context.Context, slice1, slice2 types.Set) (types.Set, diag.Diagnostics) {
s1 := make([]customtypes.UUID, 0, len(slice1.Elements()))

diags := slice1.ElementsAs(ctx, &s1, false)
if diags.HasError() {
if diags := slice1.ElementsAs(ctx, &s1, false); diags.HasError() {
return types.SetNull(customtypes.UUIDType{}), diags
}

s2 := make([]customtypes.UUID, 0, len(slice1.Elements()))

diags = slice2.ElementsAs(ctx, &s2, false)
if diags.HasError() {
if diags := slice2.ElementsAs(ctx, &s2, false); diags.HasError() {
return types.SetNull(customtypes.UUIDType{}), diags
}

Expand Down Expand Up @@ -350,5 +349,24 @@ func (r *resourceDomainWorkspaceAssignments) list(ctx context.Context, model *re
return diags
}

return model.setWorkspaces(ctx, respList)
workspaceIDs, diags := getWorkspaceIDs(ctx, *model)
if diags.HasError() {
return diags
}

elements := make([]string, 0, len(respList))

for _, element := range respList {
elements = append(elements, *element.ID)
}

var values []string

for _, workspaceID := range workspaceIDs {
if slices.Contains(elements, workspaceID) {
values = append(values, workspaceID)
}
}

return model.setWorkspaces(ctx, values)
}

0 comments on commit 3ca1c37

Please sign in to comment.