From 780c1d20c3abb3b1fa24023d00f830bd71348a4b Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Wed, 27 Sep 2023 08:26:29 +0300 Subject: [PATCH] improve default logic --- jetbrains-gateway/README.md | 9 +++++++++ jetbrains-gateway/main.tf | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/jetbrains-gateway/README.md b/jetbrains-gateway/README.md index d7fdfb0..27768f5 100644 --- a/jetbrains-gateway/README.md +++ b/jetbrains-gateway/README.md @@ -11,6 +11,15 @@ tags: [ide, jetbrains, helper, parameter] This module adds a JetBrains Gateway Button to open any workspace with a single click. +```hcl +module "jetbrains_gateway" { + source = "https://registry.coder.com/modules/jetbrains-gateway" + agent_id = coder_agent.example.id + agent_name = "example" + project_directory = "/home/coder/example" + jetbrains_ides = ["GO", "WS", "IU", "IC", "PY", "PC", "PS", "CL", "RM", "DB", "RD"] +} + ![JetBrains Gateway IDes list](../.images/jetbrains-gateway.png) ## Examples diff --git a/jetbrains-gateway/main.tf b/jetbrains-gateway/main.tf index 59ce493..1b01242 100644 --- a/jetbrains-gateway/main.tf +++ b/jetbrains-gateway/main.tf @@ -25,6 +25,7 @@ variable "project_directory" { } variable "default" { + default = null type = string description = "Default IDE" } @@ -40,6 +41,16 @@ variable "jetbrains_ides" { ) error_message = "The jetbrains_ides must be a list of valid product codes. https://plugins.jetbrains.com/docs/marketplace/product-codes.html" } + # check if the list is empty + validation { + condition = length(var.jetbrains_ides) > 0 + error_message = "The jetbrains_ides must not be empty." + } + #ccheck if the list contains duplicates + validation { + condition = length(var.jetbrains_ides) == length(set(var.jetbrains_ides)) + error_message = "The jetbrains_ides must not contain duplicates." + } } locals { @@ -108,7 +119,8 @@ data "coder_parameter" "jetbrains_ide" { display_name = "JetBrains IDE" icon = "/icon/gateway.svg" mutable = true - default = var.default != null && var.default != "" ? local.jetbrains_ides[var.default].value : null + # check if default is in the jet_brains_ides list and if it is not empty or null otherwise set it to null + default = contains(var.jetbrains_ides.keys, var.default) && var.default != null && var.default != "" ? var.default : null dynamic "option" { for_each = { for key, value in local.jetbrains_ides : key => value if contains(var.jetbrains_ides, key) }