Skip to content

Commit

Permalink
feat: add coderd_template resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Jul 19, 2024
1 parent 2fca026 commit eb0c94a
Show file tree
Hide file tree
Showing 13 changed files with 727 additions and 1 deletion.
48 changes: 48 additions & 0 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coderd_template Resource - coderd"
subcategory: ""
description: |-
A Coder template
---

# coderd_template (Resource)

A Coder template



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the template.

### Optional

- `description` (String) A description of the template.
- `display_name` (String) The display name of the template. Defaults to the template name.
- `organization_id` (String) The ID of the organization. Defaults to the provider's default organization
- `version` (Block List) (see [below for nested schema](#nestedblock--version))

### Read-Only

- `id` (String) The ID of the template.

<a id="nestedblock--version"></a>
### Nested Schema for `version`

Required:

- `directory` (String) A path to the directory to create the template version from. Changes in the directory contents will trigger the creation of a new template version.

Optional:

- `active` (Boolean) Whether this version is the active version of the template. Only one version can be active at a time.
- `message` (String) A message describing the changes in this version of the template. Messages longer than 72 characters will be truncated..
- `name` (String) The name of the template version. Automatically generated if not provided.

Read-Only:

- `directory_hash` (String)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ require (
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6 h1:KHblWIE/KHOwQ6lEbMZt6YpcGve2FEZ1sDtrW1Am5UI=
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6/go.mod h1:NaoTA7KwopCrnaSb0JXTC0PTp/O/Y83Lndnq0OEV3ZQ=
cloud.google.com/go v0.110.7 h1:rJyC7nWRg2jWGZ4wSJ5nY65GTdYJkg0cd/uXb+ACI6o=
cloud.google.com/go v0.110.10 h1:LXy9GEO+timppncPIAZoOj3l58LIU9k+kn48AN7IO3Y=
cloud.google.com/go/compute v1.25.1 h1:ZRpHJedLtTpKgr3RV1Fx23NuaAEN1Zfx9hw1u4aJdjU=
cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc=
cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
Expand Down Expand Up @@ -132,6 +132,8 @@ github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
Expand Down Expand Up @@ -388,6 +390,8 @@ github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
Expand Down
5 changes: 5 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ func TestIntegration(t *testing.T) {
assert.Equal(t, group.QuotaAllowance, 100)
},
},
{
name: "template-test",
preF: func(t testing.TB, c *codersdk.Client) {},
assertF: func(t testing.TB, c *codersdk.Client) {},
},
} {
t.Run(tt.name, func(t *testing.T) {
client := StartCoder(ctx, t, tt.name, true)
Expand Down
1 change: 1 addition & 0 deletions integration/template-test/example-template-2/new
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
12 changes: 12 additions & 0 deletions integration/template-test/example-template/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "name" {
type = string
}

resource "local_file" "a" {
filename = "${path.module}/a.txt"
content = "hello ${var.name}"
}

output "a" {
value = local_file.a.content
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "world"
18 changes: 18 additions & 0 deletions integration/template-test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
coderd = {
source = "coder/coderd"
version = ">=0.0.0"
}
}
}

resource "coderd_template" "sample" {
name = "example-template"
versions = [
{
name = "v1"
directory = "./example-template"
}
]
}
45 changes: 45 additions & 0 deletions internal/provider/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package provider

import (
"context"

"cdr.dev/slog"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

var _ slog.Sink = &tfLogSink{}

type tfLogSink struct {
tfCtx context.Context
}

func newTFLogSink(tfCtx context.Context) *tfLogSink {
return &tfLogSink{
tfCtx: tfCtx,
}
}

func (s *tfLogSink) LogEntry(ctx context.Context, e slog.SinkEntry) {
var logFn func(ctx context.Context, msg string, additionalFields ...map[string]interface{})
switch e.Level {
case slog.LevelDebug:
logFn = tflog.Debug
case slog.LevelInfo:
logFn = tflog.Info
case slog.LevelWarn:
logFn = tflog.Warn
default:
logFn = tflog.Error
}
logFn(s.tfCtx, e.Message, mapToFields(e.Fields))
}

func (s *tfLogSink) Sync() {}

func mapToFields(m slog.Map) map[string]interface{} {
fields := make(map[string]interface{}, len(m))
for _, v := range m {
fields[v.Name] = v.Value
}
return fields
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (p *CoderdProvider) Resources(ctx context.Context) []func() resource.Resour
return []func() resource.Resource{
NewUserResource,
NewGroupResource,
NewTemplateResource,
}
}

Expand Down
Loading

0 comments on commit eb0c94a

Please sign in to comment.