Merge "Add update method to db interface"
[multicloud/k8s.git] / src / k8splugin / api / profilehandler.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         "io"
22         "io/ioutil"
23         "k8splugin/internal/rb"
24         "net/http"
25
26         "github.com/gorilla/mux"
27 )
28
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
35 }
36
37 // createHandler handles creation of the definition entry in the database
38 func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) {
39         var p rb.Profile
40
41         err := json.NewDecoder(r.Body).Decode(&p)
42         switch {
43         case err == io.EOF:
44                 http.Error(w, "Empty body", http.StatusBadRequest)
45                 return
46         case err != nil:
47                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
48                 return
49         }
50
51         // Name is required.
52         if p.Name == "" {
53                 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
54                 return
55         }
56
57         ret, err := h.client.Create(p)
58         if err != nil {
59                 http.Error(w, err.Error(), http.StatusInternalServerError)
60                 return
61         }
62
63         w.Header().Set("Content-Type", "application/json")
64         w.WriteHeader(http.StatusCreated)
65         err = json.NewEncoder(w).Encode(ret)
66         if err != nil {
67                 http.Error(w, err.Error(), http.StatusInternalServerError)
68                 return
69         }
70 }
71
72 // uploadHandler handles upload of the bundle tar file into the database
73 func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request) {
74         vars := mux.Vars(r)
75         rbName := vars["rbname"]
76         rbVersion := vars["rbversion"]
77         prName := vars["prname"]
78
79         inpBytes, err := ioutil.ReadAll(r.Body)
80         if err != nil {
81                 http.Error(w, "Unable to read body", http.StatusBadRequest)
82                 return
83         }
84
85         if len(inpBytes) == 0 {
86                 http.Error(w, "Empty body", http.StatusBadRequest)
87                 return
88         }
89
90         err = h.client.Upload(rbName, rbVersion, prName, inpBytes)
91         if err != nil {
92                 http.Error(w, err.Error(), http.StatusInternalServerError)
93                 return
94         }
95
96         w.WriteHeader(http.StatusOK)
97 }
98
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) {
102         vars := mux.Vars(r)
103         rbName := vars["rbname"]
104         rbVersion := vars["rbversion"]
105         prName := vars["prname"]
106
107         ret, err := h.client.Get(rbName, rbVersion, prName)
108         if err != nil {
109                 http.Error(w, err.Error(), http.StatusInternalServerError)
110                 return
111         }
112
113         w.Header().Set("Content-Type", "application/json")
114         w.WriteHeader(http.StatusOK)
115         err = json.NewEncoder(w).Encode(ret)
116         if err != nil {
117                 http.Error(w, err.Error(), http.StatusInternalServerError)
118                 return
119         }
120 }
121
122 // deleteHandler handles DELETE operations on a particular bundle definition id
123 func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
124         vars := mux.Vars(r)
125         rbName := vars["rbname"]
126         rbVersion := vars["rbversion"]
127         prName := vars["prname"]
128
129         err := h.client.Delete(rbName, rbVersion, prName)
130         if err != nil {
131                 http.Error(w, err.Error(), http.StatusInternalServerError)
132                 return
133         }
134
135         w.WriteHeader(http.StatusNoContent)
136 }