Skip to content

Commit 207a50e

Browse files
committed
resource(switch_port_settings): fix incorrect types
Some of the types in the Terraform model were incorrect both at the type level and at the struct field tag level. Those were fixed. Also fixed a few places where there could be a nil pointer exception.
1 parent aaa884d commit 207a50e

1 file changed

Lines changed: 112 additions & 44 deletions

File tree

internal/provider/resource_switch_port_settings.go

Lines changed: 112 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ package provider
77
import (
88
"context"
99
"fmt"
10+
"strconv"
1011

1112
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
13+
"github.com/hashicorp/terraform-plugin-framework/diag"
1214
"github.com/hashicorp/terraform-plugin-framework/path"
1315
"github.com/hashicorp/terraform-plugin-framework/resource"
1416
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1517
"github.com/hashicorp/terraform-plugin-framework/types"
1618
"github.com/hashicorp/terraform-plugin-log/tflog"
17-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1819
"github.com/oxidecomputer/oxide.go/oxide"
1920
)
2021

@@ -55,16 +56,16 @@ type switchPortSettingsAddressAddressModel struct {
5556
}
5657

5758
type switchPortSettingsBGPPeerModel struct {
58-
LinkName types.String `tfsdk:"link_name"`
59-
Peers []switchPortSettingsBGPPeerPeerModel
59+
LinkName types.String `tfsdk:"link_name"`
60+
Peers []switchPortSettingsBGPPeerPeerModel `tfsdk:"peers"`
6061
}
6162

