Skip to content

Commit

Permalink
remove no-longer-needed MetalGo suffix from device helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Oct 17, 2023
1 parent 40c986b commit 81a163d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 57 deletions.
6 changes: 3 additions & 3 deletions equinix/data_source_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func dataSourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta
if device.HardwareReservation != nil {
d.Set("hardware_reservation_id", device.HardwareReservation.GetId())
}
networkType, err := getNetworkTypeMetalGo(device)
networkType, err := getNetworkType(device)
if err != nil {
return fmt.Errorf("[ERR] Error computing network type for device (%s): %s", d.Id(), err)
}
Expand All @@ -290,7 +290,7 @@ func dataSourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta
keyIDs = append(keyIDs, path.Base(k.Href))
}
d.Set("ssh_key_ids", keyIDs)
networkInfo := getNetworkInfoMetalGo(device.IpAddresses)
networkInfo := getNetworkInfo(device.IpAddresses)

sort.SliceStable(networkInfo.Networks, func(i, j int) bool {
famI := networkInfo.Networks[i]["family"].(int32)
Expand All @@ -305,7 +305,7 @@ func dataSourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("access_private_ipv4", networkInfo.PrivateIPv4)
d.Set("access_public_ipv6", networkInfo.PublicIPv6)

ports := getPortsMetalGo(device.NetworkPorts)
ports := getPorts(device.NetworkPorts)
d.Set("ports", ports)

d.SetId(device.GetId())
Expand Down
56 changes: 6 additions & 50 deletions equinix/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type NetworkInfo struct {
PrivateIPv4 string
}

func getNetworkInfoMetalGo(ips []metalv1.IPAssignment) NetworkInfo {
func getNetworkInfo(ips []metalv1.IPAssignment) NetworkInfo {
ni := NetworkInfo{Networks: make([]map[string]interface{}, 0, 1)}
for _, ip := range ips {
network := map[string]interface{}{
Expand Down Expand Up @@ -99,37 +99,7 @@ func getNetworkInfoMetalGo(ips []metalv1.IPAssignment) NetworkInfo {
return ni
}

func getNetworkInfo(ips []*packngo.IPAddressAssignment) NetworkInfo {
ni := NetworkInfo{Networks: make([]map[string]interface{}, 0, 1)}
for _, ip := range ips {
network := map[string]interface{}{
"address": ip.Address,
"gateway": ip.Gateway,
"family": ip.AddressFamily,
"cidr": ip.CIDR,
"public": ip.Public,
}
ni.Networks = append(ni.Networks, network)

// Initial device IPs are fixed and marked as "Management"
if ip.Management {
if ip.AddressFamily == 4 {
if ip.Public {
ni.Host = ip.Address
ni.IPv4SubnetSize = ip.CIDR
ni.PublicIPv4 = ip.Address
} else {
ni.PrivateIPv4 = ip.Address
}
} else {
ni.PublicIPv6 = ip.Address
}
}
}
return ni
}

func getNetworkTypeMetalGo(device *metalv1.Device) (*string, error) {
func getNetworkType(device *metalv1.Device) (*string, error) {
pgDevice := packngo.Device{}
res, err := device.MarshalJSON()
if err == nil {
Expand All @@ -153,7 +123,7 @@ func getNetworkRank(family int, public bool) int {
return 3
}

func getPortsMetalGo(ps []metalv1.Port) []map[string]interface{} {
func getPorts(ps []metalv1.Port) []map[string]interface{} {
ret := make([]map[string]interface{}, 0, 1)
for _, p := range ps {
port := map[string]interface{}{
Expand All @@ -168,21 +138,6 @@ func getPortsMetalGo(ps []metalv1.Port) []map[string]interface{} {
return ret
}

func getPorts(ps []packngo.Port) []map[string]interface{} {
ret := make([]map[string]interface{}, 0, 1)
for _, p := range ps {
port := map[string]interface{}{
"name": p.Name,
"id": p.ID,
"type": p.Type,
"mac": p.Data.MAC,
"bonded": p.Data.Bonded,
}
ret = append(ret, port)
}
return ret
}

func hwReservationStateRefreshFunc(client *packngo.Client, reservationId, instanceId string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
r, _, err := client.HardwareReservations.Get(reservationId, &packngo.GetOptions{Includes: []string{"device"}})
Expand Down Expand Up @@ -284,7 +239,7 @@ func ipAddressSchema() *schema.Resource {
}

func getDeviceMap(device metalv1.Device) map[string]interface{} {
networkInfo := getNetworkInfoMetalGo(device.IpAddresses)
networkInfo := getNetworkInfo(device.IpAddresses)
sort.SliceStable(networkInfo.Networks, func(i, j int) bool {
famI := int(*networkInfo.Networks[i]["family"].(*int32))
famJ := int(*networkInfo.Networks[j]["family"].(*int32))
Expand All @@ -296,7 +251,7 @@ func getDeviceMap(device metalv1.Device) map[string]interface{} {
for _, k := range device.SshKeys {
keyIDs = append(keyIDs, path.Base(k.GetHref()))
}
ports := getPortsMetalGo(device.NetworkPorts)
ports := getPorts(device.NetworkPorts)

return map[string]interface{}{
"hostname": device.GetHostname(),
Expand All @@ -319,5 +274,6 @@ func getDeviceMap(device metalv1.Device) map[string]interface{} {
"network": networkInfo.Networks,
"ssh_key_ids": keyIDs,
"ports": ports,
"sos_hostname": device.GetSos(),
}
}
7 changes: 3 additions & 4 deletions equinix/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var (

var (
deviceCommonIncludes = []string{"project", "metro", "facility", "hardware_reservation"}
deviceReadOptions = &packngo.GetOptions{Includes: deviceCommonIncludes}
)

func resourceMetalDevice() *schema.Resource {
Expand Down Expand Up @@ -664,7 +663,7 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("deployed_hardware_reservation_id", device.HardwareReservation.GetId())
}

networkType, err := getNetworkTypeMetalGo(device)
networkType, err := getNetworkType(device)
if err != nil {
return fmt.Errorf("[ERR] Error computing network type for device (%s): %s", d.Id(), err)
}
Expand All @@ -689,7 +688,7 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
keyIDs = append(keyIDs, path.Base(k.Href))
}
d.Set("ssh_key_ids", keyIDs)
networkInfo := getNetworkInfoMetalGo(device.IpAddresses)
networkInfo := getNetworkInfo(device.IpAddresses)

sort.SliceStable(networkInfo.Networks, func(i, j int) bool {
famI := networkInfo.Networks[i]["family"].(int32)
Expand All @@ -704,7 +703,7 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
d.Set("access_private_ipv4", networkInfo.PrivateIPv4)
d.Set("access_public_ipv6", networkInfo.PublicIPv6)

ports := getPortsMetalGo(device.NetworkPorts)
ports := getPorts(device.NetworkPorts)
d.Set("ports", ports)

if networkInfo.Host != "" {
Expand Down

0 comments on commit 81a163d

Please sign in to comment.