@@ -121,14 +121,14 @@ func resourceVSphereVirtualMachine() *schema.Resource {
121121 Default : "Etc/UTC" ,
122122 },
123123
124- "dns_suffix " : & schema.Schema {
124+ "dns_suffixes " : & schema.Schema {
125125 Type : schema .TypeList ,
126126 Optional : true ,
127127 Elem : & schema.Schema {Type : schema .TypeString },
128128 ForceNew : true ,
129129 },
130130
131- "dns_server " : & schema.Schema {
131+ "dns_servers " : & schema.Schema {
132132 Type : schema .TypeList ,
133133 Optional : true ,
134134 Elem : & schema.Schema {Type : schema .TypeString },
@@ -245,83 +245,79 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
245245 vm .timeZone = v .(string )
246246 }
247247
248- dns_suffix := d .Get ("dns_suffix.#" ).(int )
249- if dns_suffix > 0 {
250- vm .dnsSuffixes = make ([]string , 0 , dns_suffix )
251- for i := 0 ; i < dns_suffix ; i ++ {
252- s := fmt .Sprintf ("dns_suffix.%d" , i )
253- vm .dnsSuffixes = append (vm .dnsSuffixes , d .Get (s ).(string ))
248+ if raw , ok := d .GetOk ("dns_suffixes" ); ok {
249+ for _ , v := range raw .([]interface {}) {
250+ vm .dnsSuffixes = append (vm .dnsSuffixes , v .(string ))
254251 }
255252 } else {
256253 vm .dnsSuffixes = DefaultDNSSuffixes
257254 }
258255
259- dns_server := d .Get ("dns_server.#" ).(int )
260- if dns_server > 0 {
261- vm .dnsServers = make ([]string , 0 , dns_server )
262- for i := 0 ; i < dns_server ; i ++ {
263- s := fmt .Sprintf ("dns_server.%d" , i )
264- vm .dnsServers = append (vm .dnsServers , d .Get (s ).(string ))
256+ if raw , ok := d .GetOk ("dns_servers" ); ok {
257+ for _ , v := range raw .([]interface {}) {
258+ vm .dnsServers = append (vm .dnsServers , v .(string ))
265259 }
266260 } else {
267261 vm .dnsServers = DefaultDNSServers
268262 }
269263
270- networksCount := d .Get ("network_interface.#" ).(int )
271- networks := make ([]networkInterface , networksCount )
272- for i := 0 ; i < networksCount ; i ++ {
273- prefix := fmt .Sprintf ("network_interface.%d" , i )
274- networks [i ].label = d .Get (prefix + ".label" ).(string )
275- if v , ok := d .GetOk (prefix + ".ip_address" ); ok {
276- networks [i ].ipAddress = v .(string )
277- }
278- if v , ok := d .GetOk (prefix + ".subnet_mask" ); ok {
279- networks [i ].subnetMask = v .(string )
264+ if vL , ok := d .GetOk ("network_interface" ); ok {
265+ networks := make ([]networkInterface , len (vL .([]interface {})))
266+ for i , v := range vL .([]interface {}) {
267+ network := v .(map [string ]interface {})
268+ networks [i ].label = network ["label" ].(string )
269+ if v , ok := network ["ip_address" ].(string ); ok && v != "" {
270+ networks [i ].ipAddress = v
271+ }
272+ if v , ok := network ["subnet_mask" ].(string ); ok && v != "" {
273+ networks [i ].subnetMask = v
274+ }
280275 }
276+ vm .networkInterfaces = networks
277+ log .Printf ("[DEBUG] network_interface init: %v" , networks )
281278 }
282- vm .networkInterfaces = networks
283- log .Printf ("[DEBUG] network_interface init: %v" , networks )
284279
285- diskCount := d .Get ("disk.#" ).(int )
286- disks := make ([]hardDisk , diskCount )
287- for i := 0 ; i < diskCount ; i ++ {
288- prefix := fmt .Sprintf ("disk.%d" , i )
289- if i == 0 {
290- if v , ok := d .GetOk (prefix + ".template" ); ok {
291- vm .template = v .(string )
280+ if vL , ok := d .GetOk ("disk" ); ok {
281+ disks := make ([]hardDisk , len (vL .([]interface {})))
282+ for i , v := range vL .([]interface {}) {
283+ disk := v .(map [string ]interface {})
284+ if i == 0 {
285+ if v , ok := disk ["template" ].(string ); ok && v != "" {
286+ vm .template = v
287+ } else {
288+ if v , ok := disk ["size" ].(int ); ok && v != 0 {
289+ disks [i ].size = int64 (v )
290+ } else {
291+ return fmt .Errorf ("If template argument is not specified, size argument is required." )
292+ }
293+ }
294+ if v , ok := disk ["datastore" ].(string ); ok && v != "" {
295+ vm .datastore = v
296+ }
292297 } else {
293- if v , ok := d . GetOk ( prefix + ". size" ); ok {
294- disks [i ].size = int64 (v .( int ) )
298+ if v , ok := disk [ " size"].( int ); ok && v != 0 {
299+ disks [i ].size = int64 (v )
295300 } else {
296- return fmt .Errorf ("If template argument is not specified, size argument is required." )
301+ return fmt .Errorf ("Size argument is required." )
297302 }
298303 }
299- if v , ok := d . GetOk ( prefix + ".datastore" ); ok {
300- vm . datastore = v .( string )
304+ if v , ok := disk [ "iops" ].( int ); ok && v != 0 {
305+ disks [ i ]. iops = int64 ( v )
301306 }
302- } else {
303- if v , ok := d .GetOk (prefix + ".size" ); ok {
304- disks [i ].size = int64 (v .(int ))
305- } else {
306- return fmt .Errorf ("Size argument is required." )
307- }
308- }
309- if v , ok := d .GetOk (prefix + ".iops" ); ok {
310- disks [i ].iops = int64 (v .(int ))
311307 }
308+ vm .hardDisks = disks
309+ log .Printf ("[DEBUG] disk init: %v" , disks )
312310 }
313- vm .hardDisks = disks
314- log .Printf ("[DEBUG] disk init: %v" , disks )
315311
316312 if vm .template != "" {
317313 err := vm .deployVirtualMachine (client )
318314 if err != nil {
319- return fmt . Errorf ( "error: %s" , err )
315+ return err
320316 }
321317 } else {
322318 err := vm .createVirtualMachine (client )
323319 if err != nil {
324- return fmt . Errorf ( "error: %s" , err )
320+ return err
325321 }
326322 }
327323
@@ -338,7 +334,7 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
338334
339335 _ , err := stateConf .WaitForState ()
340336 if err != nil {
341- return fmt . Errorf ( "error: %s" , err )
337+ return err
342338 }
343339 }
344340 }
@@ -368,7 +364,7 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
368364
369365 collector := property .DefaultCollector (client .Client )
370366 if err := collector .RetrieveOne (context .TODO (), vm .Reference (), []string {"guest" , "summary" , "datastore" }, & mvm ); err != nil {
371- log . Printf ( "[ERROR] %#v" , err )
367+ return err
372368 }
373369
374370 log .Printf ("[DEBUG] %#v" , dc )
@@ -393,18 +389,21 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
393389 networkInterfaces = append (networkInterfaces , networkInterface )
394390 }
395391 }
396- d .Set ("network_interface" , networkInterfaces )
392+ err = d .Set ("network_interface" , networkInterfaces )
393+ if err != nil {
394+ return fmt .Errorf ("Invalid network interfaces to set: %#v" , networkInterfaces )
395+ }
397396
398397 var rootDatastore string
399398 for _ , v := range mvm .Datastore {
400399 var md mo.Datastore
401400 if err := collector .RetrieveOne (context .TODO (), v , []string {"name" , "parent" }, & md ); err != nil {
402- log . Printf ( "[ERROR] %#v" , err )
401+ return err
403402 }
404403 if md .Parent .Type == "StoragePod" {
405404 var msp mo.StoragePod
406405 if err := collector .RetrieveOne (context .TODO (), * md .Parent , []string {"name" }, & msp ); err != nil {
407- log . Printf ( "[ERROR] %#v" , err )
406+ return err
408407 }
409408 rootDatastore = msp .Name
410409 log .Printf ("[DEBUG] %#v" , msp .Name )
0 commit comments