2019-06-13 09:56:14 -04:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2020-02-16 09:08:28 -05:00
|
|
|
"fmt"
|
|
|
|
|
2019-06-13 09:56:14 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Network struct {
|
2020-08-31 04:34:41 -04:00
|
|
|
driver *VCenterDriver
|
2020-02-16 09:08:28 -05:00
|
|
|
network object.NetworkReference
|
2019-06-13 09:56:14 -04:00
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func (d *VCenterDriver) NewNetwork(ref *types.ManagedObjectReference) *Network {
|
2019-06-13 09:56:14 -04:00
|
|
|
return &Network{
|
|
|
|
network: object.NewNetwork(d.client.Client, *ref),
|
|
|
|
driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func (d *VCenterDriver) FindNetwork(name string) (*Network, error) {
|
2019-06-13 09:56:14 -04:00
|
|
|
n, err := d.finder.Network(d.ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Network{
|
2020-02-16 09:08:28 -05:00
|
|
|
network: n,
|
2019-06-13 09:56:14 -04:00
|
|
|
driver: d,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func (d *VCenterDriver) FindNetworks(name string) ([]*Network, error) {
|
2020-07-09 09:59:15 -04:00
|
|
|
ns, err := d.finder.NetworkList(d.ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var networks []*Network
|
|
|
|
for _, n := range ns {
|
|
|
|
networks = append(networks, &Network{
|
|
|
|
network: n,
|
|
|
|
driver: d,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return networks, nil
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 -04:00
|
|
|
func (n *Network) Info(params ...string) (*mo.Network, error) {
|
|
|
|
var p []string
|
|
|
|
if len(params) == 0 {
|
|
|
|
p = []string{"*"}
|
|
|
|
} else {
|
|
|
|
p = params
|
|
|
|
}
|
|
|
|
var info mo.Network
|
2020-02-16 09:08:28 -05:00
|
|
|
|
|
|
|
network, ok := n.network.(*object.Network)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected %t network object type", n.network)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := network.Properties(n.driver.ctx, network.Reference(), p, &info)
|
2019-06-13 09:56:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
2020-07-09 09:59:15 -04:00
|
|
|
|
|
|
|
type MultipleNetworkFoundError struct {
|
|
|
|
path string
|
|
|
|
append string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MultipleNetworkFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("path '%s' resolves to multiple networks. %s", e.path, e.append)
|
|
|
|
}
|