6263
type switchPortSettingsBGPPeerPeerModel struct {
63-
Addr types.String `tfsdk:"peers"`
64+
Addr types.String `tfsdk:"addr"`
6465
AllowedExport *switchPortSettingsBGPPeerPeerAllowedExportModel `tfsdk:"allow_export"`
6566
AllowedImport *switchPortSettingsBGPPeerPeerAllowedImportModel `tfsdk:"allow_import"`
6667
BGPConfig types.String `tfsdk:"bgp_config"`
67-
Communities []types.String `tfsdk:"communities"`
68+
Communities []types.Int64 `tfsdk:"communities"`
6869
ConnectRetry types.Int64 `tfsdk:"connect_retry"`
6970
DelayOpen types.Int64 `tfsdk:"delay_open"`
7071
EnforceFirstAs types.Bool `tfsdk:"enforce_first_as"`
@@ -125,8 +126,8 @@ type switchPortSettingsLinkTxEqModel struct {
125126
Main types.Int32 `tfsdk:"main"`
126127
Post1 types.Int32 `tfsdk:"post1"`
127128
Post2 types.Int32 `tfsdk:"post2"`
128-
Pre1 types.Int32 `tfsdk:"Pre1"`
129-
Pre2 types.Int32 `tfsdk:"Pre2"`
129+
Pre1 types.Int32 `tfsdk:"pre1"`
130+
Pre2 types.Int32 `tfsdk:"pre2"`
130131
}
131132

132133
type switchPortSettingsPortConfigModel struct {
@@ -526,7 +527,12 @@ func (r *switchPortSettingsResource) Read(ctx context.Context, req resource.Read
526527
state.TimeCreated = types.StringValue(settings.TimeCreated.String())
527528
state.TimeModified = types.StringValue(settings.TimeModified.String())
528529

529-
model, _ := toTerraformModel(settings)
530+
model, diags := toTerraformModel(settings)
531+
resp.Diagnostics.Append(diags...)
532+
if resp.Diagnostics.HasError() {
533+
return
534+
}
535+
530536
state.Addresses = model.Addresses
531537
state.BGPPeers = model.BGPPeers
532538
state.Groups = model.Groups
@@ -590,7 +596,6 @@ func (r *switchPortSettingsResource) Update(ctx context.Context, req resource.Up
590596

591597
// Save plan into Terraform state.
592598
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
593-
594599
if resp.Diagnostics.HasError() {
595600
return
596601
}
@@ -656,7 +661,12 @@ func toTerraformModel(settings *oxide.SwitchPortSettings) (switchPortSettingsMod
656661
switchPortSettingsAddressAddressModel{
657662
Address: types.StringValue(address.Address.(string)),
658663
AddressLot: types.StringValue(string(address.AddressLotId)),
659-
VlanID: types.Int32Value(int32(*address.VlanId)),
664+
VlanID: func() types.Int32 {
665+
if address.VlanId != nil {
666+
return types.Int32Value(int32(*address.VlanId))
667+
}
668+
return types.Int32Null()
669+
}(),
660670
},
661671
)
662672
}
@@ -685,9 +695,16 @@ func toTerraformModel(settings *oxide.SwitchPortSettings) (switchPortSettingsMod
685695
allowedImportValue = append(allowedImportValue, types.StringValue(elem.(string)))
686696
}
687697

688-
communities := make([]types.String, 0)
689-
for _, community := range bgpPeer.Communities {
690-
communities = append(communities, types.StringValue(community))
698+
communities := make([]types.Int64, 0)
699+
for _, communityStr := range bgpPeer.Communities {
700+
community, err := strconv.ParseInt(communityStr, 10, 64)
701+
if err != nil {
702+
diags.AddError(
703+
"Error parsing community element",
704+
fmt.Sprintf("Could not parse %s as int64: %v", communityStr, err),
705+
)
706+
}
707+
communities = append(communities, types.Int64Value(community))
691708
}
692709

693710
bgpPeersMap[string(bgpPeer.InterfaceName)] = append(
@@ -749,28 +766,59 @@ func toTerraformModel(settings *oxide.SwitchPortSettings) (switchPortSettingsMod
749766

750767
links := make([]switchPortSettingsLinkModel, 0)
751768
for _, link := range settings.Links {
769+
lldp := &switchPortSettingsLinkLLDPModel{}
770+
if link.LldpLinkConfig != nil {
771+
lldp.ChassisID = types.StringValue(link.LldpLinkConfig.ChassisId)
772+
lldp.Enabled = types.BoolPointerValue(link.LldpLinkConfig.Enabled)
773+
lldp.LinkDescription = types.StringValue(link.LldpLinkConfig.LinkDescription)
774+
lldp.LinkName = types.StringValue(link.LldpLinkConfig.LinkName)
775+
lldp.ManagementIP = types.StringValue(link.LldpLinkConfig.ManagementIp)
776+
lldp.SystemDescription = types.StringValue(link.LldpLinkConfig.SystemDescription)
777+
lldp.SystemName = types.StringValue(link.LldpLinkConfig.SystemName)
778+
}
779+
780+
txEq := &switchPortSettingsLinkTxEqModel{}
781+
if link.TxEqConfig != nil {
782+
txEq.Main = func() types.Int32 {
783+
if link.TxEqConfig.Main != nil {
784+
return types.Int32Value(int32(*link.TxEqConfig.Main))
785+
}
786+
return types.Int32Null()
787+
}()
788+
txEq.Post1 = func() types.Int32 {
789+
if link.TxEqConfig.Post1 != nil {
790+
return types.Int32Value(int32(*link.TxEqConfig.Post1))
791+
}
792+
return types.Int32Null()
793+
}()
794+
txEq.Post2 = func() types.Int32 {
795+
if link.TxEqConfig.Post2 != nil {
796+
return types.Int32Value(int32(*link.TxEqConfig.Post2))
797+
}
798+
return types.Int32Null()
799+
}()
800+
txEq.Pre1 = func() types.Int32 {
801+
if link.TxEqConfig.Pre1 != nil {
802+
return types.Int32Value(int32(*link.TxEqConfig.Pre1))
803+
}
804+
return types.Int32Null()
805+
}()
806+
txEq.Pre2 = func() types.Int32 {
807+
if link.TxEqConfig.Pre2 != nil {
808+
return types.Int32Value(int32(*link.TxEqConfig.Pre2))
809+
}
810+
return types.Int32Null()
811+
}()
812+
}
813+
752814
links = append(links, switchPortSettingsLinkModel{
753815
Autoneg: types.BoolPointerValue(link.Autoneg),
754816
FEC: types.StringValue(string(link.Fec)),
755817
LinkName: types.StringValue(string(link.LinkName)),
756-
LLDP: &switchPortSettingsLinkLLDPModel{
757-
ChassisID: types.StringValue(link.LldpLinkConfig.ChassisId),
758-
Enabled: types.BoolPointerValue(link.LldpLinkConfig.Enabled),
759-
LinkDescription: types.StringValue(link.LldpLinkConfig.LinkDescription),
760-
LinkName: types.StringValue(string(link.LinkName)),
761-
ManagementIP: types.StringValue(link.LldpLinkConfig.ManagementIp),
762-
SystemDescription: types.StringValue(link.LldpLinkConfig.SystemDescription),
763-
SystemName: types.StringValue(link.LldpLinkConfig.SystemName),
764-
},
765-
MTU: types.Int32Value(int32(*link.Mtu)),
766-
Speed: types.StringValue(string(link.Speed)),
767-
TxEq: &switchPortSettingsLinkTxEqModel{
768-
Main: types.Int32Value(int32(*link.TxEqConfig.Main)),
769-
Post1: types.Int32Value(int32(*link.TxEqConfig.Post1)),
770-
Post2: types.Int32Value(int32(*link.TxEqConfig.Post2)),
771-
Pre1: types.Int32Value(int32(*link.TxEqConfig.Pre1)),
772-
Pre2: types.Int32Value(int32(*link.TxEqConfig.Pre2)),
773-
},
818+
LLDP: lldp,
819+
MTU: types.Int32Value(int32(*link.Mtu)),
820+
Speed: types.StringValue(string(link.Speed)),
821+
TxEq: txEq,
774822
})
775823
}
776824
model.Links = links
@@ -784,10 +832,20 @@ func toTerraformModel(settings *oxide.SwitchPortSettings) (switchPortSettingsMod
784832
routesMap[string(route.InterfaceName)] = append(
785833
routesMap[string(route.InterfaceName)],
786834
switchPortSettingsRouteRouteModel{
787-
Dst: types.StringValue(route.Dst.(string)),
788-
GW: types.StringValue(route.Gw),
789-
RIBPriority: types.Int32Value(int32(*route.RibPriority)),
790-
VID: types.Int32Value(int32(*route.VlanId)),
835+
Dst: types.StringValue(route.Dst.(string)),
836+
GW: types.StringValue(route.Gw),
837+
RIBPriority: func() types.Int32 {
838+
if route.RibPriority != nil {
839+
return types.Int32Value(int32(*route.RibPriority))
840+
}
841+
return types.Int32Null()
842+
}(),
843+
VID: func() types.Int32 {
844+
if route.VlanId != nil {
845+
return types.Int32Value(int32(*route.VlanId))
846+
}
847+
return types.Int32Null()
848+
}(),
791849
},
792850
)
793851
}
@@ -822,7 +880,12 @@ func toOxideParams(model switchPortSettingsModel) (oxide.NetworkingSwitchPortSet
822880
addrs = append(addrs, oxide.Address{
823881
Address: oxide.IpNet(addr.Address.ValueString()),
824882
AddressLot: oxide.NameOrId(addr.AddressLot.ValueString()),
825-
VlanId: oxide.NewPointer(int(addr.VlanID.ValueInt32())),
883+
VlanId: func() *int {
884+
if !addr.VlanID.IsNull() {
885+
return oxide.NewPointer(int(addr.VlanID.ValueInt32()))
886+
}
887+
return nil
888+
}(),
826889
})
827890
}
828891

@@ -839,17 +902,17 @@ func toOxideParams(model switchPortSettingsModel) (oxide.NetworkingSwitchPortSet
839902
for _, peer := range bgpPeer.Peers {
840903
allowedExportValue := make([]oxide.IpNet, 0)
841904
for _, value := range peer.AllowedExport.Value {
842-
allowedExportValue = append(allowedExportValue, oxide.IpNet(value.ValueString))
905+
allowedExportValue = append(allowedExportValue, oxide.IpNet(value.ValueString()))
843906
}
844907

845908
allowedImportValue := make([]oxide.IpNet, 0)
846909
for _, value := range peer.AllowedImport.Value {
847-
allowedImportValue = append(allowedImportValue, oxide.IpNet(value.ValueString))
910+
allowedImportValue = append(allowedImportValue, oxide.IpNet(value.ValueString()))
848911
}
849912

850913
communities := make([]string, 0)
851914
for _, community := range peer.Communities {
852-
communities = append(communities, community.ValueString())
915+
communities = append(communities, fmt.Sprintf("%d", community.ValueInt64()))
853916
}
854917

855918
peers = append(peers, oxide.BgpPeer{
@@ -898,7 +961,12 @@ func toOxideParams(model switchPortSettingsModel) (oxide.NetworkingSwitchPortSet
898961
interfaces = append(interfaces, oxide.SwitchInterfaceConfigCreate{
899962
Kind: oxide.SwitchInterfaceKind{
900963
Type: oxide.SwitchInterfaceKindType(iface.Kind.Type.ValueString()),
901-
Vid: oxide.NewPointer(int(iface.Kind.VID.ValueInt32())),
964+
Vid: func() *int {
965+
if !iface.Kind.VID.IsNull() {
966+
return oxide.NewPointer(int(iface.Kind.VID.ValueInt32()))
967+
}
968+
return nil
969+
}(),
902970
},
903971
LinkName: oxide.Name(iface.LinkName.ValueString()),
904972
V6Enabled: oxide.NewPointer(iface.V6Enabled.ValueBool()),
@@ -908,9 +976,9 @@ func toOxideParams(model switchPortSettingsModel) (oxide.NetworkingSwitchPortSet
908976

909977
links := make([]oxide.LinkConfigCreate, 0)
910978
for _, link := range model.Links {
911-
var txeq oxide.TxEqConfig
979+
var txeq *oxide.TxEqConfig
912980
if link.TxEq != nil {
913-
txeq = oxide.TxEqConfig{
981+
txeq = &oxide.TxEqConfig{
914982
Main: oxide.NewPointer(int(link.TxEq.Main.ValueInt32())),
915983
Post1: oxide.NewPointer(int(link.TxEq.Post1.ValueInt32())),
916984
Post2: oxide.NewPointer(int(link.TxEq.Post2.ValueInt32())),
@@ -930,11 +998,11 @@ func toOxideParams(model switchPortSettingsModel) (oxide.NetworkingSwitchPortSet
930998
LinkName: link.LLDP.LinkName.ValueString(),
931999
ManagementIp: link.LLDP.ManagementIP.ValueString(),
9321000
SystemDescription: link.LLDP.SystemDescription.ValueString(),
933-
SystemName: link.LLDP.SystemDescription.ValueString(),
1001+
SystemName: link.LLDP.SystemName.ValueString(),
9341002
},
9351003
Mtu: oxide.NewPointer(int(link.MTU.ValueInt32())),
9361004
Speed: oxide.LinkSpeed(link.Speed.ValueString()),
937-
TxEq: &txeq,
1005+
TxEq: txeq,
9381006
})
9391007
}
9401008
params.Body.Links = links

0 commit comments

Comments
 (0)