github.com/vmware/govmomi@v0.43.0/govc/flags/resource_pool.go (about)

     1  /*
     2  Copyright (c) 2014-2016 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 flags
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  
    25  	"github.com/vmware/govmomi/find"
    26  	"github.com/vmware/govmomi/object"
    27  )
    28  
    29  type ResourcePoolFlag struct {
    30  	common
    31  
    32  	*DatacenterFlag
    33  
    34  	name string
    35  	pool *object.ResourcePool
    36  }
    37  
    38  var resourcePoolFlagKey = flagKey("resourcePool")
    39  
    40  func NewResourcePoolFlag(ctx context.Context) (*ResourcePoolFlag, context.Context) {
    41  	if v := ctx.Value(resourcePoolFlagKey); v != nil {
    42  		return v.(*ResourcePoolFlag), ctx
    43  	}
    44  
    45  	v := &ResourcePoolFlag{}
    46  	v.DatacenterFlag, ctx = NewDatacenterFlag(ctx)
    47  	ctx = context.WithValue(ctx, resourcePoolFlagKey, v)
    48  	return v, ctx
    49  }
    50  
    51  func (flag *ResourcePoolFlag) Register(ctx context.Context, f *flag.FlagSet) {
    52  	flag.RegisterOnce(func() {
    53  		flag.DatacenterFlag.Register(ctx, f)
    54  
    55  		env := "GOVC_RESOURCE_POOL"
    56  		value := os.Getenv(env)
    57  		usage := fmt.Sprintf("Resource pool [%s]", env)
    58  		f.StringVar(&flag.name, "pool", value, usage)
    59  	})
    60  }
    61  
    62  func (flag *ResourcePoolFlag) Process(ctx context.Context) error {
    63  	return flag.ProcessOnce(func() error {
    64  		if err := flag.DatacenterFlag.Process(ctx); err != nil {
    65  			return err
    66  		}
    67  		return nil
    68  	})
    69  }
    70  
    71  func (flag *ResourcePoolFlag) IsSet() bool {
    72  	return flag.name != ""
    73  }
    74  
    75  func (flag *ResourcePoolFlag) ResourcePool() (*object.ResourcePool, error) {
    76  	if flag.pool != nil {
    77  		return flag.pool, nil
    78  	}
    79  
    80  	finder, err := flag.Finder()
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	flag.pool, err = finder.ResourcePoolOrDefault(context.TODO(), flag.name)
    86  	if err != nil {
    87  		if _, ok := err.(*find.NotFoundError); ok {
    88  			vapp, verr := finder.VirtualApp(context.TODO(), flag.name)
    89  			if verr != nil {
    90  				return nil, err
    91  			}
    92  			flag.pool = vapp.ResourcePool
    93  		} else {
    94  			return nil, err
    95  		}
    96  	}
    97  
    98  	return flag.pool, nil
    99  }
   100  
   101  func (flag *ResourcePoolFlag) ResourcePoolIfSpecified() (*object.ResourcePool, error) {
   102  	if flag.name == "" {
   103  		return nil, nil
   104  	}
   105  	return flag.ResourcePool()
   106  }