Skip to content

Commit

Permalink
Merge pull request #134 from mathewab/fix-132
Browse files Browse the repository at this point in the history
fixes #132: change type of anycast_conifg_refs to be unordered list
  • Loading branch information
unasra authored Jul 26, 2024
2 parents 13fc262 + ebe0156 commit 2312625
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 102 deletions.
28 changes: 23 additions & 5 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func FlattenFrameworkListString(ctx context.Context, l []string, diags *diag.Dia
return tfList
}

func FlattenFrameworkCustomListString(ctx context.Context, l []string, diags *diag.Diagnostics) internaltypes.UnorderedListValue[types.String] {
if len(l) == 0 {
return internaltypes.NewUnorderedListValueNull[types.String](ctx)
func FlattenFrameworkUnorderedList[T any](ctx context.Context, elemType attr.Type, data []T, diags *diag.Diagnostics) internaltypes.UnorderedListValue {
if len(data) == 0 {
return internaltypes.NewUnorderedListValueNull(elemType)
}
tfList, d := internaltypes.NewUnorderedListValueFrom[types.String](ctx, l)
tfList, d := internaltypes.NewUnorderedListValueFrom(ctx, elemType, data)
diags.Append(d...)
return tfList
}
Expand Down Expand Up @@ -180,6 +180,21 @@ func FlattenFrameworkListNestedBlock[T any, U any](ctx context.Context, data []T
return tfList
}

func FlattenFrameworkUnorderedListNestedBlock[T any, U any](ctx context.Context, data []T, attrTypes map[string]attr.Type, diags *diag.Diagnostics, f FrameworkElementFlExFunc[*T, U]) internaltypes.UnorderedListValue {
if len(data) == 0 {
return internaltypes.NewUnorderedListValueNull(types.ObjectType{AttrTypes: attrTypes})
}

tfData := ApplyToAll(data, func(t T) U {
return f(ctx, &t, diags)
})

tfList, d := internaltypes.NewUnorderedListValueFrom(ctx, types.ObjectType{AttrTypes: attrTypes}, tfData)

diags.Append(d...)
return tfList
}

func FlattenFrameworkListsNestedBlock[T any, U any, V any](ctx context.Context, data []T, model []U, attrTypes map[string]attr.Type, diags *diag.Diagnostics, f FrameworkElementFlExFuncExt[*T, *U, V]) types.List {
if len(data) == 0 || len(model) == 0 {
return types.ListNull(types.ObjectType{AttrTypes: attrTypes})
Expand Down Expand Up @@ -234,7 +249,10 @@ func ExpandFrameworkMapString(ctx context.Context, tfMap types.Map, diags *diag.
return elementsNew
}

func ExpandFrameworkListNestedBlock[T any, U any](ctx context.Context, tfList types.List, diags *diag.Diagnostics, f FrameworkElementFlExFunc[T, *U]) []U {
func ExpandFrameworkListNestedBlock[T any, U any](ctx context.Context, tfList interface {
basetypes.ListValuable
ElementsAs(ctx context.Context, target interface{}, allowUnhandled bool) diag.Diagnostics
}, diags *diag.Diagnostics, f FrameworkElementFlExFunc[T, *U]) []U {
if tfList.IsNull() || tfList.IsUnknown() {
return nil
}
Expand Down
67 changes: 65 additions & 2 deletions internal/service/anycast/api_anycast_host_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"net/http"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

"github.com/infobloxopen/bloxone-go-client/anycast"
"github.com/infobloxopen/terraform-provider-bloxone/internal/acctest"
)
Expand Down Expand Up @@ -66,6 +67,31 @@ func TestAccAnycastHostResource_disappears(t *testing.T) {
})
}

func TestAccAnycastHostResource_AnycastConfigRefs(t *testing.T) {
var resourceName = "bloxone_anycast_host.test"
var v anycast.OnpremHost
var anycastHostname = acctest.RandomNameWithPrefix("anycast_host")

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read
{
Config: testAccAnycastHostAnycastConfigRefs(anycastHostname, map[string]string{
acctest.RandomNameWithPrefix("anycast"): acctest.RandomIP(),
acctest.RandomNameWithPrefix("anycast"): acctest.RandomIP(),
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckAnycastHostExists(context.Background(), resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "anycast_config_refs.#", "2"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func TestAccAnycastHostResource_enableRouting(t *testing.T) {
var resourceName = "bloxone_anycast_host.test"
var v anycast.OnpremHost
Expand Down Expand Up @@ -276,6 +302,43 @@ resource "bloxone_anycast_host" "test" {
return strings.Join([]string{testAccBaseWithAnycastConfig(hostName, anycastConfigName, anycastIP), config}, "")
}

func testAccAnycastHostAnycastConfigRefs(hostName string, anycastIPs map[string]string) string {
var configs []string

configs = append(configs, fmt.Sprintf(`
resource "bloxone_infra_host" "test" {
display_name = %q
}
`, hostName))

for name, ip := range anycastIPs {
configs = append(configs, fmt.Sprintf(`
resource "bloxone_anycast_config" %[1]q {
anycast_ip_address = %[2]q
name = %[1]q
service = "DNS"
}
`, name, ip))
}

configRefsStr := ""
for name := range anycastIPs {
configRefsStr += fmt.Sprintf(`{
anycast_config_name = bloxone_anycast_config.%[1]s.name
},`, name)
}

configs = append(configs, fmt.Sprintf(`
resource "bloxone_anycast_host" "test" {
id = bloxone_infra_host.test.legacy_id
anycast_config_refs = [
%[1]s
]
}
`, configRefsStr))
return strings.Join(configs, "")
}

func testAccAnycastHostEnableRoutingBGP(routingProtocols, hostName, anycastConfigName, anycastIP string) string {
config := fmt.Sprintf(`
resource "bloxone_anycast_host" "test" {
Expand Down
40 changes: 15 additions & 25 deletions internal/service/anycast/model_proto_onprem_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,36 @@ import (
"context"

"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
schema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"

"github.com/infobloxopen/bloxone-go-client/anycast"
internaltypes "github.com/infobloxopen/terraform-provider-bloxone/internal/types"

"github.com/infobloxopen/terraform-provider-bloxone/internal/flex"
)

type ProtoOnpremHostModel struct {
AnycastConfigRefs types.List `tfsdk:"anycast_config_refs"`
ConfigBgp types.Object `tfsdk:"config_bgp"`
ConfigOspf types.Object `tfsdk:"config_ospf"`
ConfigOspfv3 types.Object `tfsdk:"config_ospfv3"`
CreatedAt timetypes.RFC3339 `tfsdk:"created_at"`
Id types.Int64 `tfsdk:"id"`
IpAddress types.String `tfsdk:"ip_address"`
Ipv6Address types.String `tfsdk:"ipv6_address"`
Name types.String `tfsdk:"name"`
UpdatedAt timetypes.RFC3339 `tfsdk:"updated_at"`
}

var ProtoOnpremHostAttrTypes = map[string]attr.Type{
"anycast_config_refs": types.ListType{ElemType: types.ObjectType{AttrTypes: ProtoAnycastConfigRefAttrTypes}},
"config_bgp": types.ObjectType{AttrTypes: ProtoBgpConfigAttrTypes},
"config_ospf": types.ObjectType{AttrTypes: ProtoOspfConfigAttrTypes},
"config_ospfv3": types.ObjectType{AttrTypes: ProtoOspfv3ConfigAttrTypes},
"created_at": timetypes.RFC3339Type{},
"id": types.Int64Type,
"ip_address": types.StringType,
"ipv6_address": types.StringType,
"name": types.StringType,
"updated_at": timetypes.RFC3339Type{},
AnycastConfigRefs internaltypes.UnorderedListValue `tfsdk:"anycast_config_refs"`
ConfigBgp types.Object `tfsdk:"config_bgp"`
ConfigOspf types.Object `tfsdk:"config_ospf"`
ConfigOspfv3 types.Object `tfsdk:"config_ospfv3"`
CreatedAt timetypes.RFC3339 `tfsdk:"created_at"`
Id types.Int64 `tfsdk:"id"`
IpAddress types.String `tfsdk:"ip_address"`
Ipv6Address types.String `tfsdk:"ipv6_address"`
Name types.String `tfsdk:"name"`
UpdatedAt timetypes.RFC3339 `tfsdk:"updated_at"`
}

var ProtoOnpremHostResourceSchemaAttributes = map[string]schema.Attribute{
"anycast_config_refs": schema.ListNestedAttribute{
CustomType: internaltypes.UnorderedList{ListType: basetypes.ListType{ElemType: basetypes.ObjectType{AttrTypes: ProtoAnycastConfigRefAttrTypes}}},
NestedObject: schema.NestedAttributeObject{
Attributes: ProtoAnycastConfigRefResourceSchemaAttributes,
},
Expand Down Expand Up @@ -123,7 +113,7 @@ func (m *ProtoOnpremHostModel) Flatten(ctx context.Context, from *anycast.Onprem
if m == nil {
*m = ProtoOnpremHostModel{}
}
m.AnycastConfigRefs = flex.FlattenFrameworkListNestedBlock(ctx, from.AnycastConfigRefs, ProtoAnycastConfigRefAttrTypes, diags, FlattenProtoAnycastConfigRef)
m.AnycastConfigRefs = flex.FlattenFrameworkUnorderedListNestedBlock(ctx, from.AnycastConfigRefs, ProtoAnycastConfigRefAttrTypes, diags, FlattenProtoAnycastConfigRef)
m.ConfigBgp = FlattenProtoBgpConfig(ctx, from.ConfigBgp, diags)
m.ConfigOspf = FlattenProtoOspfConfig(ctx, from.ConfigOspf, diags)
m.ConfigOspfv3 = FlattenProtoOspfv3Config(ctx, from.ConfigOspfv3, diags)
Expand Down
21 changes: 6 additions & 15 deletions internal/service/dfp/model_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

type ResolverModel struct {
Address types.String `tfsdk:"address"`
IsFallback types.Bool `tfsdk:"is_fallback"`
IsLocal types.Bool `tfsdk:"is_local"`
Protocols internaltypes.UnorderedListValue[types.String] `tfsdk:"protocols"`
Address types.String `tfsdk:"address"`
IsFallback types.Bool `tfsdk:"is_fallback"`
IsLocal types.Bool `tfsdk:"is_local"`
Protocols internaltypes.UnorderedListValue `tfsdk:"protocols"`
}

var ResolverAttrTypes = map[string]attr.Type{
Expand Down Expand Up @@ -75,7 +75,7 @@ func (m *ResolverModel) Expand(ctx context.Context, diags *diag.Diagnostics) *df
return to
}

func ExpandDNSProtocol(ctx context.Context, tfList internaltypes.UnorderedListValue[types.String], diags *diag.Diagnostics) []dfp.DNSProtocol {
func ExpandDNSProtocol(ctx context.Context, tfList internaltypes.UnorderedListValue, diags *diag.Diagnostics) []dfp.DNSProtocol {
if tfList.IsNull() || tfList.IsUnknown() {
return nil
}
Expand Down Expand Up @@ -105,14 +105,5 @@ func (m *ResolverModel) Flatten(ctx context.Context, from *dfp.Resolver, diags *
m.Address = flex.FlattenStringPointer(from.Address)
m.IsFallback = types.BoolPointerValue(from.IsFallback)
m.IsLocal = types.BoolPointerValue(from.IsLocal)
m.Protocols = FlattenDNSProtocol(ctx, from.Protocols, diags)
}

func FlattenDNSProtocol(ctx context.Context, l []dfp.DNSProtocol, diags *diag.Diagnostics) internaltypes.UnorderedListValue[types.String] {
if len(l) == 0 {
return internaltypes.NewUnorderedListValueNull[types.String](ctx)
}
tfList, d := internaltypes.NewUnorderedListValueFrom[types.String](ctx, l)
diags.Append(d...)
return tfList
m.Protocols = flex.FlattenFrameworkUnorderedList(ctx, types.StringType, from.Protocols, diags)
}
20 changes: 10 additions & 10 deletions internal/service/fw/model_atcfw_internal_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import (
)

type AtcfwInternalDomainsModel struct {
CreatedTime timetypes.RFC3339 `tfsdk:"created_time"`
Description types.String `tfsdk:"description"`
Id types.Int64 `tfsdk:"id"`
InternalDomains internaltypes.UnorderedListValue[types.String] `tfsdk:"internal_domains"`
IsDefault types.Bool `tfsdk:"is_default"`
Name types.String `tfsdk:"name"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
UpdatedTime timetypes.RFC3339 `tfsdk:"updated_time"`
CreatedTime timetypes.RFC3339 `tfsdk:"created_time"`
Description types.String `tfsdk:"description"`
Id types.Int64 `tfsdk:"id"`
InternalDomains internaltypes.UnorderedListValue `tfsdk:"internal_domains"`
IsDefault types.Bool `tfsdk:"is_default"`
Name types.String `tfsdk:"name"`
Tags types.Map `tfsdk:"tags"`
TagsAll types.Map `tfsdk:"tags_all"`
UpdatedTime timetypes.RFC3339 `tfsdk:"updated_time"`
}

var AtcfwInternalDomainsAttrTypes = map[string]attr.Type{
Expand Down Expand Up @@ -135,7 +135,7 @@ func (m *AtcfwInternalDomainsModel) Flatten(ctx context.Context, from *fw.Intern
m.CreatedTime = timetypes.NewRFC3339TimePointerValue(from.CreatedTime)
m.Description = flex.FlattenStringPointer(from.Description)
m.Id = flex.FlattenInt32Pointer(from.Id)
m.InternalDomains = flex.FlattenFrameworkCustomListString(ctx, from.InternalDomains, diags)
m.InternalDomains = flex.FlattenFrameworkUnorderedList(ctx, types.StringType, from.InternalDomains, diags)
m.IsDefault = types.BoolPointerValue(from.IsDefault)
m.Name = flex.FlattenStringPointer(from.Name)
m.TagsAll = flex.FlattenFrameworkMapString(ctx, from.Tags, diags)
Expand Down
Loading

0 comments on commit 2312625

Please sign in to comment.