github.com/vmware/govmomi@v0.43.0/object/distributed_virtual_portgroup.go (about) 1 /* 2 Copyright (c) 2015 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package object 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/vmware/govmomi/vim25" 24 "github.com/vmware/govmomi/vim25/methods" 25 "github.com/vmware/govmomi/vim25/mo" 26 "github.com/vmware/govmomi/vim25/types" 27 ) 28 29 type DistributedVirtualPortgroup struct { 30 Common 31 } 32 33 func NewDistributedVirtualPortgroup(c *vim25.Client, ref types.ManagedObjectReference) *DistributedVirtualPortgroup { 34 return &DistributedVirtualPortgroup{ 35 Common: NewCommon(c, ref), 36 } 37 } 38 39 func (p DistributedVirtualPortgroup) GetInventoryPath() string { 40 return p.InventoryPath 41 } 42 43 // EthernetCardBackingInfo returns the VirtualDeviceBackingInfo for this DistributedVirtualPortgroup 44 func (p DistributedVirtualPortgroup) EthernetCardBackingInfo(ctx context.Context) (types.BaseVirtualDeviceBackingInfo, error) { 45 var dvp mo.DistributedVirtualPortgroup 46 var dvs mo.DistributedVirtualSwitch 47 prop := "config.distributedVirtualSwitch" 48 49 if err := p.Properties(ctx, p.Reference(), []string{"key", prop}, &dvp); err != nil { 50 return nil, err 51 } 52 53 // From the docs at https://code.vmware.com/apis/196/vsphere/doc/vim.dvs.DistributedVirtualPortgroup.ConfigInfo.html: 54 // "This property should always be set unless the user's setting does not have System.Read privilege on the object referred to by this property." 55 // Note that "the object" refers to the Switch, not the PortGroup. 56 if dvp.Config.DistributedVirtualSwitch == nil { 57 name := p.InventoryPath 58 if name == "" { 59 name = p.Reference().String() 60 } 61 return nil, fmt.Errorf("failed to create EthernetCardBackingInfo for %s: System.Read privilege required for %s", name, prop) 62 } 63 64 if err := p.Properties(ctx, *dvp.Config.DistributedVirtualSwitch, []string{"uuid"}, &dvs); err != nil { 65 return nil, err 66 } 67 68 backing := &types.VirtualEthernetCardDistributedVirtualPortBackingInfo{ 69 Port: types.DistributedVirtualSwitchPortConnection{ 70 PortgroupKey: dvp.Key, 71 SwitchUuid: dvs.Uuid, 72 }, 73 } 74 75 return backing, nil 76 } 77 78 func (p DistributedVirtualPortgroup) Reconfigure(ctx context.Context, spec types.DVPortgroupConfigSpec) (*Task, error) { 79 req := types.ReconfigureDVPortgroup_Task{ 80 This: p.Reference(), 81 Spec: spec, 82 } 83 84 res, err := methods.ReconfigureDVPortgroup_Task(ctx, p.Client(), &req) 85 if err != nil { 86 return nil, err 87 } 88 89 return NewTask(p.Client(), res.Returnval), nil 90 }