Add suport for query api on root level
[multicloud/k8s.git] / src / k8splugin / api / queryhandler.go
1 /*
2 Copyright 2018 Intel Corporation.
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     http://www.apache.org/licenses/LICENSE-2.0
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         log "github.com/onap/multicloud-k8s/src/k8splugin/internal/logutils"
25 )
26
27 // Used to store the backend implementation objects
28 // Also simplifies the mocking needed for unit testing
29 type queryHandler struct {
30         // Interface that implements the Instance operations
31         client app.QueryManager
32 }
33
34 // queryHandler retrieves information about specified resources for instance
35 func (i queryHandler) queryHandler(w http.ResponseWriter, r *http.Request) {
36         namespace := r.FormValue("Namespace")
37         cloudRegion := r.FormValue("CloudRegion")
38         apiVersion := r.FormValue("ApiVersion")
39         kind := r.FormValue("Kind")
40         name := r.FormValue("Name")
41         labels := r.FormValue("Labels")
42         if cloudRegion == "" {
43                 http.Error(w, "Missing CloudRegion mandatory parameter", http.StatusBadRequest)
44                 return
45         }
46         if apiVersion == "" {
47                 http.Error(w, "Missing ApiVersion mandatory parameter", http.StatusBadRequest)
48                 return
49         }
50         if kind == "" {
51                 http.Error(w, "Missing Kind mandatory parameter", http.StatusBadRequest)
52                 return
53         }
54         // instance id is irrelevant here
55         resp, err := i.client.Query(namespace, cloudRegion, apiVersion, kind, name, labels, "query")
56         if err != nil {
57                 log.Error("Error getting Query results", log.Fields{
58                         "error":       err,
59                         "cloudRegion": cloudRegion,
60                         "namespace":   namespace,
61                         "apiVersion":  apiVersion,
62                         "kind":        kind,
63                         "name":        name,
64                         "labels":      labels,
65                 })
66                 http.Error(w, err.Error(), http.StatusInternalServerError)
67         }
68         w.Header().Set("Content-Type", "application/json")
69         w.WriteHeader(http.StatusOK)
70         err = json.NewEncoder(w).Encode(resp)
71         if err != nil {
72                 log.Error("Error Marshaling Response", log.Fields{
73                         "error":    err,
74                         "response": resp,
75                 })
76                 http.Error(w, err.Error(), http.StatusInternalServerError)
77                 return
78         }
79 }