forked from leg100/otf
/
api.go
137 lines (127 loc) · 3.24 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package run
import (
"bytes"
"io"
"net/http"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
otfapi "github.com/tofutf/tofutf/internal/api"
"github.com/tofutf/tofutf/internal/http/decode"
"github.com/tofutf/tofutf/internal/tfeapi"
)
type api struct {
*Service
*tfeapi.Responder
logr.Logger
}
func (a *api) addHandlers(r *mux.Router) {
r = r.PathPrefix(otfapi.DefaultBasePath).Subrouter()
r.HandleFunc("/runs", a.list).Methods("GET")
r.HandleFunc("/runs/{id}", a.get).Methods("GET")
r.HandleFunc("/runs/{id}/planfile", a.getPlanFile).Methods("GET")
r.HandleFunc("/runs/{id}/planfile", a.uploadPlanFile).Methods("PUT")
r.HandleFunc("/runs/{id}/lockfile", a.getLockFile).Methods("GET")
r.HandleFunc("/runs/{id}/lockfile", a.uploadLockFile).Methods("PUT")
}
func (a *api) list(w http.ResponseWriter, r *http.Request) {
var params ListOptions
if err := decode.All(¶ms, r); err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
page, err := a.List(r.Context(), params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
a.RespondWithPage(w, r, page.Items, page.Pagination)
}
func (a *api) get(w http.ResponseWriter, r *http.Request) {
id, err := decode.Param("id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
run, err := a.Get(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
a.Respond(w, r, run, http.StatusOK)
}
func (a *api) getPlanFile(w http.ResponseWriter, r *http.Request) {
id, err := decode.Param("id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
opts := PlanFileOptions{}
if err := decode.Query(&opts, r.URL.Query()); err != nil {
tfeapi.Error(w, err)
return
}
file, err := a.GetPlanFile(r.Context(), id, opts.Format)
if err != nil {
tfeapi.Error(w, err)
return
}
if _, err := w.Write(file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *api) uploadPlanFile(w http.ResponseWriter, r *http.Request) {
id, err := decode.Param("id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
opts := PlanFileOptions{}
if err := decode.Query(&opts, r.URL.Query()); err != nil {
tfeapi.Error(w, err)
return
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
tfeapi.Error(w, err)
return
}
if err := a.UploadPlanFile(r.Context(), id, buf.Bytes(), opts.Format); err != nil {
tfeapi.Error(w, err)
return
}
w.WriteHeader(http.StatusAccepted)
}
func (a *api) getLockFile(w http.ResponseWriter, r *http.Request) {
id, err := decode.Param("id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
file, err := a.GetLockFile(r.Context(), id)
if err != nil {
tfeapi.Error(w, err)
return
}
if _, err := w.Write(file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (a *api) uploadLockFile(w http.ResponseWriter, r *http.Request) {
id, err := decode.Param("id", r)
if err != nil {
tfeapi.Error(w, err)
return
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
tfeapi.Error(w, err)
return
}
if err := a.UploadLockFile(r.Context(), id, buf.Bytes()); err != nil {
tfeapi.Error(w, err)
return
}
w.WriteHeader(http.StatusAccepted)
}