github.com/vmware/govmomi@v0.43.0/ovf/importer/spec.go (about)

     1  /*
     2  Copyright (c) 2024-2024 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 importer
    18  
    19  import (
    20  	"golang.org/x/text/cases"
    21  	"golang.org/x/text/language"
    22  
    23  	"github.com/vmware/govmomi/ovf"
    24  	"github.com/vmware/govmomi/vim25/types"
    25  )
    26  
    27  var (
    28  	allDiskProvisioningOptions = types.OvfCreateImportSpecParamsDiskProvisioningType("").Strings()
    29  
    30  	allIPAllocationPolicyOptions = types.VAppIPAssignmentInfoIpAllocationPolicy("").Strings()
    31  
    32  	allIPProtocolOptions = types.VAppIPAssignmentInfoProtocols("").Strings()
    33  )
    34  
    35  func SpecMap(e *ovf.Envelope, hidden, verbose bool) (res []Property) {
    36  	if e == nil || e.VirtualSystem == nil {
    37  		return nil
    38  	}
    39  
    40  	for _, p := range e.VirtualSystem.Product {
    41  		for i, v := range p.Property {
    42  			if v.UserConfigurable == nil {
    43  				continue
    44  			}
    45  			if !*v.UserConfigurable && !hidden {
    46  				continue
    47  			}
    48  
    49  			d := ""
    50  			if v.Default != nil {
    51  				d = *v.Default
    52  			}
    53  
    54  			// vSphere only accept True/False as boolean values for some reason
    55  			if v.Type == "boolean" {
    56  				d = cases.Title(language.Und).String(d)
    57  			}
    58  
    59  			np := Property{KeyValue: KeyValue{Key: p.Key(v), Value: d}}
    60  
    61  			if verbose {
    62  				np.Spec = &p.Property[i]
    63  			}
    64  
    65  			res = append(res, np)
    66  		}
    67  	}
    68  
    69  	return
    70  }
    71  
    72  func Spec(fpath string, a Archive, hidden, verbose bool) (*Options, error) {
    73  	e := &ovf.Envelope{}
    74  	if fpath != "" {
    75  		d, err := ReadOvf(fpath, a)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  
    80  		if e, err = ReadEnvelope(d); err != nil {
    81  			return nil, err
    82  		}
    83  	}
    84  
    85  	var deploymentOptions []string
    86  	if e.DeploymentOption != nil && e.DeploymentOption.Configuration != nil {
    87  		// add default first
    88  		for _, c := range e.DeploymentOption.Configuration {
    89  			if c.Default != nil && *c.Default {
    90  				deploymentOptions = append(deploymentOptions, c.ID)
    91  			}
    92  		}
    93  
    94  		for _, c := range e.DeploymentOption.Configuration {
    95  			if c.Default == nil || !*c.Default {
    96  				deploymentOptions = append(deploymentOptions, c.ID)
    97  			}
    98  		}
    99  	}
   100  
   101  	o := Options{
   102  		DiskProvisioning:   string(types.OvfCreateImportSpecParamsDiskProvisioningTypeFlat),
   103  		IPAllocationPolicy: string(types.VAppIPAssignmentInfoIpAllocationPolicyDhcpPolicy),
   104  		IPProtocol:         string(types.VAppIPAssignmentInfoProtocolsIPv4),
   105  		MarkAsTemplate:     false,
   106  		PowerOn:            false,
   107  		WaitForIP:          false,
   108  		InjectOvfEnv:       false,
   109  		PropertyMapping:    SpecMap(e, hidden, verbose),
   110  	}
   111  
   112  	if deploymentOptions != nil {
   113  		o.Deployment = deploymentOptions[0]
   114  	}
   115  
   116  	if e.VirtualSystem != nil && e.VirtualSystem.Annotation != nil {
   117  		for _, a := range e.VirtualSystem.Annotation {
   118  			o.Annotation += a.Annotation
   119  		}
   120  	}
   121  
   122  	if e.Network != nil {
   123  		for _, net := range e.Network.Networks {
   124  			o.NetworkMapping = append(o.NetworkMapping, Network{net.Name, ""})
   125  		}
   126  	}
   127  
   128  	if verbose {
   129  		if deploymentOptions != nil {
   130  			o.AllDeploymentOptions = deploymentOptions
   131  		}
   132  		o.AllDiskProvisioningOptions = allDiskProvisioningOptions
   133  		o.AllIPAllocationPolicyOptions = allIPAllocationPolicyOptions
   134  		o.AllIPProtocolOptions = allIPProtocolOptions
   135  	}
   136  
   137  	return &o, nil
   138  }