Make definition tags specific
[multicloud/k8s.git] / src / k8splugin / api / instancehandler.go
1 /*
2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6     http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14 package api
15
16 import (
17         "encoding/json"
18         "errors"
19         "io"
20         "net/http"
21
22         "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
23
24         "github.com/gorilla/mux"
25         pkgerrors "github.com/pkg/errors"
26 )
27
28 // Used to store the backend implementation objects
29 // Also simplifies the mocking needed for unit testing
30 type instanceHandler struct {
31         // Interface that implements the Instance operations
32         client app.InstanceManager
33 }
34
35 func (i instanceHandler) validateBody(body interface{}) error {
36         switch b := body.(type) {
37         case app.InstanceRequest:
38                 if b.CloudRegion == "" {
39                         werr := pkgerrors.Wrap(errors.New("Invalid/Missing CloudRegion in POST request"), "CreateVnfRequest bad request")
40                         return werr
41                 }
42                 if b.RBName == "" || b.RBVersion == "" {
43                         werr := pkgerrors.Wrap(errors.New("Invalid/Missing resource bundle parameters in POST request"), "CreateVnfRequest bad request")
44                         return werr
45                 }
46                 if b.ProfileName == "" {
47                         werr := pkgerrors.Wrap(errors.New("Invalid/Missing profile name in POST request"), "CreateVnfRequest bad request")
48                         return werr
49                 }
50         }
51         return nil
52 }
53
54 func (i instanceHandler) createHandler(w http.ResponseWriter, r *http.Request) {
55         var resource app.InstanceRequest
56
57         err := json.NewDecoder(r.Body).Decode(&resource)
58         switch {
59         case err == io.EOF:
60                 http.Error(w, "Body empty", http.StatusBadRequest)
61                 return
62         case err != nil:
63                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
64                 return
65         }
66
67         // Check body for expected parameters
68         err = i.validateBody(resource)
69         if err != nil {
70                 http.Error(w, err.Error(), http.StatusUnprocessableEntity)
71                 return
72         }
73
74         resp, err := i.client.Create(resource)
75         if err != nil {
76                 http.Error(w, err.Error(), http.StatusInternalServerError)
77                 return
78         }
79
80         w.Header().Set("Content-Type", "application/json")
81         w.WriteHeader(http.StatusCreated)
82         err = json.NewEncoder(w).Encode(resp)
83         if err != nil {
84                 http.Error(w, err.Error(), http.StatusInternalServerError)
85                 return
86         }
87 }
88
89 // getHandler retrieves information about an instance via the ID
90 func (i instanceHandler) getHandler(w http.ResponseWriter, r *http.Request) {
91         vars := mux.Vars(r)
92         id := vars["instID"]
93
94         resp, err := i.client.Get(id)
95         if err != nil {
96                 http.Error(w, err.Error(), http.StatusInternalServerError)
97                 return
98         }
99
100         w.Header().Set("Content-Type", "application/json")
101         w.WriteHeader(http.StatusOK)
102         err = json.NewEncoder(w).Encode(resp)
103         if err != nil {
104                 http.Error(w, err.Error(), http.StatusInternalServerError)
105                 return
106         }
107 }
108
109 // deleteHandler method terminates an instance via the ID
110 func (i instanceHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
111         vars := mux.Vars(r)
112         id := vars["instID"]
113
114         err := i.client.Delete(id)
115         if err != nil {
116                 http.Error(w, err.Error(), http.StatusInternalServerError)
117                 return
118         }
119
120         w.Header().Set("Content-Type", "application/json")
121         w.WriteHeader(http.StatusAccepted)
122 }
123
124 // // UpdateHandler method to update a VNF instance.
125 // func UpdateHandler(w http.ResponseWriter, r *http.Request) {
126 //      vars := mux.Vars(r)
127 //      id := vars["vnfInstanceId"]
128
129 //      var resource UpdateVnfRequest
130
131 //      if r.Body == nil {
132 //              http.Error(w, "Body empty", http.StatusBadRequest)
133 //              return
134 //      }
135
136 //      err := json.NewDecoder(r.Body).Decode(&resource)
137 //      if err != nil {
138 //              http.Error(w, err.Error(), http.StatusUnprocessableEntity)
139 //              return
140 //      }
141
142 //      err = validateBody(resource)
143 //      if err != nil {
144 //              http.Error(w, err.Error(), http.StatusUnprocessableEntity)
145 //              return
146 //      }
147
148 //      kubeData, err := utils.ReadCSARFromFileSystem(resource.CsarID)
149
150 //      if kubeData.Deployment == nil {
151 //              werr := pkgerrors.Wrap(err, "Update VNF deployment error")
152 //              http.Error(w, werr.Error(), http.StatusInternalServerError)
153 //              return
154 //      }
155 //      kubeData.Deployment.SetUID(types.UID(id))
156
157 //      if err != nil {
158 //              werr := pkgerrors.Wrap(err, "Update VNF deployment information error")
159 //              http.Error(w, werr.Error(), http.StatusInternalServerError)
160 //              return
161 //      }
162
163 //      // (TODO): Read kubeconfig for specific Cloud Region from local file system
164 //      // if present or download it from AAI
165 //      s, err := NewVNFInstanceService("../kubeconfig/config")
166 //      if err != nil {
167 //              http.Error(w, err.Error(), http.StatusInternalServerError)
168 //              return
169 //      }
170
171 //      err = s.Client.UpdateDeployment(kubeData.Deployment, resource.Namespace)
172 //      if err != nil {
173 //              werr := pkgerrors.Wrap(err, "Update VNF error")
174
175 //              http.Error(w, werr.Error(), http.StatusInternalServerError)
176 //              return
177 //      }
178
179 //      resp := UpdateVnfResponse{
180 //              DeploymentID: id,
181 //      }
182
183 //      w.Header().Set("Content-Type", "application/json")
184 //      w.WriteHeader(http.StatusCreated)
185
186 //      err = json.NewEncoder(w).Encode(resp)
187 //      if err != nil {
188 //              werr := pkgerrors.Wrap(err, "Parsing output of new VNF error")
189 //              http.Error(w, werr.Error(), http.StatusInternalServerError)
190 //      }
191 // }