Skip to content

Commit

Permalink
refactor project resource
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Jun 12, 2024
1 parent a2472d6 commit 49f4b17
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 87 deletions.
80 changes: 76 additions & 4 deletions internal/resource/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (

"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/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
)

var _ resource.Resource = &ProjectResource{}
Expand All @@ -28,7 +32,75 @@ func (r *ProjectResource) Metadata(_ context.Context, req resource.MetadataReque
}

func (r *ProjectResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = r.schema()
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Internal identifier of this project.",
MarkdownDescription: "Internal identifier of this project.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"name": schema.StringAttribute{
Description: "Human-readable name of this project.",
MarkdownDescription: "Human-readable name of this project.",
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"description": schema.StringAttribute{
Description: "Description of this project.",
MarkdownDescription: "Description of this project.",
Optional: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"default": schema.StringAttribute{
Computed: true,
},
"bindings": schema.SingleNestedAttribute{
Description: "Read and write policies of this project.",
MarkdownDescription: "Read and write policies of this project.",
Optional: true,
Attributes: map[string]schema.Attribute{
"read": schema.SetNestedAttribute{
Description: "Read policies of this project.",
MarkdownDescription: "Read policies of this project.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group_id": schema.StringAttribute{
Optional: true,
},
"id": schema.StringAttribute{
Optional: true,
},
"user_id": schema.StringAttribute{
Optional: true,
},
},
},
},
"write": schema.SetNestedAttribute{
Description: "Write policies of this project.",
MarkdownDescription: "Write policies of this project.",
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group_id": schema.StringAttribute{
Optional: true,
},
"id": schema.StringAttribute{
Optional: true,
},
"user_id": schema.StringAttribute{
Optional: true,
},
},
},
},
},
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
},
},
}
}

func (r *ProjectResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand All @@ -50,7 +122,7 @@ func (r *ProjectResource) Configure(_ context.Context, req resource.ConfigureReq
}

func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
data := new(project)
data := new(Project)
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -72,7 +144,7 @@ func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest
}

func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
data := new(project)
data := new(Project)
resp.Diagnostics.Append(req.State.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -89,7 +161,7 @@ func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, re
}

func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
data := new(project)
data := new(Project)
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand Down
6 changes: 3 additions & 3 deletions internal/resource/project_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
gqlclient "github.com/pluralsh/console-client-go"
)

type project struct {
type Project struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Default types.Bool `tfsdk:"default"`
Bindings *common.Bindings `tfsdk:"bindings"`
}

func (p *project) Attributes(ctx context.Context, d diag.Diagnostics) (*gqlclient.ProjectAttributes, error) {
func (p *Project) Attributes(ctx context.Context, d diag.Diagnostics) (*gqlclient.ProjectAttributes, error) {
return &gqlclient.ProjectAttributes{
Name: p.Name.ValueString(),
Description: p.Description.ValueStringPointer(),
Expand All @@ -27,7 +27,7 @@ func (p *project) Attributes(ctx context.Context, d diag.Diagnostics) (*gqlclien
}, nil
}

func (p *project) From(project *gqlclient.ProjectFragment, ctx context.Context, d diag.Diagnostics) {
func (p *Project) From(project *gqlclient.ProjectFragment, ctx context.Context, d diag.Diagnostics) {
p.Id = types.StringValue(project.ID)
p.Name = types.StringValue(project.Name)
p.Description = types.StringPointerValue(project.Description)
Expand Down
80 changes: 0 additions & 80 deletions internal/resource/project_schema.go

This file was deleted.

0 comments on commit 49f4b17

Please sign in to comment.