github.com/vmware/govmomi@v0.43.0/govc/vm/markasvm.go (about)

     1  /*
     2  Copyright (c) 2014-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 vm
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  
    23  	"github.com/vmware/govmomi/govc/cli"
    24  	"github.com/vmware/govmomi/govc/flags"
    25  )
    26  
    27  type markasvm struct {
    28  	*flags.SearchFlag
    29  	*flags.ResourcePoolFlag
    30  	*flags.HostSystemFlag
    31  }
    32  
    33  func init() {
    34  	cli.Register("vm.markasvm", &markasvm{})
    35  }
    36  
    37  func (cmd *markasvm) Register(ctx context.Context, f *flag.FlagSet) {
    38  	cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
    39  	cmd.SearchFlag.Register(ctx, f)
    40  	cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
    41  	cmd.ResourcePoolFlag.Register(ctx, f)
    42  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    43  	cmd.HostSystemFlag.Register(ctx, f)
    44  }
    45  
    46  func (cmd *markasvm) Process(ctx context.Context) error {
    47  	if err := cmd.SearchFlag.Process(ctx); err != nil {
    48  		return err
    49  	}
    50  	if err := cmd.ResourcePoolFlag.Process(ctx); err != nil {
    51  		return err
    52  	}
    53  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func (cmd *markasvm) Usage() string {
    60  	return "VM..."
    61  }
    62  
    63  func (cmd *markasvm) Description() string {
    64  	return `Mark VM template as a virtual machine.
    65  
    66  Examples:
    67    govc vm.markasvm -host host1 $name
    68    govc vm.markasvm -host host1 $name`
    69  }
    70  
    71  func (cmd *markasvm) Run(ctx context.Context, f *flag.FlagSet) error {
    72  	vms, err := cmd.VirtualMachines(f.Args())
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	pool, err := cmd.ResourcePoolIfSpecified()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	host, err := cmd.HostSystemFlag.HostSystemIfSpecified()
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	if pool == nil {
    88  		if host == nil {
    89  			return flag.ErrHelp
    90  		}
    91  
    92  		pool, err = host.ResourcePool(ctx)
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	for _, vm := range vms {
    99  		err := vm.MarkAsVirtualMachine(ctx, *pool, host)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  }