chore: allowed empty default for modules (#72)

From #66, also added testing for `jetbrains-gateway`.

## Regions
Azure and AWS were simple, but there is no error message when no region is selected:

https://github.com/coder/modules/assets/58410745/9a9e317b-7764-427f-b298-86313de5fb33

Open to feedback on this.

## Jetbrains Gateway
The Jetbrains gateway default IDE satisfies a ton of logic in the template, so allowing `null` would lead to a lot of patchwork `try` statements. Now it's implemented to use the first IDE in the `jetbrains_ides` as the coder_parameter default for cleanliness. 

Let me know your thoughts.
pull/73/head
Stephen Kirby 2 years ago committed by GitHub
parent e1f4e25573
commit 2c94c82cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,7 +13,7 @@ describe("aws-region", async () => {
it("default output", async () => {
const state = await runTerraformApply(import.meta.dir, {});
expect(state.outputs.value.value).toBe("us-east-1");
expect(state.outputs.value.value).toBe("");
});
it("customized default", async () => {

@ -22,7 +22,7 @@ variable "description" {
}
variable "default" {
default = "us-east-1"
default = ""
description = "The default region to use if no region is specified."
type = string
}
@ -131,7 +131,7 @@ data "coder_parameter" "region" {
name = "aws_region"
display_name = var.display_name
description = var.description
default = var.default
default = var.default == "" ? null : var.default
mutable = var.mutable
dynamic "option" {
for_each = { for k, v in local.regions : k => v if !(contains(var.exclude, k)) }

@ -13,7 +13,7 @@ describe("azure-region", async () => {
it("default output", async () => {
const state = await runTerraformApply(import.meta.dir, {});
expect(state.outputs.value.value).toBe("eastus");
expect(state.outputs.value.value).toBe("");
});
it("customized default", async () => {

@ -21,7 +21,7 @@ variable "description" {
}
variable "default" {
default = "eastus"
default = ""
description = "The default region to use if no region is specified."
type = string
}
@ -308,7 +308,7 @@ data "coder_parameter" "region" {
name = "azure_region"
display_name = var.display_name
description = var.description
default = var.default
default = var.default == "" ? null : var.default
mutable = var.mutable
icon = "/icon/azure.png"
dynamic "option" {

@ -13,11 +13,11 @@ This module adds a JetBrains Gateway Button to open any workspace with a single
```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"]
source = "https://registry.coder.com/modules/jetbrains-gateway"
agent_id = coder_agent.example.id
agent_name = "example"
folder = "/home/coder/example"
jetbrains_ides = ["GO", "WS", "IU", "IC", "PY", "PC", "PS", "CL", "RM", "DB", "RD"]
}
```
@ -29,12 +29,12 @@ module "jetbrains_gateway" {
```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"]
default = "GO"
source = "https://registry.coder.com/modules/jetbrains-gateway"
agent_id = coder_agent.example.id
agent_name = "example"
folder = "/home/coder/example"
jetbrains_ides = ["GO", "WS"]
default = "GO"
}
```

@ -0,0 +1,16 @@
import { describe } from "bun:test";
import {
runTerraformInit,
testRequiredVariables,
} from "../test";
describe("jetbrains-gateway`", async () => {
await runTerraformInit(import.meta.dir);
await testRequiredVariables(import.meta.dir, {
agent_id: "foo",
agent_name: "bar",
folder: "/baz/",
jetbrains_ides: '["IU", "IC", "PY"]',
});
});

@ -19,13 +19,13 @@ variable "agent_name" {
description = "The name of a Coder agent."
}
variable "project_directory" {
variable "folder" {
type = string
description = "The directory to open in the IDE. e.g. /home/coder/project"
}
variable "default" {
default = null
default = ""
type = string
description = "Default IDE"
}
@ -46,7 +46,7 @@ variable "jetbrains_ides" {
condition = length(var.jetbrains_ides) > 0
error_message = "The jetbrains_ides must not be empty."
}
#ccheck if the list contains duplicates
# check if the list contains duplicates
validation {
condition = length(var.jetbrains_ides) == length(toset(var.jetbrains_ides))
error_message = "The jetbrains_ides must not contain duplicates."
@ -120,7 +120,7 @@ data "coder_parameter" "jetbrains_ide" {
icon = "/icon/gateway.svg"
mutable = true
# check if default is in the jet_brains_ides list and if it is not empty or null otherwise set it to null
default = var.default != null && var.default != "" && contains(var.jetbrains_ides, var.default) ? local.jetbrains_ides[var.default].value : null
default = var.default != null && var.default != "" && contains(var.jetbrains_ides, var.default) ? local.jetbrains_ides[var.default].value : local.jetbrains_ides[var.jetbrains_ides[0]].value
dynamic "option" {
for_each = { for key, value in local.jetbrains_ides : key => value if contains(var.jetbrains_ides, key) }
@ -146,7 +146,7 @@ resource "coder_app" "gateway" {
"&agent=",
var.agent_name,
"&folder=",
var.project_directory,
var.folder,
"&url=",
data.coder_workspace.me.access_url,
"&token=",
@ -156,7 +156,7 @@ resource "coder_app" "gateway" {
"&ide_build_number=",
jsondecode(data.coder_parameter.jetbrains_ide.value)[1],
"&ide_download_link=",
jsondecode(data.coder_parameter.jetbrains_ide.value)[2]
jsondecode(data.coder_parameter.jetbrains_ide.value)[2],
])
}

Loading…
Cancel
Save