multycloud / multy

Multy - Easily deploy multi cloud infrastructure. Write cloud-agnostic config deployed across multiple clouds
https://multy.dev
Apache License 2.0
640 stars 34 forks source link

Add overrides for VM's images #382

Closed goncalo-rodrigues closed 2 years ago

goncalo-rodrigues commented 2 years ago

Multy provides a cloud-agnostic way to specify an image. However, sometimes users want to use a specific AWS ami, or equivalent in other clouds.

This issue adds:

  1. Override field to VM's overrides

  2. Use overrides if specified in translation layer. Similar to how it's done currently for vm sizes - see example for AWS

  3. Add tests for each cloud using their overrides. Copy from this example - https://github.com/multycloud/multy/tree/main/test/_configs/virtual_machine/virtual_machine_size_override but override the image instead of the size. You can see what's currently being generated by running go test ./test --write_generated - it will create a generated.tf file with the current output of the translation

syanukov commented 2 years ago

Hi @goncalo-rodrigues,

I've had a look at the task and found an interesting pain point.

On AwsEC2 resource the Ami field has the following tags: hcl:"ami,expr" json:"ami".

,expr is what indeed needed when referencing a block by id

resource "aws_instance" "vm2_aws" {
  ...
  ami                  = data.aws_ami.vm2_aws.id
  ...

But in the case when a user wants to use a specific AWS ami, the output should be encoded as a string - that is to say, with a quotation mark

resource "aws_instance" "vm_aws" {
  ...
  ami                  = "ami-074ea14c08effb2d8"
  ...

It means, that we would need a mechanism to conditionally "turn off" the ,expr tag.

Is there already a solution for this or does the hclencoder need to be modified to support the new type of behavior?

goncalo-rodrigues commented 2 years ago

Interesting finding.

I believe that you can maybe have 2 fields,

As long as at least one is empty, there shouldn't be a conflict.

Otherwise, what we have done in the past is to call strconv.Quote to double quote any user input. This should properly escape everything as needed.

At some point adding it to Hcl Encoder would be the best option, if we see this case comes up often.