當前位置: 首頁>>代碼示例>>Golang>>正文


Golang VM.VNetworkAdapters方法代碼示例

本文整理匯總了Golang中github.com/hooklift/terraform-provider-vix/provider/vix.VM.VNetworkAdapters方法的典型用法代碼示例。如果您正苦於以下問題:Golang VM.VNetworkAdapters方法的具體用法?Golang VM.VNetworkAdapters怎麽用?Golang VM.VNetworkAdapters使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/hooklift/terraform-provider-vix/provider/vix.VM的用法示例。


在下文中一共展示了VM.VNetworkAdapters方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: net_tf_to_vix

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
}
開發者ID:,項目名稱:,代碼行數:76,代碼來源:


注:本文中的github.com/hooklift/terraform-provider-vix/provider/vix.VM.VNetworkAdapters方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。