コード例 #1
0
func resourceVIXVMCreate(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)

	vm := new(vix.VM)
	vm.Provider = config.Product
	vm.VerifySSL = config.VerifySSL

	if err := tf_to_vix(d, vm); err != nil {
		return err
	}

	id, err := vm.Create()
	if err != nil {
		return err
	}

	log.Printf("[DEBUG] Resource ID: %s\n", id)
	d.SetId(id)

	// Initialize the connection info
	d.SetConnInfo(map[string]string{
		"type": "ssh",
		"host": vm.IPAddress,
	})

	return resourceVIXVMRead(d, meta)
}
コード例 #2
0
func resourceVIXVMDelete(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)
	vmxFile := d.Id()

	vm := new(vix.VM)
	vm.Provider = config.Product
	vm.VerifySSL = config.VerifySSL

	if password := d.Get("password"); password != nil {
		vm.Image.Password = d.Get("password").(string)
	}

	return vm.Destroy(vmxFile)
}
コード例 #3
0
func resourceVIXVMUpdate(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)

	vm := new(vix.VM)
	vm.Provider = config.Product
	vm.VerifySSL = config.VerifySSL

	// Maps terraform.ResourceState attrbutes to vix.VM
	tf_to_vix(d, vm)

	err := vm.Update(d.Id())
	if err != nil {
		return err
	}

	return resourceVIXVMRead(d, meta)
}
コード例 #4
0
func cdrom_tf_to_vix(d *schema.ResourceData, vm *vix.VM) error {
	cdromCount := d.Get("cdrom.#").(int)
	vm.CDDVDDrives = make([]*govix.CDDVDDrive, 0, cdromCount)

	for i := 0; i < cdromCount; i++ {
		prefix := fmt.Sprintf("cdrom.%d.", i)
		cdrom := new(govix.CDDVDDrive)

		if attr, ok := d.Get(prefix + "bus_type").(string); ok && attr != "" {
			cdrom.Bus = vmx.BusType(attr)
		}

		if attr, ok := d.Get(prefix + "image").(string); ok && attr != "" {
			cdrom.Filename = attr
		}
		vm.CDDVDDrives = append(vm.CDDVDDrives, cdrom)
	}

	return nil
}
コード例 #5
0
func resourceVIXVMRead(d *schema.ResourceData, meta interface{}) error {
	config := meta.(*Config)

	vmxFile := d.Id()

	vm := new(vix.VM)
	vm.Provider = config.Product
	vm.VerifySSL = config.VerifySSL

	running, err := vm.Refresh(vmxFile)
	if err != nil {
		return err
	}

	// This is to let TF know the resource is gone
	if !running {
		return nil
	}

	// Refreshes only what makes sense, for example, we do not refresh settings
	// that modify the behavior of this provider
	d.Set("name", vm.Name)
	d.Set("description", vm.Description)
	d.Set("cpus", vm.CPUs)
	d.Set("memory", vm.Memory)
	d.Set("ip_address", vm.IPAddress)

	err = net_vix_to_tf(vm, d)
	if err != nil {
		return err
	}

	err = cdrom_vix_to_tf(vm, d)
	if err != nil {
		return err
	}

	return nil
}
コード例 #6
0
// Maps Terraform attributes to provider's structs
func tf_to_vix(d *schema.ResourceData, vm *vix.VM) error {
	var err error

	vm.Name = d.Get("name").(string)
	vm.Description = d.Get("description").(string)
	vm.CPUs = uint(d.Get("cpus").(int))

	vm.Memory = d.Get("memory").(string)
	vm.UpgradeVHardware = d.Get("upgrade_vhardware").(bool)
	vm.LaunchGUI = d.Get("gui").(bool)
	vm.SharedFolders = d.Get("sharedfolders").(bool)

	vm.ToolsInitTimeout, err = time.ParseDuration(d.Get("tools_init_timeout").(string))

	// Maps any defined networks to VIX provider's data types
	err = net_tf_to_vix(d, vm)
	if err != nil {
		return fmt.Errorf("Error mapping TF network adapter resource to VIX data types: %s", err)
	}

	if i := d.Get("image.#").(int); i > 0 {
		prefix := "image.0."
		vm.Image = vix.Image{
			URL:          d.Get(prefix + "url").(string),
			Checksum:     d.Get(prefix + "checksum").(string),
			ChecksumType: d.Get(prefix + "checksum_type").(string),
			Password:     d.Get(prefix + "password").(string),
		}
	}

	err = cdrom_tf_to_vix(d, vm)
	if err != nil {
		return fmt.Errorf("Error mapping TF cdrom resource to VIX data types: %s", err)
	}

	return nil
}
コード例 #7
0
func net_tf_to_vix(d *schema.ResourceData, vm *vix.VM) error {
	tf_to_vix_virtual_device := func(attr string) (govix.VNetDevice, error) {
		switch attr {
		case "vlance":
			return govix.NETWORK_DEVICE_VLANCE, nil
		case "e1000":
			return govix.NETWORK_DEVICE_E1000, nil
		case "vmxnet3":
			return govix.NETWORK_DEVICE_VMXNET3, nil
		default:
			return "", fmt.Errorf("[ERROR] Invalid virtual network device: %s", attr)
		}
	}

	tf_to_vix_network_type := func(attr string) (govix.NetworkType, error) {
		switch attr {
		case "bridged":
			return govix.NETWORK_BRIDGED, nil
		case "nat":
			return govix.NETWORK_NAT, nil
		case "hostonly":
			return govix.NETWORK_HOSTONLY, nil
		case "custom":
			return govix.NETWORK_CUSTOM, nil
		default:
			return "", fmt.Errorf("[ERROR] Invalid virtual network adapter type: %s", attr)
		}
	}

	// tf_to_vix_vswitch := func(attr string) (vix.VSwitch, error) {
	// 	return vix.VSwitch{}, nil
	// }

	var err error
	var errs []error
	adaptersCount := d.Get("network_adapter.#").(int)
	vm.VNetworkAdapters = make([]*govix.NetworkAdapter, 0, adaptersCount)

	for i := 0; i < adaptersCount; i++ {
		prefix := fmt.Sprintf("network_adapter.%d.", i)
		adapter := new(govix.NetworkAdapter)

		if attr, ok := d.Get(prefix + "driver").(string); ok && attr != "" {
			adapter.Vdevice, err = tf_to_vix_virtual_device(attr)
		}

		if attr, ok := d.Get(prefix + "mac_address").(string); ok && attr != "" {
			// Only set a MAC address if it is declared as static
			// otherwise leave Govix to assign or continue using the generated
			// one.
			if addrtype, ok := d.Get(prefix + "mac_address_type").(string); ok && addrtype == "static" {
				adapter.MacAddress, err = net.ParseMAC(attr)
			}
		}

		if attr, ok := d.Get(prefix + "type").(string); ok && attr != "" {
			adapter.ConnType, err = tf_to_vix_network_type(attr)
		}

		// if attr, ok := adapter["vswitch"].(string); ok && attr != "" {
		// 	vnic.VSwitch, err = tf_to_vix_vswitch(attr)
		// }
		if err != nil {
			errs = append(errs, err)
		}

		log.Printf("[DEBUG] Network adapter: %+v\n", adapter)
		vm.VNetworkAdapters = append(vm.VNetworkAdapters, adapter)
	}

	if len(errs) > 0 {
		return &multierror.Error{Errors: errs}
	}

	return nil
}