1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package simulator
import (
"reflect"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)
type ViewManager struct {
mo.ViewManager
entities map[string]bool
}
var entities = []struct {
Type reflect.Type
Container bool
}{
{reflect.TypeOf((*mo.ManagedEntity)(nil)).Elem(), true},
{reflect.TypeOf((*mo.Folder)(nil)).Elem(), true},
{reflect.TypeOf((*mo.StoragePod)(nil)).Elem(), true},
{reflect.TypeOf((*mo.Datacenter)(nil)).Elem(), true},
{reflect.TypeOf((*mo.ComputeResource)(nil)).Elem(), true},
{reflect.TypeOf((*mo.ClusterComputeResource)(nil)).Elem(), true},
{reflect.TypeOf((*mo.HostSystem)(nil)).Elem(), true},
{reflect.TypeOf((*mo.ResourcePool)(nil)).Elem(), true},
{reflect.TypeOf((*mo.VirtualApp)(nil)).Elem(), true},
{reflect.TypeOf((*mo.VirtualMachine)(nil)).Elem(), false},
{reflect.TypeOf((*mo.Datastore)(nil)).Elem(), false},
{reflect.TypeOf((*mo.Network)(nil)).Elem(), false},
{reflect.TypeOf((*mo.OpaqueNetwork)(nil)).Elem(), false},
{reflect.TypeOf((*mo.DistributedVirtualPortgroup)(nil)).Elem(), false},
{reflect.TypeOf((*mo.DistributedVirtualSwitch)(nil)).Elem(), false},
{reflect.TypeOf((*mo.VmwareDistributedVirtualSwitch)(nil)).Elem(), false},
}
func (m *ViewManager) init(*Registry) {
m.entities = make(map[string]bool, len(entities))
for _, e := range entities {
m.entities[e.Type.Name()] = e.Container
}
}
func destroyView(ref types.ManagedObjectReference) soap.HasFault {
return &methods.DestroyViewBody{
Res: &types.DestroyViewResponse{},
}
}
func (m *ViewManager) CreateContainerView(ctx *Context, req *types.CreateContainerView) soap.HasFault {
body := &methods.CreateContainerViewBody{}
root := Map.Get(req.Container)
if root == nil {
body.Fault_ = Fault("", &types.ManagedObjectNotFound{Obj: req.Container})
return body
}
if !m.entities[root.Reference().Type] {
body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "container"})
return body
}
container := &ContainerView{
mo.ContainerView{
Container: root.Reference(),
Recursive: req.Recursive,
Type: req.Type,
},
root,
make(map[string]bool),
}
for _, ctype := range container.Type {
if _, ok := m.entities[ctype]; !ok {
body.Fault_ = Fault("", &types.InvalidArgument{InvalidProperty: "type"})
return body
}
container.types[ctype] = true
for _, e := range entities {
// Check for embedded types
if f, ok := e.Type.FieldByName(ctype); ok && f.Anonymous {
container.types[e.Type.Name()] = true
}
}
}
ctx.Session.setReference(container)
body.Res = &types.CreateContainerViewResponse{
Returnval: container.Self,
}
seen := make(map[types.ManagedObjectReference]bool)
container.add(root, seen)
ctx.Session.Registry.Put(container)
ctx.Map.AddHandler(container)
return body
}
type ContainerView struct {
mo.ContainerView
root mo.Reference
types map[string]bool
}
func (v *ContainerView) DestroyView(ctx *Context, c *types.DestroyView) soap.HasFault {
ctx.Map.RemoveHandler(v)
ctx.Session.Remove(c.This)
return destroyView(c.This)
}
func (v *ContainerView) include(o types.ManagedObjectReference) bool {
if len(v.types) == 0 {
return true
}
return v.types[o.Type]
}
func walk(root mo.Reference, f func(child types.ManagedObjectReference)) {
if _, ok := root.(types.ManagedObjectReference); ok || root == nil {
return
}
var children []types.ManagedObjectReference
switch e := getManagedObject(root).Addr().Interface().(type) {
case *mo.Datacenter:
children = []types.ManagedObjectReference{e.VmFolder, e.HostFolder, e.DatastoreFolder, e.NetworkFolder}
case *mo.Folder:
children = e.ChildEntity
case *mo.ComputeResource:
children = e.Host
children = append(children, *e.ResourcePool)
case *mo.ClusterComputeResource:
children = e.Host
children = append(children, *e.ResourcePool)
case *mo.ResourcePool:
children = e.ResourcePool
children = append(children, e.Vm...)
case *mo.VirtualApp:
children = e.ResourcePool.ResourcePool
children = append(children, e.Vm...)
case *mo.HostSystem:
children = e.Vm
}
for _, child := range children {
f(child)
}
}
func (v *ContainerView) add(root mo.Reference, seen map[types.ManagedObjectReference]bool) {
walk(root, func(child types.ManagedObjectReference) {
if v.include(child) {
if !seen[child] {
seen[child] = true
v.View = append(v.View, child)
}
}
if v.Recursive {
v.add(Map.Get(child), seen)
}
})
}
func (v *ContainerView) find(root mo.Reference, ref types.ManagedObjectReference, found *bool) bool {
walk(root, func(child types.ManagedObjectReference) {
if *found {
return
}
if child == ref {
*found = true
return
}
if v.Recursive {
*found = v.find(Map.Get(child), ref, found)
}
})
return *found
}
func (v *ContainerView) PutObject(obj mo.Reference) {
ref := obj.Reference()
if v.include(ref) && v.find(v.root, ref, types.NewBool(false)) {
Map.Update(v, []types.PropertyChange{{Name: "view", Val: append(v.View, ref)}})
}
}
func (v *ContainerView) RemoveObject(obj types.ManagedObjectReference) {
Map.RemoveReference(v, &v.View, obj)
}
func (*ContainerView) UpdateObject(mo.Reference, []types.PropertyChange) {}
func (m *ViewManager) CreateListView(ctx *Context, req *types.CreateListView) soap.HasFault {
body := new(methods.CreateListViewBody)
list := new(ListView)
if err := list.add(req.Obj); err != nil {
body.Fault_ = Fault("", err)
return body
}
ctx.Session.Put(list)
body.Res = &types.CreateListViewResponse{
Returnval: list.Self,
}
return body
}
type ListView struct {
mo.ListView
}
func (v *ListView) update() {
Map.Update(v, []types.PropertyChange{{Name: "view", Val: v.View}})
}
func (v *ListView) add(refs []types.ManagedObjectReference) *types.ManagedObjectNotFound {
for _, ref := range refs {
obj := Map.Get(ref)
if obj == nil {
return &types.ManagedObjectNotFound{Obj: ref}
}
v.View = append(v.View, ref)
}
return nil
}
func (v *ListView) DestroyView(ctx *Context, c *types.DestroyView) soap.HasFault {
ctx.Session.Remove(c.This)
return destroyView(c.This)
}
func (v *ListView) ModifyListView(req *types.ModifyListView) soap.HasFault {
body := new(methods.ModifyListViewBody)
for _, ref := range req.Remove {
RemoveReference(&v.View, ref)
}
if err := v.add(req.Add); err != nil {
body.Fault_ = Fault("", err)
return body
}
body.Res = new(types.ModifyListViewResponse)
if len(req.Remove) != 0 || len(req.Add) != 0 {
v.update()
}
return body
}
func (v *ListView) ResetListView(req *types.ResetListView) soap.HasFault {
body := new(methods.ResetListViewBody)
v.View = nil
if err := v.add(req.Obj); err != nil {
body.Fault_ = Fault("", err)
return body
}
body.Res = new(types.ResetListViewResponse)
v.update()
return body
}
|