github.com/vmware/govmomi@v0.37.2/govc/library/checkout.go (about)

     1  /*
     2  Copyright (c) 2019 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 library
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vapi/vcenter"
    27  )
    28  
    29  type checkout struct {
    30  	*flags.ClusterFlag
    31  	*flags.FolderFlag
    32  	*flags.ResourcePoolFlag
    33  	*flags.HostSystemFlag
    34  }
    35  
    36  func init() {
    37  	cli.Register("library.checkout", &checkout{})
    38  }
    39  
    40  func (cmd *checkout) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
    42  	cmd.ClusterFlag.Register(ctx, f)
    43  
    44  	cmd.ResourcePoolFlag, ctx = flags.NewResourcePoolFlag(ctx)
    45  	cmd.ResourcePoolFlag.Register(ctx, f)
    46  
    47  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    48  	cmd.HostSystemFlag.Register(ctx, f)
    49  
    50  	cmd.FolderFlag, ctx = flags.NewFolderFlag(ctx)
    51  	cmd.FolderFlag.Register(ctx, f)
    52  }
    53  
    54  func (cmd *checkout) Process(ctx context.Context) error {
    55  	if err := cmd.ClusterFlag.Process(ctx); err != nil {
    56  		return err
    57  	}
    58  	if err := cmd.ResourcePoolFlag.Process(ctx); err != nil {
    59  		return err
    60  	}
    61  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    62  		return err
    63  	}
    64  	return cmd.FolderFlag.Process(ctx)
    65  }
    66  
    67  func (cmd *checkout) Usage() string {
    68  	return "PATH NAME"
    69  }
    70  
    71  func (cmd *checkout) Description() string {
    72  	return `Check out Content Library item PATH to vm NAME.
    73  
    74  Note: this command requires vCenter 7.0 or higher.
    75  
    76  Examples:
    77    govc library.checkout -cluster my-cluster my-content/template-vm-item my-vm`
    78  }
    79  
    80  func (cmd *checkout) Run(ctx context.Context, f *flag.FlagSet) error {
    81  	path := f.Arg(0)
    82  	name := f.Arg(1)
    83  
    84  	folder, err := cmd.FolderOrDefault("vm")
    85  	if err != nil {
    86  		return err
    87  	}
    88  	host, err := cmd.HostSystemIfSpecified()
    89  	if err != nil {
    90  		return err
    91  	}
    92  	cluster, err := cmd.ClusterIfSpecified()
    93  	if err != nil {
    94  		return err
    95  	}
    96  	pool, err := cmd.ResourcePoolIfSpecified()
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	c, err := cmd.RestClient()
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	l, err := flags.ContentLibraryItem(ctx, c, path)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	spec := vcenter.CheckOut{
   112  		Name: name,
   113  		Placement: &vcenter.Placement{
   114  			Folder: folder.Reference().Value,
   115  		},
   116  	}
   117  	if pool != nil {
   118  		spec.Placement.ResourcePool = pool.Reference().Value
   119  	}
   120  	if host != nil {
   121  		spec.Placement.Host = host.Reference().Value
   122  	}
   123  	if cluster != nil {
   124  		spec.Placement.Cluster = cluster.Reference().Value
   125  	}
   126  
   127  	id, err := vcenter.NewManager(c).CheckOut(ctx, l.ID, &spec)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	fmt.Println(id)
   133  
   134  	return nil
   135  }