github.com/vmware/govmomi@v0.37.2/govc/namespace/service/create.go (about)

     1  /*
     2  Copyright (c) 2021 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 service
    18  
    19  import (
    20  	"context"
    21  	"encoding/base64"
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/vapi/namespace"
    29  )
    30  
    31  type create struct {
    32  	*flags.ClientFlag
    33  
    34  	specType        string
    35  	trustedProvider bool
    36  	acceptEULA      bool
    37  }
    38  
    39  func init() {
    40  	cli.Register("namespace.service.create", &create{})
    41  }
    42  
    43  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  
    47  	flag.StringVar(&cmd.specType, "spec-type", "vsphere", "Type of Spec: only vsphere is supported right now")
    48  	flag.BoolVar(&cmd.trustedProvider, "trusted", false, "Define if this is a trusted provider")
    49  	flag.BoolVar(&cmd.acceptEULA, "accept-eula", false, "Auto accept EULA")
    50  
    51  }
    52  
    53  func (cmd *create) Description() string {
    54  	return `Creates a vSphere Namespace Supervisor Service.
    55  
    56  Examples:
    57    govc namespace.service.create manifest.yaml`
    58  }
    59  
    60  func (cmd *create) Usage() string {
    61  	return "MANIFEST"
    62  }
    63  
    64  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    65  	manifest := f.Args()
    66  	if len(manifest) != 1 {
    67  		return flag.ErrHelp
    68  	}
    69  
    70  	if cmd.specType != "vsphere" {
    71  		return fmt.Errorf("only vsphere specs are accepted right now")
    72  	}
    73  
    74  	manifestFile, err := os.ReadFile(manifest[0])
    75  	if err != nil {
    76  		return fmt.Errorf("failed to read manifest file: %s", err)
    77  	}
    78  
    79  	content := base64.StdEncoding.EncodeToString(manifestFile)
    80  	service := namespace.SupervisorService{
    81  		VsphereService: namespace.SupervisorServicesVSphereSpec{
    82  			VersionSpec: namespace.SupervisorServicesVSphereVersionCreateSpec{
    83  				Content:         content,
    84  				AcceptEula:      cmd.acceptEULA,
    85  				TrustedProvider: cmd.trustedProvider,
    86  			},
    87  		},
    88  	}
    89  
    90  	c, err := cmd.RestClient()
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	m := namespace.NewManager(c)
    96  	return m.CreateSupervisorService(ctx, &service)
    97  
    98  }