From bcb213e7a615f301e5baf9718a21585ee4a5570c Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Mon, 19 Jun 2023 16:17:10 +0800 Subject: [PATCH 1/5] new library function to start a Run (CreateRun) --- lib/run.go | 38 ++++++++++++++++++++++++++++++++++++++ lib/run_test.go | 12 ++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/run.go create mode 100644 lib/run_test.go diff --git a/lib/run.go b/lib/run.go new file mode 100644 index 0000000..aabcde6 --- /dev/null +++ b/lib/run.go @@ -0,0 +1,38 @@ +package lib + +import ( + "net/http" + + "github.com/Jeffail/gabs/v2" +) + +type RunConfig struct { + Message string + WorkspaceID string +} + +func CreateRun(config RunConfig) error { + u := NewTfcUrl("/runs") + payload := buildRunPayload(config.Message, config.WorkspaceID) + _ = callAPI(http.MethodPost, u.String(), payload, nil) + return nil +} + +func buildRunPayload(message, workspaceID string) string { + data := gabs.New() + + _, err := data.Object("data") + if err != nil { + return "error" + } + + if _, err = data.SetP(message, "data.attributes.message"); err != nil { + return "unable to process attribute for update:" + err.Error() + } + + if _, err = data.SetP(workspaceID, "data.relationships.data.id"); err != nil { + return "unable to process attribute for update:" + err.Error() + } + + return data.String() +} diff --git a/lib/run_test.go b/lib/run_test.go new file mode 100644 index 0000000..cdc6146 --- /dev/null +++ b/lib/run_test.go @@ -0,0 +1,12 @@ +package lib + +import ( + "testing" +) + +func Test_buildRunPayload(t *testing.T) { + got := buildRunPayload("my message", "ws_id") + if got != `{"data":{"attributes":{"message":"my message"},"relationships":{"data":{"id":"ws_id"}}}}` { + t.Fatalf("did not get expected result, got %q", got) + } +} From dc61039339caf882f49e071858c3f267c87f85b6 Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Mon, 19 Jun 2023 16:34:06 +0800 Subject: [PATCH 2/5] missed "workspace" in the request schema --- lib/run_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/run_test.go b/lib/run_test.go index cdc6146..43be5ec 100644 --- a/lib/run_test.go +++ b/lib/run_test.go @@ -6,7 +6,7 @@ import ( func Test_buildRunPayload(t *testing.T) { got := buildRunPayload("my message", "ws_id") - if got != `{"data":{"attributes":{"message":"my message"},"relationships":{"data":{"id":"ws_id"}}}}` { + if got != `{"data":{"attributes":{"message":"my message"},"relationships":{"workspace":{"data":{"id":"ws_id"}}}}}` { t.Fatalf("did not get expected result, got %q", got) } } From 5823989f5aea4df93019ec83f6c0f970f796bd50 Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Mon, 19 Jun 2023 16:45:30 +0800 Subject: [PATCH 3/5] didn't commit both files, ugh --- lib/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/run.go b/lib/run.go index aabcde6..80c9422 100644 --- a/lib/run.go +++ b/lib/run.go @@ -30,7 +30,7 @@ func buildRunPayload(message, workspaceID string) string { return "unable to process attribute for update:" + err.Error() } - if _, err = data.SetP(workspaceID, "data.relationships.data.id"); err != nil { + if _, err = data.SetP(workspaceID, "data.relationships.workspace.data.id"); err != nil { return "unable to process attribute for update:" + err.Error() } From 9cfea59deb73b824a1fef19d8928208d7fc8c80b Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Mon, 19 Jun 2023 16:57:16 +0800 Subject: [PATCH 4/5] test on all pushes --- .github/workflows/test.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7ac2a6..a5ae19f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,8 +3,6 @@ name: Test on: pull_request: push: - tags: - - '*' jobs: From ec4303dca75c191bb1b461529f59997db7d2572c Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Mon, 19 Jun 2023 20:45:37 +0800 Subject: [PATCH 5/5] document CreateRun --- lib/run.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/run.go b/lib/run.go index 80c9422..3fd485d 100644 --- a/lib/run.go +++ b/lib/run.go @@ -11,6 +11,8 @@ type RunConfig struct { WorkspaceID string } +// CreateRun creates a Run, which starts a Plan, which can later be Applied. +// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/run func CreateRun(config RunConfig) error { u := NewTfcUrl("/runs") payload := buildRunPayload(config.Message, config.WorkspaceID)