Skip to content

Commit

Permalink
fix: update error base
Browse files Browse the repository at this point in the history
  • Loading branch information
marconneves committed Nov 7, 2024
1 parent 7d088cb commit 6f1387c
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
5 changes: 4 additions & 1 deletion coolify/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func (r *ServerResource) CreateServer(ctx context.Context, req resource.CreateRe
"description": data.Description.ValueString(),
})

privateKeyUUID := data.PrivateKeyUUID.ValueString()

createDTO := coolify_sdk.CreateServerDTO{
Name: data.Name.ValueString(),
IP: data.IP.ValueString(),
Description: data.Description.ValueString(),
IsBuildServer: false,
Port: int(data.Port.ValueInt32()),
User: data.UUID.ValueString(),
User: data.User.ValueString(),
PrivateKeyUUID: data.PrivateKeyUUID.ValueString(),
}

Expand All @@ -42,6 +44,7 @@ func (r *ServerResource) CreateServer(ctx context.Context, req resource.CreateRe
}

data.UUID = types.StringValue(*serverID)
data.PrivateKeyUUID = types.StringValue(privateKeyUUID)
tflog.Trace(ctx, "Created a server", map[string]interface{}{"server_id": serverID})

tflog.Debug(ctx, "Data after server creation", map[string]interface{}{
Expand Down
6 changes: 3 additions & 3 deletions coolify/server/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
coolify_sdk "github.com/marconneves/coolify-sdk-go"
)

func fetchServerByID(client coolify_sdk.Sdk, ctx context.Context, id string) (*coolify_sdk.Server, error) {
func fetchServerByID(client coolify_sdk.Sdk, ctx context.Context, uuid string) (*coolify_sdk.Server, error) {
tflog.Debug(ctx, "Fetching server by ID", map[string]interface{}{
"server_id": id,
"server_id": uuid,
})

server, err := client.Server.Get(id)
server, err := client.Server.Get(uuid)
if err != nil {
return nil, fmt.Errorf("unable to read server data by ID, got error: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion coolify/server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func mapCommonServerFields(data *ServerModel, server *coolify_sdk.Server) {
if server.Description != nil {
data.Description = types.StringValue(*server.Description)
}
data.User = types.StringValue(server.User)

portInt64, err := strconv.ParseInt(server.Port, 10, 32)
if err != nil {
Expand Down Expand Up @@ -198,7 +199,6 @@ func mapServerDataSourceModel(data *ServerDataSourceModel, server *coolify_sdk.S
data.TeamID = types.Int64Value(int64(server.TeamID))
data.UnreachableCount = types.Int64Value(int64(server.UnreachableCount))
data.UnreachableNotificationSent = types.BoolValue(server.UnreachableNotificationSent)
data.User = types.StringValue(server.User)
data.ValidationLogs = types.StringNull()
if server.ValidationLogs != nil {
data.ValidationLogs = types.StringValue(*server.ValidationLogs)
Expand Down
9 changes: 6 additions & 3 deletions coolify/server/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
coolify_sdk "github.com/marconneves/coolify-sdk-go"
)

func readServer(ctx context.Context, client coolify_sdk.Sdk, id types.String, name types.String) (*coolify_sdk.Server, diag.Diagnostics) {
func readServer(ctx context.Context, client coolify_sdk.Sdk, uuid types.String, name types.String) (*coolify_sdk.Server, diag.Diagnostics) {
var diags diag.Diagnostics

var server *coolify_sdk.Server
var err error

if !id.IsNull() {
server, err = fetchServerByID(client, ctx, id.ValueString())
if !uuid.IsNull() {
server, err = fetchServerByID(client, ctx, uuid.ValueString())
} else if !name.IsNull() {
server, err = fetchServerByName(client, ctx, name.ValueString())
} else {
Expand Down Expand Up @@ -75,13 +75,16 @@ func (r *ServerResource) ReadServerResource(ctx context.Context, req resource.Re
return
}

privateKeyUUID := server.PrivateKeyUUID

serverSaved, diags := readServer(ctx, *r.client, server.UUID, server.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

mapServerResourceModel(&server, serverSaved)
server.PrivateKeyUUID = privateKeyUUID

tflog.Trace(ctx, "Successfully read server data", map[string]interface{}{
"server_uuid": serverSaved.UUID,
Expand Down
2 changes: 1 addition & 1 deletion coolify/server/server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ func (r *ServerResource) Delete(ctx context.Context, req resource.DeleteRequest,
}

func (r *ServerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
resource.ImportStatePassthroughID(ctx, path.Root("uuid"), req, resp)
}
30 changes: 15 additions & 15 deletions coolify/server/server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ func TestAccServerResource(t *testing.T) {
resource.TestCheckResourceAttr("coolify_server.test", "private_key_uuid", "xso0ooc4o0w4cswcwws8gswg"),
),
},
// {
// ResourceName: "coolify_server.test",
// ImportState: true,
// ImportStateVerify: true,
// },
// {
// Config: testAccServerResourceConfig("updated-server", "192.168.1.2", "2222", "newuser", "xso0ooc4o0w4cswcwws8gswg"),
// Check: resource.ComposeAggregateTestCheckFunc(
// resource.TestCheckResourceAttr("coolify_server.test", "name", "updated-server"),
// resource.TestCheckResourceAttr("coolify_server.test", "ip", "192.168.1.2"),
// resource.TestCheckResourceAttr("coolify_server.test", "port", "2222"),
// resource.TestCheckResourceAttr("coolify_server.test", "user", "newuser"),
// resource.TestCheckResourceAttr("coolify_server.test", "private_key_uuid", "xso0ooc4o0w4cswcwws8gswg"),
// ),
// },
{
ResourceName: "coolify_server.test",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccServerResourceConfig("updated-server", "192.168.1.2", "2222", "newuser", "xso0ooc4o0w4cswcwws8gswg"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("coolify_server.test", "name", "updated-server"),
resource.TestCheckResourceAttr("coolify_server.test", "ip", "192.168.1.2"),
resource.TestCheckResourceAttr("coolify_server.test", "port", "2222"),
resource.TestCheckResourceAttr("coolify_server.test", "user", "newuser"),
resource.TestCheckResourceAttr("coolify_server.test", "private_key_uuid", "xso0ooc4o0w4cswcwws8gswg"),
),
},
},
})
}
Expand Down
3 changes: 2 additions & 1 deletion coolify/server/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *ServerResource) UpdateServer(ctx context.Context, req resource.UpdateRe
Description: plan.Description.ValueString(),
IsBuildServer: false,
Port: int(plan.Port.ValueInt32()),
User: plan.UUID.ValueString(),
User: plan.User.ValueString(),
PrivateKeyUUID: plan.PrivateKeyUUID.ValueString(),
}

Expand All @@ -59,6 +59,7 @@ func (r *ServerResource) UpdateServer(ctx context.Context, req resource.UpdateRe
var newState ServerModel

mapCommonServerFields(&newState, server)
newState.PrivateKeyUUID = plan.PrivateKeyUUID

diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
Expand Down
25 changes: 25 additions & 0 deletions examples/resources/coolify_server/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions examples/resources/coolify_server/resource.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

terraform {
required_providers {
coolify = {
source = "marconneves/coolify"
version = "4.4.0-beta.1"
}
}
}

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

resource "coolify_server" "test" {
name = "example-server"
ip = "192.168.1.100"
port = "22"
user = "example-user"
private_key_uuid = "example-key-uuid"
}
user = "root"
private_key_uuid = "xso0ooc4o0w4cswcwws8gswg"
}

0 comments on commit 6f1387c

Please sign in to comment.