-
Notifications
You must be signed in to change notification settings - Fork 88
/
instance.go
131 lines (107 loc) · 3.59 KB
/
instance.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
package api
import (
"context"
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/netlify/git-gateway/conf"
"github.com/netlify/git-gateway/models"
"github.com/pborman/uuid"
)
func (a *API) loadInstance(w http.ResponseWriter, r *http.Request) (context.Context, error) {
instanceID := chi.URLParam(r, "instance_id")
logEntrySetField(r, "instance_id", instanceID)
i, err := a.db.GetInstance(instanceID)
if err != nil {
if models.IsNotFoundError(err) {
return nil, notFoundError("Instance not found")
}
return nil, internalServerError("Database error loading instance").WithInternalError(err)
}
return withInstance(r.Context(), i), nil
}
func (a *API) GetAppManifest(w http.ResponseWriter, r *http.Request) error {
// TODO update to real manifest
return sendJSON(w, http.StatusOK, map[string]string{
"version": a.version,
"name": "GitGateway",
"description": "GitGateway is an access control proxy to git repos",
})
}
type InstanceRequestParams struct {
UUID string `json:"uuid"`
BaseConfig *conf.Configuration `json:"config"`
}
type InstanceResponse struct {
models.Instance
Endpoint string `json:"endpoint"`
State string `json:"state"`
}
func (a *API) CreateInstance(w http.ResponseWriter, r *http.Request) error {
params := InstanceRequestParams{}
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return badRequestError("Error decoding params: %v", err)
}
_, err := a.db.GetInstanceByUUID(params.UUID)
if err != nil {
if !models.IsNotFoundError(err) {
return internalServerError("Database error looking up instance").WithInternalError(err)
}
} else {
return badRequestError("An instance with that UUID already exists")
}
i := models.Instance{
ID: uuid.NewRandom().String(),
UUID: params.UUID,
BaseConfig: params.BaseConfig,
}
if err = a.db.CreateInstance(&i); err != nil {
return internalServerError("Database error creating instance").WithInternalError(err)
}
resp := InstanceResponse{
Instance: i,
Endpoint: a.config.API.Endpoint,
State: "active",
}
return sendJSON(w, http.StatusCreated, resp)
}
func (a *API) GetInstance(w http.ResponseWriter, r *http.Request) error {
i := getInstance(r.Context())
return sendJSON(w, http.StatusOK, i)
}
func (a *API) UpdateInstance(w http.ResponseWriter, r *http.Request) error {
i := getInstance(r.Context())
params := InstanceRequestParams{}
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
return badRequestError("Error decoding params: %v", err)
}
if params.BaseConfig != nil {
i.BaseConfig = mergeConfig(i.BaseConfig, params.BaseConfig)
}
if err := a.db.UpdateInstance(i); err != nil {
return internalServerError("Database error updating instance").WithInternalError(err)
}
return sendJSON(w, http.StatusOK, i)
}
func (a *API) DeleteInstance(w http.ResponseWriter, r *http.Request) error {
i := getInstance(r.Context())
if err := a.db.DeleteInstance(i); err != nil {
return internalServerError("Database error deleting instance").WithInternalError(err)
}
// TODO do we delete everything associated with an instance too?
w.WriteHeader(http.StatusNoContent)
return nil
}
func mergeConfig(baseConfig *conf.Configuration, newConfig *conf.Configuration) *conf.Configuration {
if newConfig.GitHub.AccessToken != "" {
baseConfig.GitHub.AccessToken = newConfig.GitHub.AccessToken
}
if newConfig.GitHub.Endpoint != "" {
baseConfig.GitHub.Endpoint = newConfig.GitHub.Endpoint
}
if newConfig.GitHub.Repo != "" {
baseConfig.GitHub.Repo = newConfig.GitHub.Repo
}
baseConfig.Roles = newConfig.Roles
return baseConfig
}