@@ -16,6 +16,7 @@ import (
1616
1717 "github.com/equinix/terraform-provider-equinix/internal/converters"
1818 "github.com/equinix/terraform-provider-equinix/internal/network"
19+ equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"
1920
2021 equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"
2122
@@ -586,65 +587,6 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
586587 return diag .FromErr (err )
587588 }
588589
589- d .Set ("hostname" , device .GetHostname ())
590- d .Set ("plan" , device .Plan .GetSlug ())
591- d .Set ("deployed_facility" , device .Facility .GetCode ())
592- d .Set ("facilities" , []string {device .Facility .GetCode ()})
593- if device .Metro != nil {
594- d .Set ("metro" , device .Metro .GetCode ())
595- }
596- d .Set ("operating_system" , device .OperatingSystem .GetSlug ())
597- d .Set ("state" , device .GetState ())
598- d .Set ("billing_cycle" , device .GetBillingCycle ())
599- d .Set ("locked" , device .GetLocked ())
600- d .Set ("created" , device .GetCreatedAt ().Format (time .RFC3339 ))
601- d .Set ("updated" , device .GetUpdatedAt ().Format (time .RFC3339 ))
602- d .Set ("ipxe_script_url" , device .GetIpxeScriptUrl ())
603- d .Set ("always_pxe" , device .GetAlwaysPxe ())
604- d .Set ("root_password" , device .GetRootPassword ())
605- d .Set ("project_id" , device .Project .GetId ())
606- d .Set ("sos_hostname" , device .GetSos ())
607- if device .Storage != nil {
608- rawStorageBytes , err := json .Marshal (device .Storage )
609- if err != nil {
610- return diag .Errorf ("[ERR] Error getting storage JSON string for device (%s): %s" , d .Id (), err )
611- }
612-
613- storageString , err := structure .NormalizeJsonString (string (rawStorageBytes ))
614- if err != nil {
615- return diag .Errorf ("[ERR] Error normalizing storage JSON string for device (%s): %s" , d .Id (), err )
616- }
617- d .Set ("storage" , storageString )
618- }
619- if device .HardwareReservation != nil {
620- d .Set ("deployed_hardware_reservation_id" , device .HardwareReservation .GetId ())
621- }
622-
623- networkType , err := getNetworkType (device )
624- if err != nil {
625- return diag .Errorf ("[ERR] Error computing network type for device (%s): %s" , d .Id (), err )
626- }
627- d .Set ("network_type" , networkType )
628-
629- wfrd := "wait_for_reservation_deprovision"
630- if _ , ok := d .GetOk (wfrd ); ! ok {
631- d .Set (wfrd , nil )
632- }
633- fdv := "force_detach_volumes"
634- if _ , ok := d .GetOk (fdv ); ! ok {
635- d .Set (fdv , nil )
636- }
637- tt := "termination_time"
638- if _ , ok := d .GetOk (tt ); ! ok {
639- d .Set (tt , nil )
640- }
641-
642- d .Set ("tags" , device .Tags )
643- keyIDs := []string {}
644- for _ , k := range device .SshKeys {
645- keyIDs = append (keyIDs , path .Base (k .Href ))
646- }
647- d .Set ("ssh_key_ids" , keyIDs )
648590 networkInfo := getNetworkInfo (device .IpAddresses )
649591
650592 sort .SliceStable (networkInfo .Networks , func (i , j int ) bool {
@@ -655,22 +597,95 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
655597 return getNetworkRank (int (famI ), pubI ) < getNetworkRank (int (famJ ), pubJ )
656598 })
657599
658- d .Set ("network" , networkInfo .Networks )
659- d .Set ("access_public_ipv4" , networkInfo .PublicIPv4 )
660- d .Set ("access_private_ipv4" , networkInfo .PrivateIPv4 )
661- d .Set ("access_public_ipv6" , networkInfo .PublicIPv6 )
662-
663- ports := getPorts (device .NetworkPorts )
664- d .Set ("ports" , ports )
665-
666600 if networkInfo .Host != "" {
667601 d .SetConnInfo (map [string ]string {
668602 "type" : "ssh" ,
669603 "host" : networkInfo .Host ,
670604 })
671605 }
672606
673- return nil
607+ return equinix_schema .SetDiagnosticsMap (d , map [string ]interface {}{
608+ "hostname" : device .GetHostname (),
609+ "plan" : device .Plan .GetSlug (),
610+ "deployed_facility" : device .Facility .GetCode (),
611+ "facilities" : []string {device .Facility .GetCode ()},
612+ "metro" : func (d * schema.ResourceData , k string ) error {
613+ if device .Metro != nil {
614+ return d .Set (k , device .Metro .GetCode ())
615+ }
616+ return nil
617+ },
618+ "operating_system" : device .OperatingSystem .GetSlug (),
619+ "state" : device .GetState (),
620+ "billing_cycle" : device .GetBillingCycle (),
621+ "locked" : device .GetLocked (),
622+ "created" : device .GetCreatedAt ().Format (time .RFC3339 ),
623+ "updated" : device .GetUpdatedAt ().Format (time .RFC3339 ),
624+ "ipxe_script_url" : device .GetIpxeScriptUrl (),
625+ "always_pxe" : device .GetAlwaysPxe (),
626+ "root_password" : device .GetRootPassword (),
627+ "project_id" : device .Project .GetId (),
628+ "sos_hostname" : device .GetSos (),
629+ "storage" : func (d * schema.ResourceData , k string ) error {
630+ if device .Storage != nil {
631+ rawStorageBytes , err := json .Marshal (device .Storage )
632+ if err != nil {
633+ return fmt .Errorf ("[ERR] Error getting storage JSON string for device (%s): %s" , d .Id (), err )
634+ }
635+
636+ storageString , err := structure .NormalizeJsonString (string (rawStorageBytes ))
637+ if err != nil {
638+ return fmt .Errorf ("[ERR] Error normalizing storage JSON string for device (%s): %s" , d .Id (), err )
639+ }
640+ return d .Set (k , storageString )
641+ }
642+ return nil
643+ },
644+ "deployed_hardware_reservation_id" : func (d * schema.ResourceData , k string ) error {
645+ if device .HardwareReservation != nil {
646+ return d .Set (k , device .HardwareReservation .GetId ())
647+ }
648+ return nil
649+ },
650+ "network_type" : func (d * schema.ResourceData , k string ) error {
651+ networkType , err := getNetworkType (device )
652+ if err != nil {
653+ return fmt .Errorf ("[ERR] Error computing network type for device (%s): %s" , d .Id (), err )
654+ }
655+ return d .Set (k , networkType )
656+ },
657+ "wait_for_reservation_deprovision" : func (d * schema.ResourceData , k string ) error {
658+ if _ , ok := d .GetOk (k ); ! ok {
659+ return d .Set (k , nil )
660+ }
661+ return nil
662+ },
663+ "force_detach_volumes" : func (d * schema.ResourceData , k string ) error {
664+ if _ , ok := d .GetOk (k ); ! ok {
665+ return d .Set (k , nil )
666+ }
667+ return nil
668+ },
669+ "termination_time" : func (d * schema.ResourceData , k string ) error {
670+ if _ , ok := d .GetOk (k ); ! ok {
671+ return d .Set (k , nil )
672+ }
673+ return nil
674+ },
675+ "tags" : device .Tags ,
676+ "ssh_key_ids" : func (d * schema.ResourceData , k string ) error {
677+ keyIDs := []string {}
678+ for _ , k := range device .SshKeys {
679+ keyIDs = append (keyIDs , path .Base (k .Href ))
680+ }
681+ return d .Set (k , keyIDs )
682+ },
683+ "network" : networkInfo .Networks ,
684+ "access_public_ipv4" : networkInfo .PublicIPv4 ,
685+ "access_private_ipv4" : networkInfo .PrivateIPv4 ,
686+ "access_public_ipv6" : networkInfo .PublicIPv6 ,
687+ "ports" : getPorts (device .NetworkPorts ),
688+ })
674689}
675690
676691func resourceMetalDeviceUpdate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
0 commit comments