github.com/vmware/govmomi@v0.43.0/govc/pool/create.go (about)

     1  /*
     2  Copyright (c) 2015-2017 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 pool
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"path"
    24  
    25  	"github.com/vmware/govmomi/find"
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  )
    29  
    30  type create struct {
    31  	*flags.DatacenterFlag
    32  	*ResourceConfigSpecFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("pool.create", &create{})
    37  }
    38  
    39  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
    41  	cmd.DatacenterFlag.Register(ctx, f)
    42  
    43  	cmd.ResourceConfigSpecFlag = NewResourceConfigSpecFlag()
    44  	cmd.ResourceConfigSpecFlag.Register(ctx, f)
    45  }
    46  
    47  func (cmd *create) Process(ctx context.Context) error {
    48  	if err := cmd.DatacenterFlag.Process(ctx); err != nil {
    49  		return err
    50  	}
    51  	if err := cmd.ResourceConfigSpecFlag.Process(ctx); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  func (cmd *create) Usage() string {
    58  	return "POOL..."
    59  }
    60  
    61  func (cmd *create) Description() string {
    62  	return "Create one or more resource POOLs.\n" + poolCreateHelp
    63  }
    64  
    65  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    66  	if f.NArg() == 0 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	finder, err := cmd.Finder()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	for _, arg := range f.Args() {
    76  		dir := path.Dir(arg)
    77  		base := path.Base(arg)
    78  		parents, err := finder.ResourcePoolList(ctx, dir)
    79  		if err != nil {
    80  			if _, ok := err.(*find.NotFoundError); ok {
    81  				return fmt.Errorf("cannot create resource pool '%s': parent not found", base)
    82  			}
    83  			return err
    84  		}
    85  
    86  		for _, parent := range parents {
    87  			_, err = parent.Create(ctx, base, cmd.ResourceConfigSpec)
    88  			if err != nil {
    89  				return err
    90  			}
    91  		}
    92  	}
    93  
    94  	return nil
    95  }