Skip to content

Commit

Permalink
Fix DBT projects issues (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
fivetran-aleksandrboldyrev authored Sep 10, 2024
1 parent efdb4c5 commit 53e8821
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
42 changes: 22 additions & 20 deletions fivetran/framework/core/model/dbt_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,32 @@ func (d *DbtProjectResourceModel) ReadFromResponse(ctx context.Context, resp dbt
d.CreatedById = types.StringValue(resp.Data.CreatedById)
d.PublicKey = types.StringValue(resp.Data.PublicKey)

projectConfigTypes := map[string]attr.Type{
"git_remote_url": types.StringType,
"git_branch": types.StringType,
"folder_path": types.StringType,
}
projectConfigItems := map[string]attr.Value{
"git_remote_url": types.StringValue(resp.Data.ProjectConfig.GitRemoteUrl),
// "git_branch": types.StringValue(resp.Data.ProjectConfig.GitBranch),
// "folder_path": types.StringValue(resp.Data.ProjectConfig.FolderPath),
}
// If user specifies config section
if !d.ProjectConfig.IsNull() && !d.ProjectConfig.IsUnknown(){
projectConfigTypes := map[string]attr.Type{
"git_remote_url": types.StringType,
"git_branch": types.StringType,
"folder_path": types.StringType,
}
projectConfigItems := map[string]attr.Value{
"git_remote_url": types.StringValue(resp.Data.ProjectConfig.GitRemoteUrl),
}

if resp.Data.ProjectConfig.GitBranch != "" {
projectConfigItems["git_branch"] = types.StringValue(resp.Data.ProjectConfig.GitBranch)
} else {
projectConfigItems["git_branch"] = types.StringNull()
}
if resp.Data.ProjectConfig.GitBranch != "" {
projectConfigItems["git_branch"] = types.StringValue(resp.Data.ProjectConfig.GitBranch)
} else {
projectConfigItems["git_branch"] = types.StringNull()
}

if resp.Data.ProjectConfig.FolderPath != "" {
projectConfigItems["folder_path"] = types.StringValue(resp.Data.ProjectConfig.FolderPath)
} else {
projectConfigItems["folder_path"] = types.StringNull()
if resp.Data.ProjectConfig.FolderPath != "" {
projectConfigItems["folder_path"] = types.StringValue(resp.Data.ProjectConfig.FolderPath)
} else {
projectConfigItems["folder_path"] = types.StringNull()
}

d.ProjectConfig, _ = types.ObjectValue(projectConfigTypes, projectConfigItems)
}

d.ProjectConfig, _ = types.ObjectValue(projectConfigTypes, projectConfigItems)
d.EnsureReadiness = types.BoolValue(strings.ToLower(resp.Data.Status) == "ready")
if modelsResp != nil {
d.Models = GetModelsSetFromResponse(*modelsResp)
Expand Down
37 changes: 32 additions & 5 deletions fivetran/framework/resources/dbt_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,39 @@ func (r *dbtProject) Create(ctx context.Context, req resource.CreateRequest, res

projectResponse, err := svc.Do(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create dbt Project Resource.",
fmt.Sprintf("%v; code: %v; message: %v", err, projectResponse.Code, projectResponse.Message),
)
if projectResponse.Code != "DbtProjectExists" {
resp.Diagnostics.AddError(
"Unable to Create dbt Project Resource.",
fmt.Sprintf("%v; code: %v; message: %v", err, projectResponse.Code, projectResponse.Message),
)

return
return
} else {
// try to recover Id
projectListResponse, err := r.GetClient().NewDbtProjectsList().Do(ctx)

if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Dbt Project Resource.",
fmt.Sprintf("%v; code: %v; message: %v", err, projectResponse.Code, projectResponse.Message),
)
return
}

for _, v := range projectListResponse.Data.Items {
if v.GroupId == data.GroupId.ValueString() {
projectResponse, err := r.GetClient().NewDbtProjectDetails().DbtProjectID(v.ID).Do(ctx)

if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Dbt Project Resource.",
fmt.Sprintf("%v; code: %v; message: %v", err, projectResponse.Code, projectResponse.Message),
)
return
}
}
}
}
}

data.ReadFromResponse(ctx, projectResponse, nil)
Expand Down
8 changes: 8 additions & 0 deletions fivetran/framework/resources/dbt_project_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func upgradeDbtProjectState(ctx context.Context, req resource.UpgradeStateReques
"created_by_id": rawState["created_by_id"],
"public_key": rawState["public_key"],
"environment_vars": rawState["environment_vars"],
"ensure_readiness": rawState["ensure_readiness"],
"timeouts": rawState["timeouts"],
"models": rawState["models"],
"project_config": convertSetToBlock("project_config",
rawState["project_config"],
Expand All @@ -75,7 +77,13 @@ func getDbtProjectStateModel(version int) tftypes.Type {
"created_at": tftypes.String,
"created_by_id": tftypes.String,
"public_key": tftypes.String,
"ensure_readiness": tftypes.Bool,
"environment_vars": tftypes.Set{ElementType:tftypes.String},
"timeouts": tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"create": tftypes.String,
},
},
"models": tftypes.Set{
ElementType: tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
Expand Down
9 changes: 1 addition & 8 deletions fivetran/tests/e2e/resource_dbt_project_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestResourceDbtProjectE2E(t *testing.T) {
t.Skip("Now for deployment you need to use a third-party provider, which is impossible to do inside the local provider test")
//t.Skip("Now for deployment you need to use a third-party provider, which is impossible to do inside the local provider test")
destinationConfig := `
resource "fivetran_destination" "test_destination" {
provider = fivetran-provider
Expand Down Expand Up @@ -41,13 +41,6 @@ func TestResourceDbtProjectE2E(t *testing.T) {
threads = 1
default_schema = "dbt_demo_test_e2e_terraform"
type = "GIT"
project_config {
folder_path = "/folder/path"
git_remote_url = "[email protected]:fivetran/dbt_demo.git"
git_branch = "main"
}
ensure_readiness = true
}
resource "fivetran_dbt_git_project_config" "test_project_config" {
Expand Down
2 changes: 1 addition & 1 deletion templates/guides/version_1.3.0_update_guides.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ resource "fivetran_dbt_project" "test_project" {
}

resource "fivetran_dbt_git_project_config" "test_project_config" {
id = fivetran_dbt_project.test_project.id
project_id = fivetran_dbt_project.test_project.id

folder_path = "/folder/path"
git_remote_url = "[email protected]:fivetran/dbt_demo.git"
Expand Down

0 comments on commit 53e8821

Please sign in to comment.