Skip to content

Commit

Permalink
fix: variable set variables API (#589)
Browse files Browse the repository at this point in the history
Fixes #588
  • Loading branch information
leg100 authored Sep 6, 2023
1 parent daf6096 commit 8e29da1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
1 change: 1 addition & 0 deletions hack/go-tfe-tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ tests+=('TestVariableSetsListForWorkspace')
tests+=('TestVariableSetsRead')
tests+=('TestVariableSetsApplyToAndRemoveFromWorkspaces')
tests+=('TestVariableSetsDelete')
tests+=('TestVariableSetVariables')
tests+=('TestStateVersion')

# only run these tests if env vars are present - otherwise the tests fail early
Expand Down
2 changes: 1 addition & 1 deletion internal/variable/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (s *service) createVariableSetVariable(ctx context.Context, setID string, o
return nil
})
if err != nil {
s.Error(err, "adding variable to set", "subject", subject, "set", set, "variable", v)
s.Error(err, "adding variable to set", "set_id", setID)
return nil, err
}

Expand Down
67 changes: 57 additions & 10 deletions internal/variable/tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (a *tfe) addHandlers(r *mux.Router) {
r.HandleFunc("/varsets/{varset_id}", a.updateVariableSet).Methods("PATCH")
r.HandleFunc("/varsets/{varset_id}", a.deleteVariableSet).Methods("DELETE")

r.HandleFunc("/varsets/{varset_id}/relationships/vars", a.listVariableSetVariables).Methods("GET")
r.HandleFunc("/varsets/{varset_id}/relationships/vars", a.addVariableToSet).Methods("POST")
r.HandleFunc("/varsets/{varset_id}/relationships/vars/{variable_id}", a.getVariableSetVariable).Methods("GET")
r.HandleFunc("/varsets/{varset_id}/relationships/vars/{variable_id}", a.updateVariableSetVariable).Methods("PATCH")
r.HandleFunc("/varsets/{varset_id}/relationships/vars/{variable_id}", a.deleteVariableFromSet).Methods("DELETE")
r.HandleFunc("/varsets/{varset_id}/relationships/workspaces", a.applySetToWorkspaces).Methods("POST")
Expand Down Expand Up @@ -288,19 +290,40 @@ func (a *tfe) deleteVariableSet(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}

func (a *tfe) listVariableSetVariables(w http.ResponseWriter, r *http.Request) {
setID, err := decode.Param("varset_id", r)
if err != nil {
tfeapi.Error(w, err)
return
}

set, err := a.Service.getVariableSet(r.Context(), setID)
if err != nil {
tfeapi.Error(w, err)
return
}

to := make([]*types.VariableSetVariable, len(set.Variables))
for i, from := range set.Variables {
to[i] = a.convertVariableSetVariable(from, setID)
}

a.Respond(w, r, to, http.StatusOK)
}

func (a *tfe) addVariableToSet(w http.ResponseWriter, r *http.Request) {
setID, err := decode.Param("varset_id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
var opts *types.VariableCreateOptions
if err := decode.All(&opts, r); err != nil {
var opts types.VariableCreateOptions
if err := tfeapi.Unmarshal(r.Body, &opts); err != nil {
tfeapi.Error(w, err)
return
}

_, err = a.Service.createVariableSetVariable(r.Context(), setID, CreateVariableOptions{
v, err := a.Service.createVariableSetVariable(r.Context(), setID, CreateVariableOptions{
Key: opts.Key,
Value: opts.Value,
Description: opts.Description,
Expand All @@ -309,9 +332,11 @@ func (a *tfe) addVariableToSet(w http.ResponseWriter, r *http.Request) {
HCL: opts.HCL,
})
if err != nil {
tfeapi.Error(w, err)
variableError(w, err)
return
}

a.Respond(w, r, a.convertVariableSetVariable(v, setID), http.StatusOK)
}

func (a *tfe) updateVariableSetVariable(w http.ResponseWriter, r *http.Request) {
Expand All @@ -321,12 +346,12 @@ func (a *tfe) updateVariableSetVariable(w http.ResponseWriter, r *http.Request)
return
}
var opts types.VariableUpdateOptions
if err := decode.All(&opts, r); err != nil {
if err := tfeapi.Unmarshal(r.Body, &opts); err != nil {
tfeapi.Error(w, err)
return
}

_, err = a.Service.updateVariableSetVariable(r.Context(), variableID, UpdateVariableOptions{
set, err := a.Service.updateVariableSetVariable(r.Context(), variableID, UpdateVariableOptions{
Key: opts.Key,
Value: opts.Value,
Description: opts.Description,
Expand All @@ -335,19 +360,34 @@ func (a *tfe) updateVariableSetVariable(w http.ResponseWriter, r *http.Request)
HCL: opts.HCL,
})
if err != nil {
tfeapi.Error(w, err)
variableError(w, err)
return
}

v := set.getVariable(variableID)
a.Respond(w, r, a.convertVariableSetVariable(v, set.ID), http.StatusOK)
}

func (a *tfe) deleteVariableFromSet(w http.ResponseWriter, r *http.Request) {
func (a *tfe) getVariableSetVariable(w http.ResponseWriter, r *http.Request) {
variableID, err := decode.Param("variable_id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
var opts types.VariableCreateOptions
if err := decode.All(&opts, r); err != nil {

set, err := a.Service.getVariableSetByVariableID(r.Context(), variableID)
if err != nil {
variableError(w, err)
return
}

v := set.getVariable(variableID)
a.Respond(w, r, a.convertVariableSetVariable(v, set.ID), http.StatusOK)
}

func (a *tfe) deleteVariableFromSet(w http.ResponseWriter, r *http.Request) {
variableID, err := decode.Param("variable_id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
Expand Down Expand Up @@ -446,6 +486,13 @@ func (a *tfe) convertVariableSet(from *VariableSet) *types.VariableSet {
return to
}

func (a *tfe) convertVariableSetVariable(from *Variable, setID string) *types.VariableSetVariable {
return &types.VariableSetVariable{
Variable: a.convertVariable(from),
VariableSet: &types.VariableSet{ID: setID},
}
}

func (a *tfe) convertVariable(from *Variable) *types.Variable {
to := &types.Variable{
ID: from.ID,
Expand Down

0 comments on commit 8e29da1

Please sign in to comment.