Skip to content

Commit

Permalink
refactor bidings
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Apr 19, 2024
1 parent 9cec9fc commit e02339b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 31 deletions.
93 changes: 72 additions & 21 deletions internal/common/cluster_bindings.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,92 @@
package common

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
console "github.com/pluralsh/console-client-go"
)

type ClusterBindings struct {
Read []*ClusterPolicyBinding `tfsdk:"read"`
Write []*ClusterPolicyBinding `tfsdk:"write"`
Read types.Set `tfsdk:"read"`
Write types.Set `tfsdk:"write"`
}

func (cb *ClusterBindings) ReadAttributes() []*console.PolicyBindingAttributes {
func (cb *ClusterBindings) ReadAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if cb == nil {
return []*console.PolicyBindingAttributes{}
}

return clusterPolicyBindingAttributes(cb.Read)
return clusterPolicyBindingAttributes(cb.Read, ctx, d)
}

func (cb *ClusterBindings) WriteAttributes() []*console.PolicyBindingAttributes {
func (cb *ClusterBindings) WriteAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if cb == nil {
return []*console.PolicyBindingAttributes{}
}

return clusterPolicyBindingAttributes(cb.Write)
return clusterPolicyBindingAttributes(cb.Write, ctx, d)
}

func clusterPolicyBindingAttributes(bindings types.Set, ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if bindings.IsNull() {
return nil
}

result := make([]*console.PolicyBindingAttributes, 0, len(bindings.Elements()))
elements := make([]ClusterPolicyBinding, len(bindings.Elements()))
d.Append(bindings.ElementsAs(ctx, &elements, false)...)

for _, binding := range elements {
result = append(result, &console.PolicyBindingAttributes{
ID: binding.ID.ValueStringPointer(),
UserID: binding.UserID.ValueStringPointer(),
GroupID: binding.GroupID.ValueStringPointer(),
})
}

return result
}

func (cb *ClusterBindings) From(readBindings []*console.PolicyBinding, writeBindings []*console.PolicyBinding) {
func (cb *ClusterBindings) From(readBindings []*console.PolicyBinding, writeBindings []*console.PolicyBinding, ctx context.Context, d diag.Diagnostics) {
if cb == nil {
return
}

// TODO: WriteBindings
// TODO: ReadBindings
cb.Read = clusterBindingsFrom(readBindings, ctx, d)
cb.Write = clusterBindingsFrom(writeBindings, ctx, d)
}

func clusterBindingsFrom(bindings []*console.PolicyBinding, ctx context.Context, d diag.Diagnostics) types.Set {
if len(bindings) == 0 {
return types.SetNull(basetypes.ObjectType{AttrTypes: ClusterPolicyBindingAttrTypes})
}

values := make([]attr.Value, len(bindings))
for i, binding := range bindings {
value := ClusterPolicyBinding{
ID: types.StringPointerValue(binding.ID),
}

if binding.User != nil {
value.UserID = types.StringValue(binding.User.ID)
}

if binding.Group != nil {
value.GroupID = types.StringValue(binding.Group.ID)
}

objValue, diags := types.ObjectValueFrom(ctx, ClusterPolicyBindingAttrTypes, value)
values[i] = objValue
d.Append(diags...)
}

setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: ClusterPolicyBindingAttrTypes}, values)
d.Append(diags...)
return setValue
}

