f-21: code: separate and create route fonction
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
24536ed891
commit
75e02bd4f7
6 changed files with 145 additions and 35 deletions
|
|
@ -9,22 +9,33 @@ import (
|
|||
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
|
||||
if name == "" {
|
||||
http.NotFound(w, r)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"name": name})
|
||||
s.getVpc(w, r, name)
|
||||
case http.MethodDelete:
|
||||
s.queue.Submit(func() {
|
||||
deleteVpc(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
s.deleteVpc(w, r, name)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteVpc(name string) {}
|
||||
func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
|
||||
}
|
||||
|
||||
func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.queue.Submit(func() {
|
||||
destroyVpc(name)
|
||||
})
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
|
||||
}
|
||||
|
||||
func destroyVpc(name string) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue