Skip to content

Commit

Permalink
fix: conversion of resource and override selectors includes empty arr…
Browse files Browse the repository at this point in the history
…ays rather than nil values (#151)
  • Loading branch information
matt-FFFFFF authored Oct 28, 2024
1 parent 8203c5d commit 48b2dfd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
82 changes: 54 additions & 28 deletions internal/provider/architecture_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,35 @@ func convertPolicyAssignmentOverridesToSdkType(ctx context.Context, input []gen.
"unable to convert override selectors attr.Value to concrete type",
)
}
in, err := frameworktype.SliceOfPrimitiveToGo[string](ctx, osv.In.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentOverridesToSdkType: error",
fmt.Sprintf("unable to convert OverrideSelctorsValue.In elements to Go slice: %s", err.Error()),
)
return nil

// Convert In to a go slice, start off from an uninitialized slice so that the value is nil if the input is empty.
var in []*string
if len(osv.In.Elements()) != 0 {
var err error
in, err = frameworktype.SliceOfPrimitiveToGo[string](ctx, osv.In.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentOverridesToSdkType: error",
fmt.Sprintf("unable to convert OverrideSelctorsValue.In elements to Go slice: %s", err.Error()),
)
return nil
}
}
notIn, err := frameworktype.SliceOfPrimitiveToGo[string](ctx, osv.NotIn.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentOverridesToSdkType: error",
fmt.Sprintf("unable to convert OverrideSelctorsValue.NotIn elements to Go slice: %s", err.Error()),
)
return nil

// Convert NotIn to a go slice, start off from an uninitialized slice so that the value is nil if the input is empty.
var notIn []*string
if len(osv.NotIn.Elements()) != 0 {
var err error
notIn, err = frameworktype.SliceOfPrimitiveToGo[string](ctx, osv.NotIn.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentOverridesToSdkType: error",
fmt.Sprintf("unable to convert OverrideSelctorsValue.NotIn elements to Go slice: %s", err.Error()),
)
return nil
}
}

selectors[j] = &armpolicy.Selector{
Kind: to.Ptr(armpolicy.SelectorKind(osv.Kind.ValueString())),
In: in,
Expand Down Expand Up @@ -376,22 +389,35 @@ func convertPolicyAssignmentResourceSelectorsToSdkType(ctx context.Context, inpu
"unable to convert resource selector selectors attr.Value to concrete type",
)
}
in, err := frameworktype.SliceOfPrimitiveToGo[string](ctx, rssv.In.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentResourceSelectorsToSdkType: error",
fmt.Sprintf("unable to convert ResourceSelectorSelectorsValue.In elements to Go slice: %s", err.Error()),
)
return nil

// Convert In to a go slice, start off from an uninitialized slice so that the value is nil if the input is empty.
var in []*string
if len(rssv.In.Elements()) != 0 {
var err error
in, err = frameworktype.SliceOfPrimitiveToGo[string](ctx, rssv.In.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentResourceSelectorsToSdkType: error",
fmt.Sprintf("unable to convert ResourceSelectorSelectorsValue.In elements to Go slice: %s", err.Error()),
)
return nil
}
}
notIn, err := frameworktype.SliceOfPrimitiveToGo[string](ctx, rssv.NotIn.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentResourceSelectorsToSdkType: error",
fmt.Sprintf("unable to convert ResourceSelectorSelectorsValue.NotIn elements to Go slice: %s", err.Error()),
)
return nil

// Convert NotIn to a go slice, start off from an uninitialized slice so that the value is nil if the input is empty.
var notIn []*string
if len(rssv.NotIn.Elements()) != 0 {
var err error
notIn, err = frameworktype.SliceOfPrimitiveToGo[string](ctx, rssv.NotIn.Elements())
if err != nil {
resp.Diagnostics.AddError(
"convertPolicyAssignmentResourceSelectorsToSdkType: error",
fmt.Sprintf("unable to convert ResourceSelectorSelectorsValue.NotIn elements to Go slice: %s", err.Error()),
)
return nil
}
}

selectors[j] = &armpolicy.Selector{
Kind: to.Ptr(armpolicy.SelectorKind(rssv.Kind.ValueString())),
In: in,
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/architecture_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestAccAlzArchitectureDataSourceWithDefaultAndModify(t *testing.T) {
resource.TestCheckOutput("policy_assignment_resource_selector_name", "test-resource-selector"),
resource.TestCheckOutput("policy_assignment_resource_selector_kind", "resourceLocation"),
resource.TestCheckOutput("policy_assignment_resource_selector_in", "northeurope"),
resource.TestCheckOutput("policy_assignment_resource_selector_notin_should_be_null", "true"),
),
},
},
Expand Down Expand Up @@ -184,7 +185,7 @@ data "alz_architecture" "test" {
parameters = {
metricsEnabled = jsonencode({ value = false })
}
resource_selectors = [
resource_selectors = [
{
name = "test-resource-selector"
resource_selector_selectors = [
Expand Down Expand Up @@ -268,6 +269,10 @@ output "policy_assignment_resource_selector_kind" {
output "policy_assignment_resource_selector_in" {
value = local.test_policy_assignment_decoded.properties.resourceSelectors[0].selectors[0].in[0]
}
output "policy_assignment_resource_selector_notin_should_be_null" {
value = lookup(local.test_policy_assignment_decoded.properties.resourceSelectors[0].selectors[0], "notIn", null) == null
}
`
}

Expand Down

0 comments on commit 48b2dfd

Please sign in to comment.