1/* 2Copyright (c) 2015 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package object 18 19import ( 20 "fmt" 21 22 "github.com/vmware/govmomi/vim25" 23 "github.com/vmware/govmomi/vim25/methods" 24 "github.com/vmware/govmomi/vim25/mo" 25 "github.com/vmware/govmomi/vim25/types" 26 "golang.org/x/net/context" 27) 28 29type ResourcePool struct { 30 Common 31 32 InventoryPath string 33} 34 35func (p ResourcePool) String() string { 36 if p.InventoryPath == "" { 37 return p.Common.String() 38 } 39 return fmt.Sprintf("%v @ %v", p.Common, p.InventoryPath) 40} 41 42func NewResourcePool(c *vim25.Client, ref types.ManagedObjectReference) *ResourcePool { 43 return &ResourcePool{ 44 Common: NewCommon(c, ref), 45 } 46} 47 48func (p ResourcePool) Name(ctx context.Context) (string, error) { 49 var o mo.ResourcePool 50 51 err := p.Properties(ctx, p.Reference(), []string{"name"}, &o) 52 if err != nil { 53 return "", err 54 } 55 56 return o.Name, nil 57} 58 59func (p ResourcePool) ImportVApp(ctx context.Context, spec types.BaseImportSpec, folder *Folder, host *HostSystem) (*HttpNfcLease, error) { 60 req := types.ImportVApp{ 61 This: p.Reference(), 62 Spec: spec, 63 } 64 65 if folder != nil { 66 ref := folder.Reference() 67 req.Folder = &ref 68 } 69 70 if host != nil { 71 ref := host.Reference() 72 req.Host = &ref 73 } 74 75 res, err := methods.ImportVApp(ctx, p.c, &req) 76 if err != nil { 77 return nil, err 78 } 79 80 return NewHttpNfcLease(p.c, res.Returnval), nil 81} 82 83func (p ResourcePool) Create(ctx context.Context, name string, spec types.ResourceConfigSpec) (*ResourcePool, error) { 84 req := types.CreateResourcePool{ 85 This: p.Reference(), 86 Name: name, 87 Spec: spec, 88 } 89 90 res, err := methods.CreateResourcePool(ctx, p.c, &req) 91 if err != nil { 92 return nil, err 93 } 94 95 return NewResourcePool(p.c, res.Returnval), nil 96} 97 98func (p ResourcePool) CreateVApp(ctx context.Context, name string, resSpec types.ResourceConfigSpec, configSpec types.VAppConfigSpec, folder *Folder) (*VirtualApp, error) { 99 req := types.CreateVApp{ 100 This: p.Reference(), 101 Name: name, 102 ResSpec: resSpec, 103 ConfigSpec: configSpec, 104 } 105 106 if folder != nil { 107 ref := folder.Reference() 108 req.VmFolder = &ref 109 } 110 111 res, err := methods.CreateVApp(ctx, p.c, &req) 112 if err != nil { 113 return nil, err 114 } 115 116 return NewVirtualApp(p.c, res.Returnval), nil 117} 118 119func (p ResourcePool) UpdateConfig(ctx context.Context, name string, config *types.ResourceConfigSpec) error { 120 req := types.UpdateConfig{ 121 This: p.Reference(), 122 Name: name, 123 Config: config, 124 } 125 126 if config != nil && config.Entity == nil { 127 ref := p.Reference() 128 129 // Create copy of config so changes won't leak back to the caller 130 newConfig := *config 131 newConfig.Entity = &ref 132 req.Config = &newConfig 133 } 134 135 _, err := methods.UpdateConfig(ctx, p.c, &req) 136 return err 137} 138 139func (p ResourcePool) DestroyChildren(ctx context.Context) error { 140 req := types.DestroyChildren{ 141 This: p.Reference(), 142 } 143 144 _, err := methods.DestroyChildren(ctx, p.c, &req) 145 return err 146} 147 148func (p ResourcePool) Destroy(ctx context.Context) (*Task, error) { 149 req := types.Destroy_Task{ 150 This: p.Reference(), 151 } 152 153 res, err := methods.Destroy_Task(ctx, p.c, &req) 154 if err != nil { 155 return nil, err 156 } 157 158 return NewTask(p.c, res.Returnval), nil 159} 160