hcl2_upgrade: add share_image_gallery workaround (#12087)

In azure templates, the shared_image_gallery was mistakenly considered
an attribute while this is supposed to be a block.

This is due to a heuristic we use for deciding whether a JSON object is
to be translated to an attribute or a block that fell short as the
shared_image_gallery does not contain complex types.

This cannot be fixed trivially for the general case, so we add this
entity to the list of workarounds until we can implement something more
robust.
This commit is contained in:
Lucas Bajolet 2022-10-27 18:09:40 -04:00 committed by GitHub
parent 0bac9b6f63
commit 68ddb2f89d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 1 deletions

View file

@ -121,6 +121,7 @@ func getBareComponentFinder() packer.ComponentFinder {
"file": func() (packersdk.Builder, error) { return &file.Builder{}, nil },
"null": func() (packersdk.Builder, error) { return &null.Builder{}, nil },
"amazon-ebs": func() (packersdk.Builder, error) { return &ebs.Builder{}, nil },
"azure-arm": func() (packersdk.Builder, error) { return &ebs.Builder{}, nil },
},
Provisioners: packer.MapOfProvisioner{
"shell-local": func() (packersdk.Provisioner, error) { return &shell_local.Provisioner{}, nil },

View file

@ -637,7 +637,7 @@ func jsonBodyToHCL2Body(out *hclwrite.Body, kvs map[string]interface{}) {
case map[string]interface{}:
var mostComplexElem interface{}
for _, randomElem := range value {
if k == "linux_options" || k == "network_interface" {
if k == "linux_options" || k == "network_interface" || k == "shared_image_gallery" {
break
}
// HACK: we take the most complex element of that map because

View file

@ -32,6 +32,7 @@ func Test_hcl2_upgrade(t *testing.T) {
{folder: "nonexistent", flags: []string{}, exitCode: 1, exitEarly: true},
{folder: "placeholders", flags: []string{}, exitCode: 0},
{folder: "ami_test", flags: []string{}, exitCode: 0},
{folder: "azure_shg", flags: []string{}, exitCode: 0},
}
for _, tc := range tc {

View file

@ -0,0 +1,11 @@
source "azure-arm" "autogenerated_1" {
shared_image_gallery {
gallery_name = "web_app"
}
}
build {
sources = ["source.azure-arm.autogenerated_1"]
}

View file

@ -0,0 +1,8 @@
{
"builders": [{
"type": "azure-arm",
"shared_image_gallery": {
"gallery_name": "web_app"
}
}]
}