github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/vsphere/resource_vsphere_virtual_machine_migrate.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func resourceVSphereVirtualMachineMigrateState(
    12  	v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
    13  	if is.Empty() {
    14  		log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
    15  		return is, nil
    16  	}
    17  
    18  	switch v {
    19  	case 0:
    20  		log.Println("[INFO] Found Compute Instance State v0; migrating to v1")
    21  		is, err := migrateVSphereVirtualMachineStateV0toV1(is)
    22  		if err != nil {
    23  			return is, err
    24  		}
    25  		return is, nil
    26  	default:
    27  		return is, fmt.Errorf("Unexpected schema version: %d", v)
    28  	}
    29  }
    30  
    31  func migrateVSphereVirtualMachineStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
    32  	if is.Empty() || is.Attributes == nil {
    33  		log.Println("[DEBUG] Empty VSphere Virtual Machine State; nothing to migrate.")
    34  		return is, nil
    35  	}
    36  
    37  	log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
    38  
    39  	if is.Attributes["skip_customization"] == "" {
    40  		is.Attributes["skip_customization"] = "false"
    41  	}
    42  
    43  	if is.Attributes["enable_disk_uuid"] == "" {
    44  		is.Attributes["enable_disk_uuid"] = "false"
    45  	}
    46  
    47  	for k, _ := range is.Attributes {
    48  		if strings.HasPrefix(k, "disk.") && strings.HasSuffix(k, ".size") {
    49  			diskParts := strings.Split(k, ".")
    50  			if len(diskParts) != 3 {
    51  				continue
    52  			}
    53  			s := strings.Join([]string{diskParts[0], diskParts[1], "controller_type"}, ".")
    54  			if _, ok := is.Attributes[s]; !ok {
    55  				is.Attributes[s] = "scsi"
    56  			}
    57  		}
    58  	}
    59  
    60  	log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
    61  	return is, nil
    62  }