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: diff --git a/lib/run.go b/lib/run.go new file mode 100644 index 0000000..3fd485d --- /dev/null +++ b/lib/run.go @@ -0,0 +1,40 @@ +package lib + +import ( + "net/http" + + "github.com/Jeffail/gabs/v2" +) + +type RunConfig struct { + Message string + 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) + _ = 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.workspace.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..43be5ec --- /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":{"workspace":{"data":{"id":"ws_id"}}}}}` { + t.Fatalf("did not get expected result, got %q", got) + } +}