Add vnf definition APIs
[multicloud/k8s.git] / src / k8splugin / api / vnfdhandler.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         "k8splugin/vnfd"
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 vnfdHandler struct {
31         // Interface that implements vnfDefinition operations
32         // We will set this variable with a mock interface for testing
33         vnfdClient vnfd.VNFDefinitionInterface
34 }
35
36 // vnfdCreateHandler handles creation of the vnfd entry in the database
37 func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) {
38         var v vnfd.VNFDefinition
39
40         if r.Body == nil {
41                 http.Error(w, "Empty body", http.StatusBadRequest)
42                 return
43         }
44
45         err := json.NewDecoder(r.Body).Decode(&v)
46         if err != nil {
47                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
48                 return
49         }
50
51         // Name is required.
52         if v.Name == "" {
53                 http.Error(w, "Missing name in POST request", http.StatusBadRequest)
54                 return
55         }
56
57         ret, err := h.vnfdClient.Create(v)
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 // vnfdUploadHandler handles upload of the vnf tar file into the database
73 // Note: This will be implemented in a different patch
74 func (h vnfdHandler) vnfdUploadHandler(w http.ResponseWriter, r *http.Request) {
75 }
76
77 // vnfdListHandler handles GET (list) operations on the /v1/vnfd endpoint
78 // Returns a list of vnfd.VNFDefinitions
79 func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) {
80         ret, err := h.vnfdClient.List()
81         if err != nil {
82                 http.Error(w, err.Error(), http.StatusInternalServerError)
83                 return
84         }
85
86         w.Header().Set("Content-Type", "application/json")
87         w.WriteHeader(http.StatusOK)
88         err = json.NewEncoder(w).Encode(ret)
89         if err != nil {
90                 http.Error(w, err.Error(), http.StatusInternalServerError)
91                 return
92         }
93 }
94
95 // vnfdGetHandler handles GET operations on a particular VNFID
96 // Returns a vnfd.VNFDefinition
97 func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) {
98         vars := mux.Vars(r)
99         vnfdID := vars["vnfdID"]
100
101         ret, err := h.vnfdClient.Get(vnfdID)
102         if err != nil {
103                 http.Error(w, err.Error(), http.StatusInternalServerError)
104                 return
105         }
106
107         w.Header().Set("Content-Type", "application/json")
108         w.WriteHeader(http.StatusOK)
109         err = json.NewEncoder(w).Encode(ret)
110         if err != nil {
111                 http.Error(w, err.Error(), http.StatusInternalServerError)
112                 return
113         }
114 }
115
116 // vnfdDeleteHandler handles DELETE operations on a particular VNFID
117 func (h vnfdHandler) vnfdDeleteHandler(w http.ResponseWriter, r *http.Request) {
118         vars := mux.Vars(r)
119         vnfdID := vars["vnfdID"]
120
121         err := h.vnfdClient.Delete(vnfdID)
122         if err != nil {
123                 http.Error(w, err.Error(), http.StatusInternalServerError)
124                 return
125         }
126
127         w.WriteHeader(http.StatusNoContent)
128 }