From 9975301a5eedf3308ab2ec6c4f7c8179418fe197 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Mon, 15 Aug 2022 16:29:12 -0500 Subject: [PATCH] feat: Expose access_port on coder_workspace to allow for alternative hosts (#37) This allows our Docker template to use a custom host with the correct port. --- docs/data-sources/workspace.md | 1 + internal/provider/provider.go | 19 +++++++++++++++++++ internal/provider/provider_test.go | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/data-sources/workspace.md b/docs/data-sources/workspace.md index b570d939..665fef2b 100644 --- a/docs/data-sources/workspace.md +++ b/docs/data-sources/workspace.md @@ -26,6 +26,7 @@ resource "kubernetes_pod" "dev" { ### Read-Only +- `access_port` (Number) The access port of the Coder deployment provisioning this workspace. - `access_url` (String) The access URL of the Coder deployment provisioning this workspace. - `id` (String) UUID of the workspace. - `name` (String) Name of the workspace. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 21227214..20a4818d 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -8,6 +8,7 @@ import ( "os" "reflect" "runtime" + "strconv" "strings" "github.com/google/uuid" @@ -106,6 +107,19 @@ func New() *schema.Provider { } rd.Set("access_url", config.URL.String()) + rawPort := config.URL.Port() + if rawPort == "" { + rawPort = "80" + if config.URL.Scheme == "https" { + rawPort = "443" + } + } + port, err := strconv.Atoi(rawPort) + if err != nil { + return diag.Errorf("couldn't parse port %q", port) + } + rd.Set("access_port", port) + return nil }, Schema: map[string]*schema.Schema{ @@ -114,6 +128,11 @@ func New() *schema.Provider { Computed: true, Description: "The access URL of the Coder deployment provisioning this workspace.", }, + "access_port": { + Type: schema.TypeInt, + Computed: true, + Description: "The access port of the Coder deployment provisioning this workspace.", + }, "start_count": { Type: schema.TypeInt, Computed: true, diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 40c659c8..7707b0ff 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -31,7 +31,7 @@ func TestWorkspace(t *testing.T) { Steps: []resource.TestStep{{ Config: ` provider "coder" { - url = "https://example.com" + url = "https://example.com:8080" } data "coder_workspace" "me" { }`, @@ -45,6 +45,7 @@ func TestWorkspace(t *testing.T) { value := attribs["transition"] require.NotNil(t, value) t.Log(value) + require.Equal(t, "8080", attribs["access_port"]) require.Equal(t, "owner123", attribs["owner"]) require.Equal(t, "owner123@example.com", attribs["owner_email"]) return nil