UpCloudLtd/packer-plugin-upcloud

[Bug] Unsupported block type network_interfaces when using HCL2

Closed this issue · 2 comments

When trying to add a custom network interface I get an error that network_interfaces is an unsupported block.
Looking at the code I see that there is no HCL config spec for network interfaces, which is the issue.

variable "username" {
  type = string
  default = "${env("UPCLOUD_API_USER")}"
}

variable "password" {
  type = string
  default = "${env("UPCLOUD_API_PASSWORD")}"
}

packer {
    required_plugins {
        upcloud = {
            version = ">=v1.0.0"
            source = "github.com/UpCloudLtd/upcloud"
        }
    }
}

source "upcloud" "test" {
  username = "${var.username}"
  password = "${var.password}"
  zone = "nl-ams1"
  storage_name = "ubuntu server 20.04"
  template_prefix = "ubuntu-server-test"
  network_interfaces {}
}

build {
  sources = ["source.upcloud.test"]

  provisioner "shell" {
    inline = [
      "apt-get update",
      "apt-get upgrade -y",
      "echo '<ssh-rsa_key>' | tee /root/.ssh/authorized_keys"
    ]
  }
}

Error obtained

~>  packer build examples/basic_example.pkr.hcl
Error: Unsupported block type

  on examples/basic_example.pkr.hcl line 27:
  (source code not available)

Blocks of type "network_interfaces" are not expected here.



==> Wait completed after 1 microsecond

==> Builds finished but no artifacts were created.

I think the expected HCL syntax would be something like the following. I'm not too certain on the ip_addresses block.

network_interfaces {
  type = "public"
  ip_addresses {
   family = "IPV4"
  address = "75.74.63.115"
 } 
  ip_addresses {
   family = "IPV6"
  address = "::ffff:4b4a:3f73"
 } 
}

A good first step here would be to look at what the mapstructure-to-hcl2 command would generate for the configuration.

Using packers hcl2 upgrade results in:

# This file was autogenerated by the 'packer hcl2_upgrade' command. We
# recommend double checking that everything is correct before going forward. We
# also recommend treating this file as disposable. The HCL2 blocks in this
# file can be moved to other files. For example, the variable blocks could be
# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be
# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at
# once they also need to be in the same folder. 'packer inspect folder/'
# will describe to you what is in that folder.

# Avoid mixing go templating calls ( for example ```{{ upper(`string`) }}``` )
# and HCL2 calls (for example '${ var.string_value_example }' ). They won't be
# executed together and the outcome will be unknown.

# All generated input variables will be of 'string' type as this is how Packer JSON
# views them; you can change their type later on. Read the variables type
# constraints documentation
# https://www.packer.io/docs/templates/hcl_templates/variables#type-constraints for more info.
variable "password" {
  type    = string
  default = "${env("UPCLOUD_API_PASSWORD")}"
}

variable "username" {
  type    = string
  default = "${env("UPCLOUD_API_USER")}"
}

# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors on a
# source. Read the documentation for source blocks here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/source
# could not parse template for following block: "template: hcl2_upgrade:14: function \"private_network_uuid\" not defined"

source "upcloud" "autogenerated_1" {
  network_interfaces {
    ip_addresses {
      family = "IPv4"
    }
    type = "public"
  }
  network_interfaces {
    ip_addresses {
      address = "192.168.3.123"
      family  = "IPv4"
    }
    network = "{{ private_network_uuid }}"
    type    = "private"
  }
  network_interfaces {
    ip_addresses {
      family = "IPv4"
    }
    type = "utility"
  }
  password     = "{{ user `password` }}"
  storage_uuid = "01000000-0000-4000-8000-000030200200"
  username     = "{{ user `username` }}"
  zone         = "nl-ams1"
}

# a build block invokes sources and runs provisioning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/build
build {
  sources = ["source.upcloud.autogenerated_1"]

  provisioner "shell" {
    inline = ["apt-get update", "apt-get upgrade -y", "echo '<ssh-rsa_key>' | tee /root/.ssh/authorized_keys"]
  }

}

which has the same issue

Fixed by #22