type ClusterPolicyBinding struct {
Expand All @@ -41,19 +95,16 @@ type ClusterPolicyBinding struct {
UserID types.String `tfsdk:"user_id"`
}

func (c *ClusterPolicyBinding) Attributes() *console.PolicyBindingAttributes {
return &console.PolicyBindingAttributes{
ID: c.ID.ValueStringPointer(),
UserID: c.UserID.ValueStringPointer(),
GroupID: c.GroupID.ValueStringPointer(),
}
var ClusterPolicyBindingAttrTypes = map[string]attr.Type{
"group_id": types.StringType,
"id": types.StringType,
"user_id": types.StringType,
}

func clusterPolicyBindingAttributes(bindings []*ClusterPolicyBinding) []*console.PolicyBindingAttributes {
result := make([]*console.PolicyBindingAttributes, len(bindings))
for i, b := range bindings {
result[i] = b.Attributes()
func (cpb *ClusterPolicyBinding) Attributes() *console.PolicyBindingAttributes {
return &console.PolicyBindingAttributes{
ID: cpb.ID.ValueStringPointer(),
UserID: cpb.UserID.ValueStringPointer(),
GroupID: cpb.GroupID.ValueStringPointer(),
}

return result
}
4 changes: 2 additions & 2 deletions internal/resource/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (c *cluster) Attributes(ctx context.Context, d diag.Diagnostics) console.Cl
Version: c.Version.ValueStringPointer(),
Protect: c.Protect.ValueBoolPointer(),
CloudSettings: c.CloudSettings.Attributes(),
ReadBindings: c.Bindings.ReadAttributes(),
WriteBindings: c.Bindings.WriteAttributes(),
ReadBindings: c.Bindings.ReadAttributes(ctx, d),
WriteBindings: c.Bindings.WriteAttributes(ctx, d),
Tags: c.TagsAttribute(ctx, d),
NodePools: c.NodePoolsAttribute(ctx, d),
Metadata: c.Metadata.ValueStringPointer(),
Expand Down
6 changes: 3 additions & 3 deletions internal/resource/infrastructure_stack_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (is *infrastructureStack) Attributes(ctx context.Context, d diag.Diagnostic
JobSpec: nil,
Configuration: gqlclient.StackConfigurationAttributes{},
Approval: is.Approval.ValueBoolPointer(),
ReadBindings: is.Bindings.ReadAttributes(),
WriteBindings: is.Bindings.WriteAttributes(),
ReadBindings: is.Bindings.ReadAttributes(ctx, d),
WriteBindings: is.Bindings.WriteAttributes(ctx, d),
Files: nil,
Environemnt: nil,
}
Expand All @@ -53,7 +53,7 @@ func (is *infrastructureStack) From(stack *gqlclient.InfrastructureStack, ctx co
is.Configuration.From(stack.Configuration)
is.Files = infrastructureStackFilesFrom(stack.Files, d)
is.Environment = infrastructureStackEnvironmentsFrom(stack.Environment, ctx, d)
is.Bindings.From(stack.ReadBindings, stack.WriteBindings)
is.Bindings.From(stack.ReadBindings, stack.WriteBindings, ctx, d)
is.JobSpec.From(stack.JobSpec)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/resource/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *rbacResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

_, err := r.client.UpdateRbac(ctx, data.Attributes(), data.ServiceId.ValueStringPointer(), data.ClusterId.ValueStringPointer(), nil)
_, 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))
return
Expand All @@ -146,7 +146,7 @@ func (r *rbacResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

_, err := r.client.UpdateRbac(ctx, data.Attributes(), data.ServiceId.ValueStringPointer(), data.ClusterId.ValueStringPointer(), nil)
_, 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))
return
Expand Down
9 changes: 6 additions & 3 deletions internal/resource/rbac_model.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package resource

import (
"context"

"terraform-provider-plural/internal/common"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
gqlclient "github.com/pluralsh/console-client-go"
)
Expand All @@ -13,9 +16,9 @@ type rbac struct {
Bindings *common.ClusterBindings `tfsdk:"rbac"`
}

func (g *rbac) Attributes() gqlclient.RbacAttributes {
func (g *rbac) Attributes(ctx context.Context, d diag.Diagnostics) gqlclient.RbacAttributes {
return gqlclient.RbacAttributes{
ReadBindings: g.Bindings.ReadAttributes(),
WriteBindings: g.Bindings.WriteAttributes(),
ReadBindings: g.Bindings.ReadAttributes(ctx, d),
WriteBindings: g.Bindings.WriteAttributes(ctx, d),
}
}

0 comments on commit e02339b

Please sign in to comment.