github.com/vmware/govmomi@v0.43.0/simulator/example_test.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 simulator_test 18 19 import ( 20 "context" 21 "fmt" 22 "log" 23 24 "github.com/vmware/govmomi" 25 "github.com/vmware/govmomi/find" 26 "github.com/vmware/govmomi/object" 27 "github.com/vmware/govmomi/session" 28 "github.com/vmware/govmomi/simulator" 29 "github.com/vmware/govmomi/vim25" 30 "github.com/vmware/govmomi/vim25/mo" 31 "github.com/vmware/govmomi/vim25/types" 32 ) 33 34 // Example boilerplate for starting a simulator initialized with an ESX model. 35 func ExampleESX() { 36 ctx := context.Background() 37 38 // ESXi model + initial set of objects (VMs, network, datastore) 39 model := simulator.ESX() 40 41 defer model.Remove() 42 err := model.Create() 43 if err != nil { 44 log.Fatal(err) 45 } 46 47 s := model.Service.NewServer() 48 defer s.Close() 49 50 c, _ := govmomi.NewClient(ctx, s.URL, true) 51 52 fmt.Printf("%s with %d host", c.Client.ServiceContent.About.ApiType, model.Count().Host) 53 // Output: HostAgent with 1 host 54 } 55 56 // Example for starting a simulator with empty inventory, similar to a fresh install of vCenter. 57 func ExampleModel() { 58 ctx := context.Background() 59 60 model := simulator.VPX() 61 model.Datacenter = 0 // No DC == no inventory 62 63 defer model.Remove() 64 err := model.Create() 65 if err != nil { 66 log.Fatal(err) 67 } 68 69 s := model.Service.NewServer() 70 defer s.Close() 71 72 c, _ := govmomi.NewClient(ctx, s.URL, true) 73 74 fmt.Printf("%s with %d hosts", c.Client.ServiceContent.About.ApiType, model.Count().Host) 75 // Output: VirtualCenter with 0 hosts 76 } 77 78 // Example boilerplate for starting a simulator initialized with a vCenter model. 79 func ExampleVPX() { 80 ctx := context.Background() 81 82 // vCenter model + initial set of objects (cluster, hosts, VMs, network, datastore, etc) 83 model := simulator.VPX() 84 85 defer model.Remove() 86 err := model.Create() 87 if err != nil { 88 log.Fatal(err) 89 } 90 91 s := model.Service.NewServer() 92 defer s.Close() 93 94 c, _ := govmomi.NewClient(ctx, s.URL, true) 95 96 fmt.Printf("%s with %d hosts", c.Client.ServiceContent.About.ApiType, model.Count().Host) 97 // Output: VirtualCenter with 4 hosts 98 } 99 100 // Run simplifies startup/cleanup of a simulator instance for example or testing purposes. 101 func ExampleModel_Run() { 102 err := simulator.VPX().Run(func(ctx context.Context, c *vim25.Client) error { 103 // Client has connected and logged in to a new simulator instance. 104 // Server.Close and Model.Remove are called when this func returns. 105 s, err := session.NewManager(c).UserSession(ctx) 106 if err != nil { 107 return err 108 } 109 fmt.Print(s.UserName) 110 return nil 111 }) 112 if err != nil { 113 log.Fatal(err) 114 } 115 // Output: user 116 } 117 118 // Test simplifies startup/cleanup of a simulator instance for testing purposes. 119 func ExampleTest() { 120 simulator.Test(func(ctx context.Context, c *vim25.Client) { 121 // Client has connected and logged in to a new simulator instance. 122 // Server.Close and Model.Remove are called when this func returns. 123 s, err := session.NewManager(c).UserSession(ctx) 124 if err != nil { 125 log.Fatal(err) 126 } 127 fmt.Print(s.UserName) 128 }) 129 // Output: user 130 } 131 132 // Folder.AddOpaqueNetwork can be used to create an NSX backed OpaqueNetwork. 133 func ExampleFolder_AddOpaqueNetwork() { 134 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 135 finder := find.NewFinder(c) 136 137 // Find the network folder via vSphere API 138 obj, err := finder.Folder(ctx, "network") 139 if err != nil { 140 return err 141 } 142 143 // Get vcsim's network Folder object 144 folder := simulator.Map.Get(obj.Reference()).(*simulator.Folder) 145 146 spec := types.OpaqueNetworkSummary{ 147 NetworkSummary: types.NetworkSummary{ 148 Name: "my-nsx-network", 149 }, 150 OpaqueNetworkId: "my-nsx-id", 151 OpaqueNetworkType: "nsx.LogicalSwitch", 152 } 153 154 // Add NSX backed OpaqueNetwork, calling the simulator.Folder method directly. 155 err = folder.AddOpaqueNetwork(simulator.SpoofContext(), spec) 156 if err != nil { 157 return err 158 } 159 160 // Find the OpaqueNetwork via vSphere API 161 net, err := finder.Network(ctx, spec.Name) 162 if err != nil { 163 return err 164 } 165 166 nsx := net.(*object.OpaqueNetwork) 167 summary, err := nsx.Summary(ctx) 168 if err != nil { 169 return err 170 } 171 172 // The summary fields should match those of the spec used to create it 173 fmt.Printf("%s: %s", nsx.Name(), summary.OpaqueNetworkId) 174 return nil 175 }) 176 // Output: my-nsx-network: my-nsx-id 177 } 178 179 // AddDVPortgroup against vcsim can create both standard and nsx backed DistributedVirtualPortgroup networks 180 func ExampleDistributedVirtualSwitch_AddDVPortgroupTask() { 181 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 182 finder := find.NewFinder(c) 183 184 dvs0, err := finder.Network(ctx, "DVS0") 185 if err != nil { 186 return err 187 } 188 189 spec := types.DVPortgroupConfigSpec{ 190 Name: "my-nsx-dvpg", 191 LogicalSwitchUuid: "my-nsx-id", 192 } 193 194 dvs := dvs0.(*object.DistributedVirtualSwitch) 195 task, err := dvs.AddPortgroup(ctx, []types.DVPortgroupConfigSpec{spec}) 196 if err != nil { 197 return err 198 } 199 if err = task.Wait(ctx); err != nil { 200 return err 201 } 202 203 pg0, err := finder.Network(ctx, spec.Name) 204 if err != nil { 205 return err 206 } 207 208 pg := pg0.(*object.DistributedVirtualPortgroup) 209 210 var props mo.DistributedVirtualPortgroup 211 err = pg.Properties(ctx, pg.Reference(), []string{"config"}, &props) 212 if err != nil { 213 return err 214 } 215 216 fmt.Printf("%s: %s %s", pg.Name(), props.Config.BackingType, props.Config.LogicalSwitchUuid) 217 218 return nil 219 }) 220 // Output: my-nsx-dvpg: nsx my-nsx-id 221 }