From 41f2de0f95969fdc5aac11e345cff0f50700cd99 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 1 Jul 2024 16:36:51 +0200 Subject: [PATCH 01/11] fix: multiple agents, independent apps --- provisioner/terraform/resources_test.go | 54 +- .../multiple-agents-multiple-apps.tf | 57 ++ .../multiple-agents-multiple-apps.tfplan.dot | 31 + .../multiple-agents-multiple-apps.tfplan.json | 594 ++++++++++++++++++ .../multiple-agents-multiple-apps.tfstate.dot | 31 + ...multiple-agents-multiple-apps.tfstate.json | 230 +++++++ 6 files changed, 996 insertions(+), 1 deletion(-) create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tf create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.json create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.json diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index adfdc6c5e9ac2..09c337149760d 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -219,6 +219,56 @@ func TestConvertResources(t *testing.T) { }}, }}, }, + "multiple-agents-multiple-apps": { + resources: []*proto.Resource{{ + Name: "dev1", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev1", + OperatingSystem: "linux", + Architecture: "amd64", + Apps: []*proto.App{ + { + Slug: "app1", + DisplayName: "app1", + // Subdomain defaults to false if unspecified. + Subdomain: false, + }, + { + Slug: "app2", + DisplayName: "app2", + Subdomain: true, + Healthcheck: &proto.Healthcheck{ + Url: "http://localhost:13337/healthz", + Interval: 5, + Threshold: 6, + }, + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }, { + Name: "dev2", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev2", + OperatingSystem: "linux", + Architecture: "amd64", + Apps: []*proto.App{ + { + Slug: "app3", + DisplayName: "app3", + Subdomain: false, + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }}, + }, // Tests fetching metadata about workspace resources. "resource-metadata": { resources: []*proto.Resource{{ @@ -536,6 +586,9 @@ func TestConvertResources(t *testing.T) { }}, }, } { + if folderName != "multiple-agents-multiple-apps" { + continue + } folderName := folderName expected := expected t.Run(folderName, func(t *testing.T) { @@ -642,7 +695,6 @@ func TestConvertResources(t *testing.T) { var resourcesMap []map[string]interface{} err = json.Unmarshal(data, &resourcesMap) require.NoError(t, err) - require.Equal(t, expectedMap, resourcesMap) require.ElementsMatch(t, expected.externalAuthProviders, state.ExternalAuthProviders) }) diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tf b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tf new file mode 100644 index 0000000000000..02c6ff6c1b67f --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tf @@ -0,0 +1,57 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + version = "0.22.0" + } + } +} + +resource "coder_agent" "dev1" { + os = "linux" + arch = "amd64" +} + +resource "coder_agent" "dev2" { + os = "linux" + arch = "amd64" +} + +# app1 is for testing subdomain default. +resource "coder_app" "app1" { + agent_id = coder_agent.dev1.id + slug = "app1" + # subdomain should default to false. + # subdomain = false +} + +# app2 tests that subdomaincan be true, and that healthchecks work. +resource "coder_app" "app2" { + agent_id = coder_agent.dev1.id + slug = "app2" + subdomain = true + healthcheck { + url = "http://localhost:13337/healthz" + interval = 5 + threshold = 6 + } +} + +# app3 tests that subdomain can explicitly be false. +resource "coder_app" "app3" { + agent_id = coder_agent.dev2.id + slug = "app3" + subdomain = false +} + +resource "null_resource" "dev1" { + depends_on = [ + coder_agent.dev1 + ] +} + +resource "null_resource" "dev2" { + depends_on = [ + coder_agent.dev2 + ] +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.dot b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.dot new file mode 100644 index 0000000000000..e40607dbee5dd --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_app.app1 (expand)" [label = "coder_app.app1", shape = "box"] + "[root] coder_app.app2 (expand)" [label = "coder_app.app2", shape = "box"] + "[root] coder_app.app3 (expand)" [label = "coder_app.app3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_app.app1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_app.app2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_app.app3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.json b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.json new file mode 100644 index 0000000000000..e55d3c536c9e5 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfplan.json @@ -0,0 +1,594 @@ +{ + "format_version": "1.2", + "terraform_version": "1.8.5", + "planned_values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [], + "metadata": [], + "token": true + } + }, + { + "address": "coder_app.app1", + "mode": "managed", + "type": "coder_app", + "name": "app1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app1", + "subdomain": null, + "url": null + }, + "sensitive_values": { + "healthcheck": [] + } + }, + { + "address": "coder_app.app2", + "mode": "managed", + "type": "coder_app", + "name": "app2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [ + { + "interval": 5, + "threshold": 6, + "url": "http://localhost:13337/healthz" + } + ], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app2", + "subdomain": true, + "url": null + }, + "sensitive_values": { + "healthcheck": [ + {} + ] + } + }, + { + "address": "coder_app.app3", + "mode": "managed", + "type": "coder_app", + "name": "app3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app3", + "subdomain": false, + "url": null + }, + "sensitive_values": { + "healthcheck": [] + } + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "triggers": null + }, + "sensitive_values": {} + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "triggers": null + }, + "sensitive_values": {} + } + ] + } + }, + "resource_changes": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, + "metadata": [], + "token": true + }, + "before_sensitive": false, + "after_sensitive": { + "display_apps": [], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, + "metadata": [], + "token": true + }, + "before_sensitive": false, + "after_sensitive": { + "display_apps": [], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_app.app1", + "mode": "managed", + "type": "coder_app", + "name": "app1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app1", + "subdomain": null, + "url": null + }, + "after_unknown": { + "agent_id": true, + "healthcheck": [], + "id": true + }, + "before_sensitive": false, + "after_sensitive": { + "healthcheck": [] + } + } + }, + { + "address": "coder_app.app2", + "mode": "managed", + "type": "coder_app", + "name": "app2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [ + { + "interval": 5, + "threshold": 6, + "url": "http://localhost:13337/healthz" + } + ], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app2", + "subdomain": true, + "url": null + }, + "after_unknown": { + "agent_id": true, + "healthcheck": [ + {} + ], + "id": true + }, + "before_sensitive": false, + "after_sensitive": { + "healthcheck": [ + {} + ] + } + } + }, + { + "address": "coder_app.app3", + "mode": "managed", + "type": "coder_app", + "name": "app3", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app3", + "subdomain": false, + "url": null + }, + "after_unknown": { + "agent_id": true, + "healthcheck": [], + "id": true + }, + "before_sensitive": false, + "after_sensitive": { + "healthcheck": [] + } + } + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "triggers": null + }, + "after_unknown": { + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "triggers": null + }, + "after_unknown": { + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + } + ], + "configuration": { + "provider_config": { + "coder": { + "name": "coder", + "full_name": "registry.terraform.io/coder/coder", + "version_constraint": "0.22.0" + }, + "null": { + "name": "null", + "full_name": "registry.terraform.io/hashicorp/null" + } + }, + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_app.app1", + "mode": "managed", + "type": "coder_app", + "name": "app1", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "slug": { + "constant_value": "app1" + } + }, + "schema_version": 0 + }, + { + "address": "coder_app.app2", + "mode": "managed", + "type": "coder_app", + "name": "app2", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "healthcheck": [ + { + "interval": { + "constant_value": 5 + }, + "threshold": { + "constant_value": 6 + }, + "url": { + "constant_value": "http://localhost:13337/healthz" + } + } + ], + "slug": { + "constant_value": "app2" + }, + "subdomain": { + "constant_value": true + } + }, + "schema_version": 0 + }, + { + "address": "coder_app.app3", + "mode": "managed", + "type": "coder_app", + "name": "app3", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev2.id", + "coder_agent.dev2" + ] + }, + "slug": { + "constant_value": "app3" + }, + "subdomain": { + "constant_value": false + } + }, + "schema_version": 0 + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + }, + "relevant_attributes": [ + { + "resource": "coder_agent.dev1", + "attribute": [ + "id" + ] + }, + { + "resource": "coder_agent.dev2", + "attribute": [ + "id" + ] + } + ], + "timestamp": "2024-07-01T14:04:52Z", + "applyable": true, + "complete": true, + "errored": false +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.dot b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.dot new file mode 100644 index 0000000000000..e40607dbee5dd --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_app.app1 (expand)" [label = "coder_app.app1", shape = "box"] + "[root] coder_app.app2 (expand)" [label = "coder_app.app2", shape = "box"] + "[root] coder_app.app3 (expand)" [label = "coder_app.app3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_app.app1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_app.app2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_app.app3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_app.app3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.json b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.json new file mode 100644 index 0000000000000..406e8b0aba351 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-apps/multiple-agents-multiple-apps.tfstate.json @@ -0,0 +1,230 @@ +{ + "format_version": "1.0", + "terraform_version": "1.8.5", + "values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "f70d5406-a47c-43f5-bc9f-303754927057", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "ca4bb419-556c-4e05-9ff1-9770f374da4e", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "ad49af97-8978-4464-a33a-7a078384292f", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "f6570798-baae-48a3-991c-a8560ce89d4f", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_app.app1", + "mode": "managed", + "type": "coder_app", + "name": "app1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "f70d5406-a47c-43f5-bc9f-303754927057", + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "id": "64cda2f9-04da-42bc-9354-da760d063e59", + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app1", + "subdomain": null, + "url": null + }, + "sensitive_values": { + "healthcheck": [] + }, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_app.app2", + "mode": "managed", + "type": "coder_app", + "name": "app2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "f70d5406-a47c-43f5-bc9f-303754927057", + "command": null, + "display_name": null, + "external": false, + "healthcheck": [ + { + "interval": 5, + "threshold": 6, + "url": "http://localhost:13337/healthz" + } + ], + "icon": null, + "id": "6ff03bae-cb3a-4b32-a475-05a4e104755d", + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app2", + "subdomain": true, + "url": null + }, + "sensitive_values": { + "healthcheck": [ + {} + ] + }, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_app.app3", + "mode": "managed", + "type": "coder_app", + "name": "app3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "ad49af97-8978-4464-a33a-7a078384292f", + "command": null, + "display_name": null, + "external": false, + "healthcheck": [], + "icon": null, + "id": "59a1c0f0-21fb-4867-a7db-33a4d8a3d18e", + "name": null, + "order": null, + "relative_path": null, + "share": "owner", + "slug": "app3", + "subdomain": false, + "url": null + }, + "sensitive_values": { + "healthcheck": [] + }, + "depends_on": [ + "coder_agent.dev2" + ] + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "3152137311613337201", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "4927717367839364271", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + } +} From 8a1f50f7df81c4f9de29f7cea94b84bb0c34a62e Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 2 Jul 2024 14:36:58 +0200 Subject: [PATCH 02/11] WIP --- provisioner/terraform/resources.go | 28 +++++++++++++++++++++- provisioner/terraform/testdata/version.txt | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index 2858af3a38321..6b9aaba733237 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -427,9 +427,11 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error for _, agents := range resourceAgents { for _, agent := range agents { // Find agents with the matching ID and associate them! - if agent.Id != attrs.AgentID { + + if !dependsOnAgent(graph, agent, attrs.AgentID, resource) { continue } + agent.Apps = append(agent.Apps, &proto.App{ Slug: attrs.Slug, DisplayName: attrs.DisplayName, @@ -748,6 +750,30 @@ func convertAddressToLabel(address string) string { return cut } +func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, appAgentID string, resource *tfjson.StateResource) bool { + // Plan: we need to find if there is edge between the agent and the app. + if agent.Id == "" && appAgentID == "" { + appNodeSuffix := fmt.Sprintf(`] coder_app.%s (expand)"`, resource.Name) + agentNodeSuffix := fmt.Sprintf(`] coder_agent.%s (expand)"`, agent.Name) + + // Traverse the graph to check if the coder_app depends on coder_agent. + for _, dst := range graph.Edges.SrcToDsts { + for _, edges := range dst { + for _, edge := range edges { + if strings.HasSuffix(edge.Src, appNodeSuffix) && + strings.HasSuffix(edge.Dst, agentNodeSuffix) { + return true + } + } + } + } + return false + } + + // Provision: agent ID and app ID are present + return agent.Id == appAgentID +} + type graphResource struct { Label string Depth uint diff --git a/provisioner/terraform/testdata/version.txt b/provisioner/terraform/testdata/version.txt index bfa363e76ed71..8decb929b9863 100644 --- a/provisioner/terraform/testdata/version.txt +++ b/provisioner/terraform/testdata/version.txt @@ -1 +1 @@ -1.8.4 +1.8.5 From 38a0251e2034e6a3112d6b8f12b2d158cf2a5f80 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 2 Jul 2024 15:27:16 +0200 Subject: [PATCH 03/11] scripts support --- provisioner/terraform/resources.go | 8 +- provisioner/terraform/resources_test.go | 50 ++ .../multiple-agents-multiple-envs.tf | 48 ++ .../multiple-agents-multiple-envs.tfplan.dot | 31 ++ .../multiple-agents-multiple-envs.tfplan.json | 491 ++++++++++++++++++ .../multiple-agents-multiple-envs.tfstate.dot | 31 ++ ...multiple-agents-multiple-envs.tfstate.json | 186 +++++++ 7 files changed, 841 insertions(+), 4 deletions(-) create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tf create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.json create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.json diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index 6b9aaba733237..3ad1e451bc636 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -463,7 +463,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error for _, agents := range resourceAgents { for _, agent := range agents { // Find agents with the matching ID and associate them! - if agent.Id != attrs.AgentID { + if !dependsOnAgent(graph, agent, attrs.AgentID, resource) { continue } agent.ExtraEnvs = append(agent.ExtraEnvs, &proto.Env{ @@ -751,12 +751,12 @@ func convertAddressToLabel(address string) string { } func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, appAgentID string, resource *tfjson.StateResource) bool { - // Plan: we need to find if there is edge between the agent and the app. + // Plan: we need to find if there is edge between the agent and the resource. if agent.Id == "" && appAgentID == "" { - appNodeSuffix := fmt.Sprintf(`] coder_app.%s (expand)"`, resource.Name) + appNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, resource.Type, resource.Name) agentNodeSuffix := fmt.Sprintf(`] coder_agent.%s (expand)"`, agent.Name) - // Traverse the graph to check if the coder_app depends on coder_agent. + // Traverse the graph to check if the coder_ depends on coder_agent. for _, dst := range graph.Edges.SrcToDsts { for _, edges := range dst { for _, edge := range edges { diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index 09c337149760d..0decd918f756e 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -269,6 +269,56 @@ func TestConvertResources(t *testing.T) { }}, }}, }, + "multiple-agents-multiple-envs": { + resources: []*proto.Resource{{ + Name: "dev1", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev1", + OperatingSystem: "linux", + Architecture: "amd64", + ExtraEnvs: []*proto.Env{ + { + Name: "ENV_1", + Value: "Env 1", + }, + { + Name: "ENV_2", + Value: "Env 2", + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }, { + Name: "dev2", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev2", + OperatingSystem: "linux", + Architecture: "amd64", + ExtraEnvs: []*proto.Env{ + { + Name: "ENV_3", + Value: "Env 3", + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }, { + Name: "env1", + Type: "coder_env", + }, { + Name: "env2", + Type: "coder_env", + }, { + Name: "env3", + Type: "coder_env", + }}, + }, // Tests fetching metadata about workspace resources. "resource-metadata": { resources: []*proto.Resource{{ diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tf b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tf new file mode 100644 index 0000000000000..d167d44942776 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tf @@ -0,0 +1,48 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + version = "0.22.0" + } + } +} + +resource "coder_agent" "dev1" { + os = "linux" + arch = "amd64" +} + +resource "coder_agent" "dev2" { + os = "linux" + arch = "amd64" +} + +resource "coder_env" "env1" { + agent_id = coder_agent.dev1.id + name = "ENV_1" + value = "Env 1" +} + +resource "coder_env" "env2" { + agent_id = coder_agent.dev1.id + name = "ENV_2" + value = "Env 2" +} + +resource "coder_env" "env3" { + agent_id = coder_agent.dev2.id + name = "ENV_3" + value = "Env 3" +} + +resource "null_resource" "dev1" { + depends_on = [ + coder_agent.dev1 + ] +} + +resource "null_resource" "dev2" { + depends_on = [ + coder_agent.dev2 + ] +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.dot b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.dot new file mode 100644 index 0000000000000..e6f0a05c530fa --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_env.env1 (expand)" [label = "coder_env.env1", shape = "box"] + "[root] coder_env.env2 (expand)" [label = "coder_env.env2", shape = "box"] + "[root] coder_env.env3 (expand)" [label = "coder_env.env3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_env.env1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_env.env2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_env.env3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.json b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.json new file mode 100644 index 0000000000000..ee79410e810c7 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfplan.json @@ -0,0 +1,491 @@ +{ + "format_version": "1.2", + "terraform_version": "1.8.5", + "planned_values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [], + "metadata": [], + "token": true + } + }, + { + "address": "coder_env.env1", + "mode": "managed", + "type": "coder_env", + "name": "env1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "name": "ENV_1", + "value": "Env 1" + }, + "sensitive_values": {} + }, + { + "address": "coder_env.env2", + "mode": "managed", + "type": "coder_env", + "name": "env2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "name": "ENV_2", + "value": "Env 2" + }, + "sensitive_values": {} + }, + { + "address": "coder_env.env3", + "mode": "managed", + "type": "coder_env", + "name": "env3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "name": "ENV_3", + "value": "Env 3" + }, + "sensitive_values": {} + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "triggers": null + }, + "sensitive_values": {} + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "triggers": null + }, + "sensitive_values": {} + } + ] + } + }, + "resource_changes": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, + "metadata": [], + "token": true + }, + "before_sensitive": false, + "after_sensitive": { + "display_apps": [], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "env": null, + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "troubleshooting_url": null + }, + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, + "metadata": [], + "token": true + }, + "before_sensitive": false, + "after_sensitive": { + "display_apps": [], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_env.env1", + "mode": "managed", + "type": "coder_env", + "name": "env1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "name": "ENV_1", + "value": "Env 1" + }, + "after_unknown": { + "agent_id": true, + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + }, + { + "address": "coder_env.env2", + "mode": "managed", + "type": "coder_env", + "name": "env2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "name": "ENV_2", + "value": "Env 2" + }, + "after_unknown": { + "agent_id": true, + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + }, + { + "address": "coder_env.env3", + "mode": "managed", + "type": "coder_env", + "name": "env3", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "name": "ENV_3", + "value": "Env 3" + }, + "after_unknown": { + "agent_id": true, + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "triggers": null + }, + "after_unknown": { + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "create" + ], + "before": null, + "after": { + "triggers": null + }, + "after_unknown": { + "id": true + }, + "before_sensitive": false, + "after_sensitive": {} + } + } + ], + "configuration": { + "provider_config": { + "coder": { + "name": "coder", + "full_name": "registry.terraform.io/coder/coder", + "version_constraint": "0.22.0" + }, + "null": { + "name": "null", + "full_name": "registry.terraform.io/hashicorp/null" + } + }, + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_env.env1", + "mode": "managed", + "type": "coder_env", + "name": "env1", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "name": { + "constant_value": "ENV_1" + }, + "value": { + "constant_value": "Env 1" + } + }, + "schema_version": 0 + }, + { + "address": "coder_env.env2", + "mode": "managed", + "type": "coder_env", + "name": "env2", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "name": { + "constant_value": "ENV_2" + }, + "value": { + "constant_value": "Env 2" + } + }, + "schema_version": 0 + }, + { + "address": "coder_env.env3", + "mode": "managed", + "type": "coder_env", + "name": "env3", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev2.id", + "coder_agent.dev2" + ] + }, + "name": { + "constant_value": "ENV_3" + }, + "value": { + "constant_value": "Env 3" + } + }, + "schema_version": 0 + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + }, + "relevant_attributes": [ + { + "resource": "coder_agent.dev1", + "attribute": [ + "id" + ] + }, + { + "resource": "coder_agent.dev2", + "attribute": [ + "id" + ] + } + ], + "timestamp": "2024-07-02T13:02:20Z", + "applyable": true, + "complete": true, + "errored": false +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.dot b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.dot new file mode 100644 index 0000000000000..e6f0a05c530fa --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_env.env1 (expand)" [label = "coder_env.env1", shape = "box"] + "[root] coder_env.env2 (expand)" [label = "coder_env.env2", shape = "box"] + "[root] coder_env.env3 (expand)" [label = "coder_env.env3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_env.env1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_env.env2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_env.env3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_env.env3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.json b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.json new file mode 100644 index 0000000000000..843887ac818fc --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-envs/multiple-agents-multiple-envs.tfstate.json @@ -0,0 +1,186 @@ +{ + "format_version": "1.0", + "terraform_version": "1.8.5", + "values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "20f30173-700b-48fa-954a-3303ea0ea62f", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "0efed72e-b582-4573-beca-5bba2aff1a71", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "71dec38a-0885-48c3-834a-1c3455a01e43", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "cec160ae-dbab-44d9-99ba-dd4525448de7", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_env.env1", + "mode": "managed", + "type": "coder_env", + "name": "env1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "20f30173-700b-48fa-954a-3303ea0ea62f", + "id": "ad9d0cba-ace3-4257-a0e9-8521354c6eac", + "name": "ENV_1", + "value": "Env 1" + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_env.env2", + "mode": "managed", + "type": "coder_env", + "name": "env2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "20f30173-700b-48fa-954a-3303ea0ea62f", + "id": "da1c5415-4e5e-4bd4-bb6a-2848dfe751e8", + "name": "ENV_2", + "value": "Env 2" + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_env.env3", + "mode": "managed", + "type": "coder_env", + "name": "env3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "71dec38a-0885-48c3-834a-1c3455a01e43", + "id": "06a07733-acb6-4be4-852f-ba4689230d3b", + "name": "ENV_3", + "value": "Env 3" + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "4158512646691730095", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "8795184716221619179", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + } +} From 157d13ba9b31cd6f04753c04d078cfef4994932e Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 2 Jul 2024 16:59:51 +0200 Subject: [PATCH 04/11] WIP --- provisioner/terraform/resources.go | 2 +- provisioner/terraform/resources_test.go | 43 +- provisioner/terraform/testdata/generate.sh | 2 +- .../multiple-agents-multiple-scripts.tf | 54 + ...ultiple-agents-multiple-scripts.tfplan.dot | 31 + ...ltiple-agents-multiple-scripts.tfplan.json | 1093 +++++++++++++++++ ...ltiple-agents-multiple-scripts.tfstate.dot | 31 + ...tiple-agents-multiple-scripts.tfstate.json | 207 ++++ 8 files changed, 1460 insertions(+), 3 deletions(-) create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.dot create mode 100644 provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index 3ad1e451bc636..fd24f0aa86c46 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -489,7 +489,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error for _, agents := range resourceAgents { for _, agent := range agents { // Find agents with the matching ID and associate them! - if agent.Id != attrs.AgentID { + if !dependsOnAgent(graph, agent, attrs.AgentID, resource) { continue } agent.Scripts = append(agent.Scripts, &proto.Script{ diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index 0decd918f756e..81f296d3292a0 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -319,6 +319,47 @@ func TestConvertResources(t *testing.T) { Type: "coder_env", }}, }, + "multiple-agents-multiple-scripts": { + resources: []*proto.Resource{{ + Name: "dev1", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev1", + OperatingSystem: "linux", + Architecture: "amd64", + Scripts: []*proto.Script{ + { + DisplayName: "Foobar Script 1", + Script: "echo foobar 1", + }, + { + DisplayName: "Foobar Script 2", + Script: "echo foobar 2", + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }, { + Name: "dev2", + Type: "null_resource", + Agents: []*proto.Agent{{ + Name: "dev2", + OperatingSystem: "linux", + Architecture: "amd64", + Scripts: []*proto.Script{ + { + DisplayName: "Foobar Script 3", + Script: "echo foobar 3", + }, + }, + Auth: &proto.Agent_Token{}, + ConnectionTimeoutSeconds: 120, + DisplayApps: &displayApps, + }}, + }}, + }, // Tests fetching metadata about workspace resources. "resource-metadata": { resources: []*proto.Resource{{ @@ -636,7 +677,7 @@ func TestConvertResources(t *testing.T) { }}, }, } { - if folderName != "multiple-agents-multiple-apps" { + if folderName != "multiple-agents-multiple-scripts" { continue } folderName := folderName diff --git a/provisioner/terraform/testdata/generate.sh b/provisioner/terraform/testdata/generate.sh index 04ac7bdef3c64..401f8ed9fcc08 100755 --- a/provisioner/terraform/testdata/generate.sh +++ b/provisioner/terraform/testdata/generate.sh @@ -3,7 +3,7 @@ set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" -for d in */; do +for d in multiple-agents-multiple-scripts/; do pushd "$d" name=$(basename "$(pwd)") diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf new file mode 100644 index 0000000000000..795bc757c93ca --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf @@ -0,0 +1,54 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + version = "0.22.0" + } + } +} + +resource "coder_agent" "dev1" { + os = "linux" + arch = "amd64" +} + +resource "coder_agent" "dev2" { + os = "linux" + arch = "amd64" +} + +resource "coder_script" "script1" { + agent_id = coder_agent.dev1.id + display_name = "Foobar Script 1" + script = "echo foobar 1" + + run_on_start = true +} + +resource "coder_script" "script2" { + agent_id = coder_agent.dev1.id + display_name = "Foobar Script 2" + script = "echo foobar 2" + + run_on_start = true +} + +resource "coder_script" "script3" { + agent_id = coder_agent.dev2.id + display_name = "Foobar Script 3" + script = "echo foobar 3" + + run_on_start = true +} + +resource "null_resource" "dev1" { + depends_on = [ + coder_agent.dev1 + ] +} + +resource "null_resource" "dev2" { + depends_on = [ + coder_agent.dev2 + ] +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.dot b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.dot new file mode 100644 index 0000000000000..45afc475d18a0 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_script.script1 (expand)" [label = "coder_script.script1", shape = "box"] + "[root] coder_script.script2 (expand)" [label = "coder_script.script2", shape = "box"] + "[root] coder_script.script3 (expand)" [label = "coder_script.script3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_script.script1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_script.script2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_script.script3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json new file mode 100644 index 0000000000000..b9027c51e265a --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json @@ -0,0 +1,1093 @@ +{ + "format_version": "1.2", + "terraform_version": "1.8.5", + "planned_values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_script.script1", + "mode": "managed", + "type": "coder_script", + "name": "script1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 1", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 1", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {} + }, + { + "address": "coder_script.script2", + "mode": "managed", + "type": "coder_script", + "name": "script2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 2", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 2", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {} + }, + { + "address": "coder_script.script3", + "mode": "managed", + "type": "coder_script", + "name": "script3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "cron": null, + "display_name": "Foobar Script 3", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 3", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {} + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "2102344837160367826", + "triggers": null + }, + "sensitive_values": {} + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "5696575281159455178", + "triggers": null + }, + "sensitive_values": {} + } + ] + } + }, + "resource_drift": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "update" + ], + "before": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "770c0f3f-3c3a-4c74-a9e5-f8a2455e71f9", + "troubleshooting_url": null + }, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", + "troubleshooting_url": null + }, + "after_unknown": {}, + "before_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + }, + "after_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "update" + ], + "before": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "43a865e0-fcf1-483e-83ce-e5c7e041b30b", + "troubleshooting_url": null + }, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", + "troubleshooting_url": null + }, + "after_unknown": {}, + "before_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + }, + "after_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + } + } + ], + "resource_changes": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "no-op" + ], + "before": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", + "troubleshooting_url": null + }, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", + "troubleshooting_url": null + }, + "after_unknown": {}, + "before_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + }, + "after_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "no-op" + ], + "before": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", + "troubleshooting_url": null + }, + "after": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", + "troubleshooting_url": null + }, + "after_unknown": {}, + "before_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + }, + "after_sensitive": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + } + }, + { + "address": "coder_script.script1", + "mode": "managed", + "type": "coder_script", + "name": "script1", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "delete", + "create" + ], + "before": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 1", + "icon": null, + "id": "2d80a385-efb9-4db8-a4c2-94cb94f07c3b", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 1", + "start_blocks_login": false, + "timeout": 0 + }, + "after": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 1", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 1", + "start_blocks_login": false, + "timeout": 0 + }, + "after_unknown": { + "id": true + }, + "before_sensitive": {}, + "after_sensitive": {} + }, + "action_reason": "replace_because_tainted" + }, + { + "address": "coder_script.script2", + "mode": "managed", + "type": "coder_script", + "name": "script2", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "delete", + "create" + ], + "before": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 2", + "icon": null, + "id": "fb867f82-2d59-4e1e-84d5-83348f9f3b48", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 2", + "start_blocks_login": false, + "timeout": 0 + }, + "after": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 2", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 2", + "start_blocks_login": false, + "timeout": 0 + }, + "after_unknown": { + "id": true + }, + "before_sensitive": {}, + "after_sensitive": {} + }, + "action_reason": "replace_because_tainted" + }, + { + "address": "coder_script.script3", + "mode": "managed", + "type": "coder_script", + "name": "script3", + "provider_name": "registry.terraform.io/coder/coder", + "change": { + "actions": [ + "delete", + "create" + ], + "before": { + "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "cron": null, + "display_name": "Foobar Script 3", + "icon": null, + "id": "79e6cc0e-cb2f-4464-be93-4828e8acbc46", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 3", + "start_blocks_login": false, + "timeout": 0 + }, + "after": { + "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "cron": null, + "display_name": "Foobar Script 3", + "icon": null, + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 3", + "start_blocks_login": false, + "timeout": 0 + }, + "after_unknown": { + "id": true + }, + "before_sensitive": {}, + "after_sensitive": {} + }, + "action_reason": "replace_because_tainted" + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "no-op" + ], + "before": { + "id": "2102344837160367826", + "triggers": null + }, + "after": { + "id": "2102344837160367826", + "triggers": null + }, + "after_unknown": {}, + "before_sensitive": {}, + "after_sensitive": {} + } + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "change": { + "actions": [ + "no-op" + ], + "before": { + "id": "5696575281159455178", + "triggers": null + }, + "after": { + "id": "5696575281159455178", + "triggers": null + }, + "after_unknown": {}, + "before_sensitive": {}, + "after_sensitive": {} + } + } + ], + "prior_state": { + "format_version": "1.0", + "terraform_version": "1.8.5", + "values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_script.script1", + "mode": "managed", + "type": "coder_script", + "name": "script1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 1", + "icon": null, + "id": "2d80a385-efb9-4db8-a4c2-94cb94f07c3b", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 1", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ], + "tainted": true + }, + { + "address": "coder_script.script2", + "mode": "managed", + "type": "coder_script", + "name": "script2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 2", + "icon": null, + "id": "fb867f82-2d59-4e1e-84d5-83348f9f3b48", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 2", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ], + "tainted": true + }, + { + "address": "coder_script.script3", + "mode": "managed", + "type": "coder_script", + "name": "script3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "cron": null, + "display_name": "Foobar Script 3", + "icon": null, + "id": "79e6cc0e-cb2f-4464-be93-4828e8acbc46", + "log_path": null, + "run_on_start": false, + "run_on_stop": false, + "script": "echo foobar 3", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ], + "tainted": true + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "2102344837160367826", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "5696575281159455178", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + } + }, + "configuration": { + "provider_config": { + "coder": { + "name": "coder", + "full_name": "registry.terraform.io/coder/coder", + "version_constraint": "0.22.0" + }, + "null": { + "name": "null", + "full_name": "registry.terraform.io/hashicorp/null" + } + }, + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_config_key": "coder", + "expressions": { + "arch": { + "constant_value": "amd64" + }, + "os": { + "constant_value": "linux" + } + }, + "schema_version": 0 + }, + { + "address": "coder_script.script1", + "mode": "managed", + "type": "coder_script", + "name": "script1", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "display_name": { + "constant_value": "Foobar Script 1" + }, + "run_on_start": { + "constant_value": true + }, + "script": { + "constant_value": "echo foobar 1" + } + }, + "schema_version": 0 + }, + { + "address": "coder_script.script2", + "mode": "managed", + "type": "coder_script", + "name": "script2", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev1.id", + "coder_agent.dev1" + ] + }, + "display_name": { + "constant_value": "Foobar Script 2" + }, + "run_on_start": { + "constant_value": true + }, + "script": { + "constant_value": "echo foobar 2" + } + }, + "schema_version": 0 + }, + { + "address": "coder_script.script3", + "mode": "managed", + "type": "coder_script", + "name": "script3", + "provider_config_key": "coder", + "expressions": { + "agent_id": { + "references": [ + "coder_agent.dev2.id", + "coder_agent.dev2" + ] + }, + "display_name": { + "constant_value": "Foobar Script 3" + }, + "run_on_start": { + "constant_value": true + }, + "script": { + "constant_value": "echo foobar 3" + } + }, + "schema_version": 0 + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_config_key": "null", + "schema_version": 0, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + }, + "relevant_attributes": [ + { + "resource": "coder_agent.dev1", + "attribute": [ + "id" + ] + }, + { + "resource": "coder_agent.dev2", + "attribute": [ + "id" + ] + } + ], + "timestamp": "2024-07-02T14:33:34Z", + "applyable": true, + "complete": true, + "errored": false +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.dot b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.dot new file mode 100644 index 0000000000000..45afc475d18a0 --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.dot @@ -0,0 +1,31 @@ +digraph { + compound = "true" + newrank = "true" + subgraph "root" { + "[root] coder_agent.dev1 (expand)" [label = "coder_agent.dev1", shape = "box"] + "[root] coder_agent.dev2 (expand)" [label = "coder_agent.dev2", shape = "box"] + "[root] coder_script.script1 (expand)" [label = "coder_script.script1", shape = "box"] + "[root] coder_script.script2 (expand)" [label = "coder_script.script2", shape = "box"] + "[root] coder_script.script3 (expand)" [label = "coder_script.script3", shape = "box"] + "[root] null_resource.dev1 (expand)" [label = "null_resource.dev1", shape = "box"] + "[root] null_resource.dev2 (expand)" [label = "null_resource.dev2", shape = "box"] + "[root] provider[\"registry.terraform.io/coder/coder\"]" [label = "provider[\"registry.terraform.io/coder/coder\"]", shape = "diamond"] + "[root] provider[\"registry.terraform.io/hashicorp/null\"]" [label = "provider[\"registry.terraform.io/hashicorp/null\"]", shape = "diamond"] + "[root] coder_agent.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_agent.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/coder/coder\"]" + "[root] coder_script.script1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_script.script2 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] coder_script.script3 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] coder_agent.dev1 (expand)" + "[root] null_resource.dev1 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] null_resource.dev2 (expand)" -> "[root] coder_agent.dev2 (expand)" + "[root] null_resource.dev2 (expand)" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"]" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script1 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script2 (expand)" + "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" -> "[root] coder_script.script3 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev1 (expand)" + "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" -> "[root] null_resource.dev2 (expand)" + "[root] root" -> "[root] provider[\"registry.terraform.io/coder/coder\"] (close)" + "[root] root" -> "[root] provider[\"registry.terraform.io/hashicorp/null\"] (close)" + } +} diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json new file mode 100644 index 0000000000000..6f97a0251fade --- /dev/null +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json @@ -0,0 +1,207 @@ +{ + "format_version": "1.0", + "terraform_version": "1.8.5", + "values": { + "root_module": { + "resources": [ + { + "address": "coder_agent.dev1", + "mode": "managed", + "type": "coder_agent", + "name": "dev1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "a9701390-d4f1-4518-b258-31f4929018bb", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_agent.dev2", + "mode": "managed", + "type": "coder_agent", + "name": "dev2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "arch": "amd64", + "auth": "token", + "connection_timeout": 120, + "dir": null, + "display_apps": [ + { + "port_forwarding_helper": true, + "ssh_helper": true, + "vscode": true, + "vscode_insiders": false, + "web_terminal": true + } + ], + "env": null, + "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "init_script": "", + "login_before_ready": true, + "metadata": [], + "motd_file": null, + "order": null, + "os": "linux", + "shutdown_script": null, + "shutdown_script_timeout": 300, + "startup_script": null, + "startup_script_behavior": null, + "startup_script_timeout": 300, + "token": "24f26e84-d362-40bf-ad9c-cc99b8175c9b", + "troubleshooting_url": null + }, + "sensitive_values": { + "display_apps": [ + {} + ], + "metadata": [], + "token": true + } + }, + { + "address": "coder_script.script1", + "mode": "managed", + "type": "coder_script", + "name": "script1", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 1", + "icon": null, + "id": "845552f2-c2cb-4a97-9cb5-cfb7fb5492db", + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 1", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_script.script2", + "mode": "managed", + "type": "coder_script", + "name": "script2", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "cron": null, + "display_name": "Foobar Script 2", + "icon": null, + "id": "5457cf31-3fbb-452c-8198-1daeb8a3865c", + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 2", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "coder_script.script3", + "mode": "managed", + "type": "coder_script", + "name": "script3", + "provider_name": "registry.terraform.io/coder/coder", + "schema_version": 0, + "values": { + "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "cron": null, + "display_name": "Foobar Script 3", + "icon": null, + "id": "86967d7a-76a2-4206-8986-cd39946d8580", + "log_path": null, + "run_on_start": true, + "run_on_stop": false, + "script": "echo foobar 3", + "start_blocks_login": false, + "timeout": 0 + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + }, + { + "address": "null_resource.dev1", + "mode": "managed", + "type": "null_resource", + "name": "dev1", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "2102344837160367826", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev1" + ] + }, + { + "address": "null_resource.dev2", + "mode": "managed", + "type": "null_resource", + "name": "dev2", + "provider_name": "registry.terraform.io/hashicorp/null", + "schema_version": 0, + "values": { + "id": "5696575281159455178", + "triggers": null + }, + "sensitive_values": {}, + "depends_on": [ + "coder_agent.dev2" + ] + } + ] + } + } +} From f5484ecc2ef9096d9b31a01024254f8d9a8485f9 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 12:12:34 +0200 Subject: [PATCH 05/11] Fix: script tests --- provisioner/terraform/resources_test.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index 81f296d3292a0..bf1bc68a2f0d0 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -677,9 +677,6 @@ func TestConvertResources(t *testing.T) { }}, }, } { - if folderName != "multiple-agents-multiple-scripts" { - continue - } folderName := folderName expected := expected t.Run(folderName, func(t *testing.T) { @@ -709,6 +706,18 @@ func TestConvertResources(t *testing.T) { sortResources(state.Resources) sortExternalAuthProviders(state.ExternalAuthProviders) + for _, resource := range state.Resources { + for _, agent := range resource.Agents { + agent.Id = "" + if agent.GetToken() != "" { + agent.Auth = &proto.Agent_Token{} + } + if agent.GetInstanceId() != "" { + agent.Auth = &proto.Agent_InstanceId{} + } + } + } + expectedNoMetadata := make([]*proto.Resource, 0) for _, resource := range expected.resources { resourceCopy, _ := protobuf.Clone(resource).(*proto.Resource) @@ -771,6 +780,12 @@ func TestConvertResources(t *testing.T) { if agent.GetInstanceId() != "" { agent.Auth = &proto.Agent_InstanceId{} } + + for _, script := range agent.Scripts { + // FIXME `RunOnStart`` is set only in "Provision", so we need to clear it + // to simplify table tests. Current tests do not verify `RunOnStart` properties. + script.RunOnStart = false + } } } // Convert expectedNoMetadata and resources into a From 82bbc2af376e50981b3602b926efa74dcc17c3e9 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 10:59:32 +0000 Subject: [PATCH 06/11] generate 1.8.4 --- ...ltiple-agents-multiple-scripts.tfplan.json | 643 ++---------------- ...tiple-agents-multiple-scripts.tfstate.json | 26 +- provisioner/terraform/testdata/version.txt | 2 +- 3 files changed, 60 insertions(+), 611 deletions(-) diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json index b9027c51e265a..36b1d022ccb85 100644 --- a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfplan.json @@ -1,6 +1,6 @@ { "format_version": "1.2", - "terraform_version": "1.8.5", + "terraform_version": "1.8.4", "planned_values": { "root_module": { "resources": [ @@ -16,18 +16,7 @@ "auth": "token", "connection_timeout": 120, "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", "login_before_ready": true, "metadata": [], "motd_file": null, @@ -38,13 +27,10 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", "troubleshooting_url": null }, "sensitive_values": { - "display_apps": [ - {} - ], + "display_apps": [], "metadata": [], "token": true } @@ -61,18 +47,7 @@ "auth": "token", "connection_timeout": 120, "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", "login_before_ready": true, "metadata": [], "motd_file": null, @@ -83,13 +58,10 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", "troubleshooting_url": null }, "sensitive_values": { - "display_apps": [ - {} - ], + "display_apps": [], "metadata": [], "token": true } @@ -102,7 +74,6 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", "cron": null, "display_name": "Foobar Script 1", "icon": null, @@ -123,7 +94,6 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", "cron": null, "display_name": "Foobar Script 2", "icon": null, @@ -144,7 +114,6 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", "cron": null, "display_name": "Foobar Script 3", "icon": null, @@ -165,7 +134,6 @@ "provider_name": "registry.terraform.io/hashicorp/null", "schema_version": 0, "values": { - "id": "2102344837160367826", "triggers": null }, "sensitive_values": {} @@ -178,7 +146,6 @@ "provider_name": "registry.terraform.io/hashicorp/null", "schema_version": 0, "values": { - "id": "5696575281159455178", "triggers": null }, "sensitive_values": {} @@ -186,182 +153,6 @@ ] } }, - "resource_drift": [ - { - "address": "coder_agent.dev1", - "mode": "managed", - "type": "coder_agent", - "name": "dev1", - "provider_name": "registry.terraform.io/coder/coder", - "change": { - "actions": [ - "update" - ], - "before": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "770c0f3f-3c3a-4c74-a9e5-f8a2455e71f9", - "troubleshooting_url": null - }, - "after": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", - "troubleshooting_url": null - }, - "after_unknown": {}, - "before_sensitive": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - }, - "after_sensitive": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - } - } - }, - { - "address": "coder_agent.dev2", - "mode": "managed", - "type": "coder_agent", - "name": "dev2", - "provider_name": "registry.terraform.io/coder/coder", - "change": { - "actions": [ - "update" - ], - "before": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "43a865e0-fcf1-483e-83ce-e5c7e041b30b", - "troubleshooting_url": null - }, - "after": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", - "troubleshooting_url": null - }, - "after_unknown": {}, - "before_sensitive": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - }, - "after_sensitive": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - } - } - } - ], "resource_changes": [ { "address": "coder_agent.dev1", @@ -371,55 +162,15 @@ "provider_name": "registry.terraform.io/coder/coder", "change": { "actions": [ - "no-op" + "create" ], - "before": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", - "troubleshooting_url": null - }, + "before": null, "after": { "arch": "amd64", "auth": "token", "connection_timeout": 120, "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", "login_before_ready": true, "metadata": [], "motd_file": null, @@ -430,21 +181,18 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", "troubleshooting_url": null }, - "after_unknown": {}, - "before_sensitive": { - "display_apps": [ - {} - ], + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, "metadata": [], "token": true }, + "before_sensitive": false, "after_sensitive": { - "display_apps": [ - {} - ], + "display_apps": [], "metadata": [], "token": true } @@ -458,55 +206,15 @@ "provider_name": "registry.terraform.io/coder/coder", "change": { "actions": [ - "no-op" + "create" ], - "before": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", - "troubleshooting_url": null - }, + "before": null, "after": { "arch": "amd64", "auth": "token", "connection_timeout": 120, "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", "login_before_ready": true, "metadata": [], "motd_file": null, @@ -517,21 +225,18 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", "troubleshooting_url": null }, - "after_unknown": {}, - "before_sensitive": { - "display_apps": [ - {} - ], + "after_unknown": { + "display_apps": true, + "id": true, + "init_script": true, "metadata": [], "token": true }, + "before_sensitive": false, "after_sensitive": { - "display_apps": [ - {} - ], + "display_apps": [], "metadata": [], "token": true } @@ -545,24 +250,10 @@ "provider_name": "registry.terraform.io/coder/coder", "change": { "actions": [ - "delete", "create" ], - "before": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "cron": null, - "display_name": "Foobar Script 1", - "icon": null, - "id": "2d80a385-efb9-4db8-a4c2-94cb94f07c3b", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 1", - "start_blocks_login": false, - "timeout": 0 - }, + "before": null, "after": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", "cron": null, "display_name": "Foobar Script 1", "icon": null, @@ -574,12 +265,12 @@ "timeout": 0 }, "after_unknown": { + "agent_id": true, "id": true }, - "before_sensitive": {}, + "before_sensitive": false, "after_sensitive": {} - }, - "action_reason": "replace_because_tainted" + } }, { "address": "coder_script.script2", @@ -589,24 +280,10 @@ "provider_name": "registry.terraform.io/coder/coder", "change": { "actions": [ - "delete", "create" ], - "before": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "cron": null, - "display_name": "Foobar Script 2", - "icon": null, - "id": "fb867f82-2d59-4e1e-84d5-83348f9f3b48", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 2", - "start_blocks_login": false, - "timeout": 0 - }, + "before": null, "after": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", "cron": null, "display_name": "Foobar Script 2", "icon": null, @@ -618,12 +295,12 @@ "timeout": 0 }, "after_unknown": { + "agent_id": true, "id": true }, - "before_sensitive": {}, + "before_sensitive": false, "after_sensitive": {} - }, - "action_reason": "replace_because_tainted" + } }, { "address": "coder_script.script3", @@ -633,24 +310,10 @@ "provider_name": "registry.terraform.io/coder/coder", "change": { "actions": [ - "delete", "create" ], - "before": { - "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "cron": null, - "display_name": "Foobar Script 3", - "icon": null, - "id": "79e6cc0e-cb2f-4464-be93-4828e8acbc46", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 3", - "start_blocks_login": false, - "timeout": 0 - }, + "before": null, "after": { - "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", "cron": null, "display_name": "Foobar Script 3", "icon": null, @@ -662,12 +325,12 @@ "timeout": 0 }, "after_unknown": { + "agent_id": true, "id": true }, - "before_sensitive": {}, + "before_sensitive": false, "after_sensitive": {} - }, - "action_reason": "replace_because_tainted" + } }, { "address": "null_resource.dev1", @@ -677,18 +340,16 @@ "provider_name": "registry.terraform.io/hashicorp/null", "change": { "actions": [ - "no-op" + "create" ], - "before": { - "id": "2102344837160367826", - "triggers": null - }, + "before": null, "after": { - "id": "2102344837160367826", "triggers": null }, - "after_unknown": {}, - "before_sensitive": {}, + "after_unknown": { + "id": true + }, + "before_sensitive": false, "after_sensitive": {} } }, @@ -700,232 +361,20 @@ "provider_name": "registry.terraform.io/hashicorp/null", "change": { "actions": [ - "no-op" + "create" ], - "before": { - "id": "5696575281159455178", - "triggers": null - }, + "before": null, "after": { - "id": "5696575281159455178", "triggers": null }, - "after_unknown": {}, - "before_sensitive": {}, + "after_unknown": { + "id": true + }, + "before_sensitive": false, "after_sensitive": {} } } ], - "prior_state": { - "format_version": "1.0", - "terraform_version": "1.8.5", - "values": { - "root_module": { - "resources": [ - { - "address": "coder_agent.dev1", - "mode": "managed", - "type": "coder_agent", - "name": "dev1", - "provider_name": "registry.terraform.io/coder/coder", - "schema_version": 0, - "values": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "aafdccb0-badd-488f-8ab5-af3984b328fe", - "troubleshooting_url": null - }, - "sensitive_values": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - } - }, - { - "address": "coder_agent.dev2", - "mode": "managed", - "type": "coder_agent", - "name": "dev2", - "provider_name": "registry.terraform.io/coder/coder", - "schema_version": 0, - "values": { - "arch": "amd64", - "auth": "token", - "connection_timeout": 120, - "dir": null, - "display_apps": [ - { - "port_forwarding_helper": true, - "ssh_helper": true, - "vscode": true, - "vscode_insiders": false, - "web_terminal": true - } - ], - "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "init_script": "", - "login_before_ready": true, - "metadata": [], - "motd_file": null, - "order": null, - "os": "linux", - "shutdown_script": null, - "shutdown_script_timeout": 300, - "startup_script": null, - "startup_script_behavior": null, - "startup_script_timeout": 300, - "token": "89f0aca1-8619-4458-9e1e-a251f4083e73", - "troubleshooting_url": null - }, - "sensitive_values": { - "display_apps": [ - {} - ], - "metadata": [], - "token": true - } - }, - { - "address": "coder_script.script1", - "mode": "managed", - "type": "coder_script", - "name": "script1", - "provider_name": "registry.terraform.io/coder/coder", - "schema_version": 0, - "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "cron": null, - "display_name": "Foobar Script 1", - "icon": null, - "id": "2d80a385-efb9-4db8-a4c2-94cb94f07c3b", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 1", - "start_blocks_login": false, - "timeout": 0 - }, - "sensitive_values": {}, - "depends_on": [ - "coder_agent.dev1" - ], - "tainted": true - }, - { - "address": "coder_script.script2", - "mode": "managed", - "type": "coder_script", - "name": "script2", - "provider_name": "registry.terraform.io/coder/coder", - "schema_version": 0, - "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", - "cron": null, - "display_name": "Foobar Script 2", - "icon": null, - "id": "fb867f82-2d59-4e1e-84d5-83348f9f3b48", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 2", - "start_blocks_login": false, - "timeout": 0 - }, - "sensitive_values": {}, - "depends_on": [ - "coder_agent.dev1" - ], - "tainted": true - }, - { - "address": "coder_script.script3", - "mode": "managed", - "type": "coder_script", - "name": "script3", - "provider_name": "registry.terraform.io/coder/coder", - "schema_version": 0, - "values": { - "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", - "cron": null, - "display_name": "Foobar Script 3", - "icon": null, - "id": "79e6cc0e-cb2f-4464-be93-4828e8acbc46", - "log_path": null, - "run_on_start": false, - "run_on_stop": false, - "script": "echo foobar 3", - "start_blocks_login": false, - "timeout": 0 - }, - "sensitive_values": {}, - "depends_on": [ - "coder_agent.dev2" - ], - "tainted": true - }, - { - "address": "null_resource.dev1", - "mode": "managed", - "type": "null_resource", - "name": "dev1", - "provider_name": "registry.terraform.io/hashicorp/null", - "schema_version": 0, - "values": { - "id": "2102344837160367826", - "triggers": null - }, - "sensitive_values": {}, - "depends_on": [ - "coder_agent.dev1" - ] - }, - { - "address": "null_resource.dev2", - "mode": "managed", - "type": "null_resource", - "name": "dev2", - "provider_name": "registry.terraform.io/hashicorp/null", - "schema_version": 0, - "values": { - "id": "5696575281159455178", - "triggers": null - }, - "sensitive_values": {}, - "depends_on": [ - "coder_agent.dev2" - ] - } - ] - } - } - }, "configuration": { "provider_config": { "coder": { @@ -1074,19 +523,19 @@ }, "relevant_attributes": [ { - "resource": "coder_agent.dev1", + "resource": "coder_agent.dev2", "attribute": [ "id" ] }, { - "resource": "coder_agent.dev2", + "resource": "coder_agent.dev1", "attribute": [ "id" ] } ], - "timestamp": "2024-07-02T14:33:34Z", + "timestamp": "2024-07-03T10:58:35Z", "applyable": true, "complete": true, "errored": false diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json index 6f97a0251fade..615716b3ea521 100644 --- a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tfstate.json @@ -1,6 +1,6 @@ { "format_version": "1.0", - "terraform_version": "1.8.5", + "terraform_version": "1.8.4", "values": { "root_module": { "resources": [ @@ -26,7 +26,7 @@ } ], "env": null, - "id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "id": "26676b01-8c32-4fe2-af05-8409004c2132", "init_script": "", "login_before_ready": true, "metadata": [], @@ -38,7 +38,7 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "a9701390-d4f1-4518-b258-31f4929018bb", + "token": "4d98aa2e-1b27-4a22-9658-0ccde329415c", "troubleshooting_url": null }, "sensitive_values": { @@ -71,7 +71,7 @@ } ], "env": null, - "id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "id": "ad10d725-ec7d-45f4-8b83-d67f94878f3c", "init_script": "", "login_before_ready": true, "metadata": [], @@ -83,7 +83,7 @@ "startup_script": null, "startup_script_behavior": null, "startup_script_timeout": 300, - "token": "24f26e84-d362-40bf-ad9c-cc99b8175c9b", + "token": "de109669-b8e5-479d-82d0-2d0471f9a2cf", "troubleshooting_url": null }, "sensitive_values": { @@ -102,11 +102,11 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "agent_id": "26676b01-8c32-4fe2-af05-8409004c2132", "cron": null, "display_name": "Foobar Script 1", "icon": null, - "id": "845552f2-c2cb-4a97-9cb5-cfb7fb5492db", + "id": "3083dd1d-67a0-46eb-a8c1-8d3d83a411c1", "log_path": null, "run_on_start": true, "run_on_stop": false, @@ -127,11 +127,11 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "24deb97d-1b74-4bb8-88fc-0f06e5727847", + "agent_id": "26676b01-8c32-4fe2-af05-8409004c2132", "cron": null, "display_name": "Foobar Script 2", "icon": null, - "id": "5457cf31-3fbb-452c-8198-1daeb8a3865c", + "id": "ddb41617-27e2-43c8-b735-99d8567f46ca", "log_path": null, "run_on_start": true, "run_on_stop": false, @@ -152,11 +152,11 @@ "provider_name": "registry.terraform.io/coder/coder", "schema_version": 0, "values": { - "agent_id": "2ed40522-1a32-48aa-a3f1-c68039426f15", + "agent_id": "ad10d725-ec7d-45f4-8b83-d67f94878f3c", "cron": null, "display_name": "Foobar Script 3", "icon": null, - "id": "86967d7a-76a2-4206-8986-cd39946d8580", + "id": "d793afab-f40a-4ae2-99d5-eae9e3d0d45f", "log_path": null, "run_on_start": true, "run_on_stop": false, @@ -177,7 +177,7 @@ "provider_name": "registry.terraform.io/hashicorp/null", "schema_version": 0, "values": { - "id": "2102344837160367826", + "id": "4183830202442917773", "triggers": null }, "sensitive_values": {}, @@ -193,7 +193,7 @@ "provider_name": "registry.terraform.io/hashicorp/null", "schema_version": 0, "values": { - "id": "5696575281159455178", + "id": "6920808379078063017", "triggers": null }, "sensitive_values": {}, diff --git a/provisioner/terraform/testdata/version.txt b/provisioner/terraform/testdata/version.txt index 8decb929b9863..bfa363e76ed71 100644 --- a/provisioner/terraform/testdata/version.txt +++ b/provisioner/terraform/testdata/version.txt @@ -1 +1 @@ -1.8.5 +1.8.4 From 0fc48c4816982f35277db7d6da4c0cf04d9a62be Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 13:14:42 +0200 Subject: [PATCH 07/11] fmt --- .../multiple-agents-multiple-scripts.tf | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf index 795bc757c93ca..af041e2da350d 100644 --- a/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf +++ b/provisioner/terraform/testdata/multiple-agents-multiple-scripts/multiple-agents-multiple-scripts.tf @@ -18,25 +18,25 @@ resource "coder_agent" "dev2" { } resource "coder_script" "script1" { - agent_id = coder_agent.dev1.id + agent_id = coder_agent.dev1.id display_name = "Foobar Script 1" - script = "echo foobar 1" + script = "echo foobar 1" run_on_start = true } resource "coder_script" "script2" { - agent_id = coder_agent.dev1.id + agent_id = coder_agent.dev1.id display_name = "Foobar Script 2" - script = "echo foobar 2" + script = "echo foobar 2" run_on_start = true } resource "coder_script" "script3" { - agent_id = coder_agent.dev2.id + agent_id = coder_agent.dev2.id display_name = "Foobar Script 3" - script = "echo foobar 3" + script = "echo foobar 3" run_on_start = true } From 420c352a101114dabaa010e07895820055c58386 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 13:21:32 +0200 Subject: [PATCH 08/11] fix --- provisioner/terraform/testdata/generate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisioner/terraform/testdata/generate.sh b/provisioner/terraform/testdata/generate.sh index 401f8ed9fcc08..04ac7bdef3c64 100755 --- a/provisioner/terraform/testdata/generate.sh +++ b/provisioner/terraform/testdata/generate.sh @@ -3,7 +3,7 @@ set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" -for d in multiple-agents-multiple-scripts/; do +for d in */; do pushd "$d" name=$(basename "$(pwd)") From e3be04760f6141b0d13f13f5b9b675a68924951a Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 13:27:34 +0200 Subject: [PATCH 09/11] scripts --- provisioner/terraform/resources_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index bf1bc68a2f0d0..e88f36ab25927 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -331,10 +331,12 @@ func TestConvertResources(t *testing.T) { { DisplayName: "Foobar Script 1", Script: "echo foobar 1", + RunOnStart: true, }, { DisplayName: "Foobar Script 2", Script: "echo foobar 2", + RunOnStart: true, }, }, Auth: &proto.Agent_Token{}, @@ -352,6 +354,7 @@ func TestConvertResources(t *testing.T) { { DisplayName: "Foobar Script 3", Script: "echo foobar 3", + RunOnStart: true, }, }, Auth: &proto.Agent_Token{}, @@ -780,12 +783,6 @@ func TestConvertResources(t *testing.T) { if agent.GetInstanceId() != "" { agent.Auth = &proto.Agent_InstanceId{} } - - for _, script := range agent.Scripts { - // FIXME `RunOnStart`` is set only in "Provision", so we need to clear it - // to simplify table tests. Current tests do not verify `RunOnStart` properties. - script.RunOnStart = false - } } } // Convert expectedNoMetadata and resources into a From 3aea6660bd176baf97e9b50352e04a4922117900 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 13:37:00 +0200 Subject: [PATCH 10/11] Envs and scripts --- provisioner/terraform/resources_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/provisioner/terraform/resources_test.go b/provisioner/terraform/resources_test.go index e88f36ab25927..5842cbca46833 100644 --- a/provisioner/terraform/resources_test.go +++ b/provisioner/terraform/resources_test.go @@ -1066,6 +1066,12 @@ func sortResources(resources []*proto.Resource) { sort.Slice(agent.Apps, func(i, j int) bool { return agent.Apps[i].Slug < agent.Apps[j].Slug }) + sort.Slice(agent.ExtraEnvs, func(i, j int) bool { + return agent.ExtraEnvs[i].Name < agent.ExtraEnvs[j].Name + }) + sort.Slice(agent.Scripts, func(i, j int) bool { + return agent.Scripts[i].DisplayName < agent.Scripts[j].DisplayName + }) } sort.Slice(resource.Agents, func(i, j int) bool { return resource.Agents[i].Name < resource.Agents[j].Name From 1d2f7977746bf8f343883ada0ea1882f8b1bdea2 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Wed, 3 Jul 2024 14:46:53 +0200 Subject: [PATCH 11/11] fix: naming --- provisioner/terraform/resources.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/provisioner/terraform/resources.go b/provisioner/terraform/resources.go index fd24f0aa86c46..b814ffefd3544 100644 --- a/provisioner/terraform/resources.go +++ b/provisioner/terraform/resources.go @@ -750,17 +750,17 @@ func convertAddressToLabel(address string) string { return cut } -func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, appAgentID string, resource *tfjson.StateResource) bool { +func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, resourceAgentID string, resource *tfjson.StateResource) bool { // Plan: we need to find if there is edge between the agent and the resource. - if agent.Id == "" && appAgentID == "" { - appNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, resource.Type, resource.Name) + if agent.Id == "" && resourceAgentID == "" { + resourceNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, resource.Type, resource.Name) agentNodeSuffix := fmt.Sprintf(`] coder_agent.%s (expand)"`, agent.Name) // Traverse the graph to check if the coder_ depends on coder_agent. for _, dst := range graph.Edges.SrcToDsts { for _, edges := range dst { for _, edge := range edges { - if strings.HasSuffix(edge.Src, appNodeSuffix) && + if strings.HasSuffix(edge.Src, resourceNodeSuffix) && strings.HasSuffix(edge.Dst, agentNodeSuffix) { return true } @@ -770,8 +770,8 @@ func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, appAgentID stri return false } - // Provision: agent ID and app ID are present - return agent.Id == appAgentID + // Provision: agent ID and child resource ID are present + return agent.Id == resourceAgentID } type graphResource struct {