Skip to content

Commit

Permalink
fix kubernetes ACL type conversion error (#87)
Browse files Browse the repository at this point in the history
* fix acl type conversion error

Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
  • Loading branch information
do87 and Dean Oren authored Jan 18, 2023
1 parent 122ec0d commit 6b4fa83
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
25 changes: 18 additions & 7 deletions stackit/internal/data-sources/kubernetes/cluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,36 @@ 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{
Enabled: types.BoolValue(cl.Extensions.Argus.Enabled),
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,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
56 changes: 44 additions & 12 deletions stackit/internal/resources/kubernetes/cluster/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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{
Expand All @@ -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,
})
}
}
4 changes: 2 additions & 2 deletions stackit/internal/resources/kubernetes/cluster/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down

0 comments on commit 6b4fa83

Please sign in to comment.