@@ -62,7 +62,7 @@ func newRDU2Hosts() (*RDU2Hosts, error) {
6262 // Unmarshal the yaml into a slice of RDU2Host objects
6363 var hostsData []RDU2Host
6464 dec := yaml .NewDecoder (bytes .NewReader (yamlBytes ))
65- dec .KnownFields (true )
65+ dec .KnownFields (false )
6666 err = dec .Decode (& hostsData )
6767 if err != nil {
6868 return nil , fmt .Errorf ("failed to parse hosts.yaml: %w" , err )
@@ -72,16 +72,33 @@ func newRDU2Hosts() (*RDU2Hosts, error) {
7272 return nil , fmt .Errorf ("hosts.yaml contains no hosts" )
7373 }
7474
75- // Convert slice to map of name to RDU2Host objects to allow lookup by name
75+ // Validate required fields and build map in a single iteration
7676 hostsMap := make (map [string ]* RDU2Host , len (hostsData ))
7777 for i := range hostsData {
78- if hostsData [i ].Name == "" {
79- return nil , fmt .Errorf ("hosts.yaml entry at index %d has empty name" , i )
78+ host := & hostsData [i ]
79+
80+ // Validate required fields
81+ if host .Name == "" {
82+ return nil , fmt .Errorf ("hosts.yaml entry at index %d is missing required field 'name'" , i )
83+ }
84+ if host .BmcAddress == "" {
85+ return nil , fmt .Errorf ("hosts.yaml entry %q at index %d is missing required field 'bmc_address'" , host .Name , i )
86+ }
87+ if host .BmcUser == "" {
88+ return nil , fmt .Errorf ("hosts.yaml entry %q at index %d is missing required field 'bmc_user'" , host .Name , i )
8089 }
81- if _ , exists := hostsMap [hostsData [i ].Name ]; exists {
82- return nil , fmt .Errorf ("duplicate host name %q in hosts.yaml" , hostsData [i ].Name )
90+ if host .BmcPassword == "" {
91+ return nil , fmt .Errorf ("hosts.yaml entry %q at index %d is missing required field 'bmc_pass'" , host .Name , i )
92+ }
93+ if host .BmcForwardedPort == 0 {
94+ return nil , fmt .Errorf ("hosts.yaml entry %q at index %d is missing required field 'bmc_forwarded_port'" , host .Name , i )
95+ }
96+
97+ // Check for duplicates and add to map
98+ if _ , exists := hostsMap [host .Name ]; exists {
99+ return nil , fmt .Errorf ("duplicate host name %q in hosts.yaml" , host .Name )
83100 }
84- hostsMap [hostsData [ i ] .Name ] = & hostsData [ i ]
101+ hostsMap [host .Name ] = host
85102 }
86103
87104 return & RDU2Hosts {
0 commit comments