Skip to content

Commit

Permalink
fix: update error on project
Browse files Browse the repository at this point in the history
  • Loading branch information
marconneves committed Nov 7, 2024
1 parent ffe7516 commit 4ed2d61
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
63 changes: 36 additions & 27 deletions coolify/project/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package project

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -97,20 +94,7 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

stateFilePath := filepath.Join(".", "project_state_after_create.json")
stateData, err := json.MarshalIndent(data, "", " ")
if err != nil {
resp.Diagnostics.AddError("State Serialization Error", fmt.Sprintf("Unable to serialize state to JSON: %s", err))
return
}

err = os.WriteFile(stateFilePath, stateData, 0644)
if err != nil {
resp.Diagnostics.AddError("File Write Error", fmt.Sprintf("Unable to write state to file: %s", err))
return
}

tflog.Debug(ctx, "Project state saved to file after creation", map[string]interface{}{"file_path": stateFilePath})
tflog.Debug(ctx, "Project state saved to file after creation")
}

func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -139,34 +123,59 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
}

func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data ProjectResourceModel
var plan ProjectResourceModel
var state ProjectResourceModel

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

if data.Id.IsNull() {
resp.Diagnostics.AddError("ID Missing", "UUID is required for updating a project.")
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "Project ID for update", map[string]interface{}{"project_id": data.Id.ValueString()})
if r.client == nil {
resp.Diagnostics.AddError("Client Error", "Client is not configured. Please ensure the provider is properly configured.")
return
}

if state.Id.IsNull() || state.Id.ValueString() == "" {
resp.Diagnostics.AddError("ID Missing", "UUID is required to update the project.")
return
}

updateDTO := coolify_sdk.UpdateProjectDTO{
Name: data.Name.ValueStringPointer(),
Description: data.Description.ValueStringPointer(),
Name: plan.Name.ValueStringPointer(),
Description: plan.Description.ValueStringPointer(),
}

err := r.client.Project.Update(data.Id.ValueString(), &updateDTO)
err := r.client.Project.Update(state.Id.ValueString(), &updateDTO)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update project, got error: %s", err))
return
}

tflog.Trace(ctx, "Updated project", map[string]interface{}{"project_id": data.Id.ValueString()})
project, err := r.client.Project.Get(state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read project after update, got error: %s", err))
return
}

var newState ProjectResourceModel
newState.Id = types.StringValue(project.UUID)
newState.Name = types.StringValue(project.Name)
if project.Description != nil {
newState.Description = types.StringValue(*project.Description)
} else {
newState.Description = types.StringNull()
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
}

func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
20 changes: 20 additions & 0 deletions examples/resouce/coolfify_project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
coolify = {
source = "marconneves/coolify"
version = "4.3.3"
}
}
}


provider "coolify" {
address = "https://cloud.ferramenta.digital"
token = "3|Tt6iybaozpfPZojdlK5gGGotoxK8Cn1u1xTUCe2T1e65744d"
}


resource "coolify_project" "test" {
name = "teste-project"
description = "Descricao base"
}

0 comments on commit 4ed2d61

Please sign in to comment.