github.com/gdavison/packer@v0.10.1/fix/fixer_pp_vagrant_override.go (about)

     1  package fix
     2  
     3  import (
     4  	"github.com/mitchellh/mapstructure"
     5  )
     6  
     7  // FixerVagrantPPOvveride is a Fixer that replaces the provider-specific
     8  // overrides for the Vagrant post-processor with the new style introduced
     9  // as part of Packer 0.5.0.
    10  type FixerVagrantPPOverride struct{}
    11  
    12  func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]interface{}, error) {
    13  	// Our template type we'll use for this fixer only
    14  	type template struct {
    15  		PostProcessors []interface{} `mapstructure:"post-processors"`
    16  	}
    17  
    18  	// Decode the input into our structure, if we can
    19  	var tpl template
    20  	if err := mapstructure.Decode(input, &tpl); err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	// Go through each post-processor and get out all the complex configs
    25  	pps := make([]map[string]interface{}, 0, len(tpl.PostProcessors))
    26  	for _, rawPP := range tpl.PostProcessors {
    27  		switch pp := rawPP.(type) {
    28  		case string:
    29  		case map[string]interface{}:
    30  			pps = append(pps, pp)
    31  		case []interface{}:
    32  			for _, innerRawPP := range pp {
    33  				if innerPP, ok := innerRawPP.(map[string]interface{}); ok {
    34  					pps = append(pps, innerPP)
    35  				}
    36  			}
    37  		}
    38  	}
    39  
    40  	// Go through each post-processor and make the fix if necessary
    41  	possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
    42  	for _, pp := range pps {
    43  		typeRaw, ok := pp["type"]
    44  		if !ok {
    45  			continue
    46  		}
    47  
    48  		if typeName, ok := typeRaw.(string); !ok {
    49  			continue
    50  		} else if typeName != "vagrant" {
    51  			continue
    52  		}
    53  
    54  		overrides := make(map[string]interface{})
    55  		for _, name := range possible {
    56  			if _, ok := pp[name]; !ok {
    57  				continue
    58  			}
    59  
    60  			overrides[name] = pp[name]
    61  			delete(pp, name)
    62  		}
    63  
    64  		if len(overrides) > 0 {
    65  			pp["override"] = overrides
    66  		}
    67  	}
    68  
    69  	input["post-processors"] = tpl.PostProcessors
    70  	return input, nil
    71  }
    72  
    73  func (FixerVagrantPPOverride) Synopsis() string {
    74  	return `Fixes provider-specific overrides for Vagrant post-processor`
    75  }