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/methods"
24	"github.com/vmware/govmomi/vim25/mo"
25	"github.com/vmware/govmomi/vim25/types"
26)
27
28type HostVirtualNicManager struct {
29	Common
30	Host *HostSystem
31}
32
33func NewHostVirtualNicManager(c *vim25.Client, ref types.ManagedObjectReference, host types.ManagedObjectReference) *HostVirtualNicManager {
34	return &HostVirtualNicManager{
35		Common: NewCommon(c, ref),
36		Host:   NewHostSystem(c, host),
37	}
38}
39
40func (m HostVirtualNicManager) Info(ctx context.Context) (*types.HostVirtualNicManagerInfo, error) {
41	var vnm mo.HostVirtualNicManager
42
43	err := m.Properties(ctx, m.Reference(), []string{"info"}, &vnm)
44	if err != nil {
45		return nil, err
46	}
47
48	return &vnm.Info, nil
49}
50
51func (m HostVirtualNicManager) DeselectVnic(ctx context.Context, nicType string, device string) error {
52	if nicType == string(types.HostVirtualNicManagerNicTypeVsan) {
53		// Avoid fault.NotSupported:
54		// "Error deselecting device '$device': VSAN interfaces must be deselected using vim.host.VsanSystem"
55		s, err := m.Host.ConfigManager().VsanSystem(ctx)
56		if err != nil {
57			return err
58		}
59
60		return s.updateVnic(ctx, device, false)
61	}
62
63	req := types.DeselectVnicForNicType{
64		This:    m.Reference(),
65		NicType: nicType,
66		Device:  device,
67	}
68
69	_, err := methods.DeselectVnicForNicType(ctx, m.Client(), &req)
70	return err
71}
72
73func (m HostVirtualNicManager) SelectVnic(ctx context.Context, nicType string, device string) error {
74	if nicType == string(types.HostVirtualNicManagerNicTypeVsan) {
75		// Avoid fault.NotSupported:
76		// "Error selecting device '$device': VSAN interfaces must be selected using vim.host.VsanSystem"
77		s, err := m.Host.ConfigManager().VsanSystem(ctx)
78		if err != nil {
79			return err
80		}
81
82		return s.updateVnic(ctx, device, true)
83	}
84
85	req := types.SelectVnicForNicType{
86		This:    m.Reference(),
87		NicType: nicType,
88		Device:  device,
89	}
90
91	_, err := methods.SelectVnicForNicType(ctx, m.Client(), &req)
92	return err
93}
94