Add suport for query api on root level
[multicloud/k8s.git] / src / k8splugin / api / api.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         "github.com/onap/multicloud-k8s/src/k8splugin/internal/app"
21         "github.com/onap/multicloud-k8s/src/k8splugin/internal/connection"
22         "github.com/onap/multicloud-k8s/src/k8splugin/internal/healthcheck"
23         "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb"
24
25         "github.com/gorilla/mux"
26 )
27
28 // NewRouter creates a router that registers the various urls that are supported
29 func NewRouter(defClient rb.DefinitionManager,
30         profileClient rb.ProfileManager,
31         instClient app.InstanceManager,
32         queryClient app.QueryManager,
33         configClient app.ConfigManager,
34         connectionClient connection.ConnectionManager,
35         templateClient rb.ConfigTemplateManager,
36         healthcheckClient healthcheck.InstanceHCManager) *mux.Router {
37
38         router := mux.NewRouter()
39
40         // Setup Instance handler routes
41         if instClient == nil {
42                 instClient = app.NewInstanceClient()
43         }
44         instHandler := instanceHandler{client: instClient}
45         instRouter := router.PathPrefix("/v1").Subrouter()
46         instRouter.HandleFunc("/instance", instHandler.createHandler).Methods("POST")
47         instRouter.HandleFunc("/instance", instHandler.listHandler).Methods("GET")
48         // Match rb-names, versions or profiles
49         instRouter.HandleFunc("/instance", instHandler.listHandler).
50                 Queries("rb-name", "{rb-name}",
51                         "rb-version", "{rb-version}",
52                         "profile-name", "{profile-name}").Methods("GET")
53         instRouter.HandleFunc("/instance/{instID}", instHandler.getHandler).Methods("GET")
54         instRouter.HandleFunc("/instance/{instID}/status", instHandler.statusHandler).Methods("GET")
55         instRouter.HandleFunc("/instance/{instID}/query", instHandler.queryHandler).Methods("GET")
56         instRouter.HandleFunc("/instance/{instID}/query", instHandler.queryHandler).
57                 Queries("ApiVersion", "{ApiVersion}",
58                         "Kind", "{Kind}",
59                         "Name", "{Name}",
60                         "Labels", "{Labels}").Methods("GET")
61         instRouter.HandleFunc("/instance/{instID}", instHandler.deleteHandler).Methods("DELETE")
62
63         // Query handler routes
64         if queryClient == nil {
65                 queryClient = app.NewQueryClient()
66         }
67         queryHandler := queryHandler{client: queryClient}
68         queryRouter := router.PathPrefix("/v1").Subrouter()
69         queryRouter.HandleFunc("/query", queryHandler.queryHandler).Methods("GET")
70         queryRouter.HandleFunc("/query", queryHandler.queryHandler).
71                 Queries("Namespace", "{Namespace}",
72                         "CloudRegion", "{CloudRegion}",
73                         "ApiVersion", "{ApiVersion}",
74                         "Kind", "{Kind}",
75                         "Name", "{Name}",
76                         "Labels", "{Labels}").Methods("GET")
77
78         //Setup the broker handler here
79         //Use the base router without any path prefixes
80         brokerHandler := brokerInstanceHandler{client: instClient}
81         router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload", brokerHandler.createHandler).Methods("POST")
82         router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload/{instID}", brokerHandler.getHandler).Methods("GET")
83         router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload", brokerHandler.findHandler).Queries("name", "{name}").Methods("GET")
84         router.HandleFunc("/{cloud-owner}/{cloud-region}/infra_workload/{instID}", brokerHandler.deleteHandler).Methods("DELETE")
85
86         //Setup the connectivity api handler here
87         if connectionClient == nil {
88                 connectionClient = connection.NewConnectionClient()
89         }
90         connectionHandler := connectionHandler{client: connectionClient}
91         instRouter.HandleFunc("/connectivity-info", connectionHandler.createHandler).Methods("POST")
92         instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.getHandler).Methods("GET")
93         instRouter.HandleFunc("/connectivity-info/{connname}", connectionHandler.deleteHandler).Methods("DELETE")
94
95         //Setup resource bundle definition routes
96         if defClient == nil {
97                 defClient = rb.NewDefinitionClient()
98         }
99         defHandler := rbDefinitionHandler{client: defClient}
100         resRouter := router.PathPrefix("/v1/rb").Subrouter()
101         resRouter.HandleFunc("/definition", defHandler.createHandler).Methods("POST")
102         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/content", defHandler.uploadHandler).Methods("POST")
103         resRouter.HandleFunc("/definition/{rbname}", defHandler.listVersionsHandler).Methods("GET")
104         resRouter.HandleFunc("/definition", defHandler.listAllHandler).Methods("GET")
105         resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.getHandler).Methods("GET")
106         resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.deleteHandler).Methods("DELETE")
107
108         //Setup resource bundle profile routes
109         if profileClient == nil {
110                 profileClient = rb.NewProfileClient()
111         }
112         profileHandler := rbProfileHandler{client: profileClient}
113         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.createHandler).Methods("POST")
114         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.listHandler).Methods("GET")
115         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}/content", profileHandler.uploadHandler).Methods("POST")
116         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.getHandler).Methods("GET")
117         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.deleteHandler).Methods("DELETE")
118
119         // Config Template
120         if templateClient == nil {
121                 templateClient = rb.NewConfigTemplateClient()
122         }
123         templateHandler := rbTemplateHandler{client: templateClient}
124         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template", templateHandler.createHandler).Methods("POST")
125         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}/content", templateHandler.uploadHandler).Methods("POST")
126         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}", templateHandler.getHandler).Methods("GET")
127         resRouter.HandleFunc("/definition/{rbname}/{rbversion}/config-template/{tname}", templateHandler.deleteHandler).Methods("DELETE")
128
129         // Config value
130         if configClient == nil {
131                 configClient = app.NewConfigClient()
132         }
133         configHandler := rbConfigHandler{client: configClient}
134         instRouter.HandleFunc("/instance/{instID}/config", configHandler.createHandler).Methods("POST")
135         instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.getHandler).Methods("GET")
136         instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.updateHandler).Methods("PUT")
137         instRouter.HandleFunc("/instance/{instID}/config/{cfgname}", configHandler.deleteHandler).Methods("DELETE")
138         instRouter.HandleFunc("/instance/{instID}/config/rollback", configHandler.rollbackHandler).Methods("POST")
139         instRouter.HandleFunc("/instance/{instID}/config/tagit", configHandler.tagitHandler).Methods("POST")
140
141         // Instance Healthcheck API
142         if healthcheckClient == nil {
143                 healthcheckClient = healthcheck.NewHCClient()
144         }
145         healthcheckHandler := instanceHCHandler{client: healthcheckClient}
146         instRouter.HandleFunc("/instance/{instID}/healthcheck", healthcheckHandler.listHandler).Methods("GET")
147         instRouter.HandleFunc("/instance/{instID}/healthcheck", healthcheckHandler.createHandler).Methods("POST")
148         instRouter.HandleFunc("/instance/{instID}/healthcheck/{hcID}", healthcheckHandler.getHandler).Methods("GET")
149         instRouter.HandleFunc("/instance/{instID}/healthcheck/{hcID}", healthcheckHandler.deleteHandler).Methods("DELETE")
150
151         // Add healthcheck path
152         instRouter.HandleFunc("/healthcheck", healthCheckHandler).Methods("GET")
153
154         return router
155 }