2 * Copyright 2018 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 "k8splugin/internal/rb"
26 "github.com/gorilla/mux"
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
37 // createHandler handles creation of the definition entry in the database
38 func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) {
41 err := json.NewDecoder(r.Body).Decode(&v)
44 http.Error(w, "Empty body", http.StatusBadRequest)
47 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
53 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
57 // Definition ID is required
59 http.Error(w, "Missing Resource Bundle Definition ID in POST request", http.StatusBadRequest)
63 ret, err := h.client.Create(v)
65 http.Error(w, err.Error(), http.StatusInternalServerError)
69 w.Header().Set("Content-Type", "application/json")
70 w.WriteHeader(http.StatusCreated)
71 err = json.NewEncoder(w).Encode(ret)
73 http.Error(w, err.Error(), http.StatusInternalServerError)
78 // uploadHandler handles upload of the bundle tar file into the database
79 func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request) {
83 inpBytes, err := ioutil.ReadAll(r.Body)
85 http.Error(w, "Unable to read body", http.StatusBadRequest)
89 if len(inpBytes) == 0 {
90 http.Error(w, "Empty body", http.StatusBadRequest)
94 err = h.client.Upload(uuid, inpBytes)
96 http.Error(w, err.Error(), http.StatusInternalServerError)
100 w.WriteHeader(http.StatusOK)
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()
108 http.Error(w, err.Error(), http.StatusInternalServerError)
112 w.Header().Set("Content-Type", "application/json")
113 w.WriteHeader(http.StatusOK)
114 err = json.NewEncoder(w).Encode(ret)
116 http.Error(w, err.Error(), http.StatusInternalServerError)
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)
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) {
134 ret, err := h.client.Get(id)
136 http.Error(w, err.Error(), http.StatusInternalServerError)
140 w.Header().Set("Content-Type", "application/json")
141 w.WriteHeader(http.StatusOK)
142 err = json.NewEncoder(w).Encode(ret)
144 http.Error(w, err.Error(), http.StatusInternalServerError)
149 // deleteHandler handles DELETE operations on a particular bundle definition id
150 func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
154 err := h.client.Delete(id)
156 http.Error(w, err.Error(), http.StatusInternalServerError)
160 w.WriteHeader(http.StatusNoContent)