Skip to content

Commit

Permalink
fix: state version output API (#422)
Browse files Browse the repository at this point in the history
* fixes marshaling of outputs when included alongside a state version
* runs latest upstream go-tfe API tests against the state version outputs API
  • Loading branch information
leg100 authored May 6, 2023
1 parent 57c9ca4 commit 9adb486
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ go-tfe-tests-forked:

.PHONY: go-tfe-tests-upstream
go-tfe-tests-upstream:
GO_TFE_REPO=github.com/hashicorp/go-tfe@latest ./hack/go-tfe-tests.bash 'Test(OrganizationTags|Workspaces_(Add|Remove)Tags)|TestWorkspacesList/when_searching_using_a_tag'
GO_TFE_REPO=github.com/hashicorp/go-tfe@latest ./hack/go-tfe-tests.bash 'Test(OrganizationTags|Workspaces_(Add|Remove)Tags)|TestWorkspacesList/when_searching_using_a_tag|TestStateVersionOutputsRead'

.PHONY: test
test:
Expand Down
17 changes: 17 additions & 0 deletions api/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (a *api) addStateHandlers(r *mux.Router) {
r.HandleFunc("/state-versions/{id}/download", a.downloadState).Methods("GET")
r.HandleFunc("/state-versions/{id}", a.deleteVersion).Methods("DELETE")

r.HandleFunc("/workspaces/{workspace_id}/current-state-version-outputs", a.getCurrentVersionOutputs).Methods("GET")
r.HandleFunc("/state-versions/{id}/outputs", a.listOutputs).Methods("GET")
r.HandleFunc("/state-version-outputs/{id}", a.getOutput).Methods("GET")
}
Expand Down Expand Up @@ -173,6 +174,22 @@ func (a *api) downloadState(w http.ResponseWriter, r *http.Request) {
w.Write(resp)
}

func (a *api) getCurrentVersionOutputs(w http.ResponseWriter, r *http.Request) {
workspaceID, err := decode.Param("workspace_id", r)
if err != nil {
Error(w, err)
return
}

sv, err := a.GetCurrentStateVersion(r.Context(), workspaceID)
if err != nil {
Error(w, err)
return
}

a.writeResponse(w, r, sv.Outputs)
}

func (a *api) listOutputs(w http.ResponseWriter, r *http.Request) {
versionID, err := decode.Param("id", r)
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions api/state_marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func (m *jsonapiMarshaler) toState(from *state.Version, r *http.Request) (*types
DownloadURL: fmt.Sprintf("/api/v2/state-versions/%s/download", from.ID),
Serial: from.Serial,
}
for _, out := range from.Outputs {
to.Outputs = append(to.Outputs, &types.StateVersionOutput{ID: out.ID})
}

// Support including related resources:
//
Expand All @@ -26,10 +29,11 @@ func (m *jsonapiMarshaler) toState(from *state.Version, r *http.Request) (*types
for _, inc := range strings.Split(includes, ",") {
switch inc {
case "outputs":
var include []any
for _, out := range from.Outputs {
to.Outputs = append(to.Outputs, m.toOutput(out))
opts = append(opts, jsonapi.MarshalInclude(m.toOutput(out)))
include = append(include, m.toOutput(out))
}
opts = append(opts, jsonapi.MarshalInclude(include...))
}
}
}
Expand All @@ -46,19 +50,22 @@ func (m *jsonapiMarshaler) toStateList(from *state.VersionList, r *http.Request)
}

func (*jsonapiMarshaler) toOutput(from *state.Output) *types.StateVersionOutput {
return &types.StateVersionOutput{
to := &types.StateVersionOutput{
ID: from.ID,
Name: from.Name,
Sensitive: from.Sensitive,
Type: from.Type,
Value: from.Value,
}
if to.Sensitive {
to.Value = nil
}
return to
}

func (m *jsonapiMarshaler) toOutputList(from state.OutputList) *types.StateVersionOutputList {
var to types.StateVersionOutputList
func (m *jsonapiMarshaler) toOutputList(from state.OutputList) (to []*types.StateVersionOutput) {
for _, v := range from {
to.Items = append(to.Items, m.toOutput(v))
to = append(to, m.toOutput(v))
}
return &to
return
}
8 changes: 1 addition & 7 deletions api/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ type StateVersionOutput struct {
Name string `jsonapi:"attribute" json:"name"`
Sensitive bool `jsonapi:"attribute" json:"sensitive"`
Type string `jsonapi:"attribute" json:"type"`
Value string `jsonapi:"attribute" json:"value"`
}

// StateVersionOutputList is a list of state version outputs suitable for marshaling into
// JSONAPI
type StateVersionOutputList struct {
Items []*StateVersionOutput
Value any `jsonapi:"attribute" json:"value"`
}

// StateVersionCreateVersionOptions are options for creating a state version via
Expand Down

0 comments on commit 9adb486

Please sign in to comment.