diff --git a/coolify/server/create.go b/coolify/server/create.go index b5b15a4..ba64a73 100644 --- a/coolify/server/create.go +++ b/coolify/server/create.go @@ -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(), } @@ -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{}{ diff --git a/coolify/server/fetch.go b/coolify/server/fetch.go index d640bd6..8e8e9ee 100644 --- a/coolify/server/fetch.go +++ b/coolify/server/fetch.go @@ -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) } diff --git a/coolify/server/model.go b/coolify/server/model.go index dee0fcc..b418fc7 100644 --- a/coolify/server/model.go +++ b/coolify/server/model.go @@ -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 { @@ -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) diff --git a/coolify/server/read.go b/coolify/server/read.go index 63b513c..b2061ea 100644 --- a/coolify/server/read.go +++ b/coolify/server/read.go @@ -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 { @@ -75,6 +75,8 @@ 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() { @@ -82,6 +84,7 @@ func (r *ServerResource) ReadServerResource(ctx context.Context, req resource.Re } mapServerResourceModel(&server, serverSaved) + server.PrivateKeyUUID = privateKeyUUID tflog.Trace(ctx, "Successfully read server data", map[string]interface{}{ "server_uuid": serverSaved.UUID, diff --git a/coolify/server/server_resource.go b/coolify/server/server_resource.go index 4693051..1026263 100644 --- a/coolify/server/server_resource.go +++ b/coolify/server/server_resource.go @@ -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) } diff --git a/coolify/server/server_resource_test.go b/coolify/server/server_resource_test.go index 856b104..443e9be 100644 --- a/coolify/server/server_resource_test.go +++ b/coolify/server/server_resource_test.go @@ -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"), + ), + }, }, }) } diff --git a/coolify/server/update.go b/coolify/server/update.go index ca7ce5f..7be1c16 100644 --- a/coolify/server/update.go +++ b/coolify/server/update.go @@ -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(), } @@ -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...) diff --git a/examples/resources/coolify_server/.terraform.lock.hcl b/examples/resources/coolify_server/.terraform.lock.hcl new file mode 100644 index 0000000..5ec77e4 --- /dev/null +++ b/examples/resources/coolify_server/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/marconneves/coolify" { + version = "4.4.0-beta.1" + constraints = "4.4.0-beta.1" + hashes = [ + "h1:6e7sD5h6pM2CEZf2LTrHy0mV+NbUjDkEJIj3KGCDZVs=", + "zh:1d3d1c7abb4a3e4b8b3eb657a18e75a474128e1c8b638120073ea613a68f9178", + "zh:492caaf87acca26ea8ed079894311a146e3a125a0ca405c6cc3d224381971c07", + "zh:4a49f44cca6bdfc409bc5a5102c78a0c949a6c5a09402a5ba5ec0d79b99d360f", + "zh:54996032ed8268017be8de2637d9b6d6f953d1a1d014b7c00bcaefbc72126f2c", + "zh:5b07136192700fae15c67b6cab7fe103197c776390cb1bba2fa0d0afc8985f97", + "zh:6c46bc27743ce25cca388e7146f25edb821a8d019dc3a7be51eb559a836ae3ee", + "zh:7460f9f77c77e65a57f68077f4f49aef2462d6f07d708f44bc9e00a676ce5ccb", + "zh:78b1f31252df17da6ad3be89987050a632e98d559a8ea049e3ee68c51b0a0f25", + "zh:81575edac52aff93313685a94d96f26aab5334cd8b14907b7ec1aae8660f5266", + "zh:9abae805f72ae628065004b8b184c82190f72c1b189f714002dcf86c5eea8595", + "zh:a1a5d22601a1bdacea83eb05edd4a051f12cd4b27eb21d6329d6a7f7b47d53a1", + "zh:caca09a6c3514b3a64d7fc8dbb4b87e746d20bdb00064950a0278bb874865ef7", + "zh:cfa26ed0a6f2705273c90e1162aec344da7f59f11e3f133dc60f9459c8528711", + "zh:e160d908ae70d42d7879e899a9372c390dd446bff7c3736201d301796e11d2cc", + "zh:e82bb1a9dc042c6247446f0df538c24e272b31977941d7490cd45e420887fe3d", + ] +} diff --git a/examples/resources/coolify_server/resource.tf b/examples/resources/coolify_server/resource.tf index b323652..39b1f1a 100644 --- a/examples/resources/coolify_server/resource.tf +++ b/examples/resources/coolify_server/resource.tf @@ -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" -} \ No newline at end of file + user = "root" + private_key_uuid = "xso0ooc4o0w4cswcwws8gswg" +}