Skip to content

Commit

Permalink
fix rbac resource
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Jul 25, 2024
1 parent bfd5e84 commit d20e1af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
17 changes: 10 additions & 7 deletions example/user/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ resource "plural_group" "test" {
description = "test group"
}

resource "plural_group" "empty" {
name = "empty"
}

resource "plural_group_member" "test" {
user_id = data.plural_user.user.id
group_id = plural_group.test.id
resource "plural_rbac" "rbac" {
service_id = "624bff88-05e3-45f6-bc3b-44708594e28e"
bindings = {
read = [{
user_id = data.plural_user.user.id
}]
write = [{
user_id = data.plural_user.user.id
}]
}
}
2 changes: 1 addition & 1 deletion internal/model/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type RBAC struct {
ClusterId types.String `tfsdk:"cluster_id"`
ServiceId types.String `tfsdk:"service_id"`
Bindings *common.Bindings `tfsdk:"rbac"`
Bindings *common.Bindings `tfsdk:"bindings"`
}

func (rbac *RBAC) Attributes(ctx context.Context, d diag.Diagnostics) gqlclient.RbacAttributes {
Expand Down
42 changes: 16 additions & 26 deletions internal/resource/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"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"

"terraform-provider-plural/internal/client"
"terraform-provider-plural/internal/common"
Expand Down Expand Up @@ -56,19 +58,13 @@ func (r *rbacResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
"id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
"user_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
},
},
Expand All @@ -80,24 +76,19 @@ func (r *rbacResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"group_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
"id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
"user_id": schema.StringAttribute{
Description: "",
MarkdownDescription: "",
Optional: true,
Optional: true,
},
},
},
},
},
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
},
},
}
Expand All @@ -122,45 +113,44 @@ func (r *rbacResource) Configure(_ context.Context, req resource.ConfigureReques
}

func (r *rbacResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data model.RBAC
data := new(model.RBAC)
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...)
if resp.Diagnostics.HasError() {
return
}

_, err := r.client.UpdateRbac(ctx, data.Attributes(ctx, resp.Diagnostics), data.ServiceId.ValueStringPointer(), data.ClusterId.ValueStringPointer(), nil)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update rbac, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update RBAC, got error: %s", err))
return
}

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

func (r *rbacResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
func (r *rbacResource) Read(_ context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) {
// ignore
}

func (r *rbacResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data model.RBAC
data := new(model.RBAC)
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

_, err := r.client.UpdateRbac(ctx, data.Attributes(ctx, resp.Diagnostics), data.ServiceId.ValueStringPointer(), data.ClusterId.ValueStringPointer(), nil)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update rbac, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update RBAC, got error: %s", err))
return
}

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

func (r *rbacResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
func (r *rbacResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) {
// ignore
}

func (r *rbacResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// ignore
func (r *rbacResource) ImportState(_ context.Context, _ resource.ImportStateRequest, _ *resource.ImportStateResponse) {
}

0 comments on commit d20e1af

Please sign in to comment.