Skip to content

Commit

Permalink
fix: base of uuid not founded
Browse files Browse the repository at this point in the history
  • Loading branch information
marconneves committed Nov 7, 2024
1 parent 6f1387c commit 4f1d4a0
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 24 deletions.
25 changes: 22 additions & 3 deletions coolify/private_key/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,33 @@ import (
coolify_sdk "github.com/marconneves/coolify-sdk-go"
)

func fetchPrivateKeyByID(client coolify_sdk.Sdk, ctx context.Context, id string) (*coolify_sdk.PrivateKey, error) {
tflog.Debug(ctx, "Fetching private key by ID", map[string]interface{}{
func FetchPrivateKeyByID(client coolify_sdk.Sdk, ctx context.Context, id int) (*coolify_sdk.PrivateKey, error) {
tflog.Debug(ctx, "Fetching project by Id", map[string]interface{}{
"id": id,
})

projects, err := client.PrivateKey.List()
if err != nil {
return nil, fmt.Errorf("unable to list projects, got error: %w", err)
}

for _, p := range *projects {
if p.ID == id {
return &p, nil
}
}

return nil, fmt.Errorf("no project found with id: %v", id)
}

func fetchPrivateKeyByUUID(client coolify_sdk.Sdk, ctx context.Context, id string) (*coolify_sdk.PrivateKey, error) {
tflog.Debug(ctx, "Fetching private key by UUID", map[string]interface{}{
"private_key_id": id,
})

privateKey, err := client.PrivateKey.Get(id)
if err != nil {
return nil, fmt.Errorf("unable to read private key data by ID, got error: %w", err)
return nil, fmt.Errorf("unable to read private key data by UUID, got error: %w", err)
}

return privateKey, nil
Expand Down
2 changes: 1 addition & 1 deletion coolify/private_key/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func readPrivateKey(ctx context.Context, client coolify_sdk.Sdk, id types.String
var err error

if !id.IsNull() {
privateKey, err = fetchPrivateKeyByID(client, ctx, id.ValueString())
privateKey, err = fetchPrivateKeyByUUID(client, ctx, id.ValueString())
} else {
diags.AddError("Configuration Error", "Either 'id' or 'name' must be specified.")
return nil, diags
Expand Down
4 changes: 2 additions & 2 deletions coolify/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (r *ServerResource) CreateServer(ctx context.Context, req resource.CreateRe
return
}

data.UUID = types.StringValue(*serverID)
data.ID = 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{}{
"uuid": data.UUID.ValueString(),
"uuid": data.ID.ValueString(),
"name": data.Name.ValueString(),
"ip": data.IP.ValueString(),
"description": data.Description.ValueString(),
Expand Down
4 changes: 2 additions & 2 deletions coolify/server/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (r *ServerResource) DeleteServer(ctx context.Context, req resource.DeleteRe
return
}

err := r.client.Server.Delete(data.UUID.ValueString())
err := r.client.Server.Delete(data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete server, got error: %s", err))
return
}

tflog.Trace(ctx, "Deleted server", map[string]interface{}{"server_id": data.UUID.ValueString()})
tflog.Trace(ctx, "Deleted server", map[string]interface{}{"server_id": data.ID.ValueString()})
}
4 changes: 2 additions & 2 deletions coolify/server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type ServerModel struct {
UUID types.String `tfsdk:"uuid"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
IP types.String `tfsdk:"ip"`
Expand Down Expand Up @@ -82,7 +82,7 @@ type SettingsModel struct {
}

func mapCommonServerFields(data *ServerModel, server *coolify_sdk.Server) {
data.UUID = types.StringValue(server.UUID)
data.ID = types.StringValue(server.UUID)
data.IP = types.StringValue(server.IP)
data.Name = types.StringValue(server.Name)
data.Description = types.StringNull()
Expand Down
18 changes: 12 additions & 6 deletions coolify/server/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
coolify_sdk "github.com/marconneves/coolify-sdk-go"
"github.com/marconneves/terraform-provider-coolify/coolify/private_key"
)

func readServer(ctx context.Context, client coolify_sdk.Sdk, uuid types.String, name types.String) (*coolify_sdk.Server, diag.Diagnostics) {
Expand All @@ -28,7 +29,7 @@ func readServer(ctx context.Context, client coolify_sdk.Sdk, uuid types.String,
}

if err != nil {
diags.AddError("Client Error", fmt.Sprintf("Unable to read server data: %s", err))
diags.AddError("Client Error", fmt.Sprintf("Unable to read server data: %s %s", err, uuid.ValueString()))
return nil, diags
}

Expand All @@ -49,7 +50,7 @@ func (s *ServerDataSource) ReadServerDataSource(ctx context.Context, req datasou
return
}

serverSaved, diags := readServer(ctx, *s.client, server.UUID, server.Name)
serverSaved, diags := readServer(ctx, *s.client, server.ID, server.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -75,16 +76,21 @@ func (r *ServerResource) ReadServerResource(ctx context.Context, req resource.Re
return
}

privateKeyUUID := server.PrivateKeyUUID

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

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

privateKey, err := private_key.FetchPrivateKeyByID(*r.client, ctx, serverSaved.PrivateKeyID)
if err != nil {
diags.AddError("Not Found", "No server found with the given ID or name")
return
}

server.PrivateKeyUUID = types.StringValue(privateKey.UUID)

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_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *ServerDataSource) Metadata(ctx context.Context, req datasource.Metadata
func (s *ServerDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
"id": schema.StringAttribute{
Optional: true,
Description: "The unique identifier of the server.",
},
Expand Down
4 changes: 2 additions & 2 deletions coolify/server/server_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestAccServerDataSource(t *testing.T) {
ProtoV6ProviderFactories: tests.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccServerDataSourceConfig(`uuid = "kk4wskwk0k0sssc8ckcss44c"`),
Config: testAccServerDataSourceConfig(`id = "kk4wskwk0k0sssc8ckcss44c"`),
Check: testAccServerDataSourceCheck(),
},
{
Expand All @@ -35,7 +35,7 @@ data "coolify_server" "test" {

func testAccServerDataSourceCheck() resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.coolify_server.test", "uuid", "kk4wskwk0k0sssc8ckcss44c"),
resource.TestCheckResourceAttr("data.coolify_server.test", "id", "kk4wskwk0k0sssc8ckcss44c"),
resource.TestCheckResourceAttr("data.coolify_server.test", "name", "example-server"),
resource.TestCheckResourceAttr("data.coolify_server.test", "ip", "localhost"),
resource.TestCheckResourceAttr("data.coolify_server.test", "high_disk_usage_notification_sent", "false"),
Expand Down
4 changes: 2 additions & 2 deletions coolify/server/server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *ServerResource) Schema(ctx context.Context, req resource.SchemaRequest,
MarkdownDescription: "Manage Coolify servers",

Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
"id": schema.StringAttribute{
MarkdownDescription: "Server identifier",
Computed: true,
Optional: true,
Expand Down 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("uuid"), req, resp)
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
6 changes: 3 additions & 3 deletions coolify/server/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *ServerResource) UpdateServer(ctx context.Context, req resource.UpdateRe
return
}

if state.UUID.IsNull() || state.UUID.ValueString() == "" {
if state.ID.IsNull() || state.ID.ValueString() == "" {
resp.Diagnostics.AddError("ID Missing", "UUID is required to update the server.")
return
}
Expand All @@ -44,13 +44,13 @@ func (r *ServerResource) UpdateServer(ctx context.Context, req resource.UpdateRe
PrivateKeyUUID: plan.PrivateKeyUUID.ValueString(),
}

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

server, err := r.client.Server.Get(state.UUID.ValueString())
server, err := r.client.Server.Get(state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read server after update, got error: %s", err))
return
Expand Down

0 comments on commit 4f1d4a0

Please sign in to comment.