Skip to content

feat!: extract provisioner tags from coder_workspace_tags data source #15578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Nov 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address indentation
  • Loading branch information
johnstcn committed Nov 22, 2024
commit 8ec672c849110e17d87fc326a86c090759e8b1a8
222 changes: 111 additions & 111 deletions coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,80 +254,80 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
name: "main.tf with no tags",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}`,
},
wantTags: map[string]string{"owner": "", "scope": "organization"},
},
{
name: "main.tf with empty workspace tags",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {}
}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {}
}`,
},
wantTags: map[string]string{"owner": "", "scope": "organization"},
},
{
name: "main.tf with workspace tags",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
}
}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
}
}`,
},
wantTags: map[string]string{"owner": "", "scope": "organization", "foo": "bar", "a": "1", "b": "2"},
},
{
name: "main.tf with workspace tags and request tags",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
}
}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
}
}`,
},
reqTags: map[string]string{"baz": "zap", "foo": "noclobber"},
wantTags: map[string]string{"owner": "", "scope": "organization", "foo": "bar", "baz": "zap", "a": "1", "b": "2"},
Expand All @@ -336,51 +336,51 @@ data "coder_workspace_tags" "tags" {
name: "main.tf with disallowed workspace tag value",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {
name = "foo"
}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
"test": null_resource.test.name,
}
}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {
name = "foo"
}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
"test": null_resource.test.name,
}
}`,
},
expectError: `Unknown variable; There is no variable named "null_resource".`,
},
{
name: "main.tf with disallowed function in tag value",
files: map[string]string{
`main.tf`: `
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {
name = "foo"
}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
"test": try(null_resource.test.name, "whatever"),
}
}`,
variable "a" {
type = string
default = "1"
}
data "coder_parameter" "b" {
type = string
default = "2"
}
resource "null_resource" "test" {
name = "foo"
}
data "coder_workspace_tags" "tags" {
tags = {
"foo": "bar",
"a": var.a,
"b": data.coder_parameter.b.value,
"test": try(null_resource.test.name, "whatever"),
}
}`,
},
expectError: `Function calls not allowed; Functions may not be called here.`,
},
Expand All @@ -392,40 +392,40 @@ data "coder_workspace_tags" "tags" {
name: "main.tf with workspace tags that attempts to set user scope",
files: map[string]string{
`main.tf`: `
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "user",
"owner": "12345678-1234-1234-1234-1234567890ab",
}
}`,
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "user",
"owner": "12345678-1234-1234-1234-1234567890ab",
}
}`,
},
wantTags: map[string]string{"owner": templateAdminUser.ID.String(), "scope": "user"},
},
{
name: "main.tf with workspace tags that attempt to clobber org ID",
files: map[string]string{
`main.tf`: `
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "organization",
"owner": "12345678-1234-1234-1234-1234567890ab",
}
}`,
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "organization",
"owner": "12345678-1234-1234-1234-1234567890ab",
}
}`,
},
wantTags: map[string]string{"owner": "", "scope": "organization"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be an error? Silently replacing seems like a recipe for confusion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating that, but you've sold me. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might make this a follow-up, as the existing behaviour of provisionersdk.MutateTags is to overwrite provisionersdk.TagOwner

https://github.com/coder/coder/pull/15518/files#diff-01b662bbd7ba1a97489f36a381633c882cc2126dab8fb9ede81d3cb9239dd035R36-R40

},
{
name: "main.tf with workspace tags that set scope=user",
files: map[string]string{
`main.tf`: `
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "user",
}
}`,
resource "null_resource" "test" {}
data "coder_workspace_tags" "tags" {
tags = {
"scope": "user",
}
}`,
},
wantTags: map[string]string{"owner": templateAdminUser.ID.String(), "scope": "user"},
},
Expand Down