Skip to content

Commit

Permalink
wip: use set
Browse files Browse the repository at this point in the history
  • Loading branch information
perangel committed Jul 3, 2024
1 parent a5b5141 commit ce6a92c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/planmodifiers/setplanmodifier/unique_by_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package setplanmodifier

import (
"context"

"github.com/airbytehq/terraform-provider-airbyte/internal/planmodifiers/utils"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// UniqueByKey returnsa plan modifier that identifies each item in a set by a primary key (e.g name)
func UniqueByKey(key string) planmodifier.Set {
return uniqueByKey{
key: key,
}
}

// uniqueByKey implements the plan modifier.
type uniqueByKey struct {
key string
}

// Description returns a human-readable description of the plan modifier.
func (m uniqueByKey) Description(_ context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

// MarkdownDescription returns a markdown description of the plan modifier.
func (m uniqueByKey) MarkdownDescription(_ context.Context) string {
return "Once set, the value of this attribute in state will not change."
}

// PlanModifySet implements the plan modification logic.
func (m uniqueByKey) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) {
// Do nothing if there is an unknown configuration value
if req.ConfigValue.IsUnknown() {
return
}

if utils.IsAllStateUnknown(ctx, req.State) {
return
}

planList := req.PlanValue.Elements()
//planItemsByKey := make(map[string]types.Object)
for i := 0; i < len(planList); i++ {
var set types.Set
diags := req.Plan.GetAttribute(ctx, req.Path, &set)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
}
4 changes: 4 additions & 0 deletions internal/provider/connection_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

speakeasy_listplanmodifier "github.com/airbytehq/terraform-provider-airbyte/internal/planmodifiers/listplanmodifier"
speakeasy_objectplanmodifier "github.com/airbytehq/terraform-provider-airbyte/internal/planmodifiers/objectplanmodifier"
speakeasy_setplanmodifier "github.com/airbytehq/terraform-provider-airbyte/internal/planmodifiers/setplanmodifier"
speakeasy_stringplanmodifier "github.com/airbytehq/terraform-provider-airbyte/internal/planmodifiers/stringplanmodifier"
tfTypes "github.com/airbytehq/terraform-provider-airbyte/internal/provider/types"
"github.com/airbytehq/terraform-provider-airbyte/internal/sdk"
Expand Down Expand Up @@ -73,6 +74,9 @@ func (r *ConnectionResource) Schema(ctx context.Context, req resource.SchemaRequ
"streams": schema.SetNestedAttribute{
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.Set{
speakeasy_setplanmodifier.UniqueByKey("name"),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"cursor_field": schema.ListAttribute{
Expand Down

0 comments on commit ce6a92c

Please sign in to comment.