Skip to content

Commit

Permalink
fix: use custom set plan modifier for connection streams
Browse files Browse the repository at this point in the history
  • Loading branch information
perangel committed Jul 16, 2024
1 parent ce6a92c commit 4a7be96
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions .genignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
internal/provider/connection_resource.go
internal/provider/connection_data_resource.go
1 change: 1 addition & 0 deletions airbyte.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22463,6 +22463,7 @@ components:
properties:
streams:
type: "array"
format: "set"
items:
$ref: "#/components/schemas/StreamConfiguration"
x-speakeasy-component: true
Expand Down
2 changes: 1 addition & 1 deletion gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ generation:
oAuth2ClientCredentialsEnabled: true
telemetryEnabled: true
terraform:
version: 0.6.0
version: 0.7.0
additionalDataSources: []
additionalDependencies: {}
additionalResources: []
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/airbytehq/terraform-provider-airbyte

go 1.21

toolchain go1.21.10
toolchain go1.22.1

require (
github.com/cenkalti/backoff/v4 v4.2.0
Expand Down
62 changes: 54 additions & 8 deletions internal/planmodifiers/setplanmodifier/unique_by_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package setplanmodifier

import (
"context"
"fmt"

"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)
// UniqueByKey returns 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,
Expand Down Expand Up @@ -42,14 +43,59 @@ func (m uniqueByKey) PlanModifySet(ctx context.Context, req planmodifier.SetRequ
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() {
// Create a map of all of the request plan elements, keyed by `m.key`
elementsByKey := make(map[string]types.Object)
for _, elem := range req.PlanValue.Elements() {
obj, ok := elem.(types.Object)
if !ok {
resp.Diagnostics.AddError(
"Error modifying set",
fmt.Sprintf("Expected object element, got %T", elem),
)
return
}

v, ok := obj.Attributes()[m.key]
if ok {
elementsByKey[v.String()] = obj
}
}

var updatedElements []types.Object

// Walk all the elements in the response plan and update any fields that have either an unknown or unset value
for _, elem := range resp.PlanValue.Elements() {
obj, ok := elem.(types.Object)
if !ok {
resp.Diagnostics.AddError(
"Error modifying set",
fmt.Sprintf("Expected object element, got %T", elem),
)
return
}

elemKey := obj.Attributes()[m.key]
srcObj, ok := elementsByKey[elemKey.String()]
if !ok {
resp.Diagnostics.AddError(
"Error modifying set",
fmt.Sprintf("No object found with %s=%s", m.key, elemKey.String()),
)
return
}

for k, v := range srcObj.Attributes() {
obj.Attributes()[k] = v
}

updatedElements = append(updatedElements, obj)
}

newPlanValue, diags := types.SetValueFrom(ctx, resp.PlanValue.ElementType(ctx), updatedElements)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.PlanValue = newPlanValue
}
2 changes: 1 addition & 1 deletion internal/provider/connection_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *ConnectionDataSource) Schema(ctx context.Context, req datasource.Schema
"configurations": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"streams": schema.ListNestedAttribute{
"streams": schema.SetNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
Expand Down

0 comments on commit 4a7be96

Please sign in to comment.