Skip to content

Commit

Permalink
feat: Add Terraform Provider for agent resources (#368)
Browse files Browse the repository at this point in the history
* ci: Update DataDog GitHub branch to fallback to GITHUB_REF

This was detecting branches, but not our "main" branch before.
Hopefully this fixes it!

* Add basic Terraform Provider

* Rename post files to upload

* Add tests for resources

* Skip instance identity test

* Add tests for ensuring agent get's passed through properly

* Fix linting errors

* Add echo path

* Fix agent authentication

* Update codersdk/files.go

Co-authored-by: Bryan <bryan@coder.com>

Co-authored-by: Bryan <bryan@coder.com>
  • Loading branch information
kylecarbs and bryphe-coder committed Feb 28, 2022
1 parent 512e239 commit 35ae532
Show file tree
Hide file tree
Showing 18 changed files with 1,262 additions and 201 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
INSTALL_DIR=$(shell go env GOPATH)/bin
GOOS=$(shell go env GOOS)
GOARCH=$(shell go env GOARCH)

bin/coder:
mkdir -p bin
go build -o bin/coder cmd/coder/main.go
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o bin/coder-$(GOOS)-$(GOARCH) cmd/coder/main.go
.PHONY: bin/coder

bin/coderd:
mkdir -p bin
go build -o bin/coderd cmd/coderd/main.go
.PHONY: bin/coderd

build: site/out bin/coder bin/coderd
bin/terraform-provider-coder:
mkdir -p bin
go build -o bin/terraform-provider-coder cmd/terraform-provider-coder/main.go
.PHONY: bin/terraform-provider-coder

build: site/out bin/coder bin/coderd bin/terraform-provider-coder
.PHONY: build

# Runs migrations to output a dump of the database.
Expand Down Expand Up @@ -59,6 +66,11 @@ install:
@echo "-- CLI available at $(shell ls $(INSTALL_DIR)/coder*)"
.PHONY: install

install/terraform-provider-coder: bin/terraform-provider-coder
$(eval OS_ARCH := $(shell go env GOOS)_$(shell go env GOARCH))
mkdir -p ~/.terraform.d/plugins/coder.com/internal/coder/0.2/$(OS_ARCH)
cp bin/terraform-provider-coder ~/.terraform.d/plugins/coder.com/internal/coder/0.2/$(OS_ARCH)

peerbroker/proto: peerbroker/proto/peerbroker.proto
protoc \
--go_out=. \
Expand Down
13 changes: 13 additions & 0 deletions cmd/terraform-provider-coder/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"

"github.com/coder/coder/provisioner/terraform/provider"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: provider.New,
})
}
4 changes: 2 additions & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func New(options *Options) (http.Handler, func()) {
})
})

r.Route("/files", func(r chi.Router) {
r.Route("/upload", func(r chi.Router) {
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
r.Post("/", api.postFiles)
r.Post("/", api.postUpload)
})

r.Route("/projectimport/{organization}", func(r chi.Router) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type UploadFileResponse struct {
Hash string `json:"hash"`
}

func (api *api) postFiles(rw http.ResponseWriter, r *http.Request) {
func (api *api) postUpload(rw http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
contentType := r.Header.Get("Content-Type")

Expand Down
2 changes: 1 addition & 1 deletion coderd/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/coder/coder/codersdk"
)

func TestPostFiles(t *testing.T) {
func TestPostUpload(t *testing.T) {
t.Parallel()
t.Run("BadContentType", func(t *testing.T) {
t.Parallel()
Expand Down
16 changes: 10 additions & 6 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,20 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
}
// This could be a bulk insert to improve performance.
for _, protoResource := range jobType.WorkspaceProvision.Resources {
var instanceID sql.NullString
if protoResource.Agent != nil && protoResource.Agent.GetGoogleInstanceIdentity() != nil {
instanceID = sql.NullString{
String: protoResource.Agent.GetGoogleInstanceIdentity().InstanceId,
Valid: true,
}
}
_, err = db.InsertWorkspaceResource(ctx, database.InsertWorkspaceResourceParams{
ID: uuid.New(),
CreatedAt: database.Now(),
WorkspaceHistoryID: input.WorkspaceHistoryID,
InstanceID: sql.NullString{
Valid: protoResource.InstanceId != "",
String: protoResource.InstanceId,
},
Type: protoResource.Type,
Name: protoResource.Name,
Type: protoResource.Type,
Name: protoResource.Name,
InstanceID: instanceID,
// TODO: Generate this at the variable validation phase.
// Set the value in `default_source`, and disallow overwrite.
WorkspaceAgentToken: uuid.NewString(),
Expand Down
12 changes: 9 additions & 3 deletions coderd/workspaceagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ func TestPostWorkspaceAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
Type: &proto.Provision_Response_Complete{
Complete: &proto.Provision_Complete{
Resources: []*proto.Resource{{
Name: "somename",
Type: "someinstance",
InstanceId: instanceID,
Name: "somename",
Type: "someinstance",
Agent: &proto.Agent{
Auth: &proto.Agent_GoogleInstanceIdentity{
GoogleInstanceIdentity: &proto.GoogleInstanceIdentityAuth{
InstanceId: instanceID,
},
},
},
}},
},
},
Expand Down
9 changes: 8 additions & 1 deletion codersdk/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package codersdk
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/coder/coder/coderd"
)
Expand All @@ -13,7 +15,7 @@ const (
)

func (c *Client) UploadFile(ctx context.Context, contentType string, content []byte) (coderd.UploadFileResponse, error) {
res, err := c.request(ctx, http.MethodPost, "/api/v2/files", content, func(r *http.Request) {
res, err := c.request(ctx, http.MethodPost, "/api/v2/upload", content, func(r *http.Request) {
r.Header.Set("Content-Type", contentType)
})
if err != nil {
Expand All @@ -26,3 +28,8 @@ func (c *Client) UploadFile(ctx context.Context, contentType string, content []b
var resp coderd.UploadFileResponse
return resp, json.NewDecoder(res.Body).Decode(&resp)
}

// DownloadURL returns the download URL for the specified asset
func (c *Client) DownloadURL(asset string) (*url.URL, error) {
return c.URL.Parse(fmt.Sprintf("/api/v2/downloads/%s", asset))
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f
github.com/hashicorp/terraform-exec v0.15.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87
github.com/justinas/nosurf v1.1.1
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
Expand Down Expand Up @@ -96,19 +97,35 @@ require (
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.1 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/hc-install v0.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.5.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.2.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc // indirect
github.com/klauspost/compress v1.14.3 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.0 // indirect
Expand All @@ -129,6 +146,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down

0 comments on commit 35ae532

Please sign in to comment.