-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from silinternational/develop
Release 3.4.0 - new function "CreateRun"
- Loading branch information
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ name: Test | |
on: | ||
pull_request: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
jobs: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |