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 "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
25 "github.com/gorilla/mux"
28 // Used to store backend implementations objects
29 // Also simplifies mocking for unit testing purposes
30 type rbConfigHandler struct {
31 // Interface that implements bundle Definition operations
32 // We will set this variable with a mock interface for testing
33 client app.ConfigManager
36 // createHandler handles creation of the definition entry in the database
37 func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
40 rbName := vars["rbname"]
41 rbVersion := vars["rbversion"]
42 prName := vars["prname"]
45 http.Error(w, "Empty body", http.StatusBadRequest)
49 err := json.NewDecoder(r.Body).Decode(&p)
51 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
56 if p.ConfigName == "" {
57 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
61 ret, err := h.client.Create(rbName, rbVersion, prName, p)
63 http.Error(w, err.Error(), http.StatusInternalServerError)
67 w.Header().Set("Content-Type", "application/json")
68 w.WriteHeader(http.StatusCreated)
69 err = json.NewEncoder(w).Encode(ret)
71 http.Error(w, err.Error(), http.StatusInternalServerError)
76 // getHandler handles GET operations on a particular config
77 // Returns a app.Definition
78 func (h rbConfigHandler) getHandler(w http.ResponseWriter, r *http.Request) {
80 rbName := vars["rbname"]
81 rbVersion := vars["rbversion"]
82 prName := vars["prname"]
83 cfgName := vars["cfgname"]
85 ret, err := h.client.Get(rbName, rbVersion, prName, cfgName)
87 http.Error(w, err.Error(), http.StatusInternalServerError)
91 w.Header().Set("Content-Type", "application/json")
92 w.WriteHeader(http.StatusOK)
93 err = json.NewEncoder(w).Encode(ret)
95 http.Error(w, err.Error(), http.StatusInternalServerError)
100 // deleteHandler handles DELETE operations on a config
101 func (h rbConfigHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
103 rbName := vars["rbname"]
104 rbVersion := vars["rbversion"]
105 prName := vars["prname"]
106 cfgName := vars["cfgname"]
108 ret, err := h.client.Delete(rbName, rbVersion, prName, cfgName)
110 http.Error(w, err.Error(), http.StatusInternalServerError)
114 w.Header().Set("Content-Type", "application/json")
115 w.WriteHeader(http.StatusOK)
116 err = json.NewEncoder(w).Encode(ret)
118 http.Error(w, err.Error(), http.StatusInternalServerError)
124 // UpdateHandler handles Update operations on a particular configuration
125 func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
127 rbName := vars["rbname"]
128 rbVersion := vars["rbversion"]
129 prName := vars["prname"]
130 cfgName := vars["cfgname"]
135 http.Error(w, "Empty body", http.StatusBadRequest)
139 err := json.NewDecoder(r.Body).Decode(&p)
141 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
145 ret, err := h.client.Update(rbName, rbVersion, prName, cfgName, p)
147 http.Error(w, err.Error(), http.StatusInternalServerError)
151 w.Header().Set("Content-Type", "application/json")
152 w.WriteHeader(http.StatusCreated)
153 err = json.NewEncoder(w).Encode(ret)
155 http.Error(w, err.Error(), http.StatusInternalServerError)
160 // rollbackHandler handles Rollback operations to a specific version
161 func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request) {
163 rbName := vars["rbname"]
164 rbVersion := vars["rbversion"]
165 prName := vars["prname"]
168 http.Error(w, "Empty body", http.StatusBadRequest)
172 var p app.ConfigRollback
173 err := json.NewDecoder(r.Body).Decode(&p)
175 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
178 err = h.client.Rollback(rbName, rbVersion, prName, p)
180 http.Error(w, err.Error(), http.StatusInternalServerError)
183 w.WriteHeader(http.StatusNoContent)
186 // tagitHandler handles TAGIT operation
187 func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
189 rbName := vars["rbname"]
190 rbVersion := vars["rbversion"]
191 prName := vars["prname"]
194 http.Error(w, "Empty body", http.StatusBadRequest)
198 var p app.ConfigTagit
199 err := json.NewDecoder(r.Body).Decode(&p)
201 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
205 err = h.client.Tagit(rbName, rbVersion, prName, p)
207 http.Error(w, err.Error(), http.StatusInternalServerError)
210 w.WriteHeader(http.StatusNoContent)