diff --git a/apstra/blueprint/datacenter_generic_system.go b/apstra/blueprint/datacenter_generic_system.go index 456c1cb6..51a9b652 100644 --- a/apstra/blueprint/datacenter_generic_system.go +++ b/apstra/blueprint/datacenter_generic_system.go @@ -189,7 +189,7 @@ func (o *DatacenterGenericSystem) CreateRequest(ctx context.Context, diags *diag StartIndex: 1, Schema: apstra.PortIndexingSchemaAbsolute, }, - PortGroups: []apstra.LogicalDevicePortGroup{{Count: 1, Speed: "100M", Roles: 0}}, + PortGroups: []apstra.LogicalDevicePortGroup{{Count: 1, Speed: "100M", Roles: apstra.LogicalDevicePortRoles{}}}, }}, }, } diff --git a/apstra/blueprint/vn_binding.go b/apstra/blueprint/vn_binding.go index 7d74bccb..b17fb8a2 100644 --- a/apstra/blueprint/vn_binding.go +++ b/apstra/blueprint/vn_binding.go @@ -2,8 +2,8 @@ package blueprint import ( "context" + "github.com/Juniper/apstra-go-sdk/apstra" - apstradefault "github.com/Juniper/terraform-provider-apstra/apstra/defaults" "github.com/Juniper/terraform-provider-apstra/apstra/design" "github.com/Juniper/terraform-provider-apstra/apstra/utils" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" @@ -11,6 +11,7 @@ import ( dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/diag" resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -25,7 +26,6 @@ func (o VnBinding) attrTypes() map[string]attr.Type { "vlan_id": types.Int64Type, "access_ids": types.SetType{ElemType: types.StringType}, } - } func (o VnBinding) DataSourceAttributes() map[string]dataSourceSchema.Attribute { @@ -74,7 +74,7 @@ func (o VnBinding) ResourceAttributes() map[string]resourceSchema.Attribute { Optional: true, Computed: true, ElementType: types.StringType, - Default: apstradefault.StaticDefaultAny(types.SetValueMust(types.StringType, []attr.Value{})), + Default: setdefault.StaticValue(types.SetValueMust(types.StringType, []attr.Value{})), }, } } diff --git a/apstra/defaults/default_any.go b/apstra/defaults/default_any.go deleted file mode 100644 index b4742f9d..00000000 --- a/apstra/defaults/default_any.go +++ /dev/null @@ -1,77 +0,0 @@ -package apstradefault - -import ( - "context" - "fmt" - "github.com/hashicorp/terraform-plugin-framework/attr" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults" - "github.com/hashicorp/terraform-plugin-framework/types" -) - -type DefaultAny interface { - defaults.Bool - defaults.Int64 - defaults.Float64 - defaults.List - defaults.Map - defaults.Number - defaults.Object - defaults.Set - defaults.String -} - -var _ DefaultAny = StaticAnyTypeDefaulter{} - -type StaticAnyTypeDefaulter struct { - value attr.Value -} - -func (o StaticAnyTypeDefaulter) Description(_ context.Context) string { - return fmt.Sprintf("set the default value to a %T with value %q", o.value, o.value) -} - -func (o StaticAnyTypeDefaulter) MarkdownDescription(ctx context.Context) string { - return o.Description(ctx) -} - -func (o StaticAnyTypeDefaulter) DefaultBool(_ context.Context, _ defaults.BoolRequest, resp *defaults.BoolResponse) { - resp.PlanValue = o.value.(types.Bool) -} - -func (o StaticAnyTypeDefaulter) DefaultInt64(_ context.Context, _ defaults.Int64Request, resp *defaults.Int64Response) { - resp.PlanValue = o.value.(types.Int64) -} - -func (o StaticAnyTypeDefaulter) DefaultFloat64(_ context.Context, _ defaults.Float64Request, resp *defaults.Float64Response) { - resp.PlanValue = o.value.(types.Float64) -} - -func (o StaticAnyTypeDefaulter) DefaultList(_ context.Context, _ defaults.ListRequest, resp *defaults.ListResponse) { - resp.PlanValue = o.value.(types.List) -} - -func (o StaticAnyTypeDefaulter) DefaultMap(_ context.Context, _ defaults.MapRequest, resp *defaults.MapResponse) { - resp.PlanValue = o.value.(types.Map) -} - -func (o StaticAnyTypeDefaulter) DefaultNumber(_ context.Context, _ defaults.NumberRequest, resp *defaults.NumberResponse) { - resp.PlanValue = o.value.(types.Number) -} - -func (o StaticAnyTypeDefaulter) DefaultObject(_ context.Context, _ defaults.ObjectRequest, resp *defaults.ObjectResponse) { - resp.PlanValue = o.value.(types.Object) -} - -func (o StaticAnyTypeDefaulter) DefaultSet(_ context.Context, _ defaults.SetRequest, resp *defaults.SetResponse) { - resp.PlanValue = o.value.(types.Set) -} - -func (o StaticAnyTypeDefaulter) DefaultString(_ context.Context, _ defaults.StringRequest, resp *defaults.StringResponse) { - resp.PlanValue = o.value.(types.String) -} - -func StaticDefaultAny(v attr.Value) StaticAnyTypeDefaulter { - return StaticAnyTypeDefaulter{ - value: v, - } -} diff --git a/apstra/defaults/port_roles.go b/apstra/defaults/port_roles.go deleted file mode 100644 index 3db3808c..00000000 --- a/apstra/defaults/port_roles.go +++ /dev/null @@ -1,30 +0,0 @@ -package apstradefault - -import ( - "context" - "github.com/Juniper/apstra-go-sdk/apstra" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults" - "github.com/hashicorp/terraform-plugin-framework/types" -) - -var _ defaults.Set = PortRolesDefault{} - -type PortRolesDefault struct{} - -func (o PortRolesDefault) Description(_ context.Context) string { - return "Enables all port roles." -} - -func (o PortRolesDefault) MarkdownDescription(ctx context.Context) string { - return o.Description(ctx) -} - -func (o PortRolesDefault) DefaultSet(ctx context.Context, _ defaults.SetRequest, resp *defaults.SetResponse) { - var allRoleFlagsSet apstra.LogicalDevicePortRoleFlags - allRoleFlagsSet.SetAll() - - var d diag.Diagnostics - resp.PlanValue, d = types.SetValueFrom(ctx, types.StringType, allRoleFlagsSet.Strings()) - resp.Diagnostics.Append(d...) -} diff --git a/apstra/design/logical_device_panel.go b/apstra/design/logical_device_panel.go index 362991b7..4e2a8f95 100644 --- a/apstra/design/logical_device_panel.go +++ b/apstra/design/logical_device_panel.go @@ -3,6 +3,8 @@ package design import ( "context" "fmt" + "strings" + "github.com/Juniper/apstra-go-sdk/apstra" "github.com/Juniper/terraform-provider-apstra/apstra/utils" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" @@ -17,7 +19,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "strings" ) type LogicalDevicePanel struct { @@ -134,7 +135,7 @@ func (o *LogicalDevicePanel) Request(ctx context.Context, diags *diag.Diagnostic if diags.HasError() { return nil } - var reqRoles apstra.LogicalDevicePortRoleFlags + var reqRoles apstra.LogicalDevicePortRoles err := reqRoles.FromStrings(roleStrings) if err != nil { diags.AddError(fmt.Sprintf("error parsing port roles: '%s'", strings.Join(roleStrings, "','")), err.Error()) diff --git a/apstra/design/logical_device_panel_port_group.go b/apstra/design/logical_device_panel_port_group.go index c54fb9ed..7bdeca74 100644 --- a/apstra/design/logical_device_panel_port_group.go +++ b/apstra/design/logical_device_panel_port_group.go @@ -3,9 +3,10 @@ package design import ( "context" "fmt" + "strings" + "github.com/Juniper/apstra-go-sdk/apstra" apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/apstra_validator" - apstradefault "github.com/Juniper/terraform-provider-apstra/apstra/defaults" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -15,11 +16,11 @@ import ( resourceSchema "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/setdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "strings" ) type LogicalDevicePanelPortGroup struct { @@ -47,8 +48,15 @@ func (o LogicalDevicePanelPortGroup) DataSourceAttributes() map[string]dataSourc } func (o LogicalDevicePanelPortGroup) ResourceAttributes() map[string]resourceSchema.Attribute { - var allRoleFlagsSet apstra.LogicalDevicePortRoleFlags - allRoleFlagsSet.SetAll() + // collect all port roles for use in inline documentation and defaulter + var allPortRoles apstra.LogicalDevicePortRoles + allPortRoles.IncludeAllUses() + + // prepare []attr.Value for defaulter + defaultRoles := make([]attr.Value, len(allPortRoles.Strings())) + for i, role := range allPortRoles.Strings() { + defaultRoles[i] = types.StringValue(role) + } return map[string]resourceSchema.Attribute{ "port_count": resourceSchema.Int64Attribute{ @@ -68,13 +76,13 @@ func (o LogicalDevicePanelPortGroup) ResourceAttributes() map[string]resourceSch Computed: true, Optional: true, MarkdownDescription: fmt.Sprintf( - "One or more of: '%s', by default all values except 'unused' are selected", - strings.Join(allRoleFlagsSet.Strings(), "', '")), + "One or more of: '%s', by default all values are selected.", + strings.Join(allPortRoles.Strings(), "', '")), Validators: []validator.Set{ setvalidator.SizeAtLeast(1), - setvalidator.ValueStringsAre(stringvalidator.OneOf(allRoleFlagsSet.Strings()...)), + setvalidator.ValueStringsAre(stringvalidator.OneOf(allPortRoles.Strings()...)), }, - Default: apstradefault.PortRolesDefault{}, + Default: setdefault.StaticValue(types.SetValueMust(types.StringType, defaultRoles)), }, } } diff --git a/apstra/freeform/system.go b/apstra/freeform/system.go index 57203cb5..199d1155 100644 --- a/apstra/freeform/system.go +++ b/apstra/freeform/system.go @@ -178,7 +178,7 @@ func (o *System) Request(ctx context.Context, diags *diag.Diagnostics) *apstra.F Label: o.Name.ValueString(), Hostname: o.Hostname.ValueString(), Tags: tags, - DeviceProfileId: apstra.ObjectId(o.DeviceProfileId.ValueString()), + DeviceProfileId: (*apstra.ObjectId)(o.DeviceProfileId.ValueStringPointer()), } } @@ -186,8 +186,7 @@ func (o *System) LoadApiData(ctx context.Context, in *apstra.FreeformSystemData, o.Name = types.StringValue(in.Label) o.Hostname = types.StringValue(in.Hostname) o.Type = types.StringValue(utils.StringersToFriendlyString(in.Type)) - o.DeviceProfileId = types.StringValue(string(in.DeviceProfileId)) - o.DeviceProfileId = utils.StringValueOrNull(ctx, in.DeviceProfileId.String(), diags) + o.DeviceProfileId = types.StringPointerValue((*string)(in.DeviceProfileId)) o.SystemId = types.StringPointerValue((*string)(in.SystemId)) o.Tags = utils.SetValueOrNull(ctx, types.StringType, in.Tags, diags) // safe to ignore diagnostic here } diff --git a/apstra/resource_interface_map.go b/apstra/resource_interface_map.go index 135fd172..bb554c3d 100644 --- a/apstra/resource_interface_map.go +++ b/apstra/resource_interface_map.go @@ -4,7 +4,13 @@ import ( "context" "encoding/json" "fmt" + "regexp" + "sort" + "strconv" + "strings" + "github.com/Juniper/apstra-go-sdk/apstra" + "github.com/Juniper/apstra-go-sdk/apstra/enum" "github.com/Juniper/terraform-provider-apstra/apstra/utils" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" @@ -19,10 +25,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" - "regexp" - "sort" - "strconv" - "strings" ) const ( @@ -31,9 +33,11 @@ const ( ldInterfaceSynax = "" + ldInterfaceSep + "" ) -var _ resource.ResourceWithConfigure = &resourceInterfaceMap{} -var _ resource.ResourceWithValidateConfig = &resourceInterfaceMap{} -var _ resourceWithSetClient = &resourceInterfaceMap{} +var ( + _ resource.ResourceWithConfigure = &resourceInterfaceMap{} + _ resource.ResourceWithValidateConfig = &resourceInterfaceMap{} + _ resourceWithSetClient = &resourceInterfaceMap{} +) type resourceInterfaceMap struct { client *apstra.Client @@ -519,7 +523,7 @@ func (o *rInterfaceMap) iMapInterfaces(ctx context.Context, ld *apstra.LogicalDe transformId: transformId, interfaces: unused, } - //sliceWithoutInt(transformInterfacesUnused, transformInterface.InterfaceId) + // sliceWithoutInt(transformInterfacesUnused, transformInterface.InterfaceId) } else { // New port+transform. // Add it to the tracking map with this interface ID removed from the list @@ -572,7 +576,7 @@ func (o *rInterfaceMap) iMapInterfaces(ctx context.Context, ld *apstra.LogicalDe } result = append(result, apstra.InterfaceMapInterface{ Name: intf.Name, - Roles: apstra.LogicalDevicePortRoleUnused, + Roles: apstra.LogicalDevicePortRoles{enum.PortRoleUnused}, Mapping: apstra.InterfaceMapMapping{ DPPortId: portId, DPTransformId: unused.transformId, @@ -756,7 +760,7 @@ func ldPanelAndPortFromString(in string, diags *diag.Diagnostics) (int, int) { type ldPortInfo struct { Speed apstra.LogicalDevicePortSpeed - Roles apstra.LogicalDevicePortRoleFlags + Roles apstra.LogicalDevicePortRoles } // getLogicalDevicePortInfo extracts a map[string]ldPortInfo keyed by logical @@ -836,7 +840,7 @@ func iMapUnallocaedInterfaces(allocatedPorts []apstra.InterfaceMapInterface, dp result[i] = apstra.InterfaceMapInterface{ Name: transformation.Interfaces[0].Name, - Roles: apstra.LogicalDevicePortRoleUnused, + Roles: apstra.LogicalDevicePortRoles{enum.PortRoleUnused}, Mapping: apstra.InterfaceMapMapping{ DPPortId: dpPort.PortId, DPTransformId: transformation.TransformationId, diff --git a/apstra/test_utils/blueprint.go b/apstra/test_utils/blueprint.go index e859389d..ad05db0d 100644 --- a/apstra/test_utils/blueprint.go +++ b/apstra/test_utils/blueprint.go @@ -299,7 +299,7 @@ func FfBlueprintB(t testing.TB, ctx context.Context, intSystemCount, extSystemCo intSystemIds[i], err = c.CreateSystem(ctx, &apstra.FreeformSystemData{ Type: apstra.SystemTypeInternal, Label: acctest.RandString(6), - DeviceProfileId: dpId, + DeviceProfileId: &dpId, }) require.NoError(t, err) } diff --git a/docs/resources/logical_device.md b/docs/resources/logical_device.md index 41aa2d0c..3dcff8a4 100644 --- a/docs/resources/logical_device.md +++ b/docs/resources/logical_device.md @@ -69,7 +69,7 @@ Required: Optional: -- `port_roles` (Set of String) One or more of: 'spine', 'superspine', 'leaf', 'access', 'l3_server', 'peer', 'unused', 'generic', by default all values except 'unused' are selected +- `port_roles` (Set of String) One or more of: 'access', 'generic', 'leaf', 'peer', 'spine', 'superspine', 'unused', by default all values are selected. diff --git a/go.mod b/go.mod index 845ed5e7..97ee777a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ go 1.22.5 require ( github.com/IBM/netaddr v1.5.0 - github.com/Juniper/apstra-go-sdk v0.0.0-20241004155941-a48e304bd4b2 + github.com/Juniper/apstra-go-sdk v0.0.0-20241010000234-c27c2a93c8cc github.com/chrismarget-j/go-licenses v0.0.0-20240224210557-f22f3e06d3d4 github.com/chrismarget-j/version-constraints v0.0.0-20240925155624-26771a0a6820 github.com/google/go-cmp v0.6.0 diff --git a/go.sum b/go.sum index a227a719..b9ebc1af 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0 github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/IBM/netaddr v1.5.0 h1:IJlFZe1+nFs09TeMB/HOP4+xBnX2iM/xgiDOgZgTJq0= github.com/IBM/netaddr v1.5.0/go.mod h1:DDBPeYgbFzoXHjSz9Jwk7K8wmWV4+a/Kv0LqRnb8we4= -github.com/Juniper/apstra-go-sdk v0.0.0-20241004155941-a48e304bd4b2 h1:6O56557qPVaB2EZdjVR/OC+ZFF/mpKPTHDD9ATSE6CA= -github.com/Juniper/apstra-go-sdk v0.0.0-20241004155941-a48e304bd4b2/go.mod h1:qXNVTdnVa40aMTOsBTnKoFNYT5ftga2NAkGJhx4o6bY= +github.com/Juniper/apstra-go-sdk v0.0.0-20241010000234-c27c2a93c8cc h1:RjkmXaPqbCWNWc7QoS2O71dh+hNideqLWkY5fLgHGls= +github.com/Juniper/apstra-go-sdk v0.0.0-20241010000234-c27c2a93c8cc/go.mod h1:qXNVTdnVa40aMTOsBTnKoFNYT5ftga2NAkGJhx4o6bY= github.com/Kunde21/markdownfmt/v3 v3.1.0 h1:KiZu9LKs+wFFBQKhrZJrFZwtLnCCWJahL+S+E/3VnM0= github.com/Kunde21/markdownfmt/v3 v3.1.0/go.mod h1:tPXN1RTyOzJwhfHoon9wUr4HGYmWgVxSQN6VBJDkrVc= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=