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(&p)
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 ret, err := h.client.Create(p)
59 http.Error(w, err.Error(), http.StatusInternalServerError)
63 w.Header().Set("Content-Type", "application/json")
64 w.WriteHeader(http.StatusCreated)
65 err = json.NewEncoder(w).Encode(ret)
67 http.Error(w, err.Error(), http.StatusInternalServerError)
72 // uploadHandler handles upload of the bundle tar file into the database
73 func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request) {
75 rbName := vars["rbname"]
76 rbVersion := vars["rbversion"]
77 prName := vars["prname"]
79 inpBytes, err := ioutil.ReadAll(r.Body)
81 http.Error(w, "Unable to read body", http.StatusBadRequest)
85 if len(inpBytes) == 0 {
86 http.Error(w, "Empty body", http.StatusBadRequest)
90 err = h.client.Upload(rbName, rbVersion, prName, inpBytes)
92 http.Error(w, err.Error(), http.StatusInternalServerError)
96 w.WriteHeader(http.StatusOK)
99 // getHandler handles GET operations on a particular ids
100 // Returns a rb.Definition
101 func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) {
103 rbName := vars["rbname"]
104 rbVersion := vars["rbversion"]
105 prName := vars["prname"]
107 ret, err := h.client.Get(rbName, rbVersion, prName)
109 http.Error(w, err.Error(), http.StatusInternalServerError)
113 w.Header().Set("Content-Type", "application/json")
114 w.WriteHeader(http.StatusOK)
115 err = json.NewEncoder(w).Encode(ret)
117 http.Error(w, err.Error(), http.StatusInternalServerError)
122 // deleteHandler handles DELETE operations on a particular bundle definition id
123 func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
125 rbName := vars["rbname"]
126 rbVersion := vars["rbversion"]
127 prName := vars["prname"]
129 err := h.client.Delete(rbName, rbVersion, prName)
131 http.Error(w, err.Error(), http.StatusInternalServerError)
135 w.WriteHeader(http.StatusNoContent)