Skip to content

Commit

Permalink
dbt_cloud related updates and remove deprecated fields on RETL models
Browse files Browse the repository at this point in the history
  • Loading branch information
rexatsegment committed Jan 14, 2025
1 parent 3bcca03 commit ea41bbd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 79 deletions.
6 changes: 0 additions & 6 deletions docs/resources/reverse_etl_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ resource "segment_reverse_etl_model" "example" {
name = "Example Reverse ETL model"
enabled = true
description = "Example Reverse ETL model"
schedule_strategy = "SPECIFIC_DAYS"
query = "SELECT good_stuff FROM stuff"
query_identifier_column = "good_stuff"
schedule_config = jsonencode({
"days" : [0, 1, 2, 3],
"hours" : [0, 1, 3],
"timezone" : "America/Los_Angeles"
})
}
```

Expand Down
6 changes: 0 additions & 6 deletions examples/resources/segment_reverse_etl_model/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ resource "segment_reverse_etl_model" "example" {
name = "Example Reverse ETL model"
enabled = true
description = "Example Reverse ETL model"
schedule_strategy = "SPECIFIC_DAYS"
query = "SELECT good_stuff FROM stuff"
query_identifier_column = "good_stuff"
schedule_config = jsonencode({
"days" : [0, 1, 2, 3],
"hours" : [0, 1, 3],
"timezone" : "America/Los_Angeles"
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/segmentio/public-api-sdk-go v0.0.0-20241025180535-501a23c07559
github.com/segmentio/public-api-sdk-go v0.0.0-20250113195817-34106b6e08dd
gotest.tools/gotestsum v1.12.0
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3V
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/segmentio/public-api-sdk-go v0.0.0-20241025180535-501a23c07559 h1:6jgXPksz5bEJUMbhp4biSIViye/Os2yfAOx9yy44e1g=
github.com/segmentio/public-api-sdk-go v0.0.0-20241025180535-501a23c07559/go.mod h1:yKkoPfcOkkYjiZQj4lRWxji0Qwc6ncNEf7wCfywochY=
github.com/segmentio/public-api-sdk-go v0.0.0-20250113195817-34106b6e08dd h1:slroHJmwguMVr+wLnpigxN+51E6rkeoSsdp0f2YdmTI=
github.com/segmentio/public-api-sdk-go v0.0.0-20250113195817-34106b6e08dd/go.mod h1:rtbFvYN1VVsfSc55BtGTC45YwkcrbOgCwTqZwlauBh0=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand All @@ -204,6 +206,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
28 changes: 27 additions & 1 deletion internal/provider/destination_subscription_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r *destinationSubscriptionResource) Schema(_ context.Context, _ resource.S
Attributes: map[string]schema.Attribute{
"strategy": schema.StringAttribute{
Required: true,
Description: "Strategy supports three modes: PERIODIC, SPECIFIC_DAYS, or MANUAL.",
Description: "Strategy supports three modes: PERIODIC, SPECIFIC_DAYS, DBT_CLOUD or MANUAL.",
},
"config": schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -494,6 +494,32 @@ func getSchedule(ctx context.Context, planSchedule basetypes.ObjectValue) (*api.
"Manual reverse ETL schedule strategy does not require a config",
)
reverseETLSchedule.Config = *api.NewNullableConfig(nil)
} else if reverseETLSchedule.Strategy == "DBT_CLOUD" {
reverseETLModelScheduleConfig := api.ReverseEtlDbtCloudScheduleConfig{}
var config string
err = wrappedReverseETLModelScheduleConfig.As(&config)
if err != nil {
diags.AddError(
"Unable to decode reverse ETL schedule config",
err.Error(),
)

return nil, diags
}

err = json.Unmarshal([]byte(config), &reverseETLModelScheduleConfig)
if err != nil {
diags.AddError(
"Unable to decode reverse ETL schedule config",
err.Error(),
)

return nil, diags
}

reverseETLSchedule.Config = *api.NewNullableConfig(&api.Config{
ReverseEtlDbtCloudScheduleConfig: &reverseETLModelScheduleConfig,
})
} else {
diags.AddError(
"Unsupported reverse ETL schedule strategy",
Expand Down
44 changes: 7 additions & 37 deletions internal/provider/models/reverse_etl_model.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package models

import (
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/segmentio/public-api-sdk-go/api"
)

type ReverseETLModelState struct {
ID types.String `tfsdk:"id"`
SourceID types.String `tfsdk:"source_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Enabled types.Bool `tfsdk:"enabled"`
ScheduleStrategy types.String `tfsdk:"schedule_strategy"`
Query types.String `tfsdk:"query"`
QueryIdentifierColumn types.String `tfsdk:"query_identifier_column"`
ScheduleConfig jsontypes.Normalized `tfsdk:"schedule_config"`
ID types.String `tfsdk:"id"`
SourceID types.String `tfsdk:"source_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Enabled types.Bool `tfsdk:"enabled"`
Query types.String `tfsdk:"query"`
QueryIdentifierColumn types.String `tfsdk:"query_identifier_column"`
}

func (r *ReverseETLModelState) Fill(model api.ReverseEtlModel) error {
Expand All @@ -24,35 +21,8 @@ func (r *ReverseETLModelState) Fill(model api.ReverseEtlModel) error {
r.Name = types.StringValue(model.Name)
r.Description = types.StringValue(model.Description)
r.Enabled = types.BoolValue(model.Enabled)
r.ScheduleStrategy = types.StringValue(model.ScheduleStrategy)
r.Query = types.StringValue(model.Query)
r.QueryIdentifierColumn = types.StringValue(model.QueryIdentifierColumn)
scheduleConfig, err := GetScheduleConfig(model.ScheduleConfig)
if err != nil {
return err
}
r.ScheduleConfig = scheduleConfig
if r.ScheduleConfig.IsNull() {
empty := "{}"
r.ScheduleConfig = jsontypes.NewNormalizedPointerValue(&empty)
}

return nil
}

func GetScheduleConfig(scheduleConfig api.NullableScheduleConfig) (jsontypes.Normalized, error) {
if !scheduleConfig.IsSet() {
return jsontypes.NewNormalizedNull(), nil
}

jsonScheduleConfigString, err := scheduleConfig.Get().MarshalJSON()
if err != nil {
return jsontypes.NewNormalizedNull(), err
}

if jsonScheduleConfigString == nil {
return jsontypes.NewNormalizedValue("{}"), nil
}

return jsontypes.NewNormalizedValue(string(jsonScheduleConfigString)), nil
}
39 changes: 11 additions & 28 deletions internal/provider/reverse_etl_model_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
"github.com/segmentio/terraform-provider-segment/internal/provider/models"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -67,11 +66,11 @@ func (r *reverseETLModelResource) Schema(_ context.Context, _ resource.SchemaReq
Required: true,
Description: "Indicates whether the Model should have syncs enabled. When disabled, no syncs will be triggered, regardless of the enabled status of the attached destinations/subscriptions.",
},
"schedule_strategy": schema.StringAttribute{
Optional: true,
DeprecationMessage: "Remove this attribute's configuration as it no longer is used and the attribute will be removed in the next major version of the provider. Please use `reverse_etl_schedule` in the destination_subscription resource instead.",
Description: "Determines the strategy used for triggering syncs, which will be used in conjunction with scheduleConfig.",
},
// "schedule_strategy": schema.StringAttribute{
// Optional: true,
// DeprecationMessage: "Remove this attribute's configuration as it no longer is used and the attribute will be removed in the next major version of the provider. Please use `reverse_etl_schedule` in the destination_subscription resource instead.",
// Description: "Determines the strategy used for triggering syncs, which will be used in conjunction with scheduleConfig.",
// },
"query": schema.StringAttribute{
Required: true,
Description: "The SQL query that will be executed to extract data from the connected Source.",
Expand All @@ -80,12 +79,12 @@ func (r *reverseETLModelResource) Schema(_ context.Context, _ resource.SchemaReq
Required: true,
Description: "Indicates the column named in `query` that should be used to uniquely identify the extracted records.",
},
"schedule_config": schema.StringAttribute{
Optional: true,
DeprecationMessage: "Remove this attribute's configuration as it no longer is used and the attribute will be removed in the next major version of the provider. Please use `reverse_etl_schedule` in the destination_subscription resource instead.",
Description: "Depending on the chosen strategy, configures the schedule for this model.",
CustomType: jsontypes.NormalizedType{},
},
// "schedule_config": schema.StringAttribute{
// Optional: true,
// DeprecationMessage: "Remove this attribute's configuration as it no longer is used and the attribute will be removed in the next major version of the provider. Please use `reverse_etl_schedule` in the destination_subscription resource instead.",
// Description: "Depending on the chosen strategy, configures the schedule for this model.",
// CustomType: jsontypes.NormalizedType{},
// },
},
}
}
Expand Down Expand Up @@ -139,10 +138,6 @@ func (r *reverseETLModelResource) Create(ctx context.Context, req resource.Creat
if resp.Diagnostics.HasError() {
return
}

// Since we deprecated these values, we just need to set them to the plan values so there are no errors
resp.State.SetAttribute(ctx, path.Root("schedule_config"), plan.ScheduleConfig)
resp.State.SetAttribute(ctx, path.Root("schedule_strategy"), plan.ScheduleStrategy)
}

func (r *reverseETLModelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -185,14 +180,6 @@ func (r *reverseETLModelResource) Read(ctx context.Context, req resource.ReadReq
if resp.Diagnostics.HasError() {
return
}

// Since we deprecated these values, we just need to set them to the plan values so there are no errors
if !previousState.ScheduleConfig.IsUnknown() {
resp.State.SetAttribute(ctx, path.Root("schedule_config"), previousState.ScheduleConfig)
}
if !previousState.ScheduleStrategy.IsUnknown() {
resp.State.SetAttribute(ctx, path.Root("schedule_strategy"), previousState.ScheduleStrategy)
}
}

func (r *reverseETLModelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -244,10 +231,6 @@ func (r *reverseETLModelResource) Update(ctx context.Context, req resource.Updat
if resp.Diagnostics.HasError() {
return
}

// Since we deprecated these values, we just need to set them to the plan values so there are no errors
resp.State.SetAttribute(ctx, path.Root("schedule_config"), plan.ScheduleConfig)
resp.State.SetAttribute(ctx, path.Root("schedule_strategy"), plan.ScheduleStrategy)
}

func (r *reverseETLModelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down

0 comments on commit ea41bbd

Please sign in to comment.