Merge "Change the import paths for go get access"
[multicloud/k8s.git] / src / k8splugin / api / confighandler.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package api
18
19 import (
20         "encoding/json"
21         "net/http"
22
23         "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
24
25         "github.com/gorilla/mux"
26 )
27
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
34 }
35
36 // createHandler handles creation of the definition entry in the database
37 func (h rbConfigHandler) createHandler(w http.ResponseWriter, r *http.Request) {
38         var p app.Config
39         vars := mux.Vars(r)
40         rbName := vars["rbname"]
41         rbVersion := vars["rbversion"]
42         prName := vars["prname"]
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(rbName, rbVersion, prName, 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         rbName := vars["rbname"]
81         rbVersion := vars["rbversion"]
82         prName := vars["prname"]
83         cfgName := vars["cfgname"]
84
85         ret, err := h.client.Get(rbName, rbVersion, prName, cfgName)
86         if err != nil {
87                 http.Error(w, err.Error(), http.StatusInternalServerError)
88                 return
89         }
90
91         w.Header().Set("Content-Type", "application/json")
92         w.WriteHeader(http.StatusOK)
93         err = json.NewEncoder(w).Encode(ret)
94         if err != nil {
95                 http.Error(w, err.Error(), http.StatusInternalServerError)
96                 return
97         }
98 }
99
100 // deleteHandler handles DELETE operations on a config
101 func (h rbConfigHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
102         vars := mux.Vars(r)
103         rbName := vars["rbname"]
104         rbVersion := vars["rbversion"]
105         prName := vars["prname"]
106         cfgName := vars["cfgname"]
107
108         ret, err := h.client.Delete(rbName, rbVersion, prName, cfgName)
109         if err != nil {
110                 http.Error(w, err.Error(), http.StatusInternalServerError)
111                 return
112         }
113
114         w.Header().Set("Content-Type", "application/json")
115         w.WriteHeader(http.StatusOK)
116         err = json.NewEncoder(w).Encode(ret)
117         if err != nil {
118                 http.Error(w, err.Error(), http.StatusInternalServerError)
119                 return
120         }
121
122 }
123
124 // UpdateHandler handles Update operations on a particular configuration
125 func (h rbConfigHandler) updateHandler(w http.ResponseWriter, r *http.Request) {
126         vars := mux.Vars(r)
127         rbName := vars["rbname"]
128         rbVersion := vars["rbversion"]
129         prName := vars["prname"]
130         cfgName := vars["cfgname"]
131
132         var p app.Config
133
134         if r.Body == nil {
135                 http.Error(w, "Empty body", http.StatusBadRequest)
136                 return
137         }
138
139         err := json.NewDecoder(r.Body).Decode(&p)
140         if err != nil {
141                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
142                 return
143         }
144
145         ret, err := h.client.Update(rbName, rbVersion, prName, cfgName, p)
146         if err != nil {
147                 http.Error(w, err.Error(), http.StatusInternalServerError)
148                 return
149         }
150
151         w.Header().Set("Content-Type", "application/json")
152         w.WriteHeader(http.StatusCreated)
153         err = json.NewEncoder(w).Encode(ret)
154         if err != nil {
155                 http.Error(w, err.Error(), http.StatusInternalServerError)
156                 return
157         }
158 }
159
160 // rollbackHandler handles Rollback operations to a specific version
161 func (h rbConfigHandler) rollbackHandler(w http.ResponseWriter, r *http.Request) {
162         vars := mux.Vars(r)
163         rbName := vars["rbname"]
164         rbVersion := vars["rbversion"]
165         prName := vars["prname"]
166
167         if r.Body == nil {
168                 http.Error(w, "Empty body", http.StatusBadRequest)
169                 return
170         }
171
172         var p app.ConfigRollback
173         err := json.NewDecoder(r.Body).Decode(&p)
174         if err != nil {
175                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
176                 return
177         }
178         err = h.client.Rollback(rbName, rbVersion, prName, p)
179         if err != nil {
180                 http.Error(w, err.Error(), http.StatusInternalServerError)
181                 return
182         }
183         w.WriteHeader(http.StatusNoContent)
184 }
185
186 // tagitHandler handles TAGIT operation
187 func (h rbConfigHandler) tagitHandler(w http.ResponseWriter, r *http.Request) {
188         vars := mux.Vars(r)
189         rbName := vars["rbname"]
190         rbVersion := vars["rbversion"]
191         prName := vars["prname"]
192
193         if r.Body == nil {
194                 http.Error(w, "Empty body", http.StatusBadRequest)
195                 return
196         }
197
198         var p app.ConfigTagit
199         err := json.NewDecoder(r.Body).Decode(&p)
200         if err != nil {
201                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
202                 return
203         }
204
205         err = h.client.Tagit(rbName, rbVersion, prName, p)
206         if err != nil {
207                 http.Error(w, err.Error(), http.StatusInternalServerError)
208                 return
209         }
210         w.WriteHeader(http.StatusNoContent)
211 }