Semyon Kolesnikov
Semyon Kolesnikov

Reputation: 55

If statement in terraform, using 2 different code blocks

I have this codeblock

  launchpad_tmpl = {
    apiVersion = "<Link for the API>"
    kind       = "mke"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }
      hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }
}

And I have another codeblock

  launchpad_tmpl = {
    apiVersion = "<Some link for the API>"
    kind       = "mke"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }

      msr = {
      ...some_code
      }
      hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }
}

I want to tell terraform something like

IF msr_count >= 1 - Use second codeblock to run ELSE use the first one...

Is it possible in Terraform Or I will need to make a wrapper somewhere? Thanks in Advance.

UPDATE:::

 Error: Inconsistent conditional result types

  on main.tf line 162, in output "mke_cluster":
 162:   value = yamlencode(var.msr_count > 0 ? local.msr_launchpad_tmpl : local.mke_launchpad_tmpl)
    |----------------
    | local.mke_launchpad_tmpl is object with 3 attributes
    | local.msr_launchpad_tmpl is object with 3 attributes
    | var.msr_count is 1

The true and false result expressions must have consistent types. The given
expressions are object and object, respectively.

This is the output when it placed like this:

 mke_launchpad_tmpl = {
    apiVersion = "API"
    kind       = "mke"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }
      hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }


  msr_launchpad_tmpl = {
    apiVersion = "API"
    kind       = "mke+msr"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }
      msr = {
        installFlags : [
          "--ucp-insecure-tls",
          "--dtr-external-url ${module.msrs.lb_dns_name}",
        ]
        }
    hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }
  launchpad_tmpl = var.msr_count > 0 ? local.msr_launchpad_tmpl : local.mke_launchpad_tmpl
}

output "mke_cluster" {
  value = yamlencode(local.launchpad_tmpl)
}

Upvotes: 0

Views: 396

Answers (1)

Marcin
Marcin

Reputation: 238239

You could use conditional expression. Also for simplicity, you can create the two alternatives as locals, which will simplify the expression.

For example:

locals {

 mke_launchpad_tmpl = {
    apiVersion = "API"
    kind       = "mke"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }
      msr = {}
      hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }


  msr_launchpad_tmpl = {
    apiVersion = "API"
    kind       = "mke+msr"
    spec = {
      mke = {
        adminUsername = "admin"
        adminPassword = var.admin_password
        installFlags : [
          "--default-node-orchestrator=kubernetes",
          "--san=${module.masters.lb_dns_name}",
        ]
      }
      msr = {
        installFlags : [
          "--ucp-insecure-tls",
          "--dtr-external-url ${module.msrs.lb_dns_name}",
        ]
        }
    hosts = concat(local.managers, local.msrs, local.workers, local.windows_workers)
    }
  }

}

Then, in your resource:

launchpad_tmpl = var.msr_count > -1 ? local.second_launchpad_tmpl : local.first_launchpad_tmpl

Upvotes: 1

Related Questions