2 * Copyright 2018 Intel Corporation, Inc
3 * Copyright © 2021 Samsung Electronics
4 * Copyright © 2021 Orange
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
25 "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
27 "github.com/gorilla/mux"
30 // Used to store backend implementations objects
31 // Also simplifies mocking for unit testing purposes
32 type rbConfigHandler struct {
33 // Interface that implements bundle Definition operations
34 // We will set this variable with a mock interface for testing
35 client app.ConfigManager
38 // createHandler handles creation of the definition entry in the database
39 func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
42 instanceID := vars["instID"]
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(instanceID, 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 instanceID := vars["instID"]
81 cfgName := vars["cfgname"]
83 ret, err := h.client.Get(instanceID, cfgName)
85 http.Error(w, err.Error(), http.StatusInternalServerError)
89 w.Header().Set("Content-Type", "application/json")
90 w.WriteHeader(http.StatusOK)
91 err = json.NewEncoder(w).Encode(ret)
93 http.Error(w, err.Error(), http.StatusInternalServerError)
98 // deleteHandler handles DELETE operations on a config
99 func (h rbConfigHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
101 instanceID := vars["instID"]
102 cfgName := vars["cfgname"]
104 ret, err := h.client.Delete(instanceID, cfgName)
106 http.Error(w, err.Error(), http.StatusInternalServerError)
110 w.Header().Set("Content-Type", "application/json")
111 w.WriteHeader(http.StatusOK)
112 err = json.NewEncoder(w).Encode(ret)
114 http.Error(w, err.Error(), http.StatusInternalServerError)
120 // UpdateHandler handles Update operations on a particular configuration
121 func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
123 instanceID := vars["instID"]
124 cfgName := vars["cfgname"]
129 http.Error(w, "Empty body", http.StatusBadRequest)
133 err := json.NewDecoder(r.Body).Decode(&p)
135 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
139 ret, err := h.client.Update(instanceID, cfgName, p)
141 http.Error(w, err.Error(), http.StatusInternalServerError)
145 w.Header().Set("Content-Type", "application/json")
146 w.WriteHeader(http.StatusCreated)
147 err = json.NewEncoder(w).Encode(ret)
149 http.Error(w, err.Error(), http.StatusInternalServerError)
154 // rollbackHandler handles Rollback operations to a specific version
155 func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request) {
157 instanceID := vars["instID"]
160 http.Error(w, "Empty body", http.StatusBadRequest)
164 var p app.ConfigRollback
165 err := json.NewDecoder(r.Body).Decode(&p)
167 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
170 err = h.client.Rollback(instanceID, p)
172 http.Error(w, err.Error(), http.StatusInternalServerError)
175 w.WriteHeader(http.StatusNoContent)
178 // tagitHandler handles TAGIT operation
179 func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
181 instanceID := vars["instID"]
184 http.Error(w, "Empty body", http.StatusBadRequest)
188 var p app.ConfigTagit
189 err := json.NewDecoder(r.Body).Decode(&p)
191 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
195 err = h.client.Tagit(instanceID, p)
197 http.Error(w, err.Error(), http.StatusInternalServerError)
200 w.WriteHeader(http.StatusNoContent)