Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fivetran-aleksandrboldyrev committed Jan 27, 2025
1 parent b12c3b9 commit 6b26334
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 42 deletions.
6 changes: 6 additions & 0 deletions fivetran/framework/core/schema/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
datasourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
)

func TransformationResource() resourceSchema.Schema {
Expand Down Expand Up @@ -158,6 +161,7 @@ func transformationConfigDatasourceSchema() map[string]datasourceSchema.Attribut
func transformationConfigResourceSchema() map[string]resourceSchema.Attribute {
return map[string]resourceSchema.Attribute{
"project_id": resourceSchema.StringAttribute{
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Optional: true,
Description: "The unique identifier for the dbt Core project within the Fivetran system",
},
Expand All @@ -166,10 +170,12 @@ func transformationConfigResourceSchema() map[string]resourceSchema.Attribute {
Description: "The transformation name",
},
"package_name": resourceSchema.StringAttribute{
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Optional: true,
Description: `The Quickstart transformation package name`,
},
"connection_ids": resourceSchema.SetAttribute{
PlanModifiers: []planmodifier.Set{setplanmodifier.RequiresReplace()},
Optional: true,
ElementType: basetypes.StringType{},
Description: "The list of the connection identifiers to be used for the integrated schedule. Also used to identify package_name automatically if package_name was not specified",
Expand Down
159 changes: 119 additions & 40 deletions fivetran/framework/resources/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,53 +56,121 @@ func (r *transformation) Create(ctx context.Context, req resource.CreateRequest,
return
}

transformationType := data.ProjectType.ValueString()
client := r.GetClient()
svc := client.NewTransformationCreate()
svc.ProjectType(data.ProjectType.ValueString())
svc.ProjectType(transformationType)
svc.Paused(data.Paused.ValueBool())

if !data.Config.IsNull() && !data.Config.IsUnknown() {
config := fivetran.NewTransformationConfig()
configAttributes := data.Config.Attributes()
/* DBT_CORE */
if !configAttributes["project_id"].(basetypes.StringValue).IsNull() && !configAttributes["project_id"].(basetypes.StringValue).IsUnknown() {
config.ProjectId(configAttributes["project_id"].(basetypes.StringValue).ValueString())
if transformationType != "DBT_CORE" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for DBT_CORE type transformation", "project_id"),
)
return
}

config.ProjectId(configAttributes["project_id"].(basetypes.StringValue).ValueString())
}

if !configAttributes["name"].(basetypes.StringValue).IsNull() && !configAttributes["name"].(basetypes.StringValue).IsUnknown() {
if transformationType != "DBT_CORE" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for DBT_CORE type transformation", "name"),
)
return
}

config.Name(configAttributes["name"].(basetypes.StringValue).ValueString())
}

if !configAttributes["steps"].IsUnknown() && !configAttributes["steps"].IsNull() {
if transformationType != "DBT_CORE" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for DBT_CORE type transformation", "steps"),
)
return
}

evars := []transformations.TransformationStep{}

for _, ev := range configAttributes["steps"].(basetypes.ListValue).Elements() {
if element, ok := ev.(basetypes.ObjectValue); ok {
step := transformations.TransformationStep{}
step.Name = element.Attributes()["name"].(basetypes.StringValue).ValueString()
step.Command = element.Attributes()["command"].(basetypes.StringValue).ValueString()
evars = append(evars, step)
}
}

config.Steps(evars)
}

/* QUICKSTART */
packageName := ""
if !configAttributes["package_name"].(basetypes.StringValue).IsNull() && !configAttributes["package_name"].(basetypes.StringValue).IsUnknown() {
config.PackageName(configAttributes["package_name"].(basetypes.StringValue).ValueString())
if transformationType != "QUICKSTART" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for QUICKSTART type transformation", "package_name"),
)
return
}

packageName = configAttributes["package_name"].(basetypes.StringValue).ValueString()

config.PackageName(packageName)
}

connectionIds := []string{}
if !configAttributes["connection_ids"].IsUnknown() && !configAttributes["connection_ids"].IsNull() {
evars := []string{}
if transformationType != "QUICKSTART" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for QUICKSTART type transformation", "connection_ids"),
)
return
}

for _, ev := range configAttributes["connection_ids"].(basetypes.SetValue).Elements() {
evars = append(evars, ev.(basetypes.StringValue).ValueString())
connectionIds = append(connectionIds, ev.(basetypes.StringValue).ValueString())
}
config.ConnectionIds(evars)


config.ConnectionIds(connectionIds)
}

if len(connectionIds) == 0 && packageName == "" && transformationType == "QUICKSTART" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("For a QUICKSTART type transformation, at least one of the `%v` or `%v` parameters must be set.", "package_name", "connection_ids"),
)
return
}

