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/types"
25)
26
27type DistributedVirtualSwitch struct {
28	Common
29}
30
31func NewDistributedVirtualSwitch(c *vim25.Client, ref types.ManagedObjectReference) *DistributedVirtualSwitch {
32	return &DistributedVirtualSwitch{
33		Common: NewCommon(c, ref),
34	}
35}
36
37func (s DistributedVirtualSwitch) EthernetCardBackingInfo(ctx context.Context) (types.BaseVirtualDeviceBackingInfo, error) {
38	return nil, ErrNotSupported // TODO: just to satisfy NetworkReference interface for the finder
39}
40
41func (s DistributedVirtualSwitch) Reconfigure(ctx context.Context, spec types.BaseDVSConfigSpec) (*Task, error) {
42	req := types.ReconfigureDvs_Task{
43		This: s.Reference(),
44		Spec: spec,
45	}
46
47	res, err := methods.ReconfigureDvs_Task(ctx, s.Client(), &req)
48	if err != nil {
49		return nil, err
50	}
51
52	return NewTask(s.Client(), res.Returnval), nil
53}
54
55func (s DistributedVirtualSwitch) AddPortgroup(ctx context.Context, spec []types.DVPortgroupConfigSpec) (*Task, error) {
56	req := types.AddDVPortgroup_Task{
57		This: s.Reference(),
58		Spec: spec,
59	}
60
61	res, err := methods.AddDVPortgroup_Task(ctx, s.Client(), &req)
62	if err != nil {
63		return nil, err
64	}
65
66	return NewTask(s.Client(), res.Returnval), nil
67}
68
69func (s DistributedVirtualSwitch) FetchDVPorts(ctx context.Context, criteria *types.DistributedVirtualSwitchPortCriteria) ([]types.DistributedVirtualPort, error) {
70	req := &types.FetchDVPorts{
71		This:     s.Reference(),
72		Criteria: criteria,
73	}
74
75	res, err := methods.FetchDVPorts(ctx, s.Client(), req)
76	if err != nil {
77		return nil, err
78	}
79	return res.Returnval, nil
80}
81