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
Adds StateVersionOutputs ReadCurrent #370
Changes from all commits
7647755
b65731e
bc3385b
0aa45b9
e97759b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ var _ StateVersionOutputs = (*stateVersionOutputs)(nil) | |
// TFE API docs: https://www.terraform.io/docs/cloud/api/state-version-outputs.html | ||
type StateVersionOutputs interface { | ||
Read(ctx context.Context, outputID string) (*StateVersionOutput, error) | ||
ReadCurrent(ctx context.Context, workspaceID string) (*StateVersionOutputsList, error) | ||
} | ||
|
||
// stateVersionOutputs implements StateVersionOutputs. | ||
|
@@ -32,10 +33,31 @@ type StateVersionOutput struct { | |
Value interface{} `jsonapi:"attr,value"` | ||
} | ||
|
||
// ReadCurrent reads the current state version outputs for the specified workspace | ||
func (s *stateVersionOutputs) ReadCurrent(ctx context.Context, workspaceID string) (*StateVersionOutputsList, error) { | ||
if !validStringID(&workspaceID) { | ||
return nil, ErrInvalidWorkspaceID | ||
} | ||
|
||
u := fmt.Sprintf("workspaces/%s/current-state-version-outputs", url.QueryEscape(workspaceID)) | ||
req, err := s.client.newRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
so := &StateVersionOutputsList{} | ||
err = s.client.do(ctx, req, so) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return so, nil | ||
} | ||
|
||
// Read a State Version Output | ||
func (s *stateVersionOutputs) Read(ctx context.Context, outputID string) (*StateVersionOutput, error) { | ||
if !validStringID(&outputID) { | ||
return nil, ErrInvalidRunID | ||
return nil, ErrInvalidOutputID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. geeze, were we returning the incorrect error message all this time? |
||
} | ||
|
||
u := fmt.Sprintf("state-version-outputs/%s", url.QueryEscape(outputID)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import ( | |
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const waitForStateVersionOutputs = 700 * time.Millisecond | ||
const waitForStateVersionOutputs = 1000 * time.Millisecond | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why 1000 instead of 700? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My tests didn't pass locally with the old timing. |
||
|
||
func TestStateVersionOutputsRead(t *testing.T) { | ||
client := testClient(t) | ||
|
@@ -38,19 +38,47 @@ func TestStateVersionOutputsRead(t *testing.T) { | |
|
||
output := sv.Outputs[0] | ||
|
||
t.Run("when a state output exists", func(t *testing.T) { | ||
so, err := client.StateVersionOutputs.Read(ctx, output.ID) | ||
require.NoError(t, err) | ||
t.Run("Read by ID", func(t *testing.T) { | ||
t.Run("when a state output exists", func(t *testing.T) { | ||
so, err := client.StateVersionOutputs.Read(ctx, output.ID) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, so.ID, output.ID) | ||
assert.Equal(t, so.Name, output.Name) | ||
assert.Equal(t, so.Value, output.Value) | ||
assert.Equal(t, so.ID, output.ID) | ||
assert.Equal(t, so.Name, output.Name) | ||
assert.Equal(t, so.Value, output.Value) | ||
}) | ||
brandonc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
t.Run("when a state output does not exist", func(t *testing.T) { | ||
so, err := client.StateVersionOutputs.Read(ctx, "wsout-J2zM24JPAAAAAAAA") | ||
assert.Nil(t, so) | ||
assert.Equal(t, ErrResourceNotFound, err) | ||
}) | ||
}) | ||
|
||
t.Run("when a state output does not exist", func(t *testing.T) { | ||
so, err := client.StateVersionOutputs.Read(ctx, "wsout-J2zM24JPAAAAAAAA") | ||
assert.Nil(t, so) | ||
assert.Equal(t, ErrResourceNotFound, err) | ||
t.Run("Read current workspace outputs", func(t *testing.T) { | ||
brandonc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
so, err := client.StateVersionOutputs.ReadCurrent(ctx, wTest1.ID) | ||
|
||
assert.Nil(t, err) | ||
assert.NotNil(t, so) | ||
|
||
assert.Greater(t, len(so.Items), 0, "workspace state version outputs were empty") | ||
}) | ||
|
||
t.Run("Sensitive secrets are null", func(t *testing.T) { | ||
so, err := client.StateVersionOutputs.ReadCurrent(ctx, wTest1.ID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, so) | ||
|
||
var found *StateVersionOutput = nil | ||
for _, s := range so.Items { | ||
if s.Name == "test_output_string" { | ||
found = s | ||
break | ||
} | ||
} | ||
|
||
assert.NotNil(t, found) | ||
assert.True(t, found.Sensitive) | ||
assert.Nil(t, found.Value) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we have this precedence for ReadCurrent and ReadCurrentwithOptions: https://github.com/hashicorp/go-tfe/blob/main/state_version.go#L33-L36 But if this endpoint won't be adding options in the near future, then we are good.