github.com/vmware/govmomi@v0.51.0/object/distributed_virtual_portgroup.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package object
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  
    11  	"github.com/vmware/govmomi/vim25"
    12  	"github.com/vmware/govmomi/vim25/methods"
    13  	"github.com/vmware/govmomi/vim25/mo"
    14  	"github.com/vmware/govmomi/vim25/types"
    15  )
    16  
    17  type DistributedVirtualPortgroup struct {
    18  	Common
    19  }
    20  
    21  func NewDistributedVirtualPortgroup(c *vim25.Client, ref types.ManagedObjectReference) *DistributedVirtualPortgroup {
    22  	return &DistributedVirtualPortgroup{
    23  		Common: NewCommon(c, ref),
    24  	}
    25  }
    26  
    27  func (p DistributedVirtualPortgroup) GetInventoryPath() string {
    28  	return p.InventoryPath
    29  }
    30  
    31  // EthernetCardBackingInfo returns the VirtualDeviceBackingInfo for this DistributedVirtualPortgroup
    32  func (p DistributedVirtualPortgroup) EthernetCardBackingInfo(ctx context.Context) (types.BaseVirtualDeviceBackingInfo, error) {
    33  	var dvp mo.DistributedVirtualPortgroup
    34  	var dvs mo.DistributedVirtualSwitch
    35  	prop := "config.distributedVirtualSwitch"
    36  
    37  	if err := p.Properties(ctx, p.Reference(), []string{"key", prop}, &dvp); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	// From the docs at https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/vim.dvs.DistributedVirtualPortgroup.ConfigInfo.html:
    42  	// "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."
    43  	// Note that "the object" refers to the Switch, not the PortGroup.
    44  	if dvp.Config.DistributedVirtualSwitch == nil {
    45  		name := p.InventoryPath
    46  		if name == "" {
    47  			name = p.Reference().String()
    48  		}
    49  		return nil, fmt.Errorf("failed to create EthernetCardBackingInfo for %s: System.Read privilege required for %s", name, prop)
    50  	}
    51  
    52  	if err := p.Properties(ctx, *dvp.Config.DistributedVirtualSwitch, []string{"uuid"}, &dvs); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	backing := &types.VirtualEthernetCardDistributedVirtualPortBackingInfo{
    57  		Port: types.DistributedVirtualSwitchPortConnection{
    58  			PortgroupKey: dvp.Key,
    59  			SwitchUuid:   dvs.Uuid,
    60  		},
    61  	}
    62  
    63  	return backing, nil
    64  }
    65  
    66  func (p DistributedVirtualPortgroup) Reconfigure(ctx context.Context, spec types.DVPortgroupConfigSpec) (*Task, error) {
    67  	req := types.ReconfigureDVPortgroup_Task{
    68  		This: p.Reference(),
    69  		Spec: spec,
    70  	}
    71  
    72  	res, err := methods.ReconfigureDVPortgroup_Task(ctx, p.Client(), &req)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	return NewTask(p.Client(), res.Returnval), nil
    78  }