From 6b4fa83c3c86baa80bfd117cdc583ccd37bf70e7 Mon Sep 17 00:00:00 2001 From: Dean Date: Wed, 18 Jan 2023 10:39:42 +0100 Subject: [PATCH] fix kubernetes ACL type conversion error (#87) * fix acl type conversion error Co-authored-by: Dean Oren --- .../kubernetes/cluster/helpers.go | 25 ++++++--- .../mongodb-flex/instance/data_source_test.go | 2 +- .../data-services/instance/actions.go | 2 +- .../resources/kubernetes/cluster/helpers.go | 56 +++++++++++++++---- .../resources/kubernetes/cluster/schema.go | 4 +- 5 files changed, 66 insertions(+), 23 deletions(-) diff --git a/stackit/internal/data-sources/kubernetes/cluster/helpers.go b/stackit/internal/data-sources/kubernetes/cluster/helpers.go index 23f63b78..adffda7e 100644 --- a/stackit/internal/data-sources/kubernetes/cluster/helpers.go +++ b/stackit/internal/data-sources/kubernetes/cluster/helpers.go @@ -144,10 +144,13 @@ func transformExtensions(c *kubernetesCluster.Cluster, cl *cluster.Cluster) { Enabled: types.BoolValue(false), ArgusInstanceID: types.StringNull(), }, - ACL: &kubernetesCluster.ACL{ - Enabled: types.BoolValue(false), - AllowedCIDRs: types.ListNull(types.StringType), - }, + ACL: types.ObjectValueMust(map[string]attr.Type{ + "enabled": types.BoolType, + "allowed_cidrs": types.ListType{ElemType: types.StringType}, + }, map[string]attr.Value{ + "enabled": types.BoolValue(false), + "allowed_cidrs": types.ListNull(types.StringType), + }), } if cl.Extensions.Argus != nil { c.Extensions.Argus = &kubernetesCluster.ArgusExtension{ @@ -155,14 +158,22 @@ func transformExtensions(c *kubernetesCluster.Cluster, cl *cluster.Cluster) { ArgusInstanceID: types.StringValue(cl.Extensions.Argus.ArgusInstanceID), } } + if cl.Extensions.Acl != nil { cidr := []attr.Value{} for _, v := range cl.Extensions.Acl.AllowedCidrs { cidr = append(cidr, types.StringValue(v)) } - c.Extensions.ACL = &kubernetesCluster.ACL{ - Enabled: types.BoolValue(cl.Extensions.Acl.Enabled), - AllowedCIDRs: types.ListValueMust(types.StringType, cidr), + cidrList, diags := types.ListValue(types.StringType, cidr) + if diags.HasError() { + cidrList = types.ListNull(types.StringType) } + c.Extensions.ACL = types.ObjectValueMust(map[string]attr.Type{ + "enabled": types.BoolType, + "allowed_cidrs": types.ListType{ElemType: types.StringType}, + }, map[string]attr.Value{ + "enabled": types.BoolValue(cl.Extensions.Acl.Enabled), + "allowed_cidrs": cidrList, + }) } } diff --git a/stackit/internal/data-sources/mongodb-flex/instance/data_source_test.go b/stackit/internal/data-sources/mongodb-flex/instance/data_source_test.go index 806dfcf1..e8958056 100644 --- a/stackit/internal/data-sources/mongodb-flex/instance/data_source_test.go +++ b/stackit/internal/data-sources/mongodb-flex/instance/data_source_test.go @@ -12,7 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -const run_this_test = true +const run_this_test = false func TestAcc_MongoDBFlexInstance(t *testing.T) { if !common.ShouldAccTestRun(run_this_test) { diff --git a/stackit/internal/resources/data-services/instance/actions.go b/stackit/internal/resources/data-services/instance/actions.go index 9117daf3..950bb151 100644 --- a/stackit/internal/resources/data-services/instance/actions.go +++ b/stackit/internal/resources/data-services/instance/actions.go @@ -185,7 +185,7 @@ func (r Resource) Update(ctx context.Context, req resource.UpdateRequest, resp * } // mitigate an API bug that returns old data after an update completed - time.Sleep(1 * time.Minute) + time.Sleep(2 * time.Minute) newRes, err := r.client.Instances.GetWithResponse(ctx, state.ProjectID.ValueString(), state.ID.ValueString()) if agg := validate.Response(newRes, err); agg != nil { diff --git a/stackit/internal/resources/kubernetes/cluster/helpers.go b/stackit/internal/resources/kubernetes/cluster/helpers.go index 437d6b24..dd9e61d1 100644 --- a/stackit/internal/resources/kubernetes/cluster/helpers.go +++ b/stackit/internal/resources/kubernetes/cluster/helpers.go @@ -14,6 +14,7 @@ import ( "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" ) const ( @@ -257,20 +258,41 @@ func (c *Cluster) extensions(ctx context.Context) (*cluster.Extension, diag.Diag return nil, nil } ex := &cluster.Extension{} + + // argus if c.Extensions.Argus != nil { ex.Argus = &cluster.Argus{ Enabled: c.Extensions.Argus.Enabled.ValueBool(), ArgusInstanceID: c.Extensions.Argus.ArgusInstanceID.ValueString(), } } - if c.Extensions.ACL != nil { - var cidrs []string - diags := c.Extensions.ACL.AllowedCIDRs.ElementsAs(ctx, &cidrs, true) + + // acl + if !c.Extensions.ACL.IsNull() && !c.Extensions.ACL.IsUnknown() { + var acl ACLExtension + obj, diags := c.Extensions.ACL.ToObjectValue(ctx) + if diags.HasError() { + return nil, diags + } + diags = obj.As(ctx, &acl, basetypes.ObjectAsOptions{}) if diags.HasError() { return nil, diags } + + var cidrs []string + if !acl.AllowedCIDRs.IsNull() && !acl.AllowedCIDRs.IsUnknown() { + diags := acl.AllowedCIDRs.ElementsAs(ctx, &cidrs, true) + if diags.HasError() { + return nil, diags + } + } + + enabled := false + if !acl.Enabled.IsNull() && !acl.Enabled.IsUnknown() { + enabled = acl.Enabled.ValueBool() + } ex.Acl = &cluster.ACL{ - Enabled: c.Extensions.ACL.Enabled.ValueBool(), + Enabled: enabled, AllowedCidrs: cidrs, } } @@ -427,10 +449,13 @@ func (c *Cluster) transformExtensions(cl cluster.Cluster) { Enabled: types.BoolValue(false), ArgusInstanceID: types.StringNull(), }, - ACL: &ACL{ - Enabled: types.BoolValue(false), - AllowedCIDRs: types.ListNull(types.StringType), - }, + ACL: types.ObjectValueMust(map[string]attr.Type{ + "enabled": types.BoolType, + "allowed_cidrs": types.ListType{ElemType: types.StringType}, + }, map[string]attr.Value{ + "enabled": types.BoolValue(false), + "allowed_cidrs": types.ListNull(types.StringType), + }), } if cl.Extensions.Argus != nil { c.Extensions.Argus = &ArgusExtension{ @@ -444,9 +469,16 @@ func (c *Cluster) transformExtensions(cl cluster.Cluster) { for _, v := range cl.Extensions.Acl.AllowedCidrs { cidr = append(cidr, types.StringValue(v)) } - c.Extensions.ACL = &ACL{ - Enabled: types.BoolValue(cl.Extensions.Acl.Enabled), - AllowedCIDRs: types.ListValueMust(types.StringType, cidr), - } + cidrList, diags := types.ListValue(types.StringType, cidr) + if diags.HasError() { + cidrList = types.ListNull(types.StringType) + } + c.Extensions.ACL = types.ObjectValueMust(map[string]attr.Type{ + "enabled": types.BoolType, + "allowed_cidrs": types.ListType{ElemType: types.StringType}, + }, map[string]attr.Value{ + "enabled": types.BoolValue(cl.Extensions.Acl.Enabled), + "allowed_cidrs": cidrList, + }) } } diff --git a/stackit/internal/resources/kubernetes/cluster/schema.go b/stackit/internal/resources/kubernetes/cluster/schema.go index 173a9923..e02bbd61 100644 --- a/stackit/internal/resources/kubernetes/cluster/schema.go +++ b/stackit/internal/resources/kubernetes/cluster/schema.go @@ -71,10 +71,10 @@ type Hibernation struct { type Extensions struct { Argus *ArgusExtension `tfsdk:"argus"` - ACL *ACL `tfsdk:"acl"` + ACL types.Object `tfsdk:"acl"` } -type ACL struct { +type ACLExtension struct { Enabled types.Bool `tfsdk:"enabled"` AllowedCIDRs types.List `tfsdk:"allowed_cidrs"` }