Skip to content

Commit

Permalink
fix ignored json meta tags for run variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Uk1288 committed Sep 14, 2022
1 parent ced985c commit 8498b0a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
13 changes: 9 additions & 4 deletions run.go
Expand Up @@ -127,7 +127,7 @@ type Run struct {
StatusTimestamps *RunStatusTimestamps `jsonapi:"attr,status-timestamps"`
TargetAddrs []string `jsonapi:"attr,target-addrs,omitempty"`
TerraformVersion string `jsonapi:"attr,terraform-version"`
Variables []*RunVariable `jsonapi:"attr,variables"`
Variables []*RunVariableAttr `jsonapi:"attr,variables"`

// Relations
Apply *Apply `jsonapi:"relation,apply"`
Expand Down Expand Up @@ -304,7 +304,7 @@ type RunCreateOptions struct {
// user confirmation. It defaults to the Workspace.AutoApply setting.
AutoApply *bool `jsonapi:"attr,auto-apply,omitempty"`

// RunVariables allows you to specify terraform input variables for
// Variables allows you to specify terraform input variables for
// a particular run, prioritized over variables defined on the workspace.
Variables []*RunVariable `jsonapi:"attr,variables,omitempty"`
}
Expand All @@ -321,14 +321,19 @@ type RunCancelOptions struct {
Comment *string `json:"comment,omitempty"`
}

// RunVariable represents a variable that can be applied to a run. All values must be expressed as an HCL literal
// RunVariableAttr represents a variable that can be applied to a run. All values must be expressed as an HCL literal
// in the same syntax you would use when writing terraform code. See https://www.terraform.io/docs/language/expressions/types.html#types
// for more details.
type RunVariable struct {
type RunVariableAttr struct {
Key string `jsonapi:"attr,key"`
Value string `jsonapi:"attr,value"`
}

type RunVariable struct {
Key string `json:"key"`
Value string `json:"value"`
}

// RunForceCancelOptions represents the options for force-canceling a run.
type RunForceCancelOptions struct {
// An optional comment explaining the reason for the force-cancel.
Expand Down
40 changes: 40 additions & 0 deletions run_integration_test.go
Expand Up @@ -7,9 +7,11 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -561,6 +563,7 @@ func TestRun_Unmarshal(t *testing.T) {
"plan-queued-at": "2020-03-16T23:15:59+00:00",
"errored-at": "2019-03-16T23:23:59+00:00",
},
"variables": []map[string]string{{"key": "a-key", "value": "\"a-value\""}},
},
},
}
Expand Down Expand Up @@ -596,4 +599,41 @@ func TestRun_Unmarshal(t *testing.T) {
assert.Equal(t, run.Permissions.CanForceCancel, true)
assert.Equal(t, run.StatusTimestamps.PlanQueuedAt, planQueuedParsedTime)
assert.Equal(t, run.StatusTimestamps.ErroredAt, erroredParsedTime)

require.NotEmpty(t, run.Variables)
assert.Equal(t, run.Variables[0].Key, "a-key")
assert.Equal(t, run.Variables[0].Value, "\"a-value\"")
}

func TestRunCreateOptions_Marshal(t *testing.T) {
client := testClient(t)

wTest, wTestCleanup := createWorkspace(t, client, nil)
defer wTestCleanup()

opts := RunCreateOptions{
Workspace: wTest,
Variables: []*RunVariable{
{
Key: "test_variable",
Value: "Hello, World!",
},
{
Key: "test_foo",
Value: "Hello, Foo!",
},
},
}

reqBody, err := serializeRequestBody(&opts)
require.NoError(t, err)
req, err := retryablehttp.NewRequest("POST", "url", reqBody)
require.NoError(t, err)
bodyBytes, err := req.BodyBytes()
require.NoError(t, err)

expectedBody := fmt.Sprintf(`{"data":{"type":"runs","attributes":{"variables":[{"key":"test_variable","value":"Hello, World!"},{"key":"test_foo","value":"Hello, Foo!"}]},"relationships":{"configuration-version":{"data":null},"workspace":{"data":{"type":"workspaces","id":"%s"}}}}}
`, wTest.ID)

assert.Equal(t, string(bodyBytes), expectedBody)
}

0 comments on commit 8498b0a

Please sign in to comment.