Skip to content

Commit

Permalink
fix: update base of provider to product base
Browse files Browse the repository at this point in the history
  • Loading branch information
marconneves committed Nov 7, 2024
1 parent 43750e2 commit 527f486
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 162 deletions.
51 changes: 51 additions & 0 deletions coolify/project/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// create.go
package project

import (
"context"
"fmt"

coolify_sdk "github.com/marconneves/coolify-sdk-go"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

func (r *ProjectResource) CreateProject(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ProjectModel

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

tflog.Debug(ctx, "Planned data before creation", map[string]interface{}{
"name": data.Name.ValueString(),
"description": data.Description.ValueString(),
})

createDTO := coolify_sdk.CreateProjectDTO{
Name: data.Name.ValueStringPointer(),
Description: data.Description.ValueStringPointer(),
}

projectID, err := r.client.Project.Create(&createDTO)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create project, got error: %s", err))
return
}

data.Id = types.StringValue(*projectID)
tflog.Trace(ctx, "Created a project", map[string]interface{}{"project_id": projectID})

tflog.Debug(ctx, "Data after project creation", map[string]interface{}{
"id": data.Id.ValueString(),
"name": data.Name.ValueString(),
"description": data.Description.ValueString(),
})

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

tflog.Debug(ctx, "Project state saved to file after creation")
}
26 changes: 26 additions & 0 deletions coolify/project/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package project

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

func (r *ProjectResource) DeleteProject(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data ProjectModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

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

tflog.Trace(ctx, "Deleted project", map[string]interface{}{"project_id": data.Id.ValueString()})
}
28 changes: 22 additions & 6 deletions coolify/project/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import (
)

type ProjectModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Environments []EnvironmentModel `tfsdk:"environments"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}

type ProjectWithEnvironmentsModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Environments *[]EnvironmentModel `tfsdk:"environments"`
}

type EnvironmentModel struct {
Expand All @@ -21,7 +27,7 @@ type EnvironmentModel struct {
UpdatedAt types.String `tfsdk:"updated_at"`
}

func mapProjectDataSourceModel(projectData *ProjectModel, project *coolify_sdk.Project) {
func mapProjectDataSourceModel(projectData *ProjectWithEnvironmentsModel, project *coolify_sdk.Project) {
projectData.Id = types.StringValue(project.UUID)
projectData.Name = types.StringValue(project.Name)
if project.Description != nil {
Expand Down Expand Up @@ -49,6 +55,16 @@ func mapProjectDataSourceModel(projectData *ProjectModel, project *coolify_sdk.P
UpdatedAt: types.StringValue(env.UpdatedAt.String()),
}
}
projectData.Environments = environments
projectData.Environments = &environments
}
}

func mapProjectResourceModel(projectData *ProjectModel, project *coolify_sdk.Project) {
projectData.Id = types.StringValue(project.UUID)
projectData.Name = types.StringValue(project.Name)
if project.Description != nil {
projectData.Description = types.StringValue(*project.Description)
} else {
projectData.Description = types.StringNull()
}
}
23 changes: 1 addition & 22 deletions coolify/project/project_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

var _ datasource.DataSource = &ProjectDataSource{}
Expand Down Expand Up @@ -83,25 +82,5 @@ func (d *ProjectDataSource) Configure(ctx context.Context, req datasource.Config
}

func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var project ProjectModel

resp.Diagnostics.Append(req.Config.Get(ctx, &project)...)
if resp.Diagnostics.HasError() {
return
}

projectSaved, diags := readProject(ctx, *d.client, project.Id, project.Name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

mapProjectDataSourceModel(&project, projectSaved)

tflog.Trace(ctx, "Successfully read team data", map[string]interface{}{
"project_id": projectSaved.ID,
"name": projectSaved.Name,
})

resp.Diagnostics.Append(resp.State.Set(ctx, &project)...)
d.ReadProjectDatasource(ctx, req, resp)
}
137 changes: 4 additions & 133 deletions coolify/project/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package project

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
coolify_sdk "github.com/marconneves/coolify-sdk-go"
configure "github.com/marconneves/terraform-provider-coolify/shared"
)
Expand All @@ -24,12 +21,6 @@ type ProjectResource struct {
client *coolify_sdk.Sdk
}

type ProjectResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}

func (r *ProjectResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}
Expand Down Expand Up @@ -61,139 +52,19 @@ func (r *ProjectResource) Configure(ctx context.Context, req resource.ConfigureR
}

func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data ProjectResourceModel

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

tflog.Debug(ctx, "Planned data before creation", map[string]interface{}{
"name": data.Name.ValueString(),
"description": data.Description.ValueString(),
})

createDTO := coolify_sdk.CreateProjectDTO{
Name: data.Name.ValueStringPointer(),
Description: data.Description.ValueStringPointer(),
}

projectID, err := r.client.Project.Create(&createDTO)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create project, got error: %s", err))
return
}

data.Id = types.StringValue(*projectID)
tflog.Trace(ctx, "Created a project", map[string]interface{}{"project_id": projectID})

tflog.Debug(ctx, "Data after project creation", map[string]interface{}{
"id": data.Id.ValueString(),
"name": data.Name.ValueString(),
"description": data.Description.ValueString(),
})

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

tflog.Debug(ctx, "Project state saved to file after creation")
r.CreateProject(ctx, req, resp)
}

func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data ProjectResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

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

data.Id = types.StringValue(project.UUID)
data.Name = types.StringValue(project.Name)
if project.Description != nil {
data.Description = types.StringValue(*project.Description)
}

tflog.Trace(ctx, "Read project", map[string]interface{}{"project_id": project.ID})

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
r.ReadProjectResource(ctx, req, resp)
}

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

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

diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

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: plan.Name.ValueStringPointer(),
Description: plan.Description.ValueStringPointer(),
}

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
}

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()
}

diags = resp.State.Set(ctx, &newState)
resp.Diagnostics.Append(diags...)
r.UpdateProject(ctx, req, resp)
}

func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data ProjectResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

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

tflog.Trace(ctx, "Deleted project", map[string]interface{}{"project_id": data.Id.ValueString()})
r.DeleteProject(ctx, req, resp)
}

func (r *ProjectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
Loading

0 comments on commit 527f486

Please sign in to comment.