1/*
2Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package object
18
19import (
20	"context"
21
22	"github.com/vmware/govmomi/vim25"
23	"github.com/vmware/govmomi/vim25/mo"
24	"github.com/vmware/govmomi/vim25/types"
25)
26
27type Network struct {
28	Common
29}
30
31func NewNetwork(c *vim25.Client, ref types.ManagedObjectReference) *Network {
32	return &Network{
33		Common: NewCommon(c, ref),
34	}
35}
36
37// EthernetCardBackingInfo returns the VirtualDeviceBackingInfo for this Network
38func (n Network) EthernetCardBackingInfo(ctx context.Context) (types.BaseVirtualDeviceBackingInfo, error) {
39	var e mo.Network
40
41	// Use Network.Name rather than Common.Name as the latter does not return the complete name if it contains a '/'
42	// We can't use Common.ObjectName here either as we need the ManagedEntity.Name field is not set since mo.Network
43	// has its own Name field.
44	err := n.Properties(ctx, n.Reference(), []string{"name"}, &e)
45	if err != nil {
46		return nil, err
47	}
48
49	backing := &types.VirtualEthernetCardNetworkBackingInfo{
50		VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
51			DeviceName: e.Name,
52		},
53	}
54
55	return backing, nil
56}
57