Add suport for query api on root level
[multicloud/k8s.git] / src / k8splugin / api / confighandler.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  * Copyright © 2021 Samsung Electronics
4  * Copyright © 2021 Orange
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package api
20
21 import (
22         "encoding/json"
23         "net/http"
24
25         "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
26
27         "github.com/gorilla/mux"
28 )
29
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
36 }
37
38 // createHandler handles creation of the definition entry in the database
39 func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
40         var p app.Config
41         vars := mux.Vars(r)
42         instanceID := vars["instID"]
43
44         if r.Body == nil {
45                 http.Error(w, "Empty body", http.StatusBadRequest)
46                 return
47         }
48
49         err := json.NewDecoder(r.Body).Decode(&p)
50         if err != nil {
51                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
52                 return
53         }
54
55         // Name is required.
56         if p.ConfigName == "" {
57                 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
58                 return
59         }
60
61         ret, err := h.client.Create(instanceID, p)
62         if err != nil {
63                 http.Error(w, err.Error(), http.StatusInternalServerError)
64                 return
65         }
66
67         w.Header().Set("Content-Type", "application/json")
68         w.WriteHeader(http.StatusCreated)
69         err = json.NewEncoder(w).Encode(ret)
70         if err != nil {
71                 http.Error(w, err.Error(), http.StatusInternalServerError)
72                 return
73         }
74 }
75
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) {
79         vars := mux.Vars(r)
80         instanceID := vars["instID"]
81         cfgName := vars["cfgname"]
82
83         ret, err := h.client.Get(instanceID, cfgName)
84         if err != nil {
85                 http.Error(w, err.Error(), http.StatusInternalServerError)
86                 return
87         }
88
89         w.Header().Set("Content-Type", "application/json")
90         w.WriteHeader(http.StatusOK)
91         err = json.NewEncoder(w).Encode(ret)
92         if err != nil {
93                 http.Error(w, err.Error(), http.StatusInternalServerError)
94                 return
95         }
96 }
97
98 // deleteHandler handles DELETE operations on a config
99 func (h rbConfigHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
100         vars := mux.Vars(r)
101         instanceID := vars["instID"]
102         cfgName := vars["cfgname"]
103
104         ret, err := h.client.Delete(instanceID, cfgName)
105         if err != nil {
106                 http.Error(w, err.Error(), http.StatusInternalServerError)
107                 return
108         }
109
110         w.Header().Set("Content-Type", "application/json")
111         w.WriteHeader(http.StatusOK)
112         err = json.NewEncoder(w).Encode(ret)
113         if err != nil {
114                 http.Error(w, err.Error(), http.StatusInternalServerError)
115                 return
116         }
117
118 }
119
120 // UpdateHandler handles Update operations on a particular configuration
121 func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
122         vars := mux.Vars(r)
123         instanceID := vars["instID"]
124         cfgName := vars["cfgname"]
125
126         var p app.Config
127
128         if r.Body == nil {
129                 http.Error(w, "Empty body", http.StatusBadRequest)
130                 return
131         }
132
133         err := json.NewDecoder(r.Body).Decode(&p)
134         if err != nil {
135                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
136                 return
137         }
138
139         ret, err := h.client.Update(instanceID, cfgName, p)
140         if err != nil {
141                 http.Error(w, err.Error(), http.StatusInternalServerError)
142                 return
143         }
144
145         w.Header().Set("Content-Type", "application/json")
146         w.WriteHeader(http.StatusCreated)
147         err = json.NewEncoder(w).Encode(ret)
148         if err != nil {
149                 http.Error(w, err.Error(), http.StatusInternalServerError)
150                 return
151         }
152 }
153
154 // rollbackHandler handles Rollback operations to a specific version
155 func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request) {
156         vars := mux.Vars(r)
157         instanceID := vars["instID"]
158
159         if r.Body == nil {
160                 http.Error(w, "Empty body", http.StatusBadRequest)
161                 return
162         }
163
164         var p app.ConfigRollback
165         err := json.NewDecoder(r.Body).Decode(&p)
166         if err != nil {
167                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
168                 return
169         }
170         err = h.client.Rollback(instanceID, p)
171         if err != nil {
172                 http.Error(w, err.Error(), http.StatusInternalServerError)
173                 return
174         }
175         w.WriteHeader(http.StatusNoContent)
176 }
177
178 // tagitHandler handles TAGIT operation
179 func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
180         vars := mux.Vars(r)
181         instanceID := vars["instID"]
182
183         if r.Body == nil {
184                 http.Error(w, "Empty body", http.StatusBadRequest)
185                 return
186         }
187
188         var p app.ConfigTagit
189         err := json.NewDecoder(r.Body).Decode(&p)
190         if err != nil {
191                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
192                 return
193         }
194
195         err = h.client.Tagit(instanceID, p)
196         if err != nil {
197                 http.Error(w, err.Error(), http.StatusInternalServerError)
198                 return
199         }
200         w.WriteHeader(http.StatusNoContent)
201 }