-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclients.go
92 lines (81 loc) · 2.21 KB
/
clients.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
package main
import (
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
)
type newClientPayload struct {
Name string `json:"name"`
}
type newClientResponse struct {
PoolID string `json:"poolID"`
PoolClientID string `json:"poolClientID"`
}
func (app *application) newClientHandler(w http.ResponseWriter, r *http.Request) {
var payload newClientPayload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
app.respondError(w, AppError{
Message: "failed to decode newClientPayload",
error: err,
statusCode: http.StatusInternalServerError,
})
return
}
pool, err := app.createCognitoUserPool(r.Context(), payload.Name)
if err != nil {
app.respondError(w, AppError{
Message: "failed to create pool",
error: err,
statusCode: http.StatusInternalServerError,
})
return
}
client, err := app.createCognitoUserPoolClient(r.Context(), fmt.Sprintf("%s_client", *pool.UserPool.Name), *pool.UserPool.Id)
if err != nil {
delErr := app.deleteCognitoUserPool(r.Context(), *pool.UserPool.Id)
if delErr != nil {
log.Println(delErr)
app.respondError(w, AppError{
Message: "failed to create pool client; failed to rollback and delete pool",
error: err,
statusCode: http.StatusInternalServerError,
})
return
}
app.respondError(w, AppError{
Message: "failed to create pool client",
error: err,
statusCode: http.StatusInternalServerError,
})
return
}
app.respondJSON(w, newClientResponse{
PoolID: *pool.UserPool.Id,
PoolClientID: *client.UserPoolClient.ClientId,
}, http.StatusOK)
}
func (app *application) deleteClientHandler(w http.ResponseWriter, r *http.Request) {
params := httprouter.ParamsFromContext(r.Context())
poolID := params.ByName("id")
if len(poolID) == 0 {
app.respondError(w, AppError{
Message: "bad client id",
statusCode: http.StatusBadRequest,
})
return
}
err := app.deleteCognitoUserPool(r.Context(), poolID)
if err != nil {
log.Println(err)
app.respondError(w, AppError{
Message: "failed to delete pool",
error: err,
statusCode: http.StatusInternalServerError,
})
return
}
w.WriteHeader(http.StatusNoContent)
}