github.com/vmware/govmomi@v0.43.0/examples/networks/main.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 main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/vmware/govmomi/examples"
    24  	"github.com/vmware/govmomi/view"
    25  	"github.com/vmware/govmomi/vim25"
    26  	"github.com/vmware/govmomi/vim25/mo"
    27  )
    28  
    29  func main() {
    30  	examples.Run(func(ctx context.Context, c *vim25.Client) error {
    31  		// Create a view of Network types
    32  		m := view.NewManager(c)
    33  
    34  		v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"Network"}, true)
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		defer v.Destroy(ctx)
    40  
    41  		// Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.Network.html
    42  		var networks []mo.Network
    43  		err = v.Retrieve(ctx, []string{"Network"}, nil, &networks)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		for _, net := range networks {
    49  			fmt.Printf("%s: %s\n", net.Name, net.Reference())
    50  		}
    51  
    52  		return nil
    53  	})
    54  }