Merge "Use httptest instead of http in unit tests"
[multicloud/k8s.git] / src / k8splugin / api / profilehandler.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package api
18
19 import (
20         "encoding/json"
21         "io"
22         "io/ioutil"
23         "k8splugin/internal/rb"
24         "net/http"
25
26         "github.com/gorilla/mux"
27 )
28
29 // Used to store backend implementations objects
30 // Also simplifies mocking for unit testing purposes
31 type rbProfileHandler struct {
32         // Interface that implements bundle Definition operations
33         // We will set this variable with a mock interface for testing
34         client rb.ProfileManager
35 }
36
37 // createHandler handles creation of the definition entry in the database
38 func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) {
39         var v rb.Profile
40
41         err := json.NewDecoder(r.Body).Decode(&v)
42         switch {
43         case err == io.EOF:
44                 http.Error(w, "Empty body", http.StatusBadRequest)
45                 return
46         case err != nil:
47                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
48                 return
49         }
50
51         // Name is required.
52         if v.Name == "" {
53                 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
54                 return
55         }
56
57         // Definition ID is required
58         if v.RBDID == "" {
59                 http.Error(w, "Missing Resource Bundle Definition ID in POST request", http.StatusBadRequest)
60                 return
61         }
62
63         ret, err := h.client.Create(v)
64         if err != nil {
65                 http.Error(w, err.Error(), http.StatusInternalServerError)
66                 return
67         }
68
69         w.Header().Set("Content-Type", "application/json")
70         w.WriteHeader(http.StatusCreated)
71         err = json.NewEncoder(w).Encode(ret)
72         if err != nil {
73                 http.Error(w, err.Error(), http.StatusInternalServerError)
74                 return
75         }
76 }
77
78 // uploadHandler handles upload of the bundle tar file into the database
79 func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request) {
80         vars := mux.Vars(r)
81         uuid := vars["rbpID"]
82
83         inpBytes, err := ioutil.ReadAll(r.Body)
84         if err != nil {
85                 http.Error(w, "Unable to read body", http.StatusBadRequest)
86                 return
87         }
88
89         if len(inpBytes) == 0 {
90                 http.Error(w, "Empty body", http.StatusBadRequest)
91                 return
92         }
93
94         err = h.client.Upload(uuid, inpBytes)
95         if err != nil {
96                 http.Error(w, err.Error(), http.StatusInternalServerError)
97                 return
98         }
99
100         w.WriteHeader(http.StatusOK)
101 }
102
103 // listHandler handles GET (list) operations on the endpoint
104 // Returns a list of rb.Definitions
105 func (h rbProfileHandler) listHandler(w http.ResponseWriter, r *http.Request) {
106         ret, err := h.client.List()
107         if err != nil {
108                 http.Error(w, err.Error(), http.StatusInternalServerError)
109                 return
110         }
111
112         w.Header().Set("Content-Type", "application/json")
113         w.WriteHeader(http.StatusOK)
114         err = json.NewEncoder(w).Encode(ret)
115         if err != nil {
116                 http.Error(w, err.Error(), http.StatusInternalServerError)
117                 return
118         }
119 }
120
121 // helpHandler handles GET (list) operations on the endpoint
122 // Returns a list of rb.Definitions
123 func (h rbProfileHandler) helpHandler(w http.ResponseWriter, r *http.Request) {
124         w.Header().Set("Content-Type", "text/html")
125         w.WriteHeader(http.StatusOK)
126 }
127
128 // getHandler handles GET operations on a particular ids
129 // Returns a rb.Definition
130 func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) {
131         vars := mux.Vars(r)
132         id := vars["rbpID"]
133
134         ret, err := h.client.Get(id)
135         if err != nil {
136                 http.Error(w, err.Error(), http.StatusInternalServerError)
137                 return
138         }
139
140         w.Header().Set("Content-Type", "application/json")
141         w.WriteHeader(http.StatusOK)
142         err = json.NewEncoder(w).Encode(ret)
143         if err != nil {
144                 http.Error(w, err.Error(), http.StatusInternalServerError)
145                 return
146         }
147 }
148
149 // deleteHandler handles DELETE operations on a particular bundle definition id
150 func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
151         vars := mux.Vars(r)
152         id := vars["rbpID"]
153
154         err := h.client.Delete(id)
155         if err != nil {
156                 http.Error(w, err.Error(), http.StatusInternalServerError)
157                 return
158         }
159
160         w.WriteHeader(http.StatusNoContent)
161 }