Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Dynamic block support for execs in lxd_instance resource #542

Open
manuelpaula2464 opened this issue Nov 25, 2024 · 1 comment

Comments

@manuelpaula2464
Copy link

It would be great to have support of dynamic block for execs in lxd_instance resource.

variable "jmeter_cmds" {
  type=map(object({
    tag=string
    command=set(string)
  }))
  default={
    "a" = {
      command = ["/bin/bash", "-c", "wget https://dlcdn.apache.org//jmeter/binaries/apache-jmeter-5.6.3.tgz"]
    }
    "b" = {
      command = ["/bin/bash", "-c", "tar -xzvf apache-jmeter-5.6.3.tgz"]
    }
    "c" = {
      command = ["/bin/bash", "-c", "mv apache-jmeter-5.6.3 /opt"]
    }
    "d" = {
      command = ["/bin/bash", "-c", "apt install default-jdk openssh-server -y"]
    }
    "e" = {
      tag="e"      
      command = ["/bin/bash", "-c", "apt update -y"]
    }
    "f" = {
      command = ["/bin/bash", "-c", "apt upgrade -y"]
    }
  }
}

resource "lxd_instance" "lxd_vm" {
  name     = "jmeter"
...
  execs = {
    "a" = {
      command = ["/bin/bash", "-c", "wget https://dlcdn.apache.org//jmeter/binaries/apache-jmeter-5.6.3.tgz"]
    }
    "b" = {
      command = ["/bin/bash", "-c", "tar -xzvf apache-jmeter-5.6.3.tgz"]
    }
    "c" = {
      command = ["/bin/bash", "-c", "mv apache-jmeter-5.6.3 /opt"]
    }
    "d" = {
      command = ["/bin/bash", "-c", "apt install default-jdk openssh-server -y"]
    }
    "e" = {
      command = ["/bin/bash", "-c", "apt update -y"]
    }
    "f" = {
      command = ["/bin/bash", "-c", "apt upgrade -y"]
    }
  }
...
  ## I tried to convert above to a dynamic block
  dynamic "execs" {
    for_each = var.jmeter_cmds
    content={
      command=execs.value.command
      }
    }
  }
  ## But failed with:
  # Error: Unsupported block type
#│ 
#│   on main.tf line 83, in resource "lxd_instance" "lxd_vm":
#│   83:   dynamic "execs" {
#│ 
#│ Blocks of type "execs" are not expected here.

@MusicDin
Copy link
Member

MusicDin commented Nov 25, 2024

Hello,

I doubt we will add support for exec blocks as maps are more versatile compared to dynamic blocks.
Is there any other reason for using blocks beside adding multiple exec commands from a variable?


There are many ways to achieve your goal. Here are some examples for inspiration.

Example 1: Map of commands

You can construct the map, which should be similar to what you are trying to achieve.

variable "cmds" {
  type = map(object({
    command = list(string)
  }))
  default = {
    "a" = { command = ["cat", "/etc/os-release"] }
    "b" = { command = ["uname", "-a"] }
  }
}

resource "lxd_instance" "inst" {
  name  = "test"
  image = "images:alpine/edge"

  execs = {
    for key, value in var.cmds:
    key => {
      command = value.command
    }
  }
}

Example 2: List of commands

To avoid specifying map keys, you can use list index as map keys.

variable "cmds" {
  type = list(list(string))
  default = [
    ["cat", "/etc/os-release"],
    ["uname", "-a"],
  ]
}

resource "lxd_instance" "inst" {
  name  = "test"
  image = "images:alpine/edge"

  execs = zipmap(
    [for i in range(length(var.cmds)) : i],   # Keys are 0, 1, 2, ...
    [for cmd in var.cmds : { command = cmd }]
  )
}

Example 3: Upload script

You can also upload the script, and invoke it using exec command.

resource "lxd_instance" "inst" {
  name  = "test"
  image = "images:alpine/edge"

  file {
    target_path = "/root/script.sh"
    content = <<EOF
#!/bin/sh
cat /etc/os-release
uname -a
    EOF
  }

  execs = {
    "run_script" = {
      command = ["sh", "/root/script.sh"]
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants