Skip to content

Commit

Permalink
Merge pull request #66 from silinternational/develop
Browse files Browse the repository at this point in the history
Release 3.4.0 - new function "CreateRun"
  • Loading branch information
briskt authored Jun 19, 2023
2 parents 6b65ad8 + ec4303d commit 7a04682
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Test
on:
pull_request:
push:
tags:
- '*'

jobs:

Expand Down
40 changes: 40 additions & 0 deletions lib/run.go
Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 12 additions & 0 deletions lib/run_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 7a04682

Please sign in to comment.