github.com/vmware/govmomi@v0.37.1/object/resource_pool.go (about)

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