From e02339b5c818680b2d3527e5c3a2930e5df66af6 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Fri, 19 Apr 2024 16:31:15 +0200 Subject: [PATCH] refactor bidings --- internal/common/cluster_bindings.go | 93 ++++++++++++++----- internal/resource/cluster_model.go | 4 +- .../resource/infrastructure_stack_model.go | 6 +- internal/resource/rbac.go | 4 +- internal/resource/rbac_model.go | 9 +- 5 files changed, 85 insertions(+), 31 deletions(-) diff --git a/internal/common/cluster_bindings.go b/internal/common/cluster_bindings.go index 7b033ca..18d3a73 100644 --- a/internal/common/cluster_bindings.go +++ b/internal/common/cluster_bindings.go @@ -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 { @@ -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 } diff --git a/internal/resource/cluster_model.go b/internal/resource/cluster_model.go index 4eff890..716d09b 100644 --- a/internal/resource/cluster_model.go +++ b/internal/resource/cluster_model.go @@ -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(), diff --git a/internal/resource/infrastructure_stack_model.go b/internal/resource/infrastructure_stack_model.go index 689c6f0..2462cb5 100644 --- a/internal/resource/infrastructure_stack_model.go +++ b/internal/resource/infrastructure_stack_model.go @@ -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, } @@ -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) } diff --git a/internal/resource/rbac.go b/internal/resource/rbac.go index c432633..4d8a0a6 100644 --- a/internal/resource/rbac.go +++ b/internal/resource/rbac.go @@ -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 @@ -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 diff --git a/internal/resource/rbac_model.go b/internal/resource/rbac_model.go index 3f10ce0..b89b4bb 100644 --- a/internal/resource/rbac_model.go +++ b/internal/resource/rbac_model.go @@ -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" ) @@ -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), } }