Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ignored json meta tags for run variables in RunCreateOptions #531

Merged
merged 1 commit into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## Bug Fixes
* Fixes null value returned in variable set relationship in `VariableSetVariable` by @sebasslash [#521](https://github.com/hashicorp/go-tfe/pull/521)
* Fix marshalling of run variables in `RunCreateOptions`. The `Variables` field type in `Run` struct has changed from `[]*RunVariable` to `[]*RunVariableAttr` by @Uk1288 [#531](https://github.com/hashicorp/go-tfe/pull/531)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🌟


# v1.9.0

Expand Down
15 changes: 10 additions & 5 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,12 +321,17 @@ 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
type RunVariableAttr struct {
Key string `jsonapi:"attr,key"`
Value string `jsonapi:"attr,value"`
}

// 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 {
Key string `jsonapi:"attr,key"`
Value string `jsonapi:"attr,value"`
Key string `json:"key"`
Value string `json:"value"`
}

// RunForceCancelOptions represents the options for force-canceling a run.
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)
}