-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
165 lines (136 loc) · 3.72 KB
/
handlers.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
package api
import (
"encoding/json"
"log"
"net/http"
"regexp"
"github.com/pablo-ruth/terraform-state-locker/store"
)
func handlePutItem(w http.ResponseWriter, r *http.Request, s store.Store) {
putItemRequest, err := ParsePutItemRequest(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request"))
log.Printf("Parsing request: %v", err)
return
}
var notExists bool
if putItemRequest.ConditionExpression != "" {
re := regexp.MustCompile(`attribute_not_exists\((.*)\)`)
match := re.FindStringSubmatch(putItemRequest.ConditionExpression)
if len(match) != 2 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid condition expression"))
log.Printf("Parsing condition expression: %v", err)
return
}
if match[1] != "LockID" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Condition expression must be on LockID"))
log.Printf("Parsing condition expression, must be on LockID: %v", err)
return
}
notExists = true
}
lockID, ok := putItemRequest.Item["LockID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("LockID is missing"))
log.Printf("LockID is missing")
return
}
attributes := map[string]string{}
for k, v := range putItemRequest.Item {
if k == "LockID" {
continue
}
attributes[k] = v.S
}
err = s.Put(putItemRequest.TableName, lockID.S, notExists, attributes)
if err != nil {
if err == store.ErrEntryAlreadyExists {
w.WriteHeader(http.StatusConflict)
w.Write([]byte("Conflict"))
return
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
log.Printf("Storing lock: %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
}
func handleGetItem(w http.ResponseWriter, r *http.Request, s store.Store) {
getItemRequest, err := ParseGetItemRequest(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request"))
log.Printf("Parsing request: %v", err)
return
}
lockID, ok := getItemRequest.Key["LockID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("LockID is missing"))
return
}
values, err := s.Get(getItemRequest.TableName, lockID.S)
if err != nil {
if err == store.ErrEntryNotFound || err == store.ErrTableNotFound {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
log.Printf("Entry not found: %v", err)
return
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
log.Printf("Storing lock: %v", err)
return
}
var resp GetItemResponse
resp.Item = map[string]Item{}
for k, v := range values {
if k == "LockID" {
continue
}
resp.Item[k] = Item{S: v}
}
itemJSON, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(itemJSON))
}
func handleDeleteItem(w http.ResponseWriter, r *http.Request, s store.Store) {
deleteItemRequest, err := ParseDeleteItemRequest(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request"))
log.Printf("Parsing request: %v", err)
return
}
lockID, ok := deleteItemRequest.Key["LockID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("LockID is missing"))
return
}
err = s.Delete(deleteItemRequest.TableName, lockID.S)
if err != nil {
if err == store.ErrEntryNotFound || err == store.ErrTableNotFound {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Not found"))
return
}
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
log.Printf("Deleting lock: %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
}