github.com/hashicorp/packer@v1.14.3/internal/hcp/registry/deprecated_ds_config.go (about)

     1  package registry
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl/v2"
     7  	sdkpacker "github.com/hashicorp/packer-plugin-sdk/packer"
     8  	"github.com/zclconf/go-cty/cty"
     9  	"github.com/zclconf/go-cty/cty/gocty"
    10  )
    11  
    12  type hcpImage struct {
    13  	ID          string
    14  	ChannelID   string
    15  	IterationID string
    16  }
    17  
    18  func imageValueToDSOutput(imageVal map[string]cty.Value) hcpImage {
    19  	image := hcpImage{}
    20  	for k, v := range imageVal {
    21  		switch k {
    22  		case "id":
    23  			image.ID = v.AsString()
    24  		case "channel_id":
    25  			image.ChannelID = v.AsString()
    26  		case "iteration_id":
    27  			image.IterationID = v.AsString()
    28  		}
    29  	}
    30  
    31  	return image
    32  }
    33  
    34  type hcpIteration struct {
    35  	ID        string
    36  	ChannelID string
    37  }
    38  
    39  func iterValueToDSOutput(iterVal map[string]cty.Value) hcpIteration {
    40  	iter := hcpIteration{}
    41  	for k, v := range iterVal {
    42  		switch k {
    43  		case "id":
    44  			iter.ID = v.AsString()
    45  		case "channel_id":
    46  			iter.ChannelID = v.AsString()
    47  		}
    48  	}
    49  	return iter
    50  }
    51  
    52  func withDeprecatedDatasourceConfiguration(vals map[string]cty.Value, ui sdkpacker.Ui) bucketConfigurationOpts {
    53  	return func(bucket *Bucket) hcl.Diagnostics {
    54  		var diags hcl.Diagnostics
    55  
    56  		imageDS, imageOK := vals[hcpImageDatasourceType]
    57  		iterDS, iterOK := vals[hcpIterationDatasourceType]
    58  
    59  		if !imageOK && !iterOK {
    60  			return nil
    61  		}
    62  
    63  		iterations := map[string]hcpIteration{}
    64  
    65  		var err error
    66  		if iterOK {
    67  			ui.Say("[WARN] Deprecation: `hcp-packer-iteration` datasource has been deprecated. " +
    68  				"Please use `hcp-packer-version` datasource instead.")
    69  			hcpData := map[string]cty.Value{}
    70  			err = gocty.FromCtyValue(iterDS, &hcpData)
    71  			if err != nil {
    72  				diags = append(diags, &hcl.Diagnostic{
    73  					Severity: hcl.DiagError,
    74  					Summary:  "Invalid HCP datasources",
    75  					Detail:   fmt.Sprintf("Failed to decode hcp-packer-iteration datasources: %s", err),
    76  				})
    77  				return diags
    78  			}
    79  
    80  			for k, v := range hcpData {
    81  				iterVals := v.AsValueMap()
    82  				iter := iterValueToDSOutput(iterVals)
    83  				iterations[k] = iter
    84  			}
    85  		}
    86  
    87  		images := map[string]hcpImage{}
    88  
    89  		if imageOK {
    90  			ui.Say("[WARN] Deprecation: `hcp-packer-image` datasource has been deprecated. " +
    91  				"Please use `hcp-packer-artifact` datasource instead.")
    92  			hcpData := map[string]cty.Value{}
    93  			err = gocty.FromCtyValue(imageDS, &hcpData)
    94  			if err != nil {
    95  				diags = append(diags, &hcl.Diagnostic{
    96  					Severity: hcl.DiagError,
    97  					Summary:  "Invalid HCP datasources",
    98  					Detail:   fmt.Sprintf("Failed to decode hcp_packer_image datasources: %s", err),
    99  				})
   100  				return diags
   101  			}
   102  
   103  			for k, v := range hcpData {
   104  				imageVals := v.AsValueMap()
   105  				img := imageValueToDSOutput(imageVals)
   106  				images[k] = img
   107  			}
   108  		}
   109  
   110  		for _, img := range images {
   111  			sourceIteration := ParentVersion{}
   112  
   113  			sourceIteration.VersionID = img.IterationID
   114  
   115  			if img.ChannelID != "" {
   116  				sourceIteration.ChannelID = img.ChannelID
   117  			} else {
   118  				for _, it := range iterations {
   119  					if it.ID == img.IterationID {
   120  						sourceIteration.ChannelID = it.ChannelID
   121  						break
   122  					}
   123  				}
   124  			}
   125  
   126  			bucket.SourceExternalIdentifierToParentVersions[img.ID] = sourceIteration
   127  		}
   128  
   129  		return diags
   130  	}
   131  }