// CreateVMSwitch will create a new private VM Switch. To convert it into an internal
// or external VMSwitch, call the SetInternalPort() and SetExternalPort() functions.
// Calling SetInternalPort() on a private switch will turn it into an "internal"
// switch. An internal switch will only facilitate traffic between VMs attached to it
// and the Host operating system. Attaching an internal port means the OS can manage
// the IP settings of the switch.
// Calling SetExternalPort() will attach an physical or virtual net adapter to the
// VM switch. This will allow trafic to flow through that interface, making it an
// external VM switch.
// If both SetInternalPort() and SetExternalPort() are called, the switch becomes an
// external VM switch with management OS, inheriting the IP settings of the external
// net adapter attached to the switch.
CreateVMSwitch will create a new private VM Switch. To convert it into an internal
or external VMSwitch, call the SetInternalPort() and SetExternalPort() functions.
Calling SetInternalPort() on a private switch will turn it into an "internal"
switch. An internal switch will only facilitate traffic between VMs attached to it
and the Host operating system. Attaching an internal port means the OS can manage
the IP settings of the switch.
Calling SetExternalPort() will attach an physical or virtual net adapter to the
VM switch. This will allow trafic to flow through that interface, making it an
external VM switch.
If both SetInternalPort() and SetExternalPort() are called, the switch becomes an
external VM switch with management OS, inheriting the IP settings of the external
net adapter attached to the switch.
// CreateVMSwitch will create a new private VM Switch . To convert it into an internal // or external VMSwitch call the SetInternalPort () and SetExternalPort () functions . // Calling SetInternalPort () on a private switch will turn it into an " internal " // switch . An internal switch will only facilitate traffic between VMs attached to it // and the Host operating system . Attaching an internal port means the OS can manage // the IP settings of the switch . // Calling SetExternalPort () will attach an physical or virtual net adapter to the // VM switch . This will allow trafic to flow through that interface making it an // external VM switch . // If both SetInternalPort () and SetExternalPort () are called the switch becomes an // external VM switch with management OS inheriting the IP settings of the external // net adapter attached to the switch .
func (m *Manager) CreateVMSwitch(name string) (VirtualSwitch, error) {
	data, err := m.con.Get(VMSwitchSettings)
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "get VMSwitchSettings")
	}
	swInstance, err := data.Get("SpawnInstance_")
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "SpawnInstance_")
	}
	if err := swInstance.Set("ElementName", name); err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "set ElementName")
	}
	switchText, err := swInstance.GetText(1)
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "GetText")
	}
	jobPath := ole.VARIANT{}
	resultingSystem := ole.VARIANT{}
	jobState, err := m.svc.Get("DefineSystem", switchText, nil, nil, &resultingSystem, &jobPath)
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "DefineSystem")
	}
	if jobState.Value().(int32) == wmi.JobStatusStarted {
		err := wmi.WaitForJob(jobPath.Value().(string))
		if err != nil {
			return VirtualSwitch{}, errors.Wrap(err, "WaitForJob")
		}
	}
	locationURI := resultingSystem.Value().(string)
	loc, err := wmi.NewLocation(locationURI)
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "getting location")
	}
	result, err := loc.GetResult()
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "getting result")
	}
	id, err := result.GetProperty("Name")
	if err != nil {
		return VirtualSwitch{}, errors.Wrap(err, "get Name")
	}
	return m.GetVMSwitch(id.Value().(string))
}
func ( m * Manager ) CreateVMSwitch ( name string ) ( VirtualSwitch , error ) { data , err := m . con . Get ( VMSwitchSettings ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } swInstance , err := data . Get ( " " ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } if err := swInstance . Set ( " " , name ) ; err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } switchText , err := swInstance . GetText ( 1 ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } jobPath := ole . VARIANT { } resultingSystem := ole . VARIANT { } jobState , err := m . svc . Get ( " " , switchText , nil , nil , & resultingSystem , & jobPath ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } if jobState . Value ( ) . ( int32 ) == wmi . JobStatusStarted { err := wmi . WaitForJob ( jobPath . Value ( ) . ( string ) ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } } locationURI := resultingSystem . Value ( ) . ( string ) loc , err := wmi . NewLocation ( locationURI ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } result , err := loc . GetResult ( ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } id , err := result . GetProperty ( " " ) if err != nil { return VirtualSwitch { } , errors . Wrap ( err , " " ) } return m . GetVMSwitch ( id . Value ( ) . ( string ) ) }
CreateVMSwitch will create a new private VM Switch.
CreateVMSwitch will create a new private VM Switch .
// The resultingSystem value for DefineSystem is always a string containing the // location of the newly created resource