if !configAttributes["excluded_models"].IsUnknown() && !configAttributes["excluded_models"].IsNull() {
if transformationType != "QUICKSTART" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for QUICKSTART type transformation", "excluded_models"),
)
return
}

evars := []string{}
for _, ev := range configAttributes["excluded_models"].(basetypes.SetValue).Elements() {
evars = append(evars, ev.(basetypes.StringValue).ValueString())
}
config.ExcludedModels(evars)
}

if !configAttributes["steps"].IsUnknown() && !configAttributes["steps"].IsNull() {
evars := []transformations.TransformationStep{}
for _, ev := range configAttributes["steps"].(basetypes.ListValue).Elements() {
if element, ok := ev.(basetypes.ObjectValue); ok {
step := transformations.TransformationStep{}
step.Name = element.Attributes()["name"].(basetypes.StringValue).ValueString()
step.Command = element.Attributes()["command"].(basetypes.StringValue).ValueString()
evars = append(evars, step)
}
}
config.Steps(evars)
}

svc.TransformationConfig(config)
}

Expand Down Expand Up @@ -237,33 +305,28 @@ func (r *transformation) Update(ctx context.Context, req resource.UpdateRequest,
if !plan.Config.IsNull() && !plan.Config.IsUnknown() {
config := fivetran.NewTransformationConfig()
configAttributes := plan.Config.Attributes()
if !configAttributes["project_id"].(basetypes.StringValue).IsNull() && !configAttributes["project_id"].(basetypes.StringValue).IsUnknown() {
config.ProjectId(configAttributes["project_id"].(basetypes.StringValue).ValueString())
}
if !configAttributes["name"].(basetypes.StringValue).IsNull() && !configAttributes["name"].(basetypes.StringValue).IsUnknown() {
config.Name(configAttributes["name"].(basetypes.StringValue).ValueString())
}
if !configAttributes["package_name"].(basetypes.StringValue).IsNull() && !configAttributes["package_name"].(basetypes.StringValue).IsUnknown() {
config.PackageName(configAttributes["package_name"].(basetypes.StringValue).ValueString())
}

if !configAttributes["connection_ids"].IsUnknown() && !configAttributes["connection_ids"].IsNull() {
evars := []string{}
for _, ev := range configAttributes["connection_ids"].(basetypes.SetValue).Elements() {
evars = append(evars, ev.(basetypes.StringValue).ValueString())
if !configAttributes["name"].(basetypes.StringValue).IsNull() && !configAttributes["name"].(basetypes.StringValue).IsUnknown() {
if state.ProjectType.ValueString() != "DBT_CORE" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for DBT_CORE type transformation", "name"),
)
return
}
config.ConnectionIds(evars)
}

if !configAttributes["excluded_models"].IsUnknown() && !configAttributes["excluded_models"].IsNull() {
evars := []string{}
for _, ev := range configAttributes["excluded_models"].(basetypes.SetValue).Elements() {
evars = append(evars, ev.(basetypes.StringValue).ValueString())
}
config.ExcludedModels(evars)
config.Name(configAttributes["name"].(basetypes.StringValue).ValueString())
}

if !configAttributes["steps"].IsUnknown() && !configAttributes["steps"].IsNull() {
if state.ProjectType.ValueString() != "DBT_CORE" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for DBT_CORE type transformation", "steps"),
)
return
}

evars := []transformations.TransformationStep{}
for _, ev := range configAttributes["steps"].(basetypes.ListValue).Elements() {
if element, ok := ev.(basetypes.ObjectValue); ok {
Expand All @@ -276,6 +339,22 @@ func (r *transformation) Update(ctx context.Context, req resource.UpdateRequest,
config.Steps(evars)
}

if !configAttributes["excluded_models"].IsUnknown() && !configAttributes["excluded_models"].IsNull() {
if state.ProjectType.ValueString() != "QUICKSTART" {
resp.Diagnostics.AddError(
"Unable to Create Transformation Resource.",
fmt.Sprintf("The parameter `%v` can be set only for QUICKSTART type transformation", "excluded_models"),
)
return
}

evars := []string{}
for _, ev := range configAttributes["excluded_models"].(basetypes.SetValue).Elements() {
evars = append(evars, ev.(basetypes.StringValue).ValueString())
}
config.ExcludedModels(evars)
}

svc.TransformationConfig(config)
}

Expand Down
3 changes: 1 addition & 2 deletions templates/data-sources/quickstart_packages.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Returns a list of available Quickstart transformation package metadata details
## Example Usage

```hcl
data "fivetran_quickstart_packages" "test" {
id = "id"
data "fivetran_quickstart_packages" "all_packages_metadata" {
}
```

Expand Down

0 comments on commit 6b26334

Please sign in to comment.