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	"context"
21
22	"github.com/vmware/govmomi/nfc"
23	"github.com/vmware/govmomi/vim25"
24	"github.com/vmware/govmomi/vim25/methods"
25	"github.com/vmware/govmomi/vim25/types"
26)
27
28type ResourcePool struct {
29	Common
30}
31
32func NewResourcePool(c *vim25.Client, ref types.ManagedObjectReference) *ResourcePool {
33	return &ResourcePool{
34		Common: NewCommon(c, ref),
35	}
36}
37
38func (p ResourcePool) ImportVApp(ctx context.Context, spec types.BaseImportSpec, folder *Folder, host *HostSystem) (*nfc.Lease, error) {
39	req := types.ImportVApp{
40		This: p.Reference(),
41		Spec: spec,
42	}
43
44	if folder != nil {
45		ref := folder.Reference()
46		req.Folder = &ref
47	}
48
49	if host != nil {
50		ref := host.Reference()
51		req.Host = &ref
52	}
53
54	res, err := methods.ImportVApp(ctx, p.c, &req)
55	if err != nil {
56		return nil, err
57	}
58
59	return nfc.NewLease(p.c, res.Returnval), nil
60}
61
62func (p ResourcePool) Create(ctx context.Context, name string, spec types.ResourceConfigSpec) (*ResourcePool, error) {
63	req := types.CreateResourcePool{
64		This: p.Reference(),
65		Name: name,
66		Spec: spec,
67	}
68
69	res, err := methods.CreateResourcePool(ctx, p.c, &req)
70	if err != nil {
71		return nil, err
72	}
73
74	return NewResourcePool(p.c, res.Returnval), nil
75}
76
77func (p ResourcePool) CreateVApp(ctx context.Context, name string, resSpec types.ResourceConfigSpec, configSpec types.VAppConfigSpec, folder *Folder) (*VirtualApp, error) {
78	req := types.CreateVApp{
79		This:       p.Reference(),
80		Name:       name,
81		ResSpec:    resSpec,
82		ConfigSpec: configSpec,
83	}
84
85	if folder != nil {
86		ref := folder.Reference()
87		req.VmFolder = &ref
88	}
89
90	res, err := methods.CreateVApp(ctx, p.c, &req)
91	if err != nil {
92		return nil, err
93	}
94
95	return NewVirtualApp(p.c, res.Returnval), nil
96}
97
98func (p ResourcePool) UpdateConfig(ctx context.Context, name string, config *types.ResourceConfigSpec) error {
99	req := types.UpdateConfig{
100		This:   p.Reference(),
101		Name:   name,
102		Config: config,
103	}
104
105	if config != nil && config.Entity == nil {
106		ref := p.Reference()
107
108		// Create copy of config so changes won't leak back to the caller
109		newConfig := *config
110		newConfig.Entity = &ref
111		req.Config = &newConfig
112	}
113
114	_, err := methods.UpdateConfig(ctx, p.c, &req)
115	return err
116}
117
118func (p ResourcePool) DestroyChildren(ctx context.Context) error {
119	req := types.DestroyChildren{
120		This: p.Reference(),
121	}
122
123	_, err := methods.DestroyChildren(ctx, p.c, &req)
124	return err
125}
126
127func (p ResourcePool) Destroy(ctx context.Context) (*Task, error) {
128	req := types.Destroy_Task{
129		This: p.Reference(),
130	}
131
132	res, err := methods.Destroy_Task(ctx, p.c, &req)
133	if err != nil {
134		return nil, err
135	}
136
137	return NewTask(p.c, res.Returnval), nil
138}
139