github.com/vmware/govmomi@v0.37.2/object/opaque_network.go (about)

     1  /*
     2  Copyright (c) 2017 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/mo"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  type OpaqueNetwork struct {
    29  	Common
    30  }
    31  
    32  func NewOpaqueNetwork(c *vim25.Client, ref types.ManagedObjectReference) *OpaqueNetwork {
    33  	return &OpaqueNetwork{
    34  		Common: NewCommon(c, ref),
    35  	}
    36  }
    37  
    38  func (n OpaqueNetwork) GetInventoryPath() string {
    39  	return n.InventoryPath
    40  }
    41  
    42  // EthernetCardBackingInfo returns the VirtualDeviceBackingInfo for this Network
    43  func (n OpaqueNetwork) EthernetCardBackingInfo(ctx context.Context) (types.BaseVirtualDeviceBackingInfo, error) {
    44  	summary, err := n.Summary(ctx)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	backing := &types.VirtualEthernetCardOpaqueNetworkBackingInfo{
    50  		OpaqueNetworkId:   summary.OpaqueNetworkId,
    51  		OpaqueNetworkType: summary.OpaqueNetworkType,
    52  	}
    53  
    54  	return backing, nil
    55  }
    56  
    57  // Summary returns the mo.OpaqueNetwork.Summary property
    58  func (n OpaqueNetwork) Summary(ctx context.Context) (*types.OpaqueNetworkSummary, error) {
    59  	var props mo.OpaqueNetwork
    60  
    61  	err := n.Properties(ctx, n.Reference(), []string{"summary"}, &props)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	summary, ok := props.Summary.(*types.OpaqueNetworkSummary)
    67  	if !ok {
    68  		return nil, fmt.Errorf("%s unsupported network summary type: %T", n, props.Summary)
    69  	}
    70  
    71  	return summary, nil
    72  }