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

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