forked from ctdk/goiardi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
232 lines (221 loc) · 7.26 KB
/
data.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Data functions */
/*
* Copyright (c) 2013-2014, Jeremy Bingham (<jbingham@gmail.com>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"net/http"
"encoding/json"
"fmt"
"github.com/ctdk/goiardi/data_bag"
"github.com/ctdk/goiardi/util"
)
func data_handler(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
path_array := SplitPath(r.URL.Path)
db_response := make(map[string]interface{})
if len(path_array) == 1 {
/* Either a list of data bags, or a POST to create a new one */
switch r.Method {
case "GET":
/* The list */
db_list := data_bag.GetList()
for _, k := range db_list {
item_url := fmt.Sprintf("/data/%s", k)
db_response[k] = util.CustomURL(item_url)
}
case "POST":
db_data, jerr := ParseObjJson(r.Body)
if jerr != nil {
JsonErrorReport(w, r, jerr.Error(), http.StatusBadRequest)
return
}
/* check that the name exists */
switch t := db_data["name"].(type) {
case string:
if t == "" {
JsonErrorReport(w, r, "Field 'name' missing", http.StatusBadRequest)
return
}
default:
JsonErrorReport(w, r, "Field 'name' missing", http.StatusBadRequest)
return
}
chef_dbag, _ := data_bag.Get(db_data["name"].(string))
if chef_dbag != nil {
httperr := fmt.Errorf("Data bag %s already exists.", db_data["name"].(string))
JsonErrorReport(w, r, httperr.Error(), http.StatusConflict)
return
}
chef_dbag, nerr := data_bag.New(db_data["name"].(string))
if nerr != nil {
JsonErrorReport(w, r, nerr.Error(), nerr.Status())
return
}
chef_dbag.Save()
db_response["uri"] = util.ObjURL(chef_dbag)
w.WriteHeader(http.StatusCreated)
default:
/* The chef-pedant spec wants this response for
* some reason. Mix it up, I guess. */
JsonErrorReport(w, r, "GET, PUT", http.StatusMethodNotAllowed)
return
}
} else {
db_name := path_array[1]
/* chef-pedant is unhappy about not reporting the HTTP status
* as 404 by fetching the data bag before we see if the method
* is allowed, so do a quick check for that here. */
if (len(path_array) == 2 && r.Method == "PUT") || (len(path_array) == 3 && r.Method == "POST"){
JsonErrorReport(w, r, "Method not allowed", http.StatusMethodNotAllowed)
return
}
chef_dbag, err := data_bag.Get(db_name)
if err != nil {
var err_msg string
status := err.Status()
if r.Method == "POST" {
/* Posts get a special snowflake message */
err_msg = fmt.Sprintf("No data bag '%s' could be found. Please create this data bag before adding items to it.", db_name)
} else {
if len(path_array) == 3 {
/* This is nuts. */
if r.Method == "DELETE" {
err_msg = fmt.Sprintf("Cannot load data bag %s item %s", db_name, path_array[2])
} else {
err_msg = fmt.Sprintf("Cannot load data bag item %s for data bag %s", path_array[2], db_name)
}
} else {
err_msg = err.Error()
}
}
JsonErrorReport(w, r, err_msg, status)
return
}
if len(path_array) == 2 {
/* getting list of data bag items and creating data bag
* items. */
switch r.Method {
case "GET":
for k, _ := range chef_dbag.DataBagItems {
db_response[k] = util.CustomObjURL(chef_dbag, k)
}
case "DELETE":
/* The chef API docs don't say anything
* about this existing, but it does,
* and without it you can't delete data
* bags at all. */
db_response["chef_type"] = "data_bag"
db_response["json_class"] = "Chef::DataBag"
db_response["name"] = chef_dbag.Name
err := chef_dbag.Delete()
if err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
case "POST":
raw_data := data_bag.RawDataBagJson(r.Body)
dbitem, nerr := chef_dbag.NewDBItem(raw_data)
if nerr != nil {
JsonErrorReport(w, r, nerr.Error(), nerr.Status())
return
}
/* The data bag return values are all
* kinds of weird. Sometimes it sends
* just the raw data, sometimes it sends
* the whole object, sometimes a special
* snowflake version. Ugh. Have to loop
* through to avoid updating the pointer
* in the cache by just assigning
* dbitem.RawData to db_response. Urk.
*/
for k, v := range dbitem.RawData {
db_response[k] = v
}
db_response["data_bag"] = dbitem.DataBagName
db_response["chef_type"] = dbitem.ChefType
w.WriteHeader(http.StatusCreated)
default:
JsonErrorReport(w, r, "GET, DELETE, POST", http.StatusMethodNotAllowed)
return
}
} else {
/* getting, editing, and deleting existing data bag items. */
db_item_name := path_array[2]
if _, ok := chef_dbag.DataBagItems[db_item_name]; !ok {
var httperr string
if r.Method != "DELETE" {
httperr = fmt.Sprintf("Cannot load data bag item %s for data bag %s", db_item_name, chef_dbag.Name)
} else {
httperr = fmt.Sprintf("Cannot load data bag %s item %s", chef_dbag.Name, db_item_name)
}
JsonErrorReport(w, r, httperr, http.StatusNotFound)
return
}
switch r.Method {
case "GET":
db_response = chef_dbag.DataBagItems[db_item_name].RawData
case "DELETE":
dbi := chef_dbag.DataBagItems[db_item_name]
/* Gotta short circuit this */
enc := json.NewEncoder(w)
if err := enc.Encode(&dbi); err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
err := chef_dbag.DeleteDBItem(db_item_name)
if err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
return
case "PUT":
raw_data := data_bag.RawDataBagJson(r.Body)
if raw_id, ok := raw_data["id"]; ok {
switch raw_id := raw_id.(type) {
case string:
if raw_id != db_item_name {
JsonErrorReport(w, r, "DataBagItem name mismatch.", http.StatusBadRequest)
return
}
default:
JsonErrorReport(w, r, "Bad request", http.StatusBadRequest)
return
}
}
dbitem, err := chef_dbag.UpdateDBItem(db_item_name, raw_data)
if err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
return
}
/* Another weird data bag item response
* which isn't at all unusual. */
for k, v := range dbitem.RawData {
db_response[k] = v
}
db_response["data_bag"] = dbitem.DataBagName
db_response["chef_type"] = dbitem.ChefType
db_response["id"] = db_item_name
default:
JsonErrorReport(w, r, "GET, DELETE, PUT", http.StatusMethodNotAllowed)
return
}
}
}
enc := json.NewEncoder(w)
if err := enc.Encode(&db_response); err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
}
}