NONRTRIC-946: Add support for Kong routes
[nonrtric/plt/sme.git] / servicemanager / internal / kongclear / kongclear.go
1 // -\r
2 //   ========================LICENSE_START=================================\r
3 //   O-RAN-SC\r
4 //   %%\r
5 //   Copyright (C) 2024: OpenInfra Foundation Europe\r
6 //   %%\r
7 //   Licensed under the Apache License, Version 2.0 (the "License");\r
8 //   you may not use this file except in compliance with the License.\r
9 //   You may obtain a copy of the License at\r
10 //\r
11 //        http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 //   Unless required by applicable law or agreed to in writing, software\r
14 //   distributed under the License is distributed on an "AS IS" BASIS,\r
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 //   See the License for the specific language governing permissions and\r
17 //   limitations under the License.\r
18 //   ========================LICENSE_END===================================\r
19 //\r
20 \r
21 package kongclear\r
22 \r
23 import (\r
24         "encoding/json"\r
25         "fmt"\r
26         "net/http"\r
27 \r
28         log "github.com/sirupsen/logrus"\r
29         resty "github.com/go-resty/resty/v2"\r
30 )\r
31 \r
32 // Service represents the structure for Kong service creation\r
33 type KongService struct {\r
34         ID   string `json:"id"`\r
35         Name string `json:"name"`\r
36         URL  string `json:"url"`\r
37 }\r
38 \r
39 type KongServiceResponse struct {\r
40         ID string `json:"id"`\r
41 }\r
42 \r
43 type ServiceResponse struct {\r
44         Data []KongService `json:"data"`\r
45 }\r
46 \r
47 type KongRoute struct {\r
48         ID        string   `json:"id"`\r
49         Name      string   `json:"name"`\r
50         Paths     []string `json:"paths"`\r
51         Service   Service  `json:"service"`\r
52         StripPath bool     `json:"strip_path"`\r
53 }\r
54 \r
55 type RouteResponse struct {\r
56         Data []KongRoute `json:"data"`\r
57 }\r
58 \r
59 type Service struct {\r
60         ID string `json:"id"`\r
61 }\r
62 \r
63 func KongClear(myEnv map[string]string, myPorts map[string]int) error {\r
64         log.Info("attempting to delete all Kong routes and services")\r
65 \r
66         kongAdminApiUrl := fmt.Sprintf("%s://%s:%d/", myEnv["KONG_PROTOCOL"], myEnv["KONG_IPV4"], myPorts["KONG_CONTROL_PLANE_PORT"])\r
67 \r
68         err := deleteAllRoutes(kongAdminApiUrl)\r
69         if err != nil {\r
70                 log.Fatalf("error deleting routes %v", err)\r
71                 return err\r
72         }\r
73 \r
74         err = deleteAllServices(kongAdminApiUrl)\r
75         if err != nil {\r
76                 log.Fatalf("error deleting services %v", err)\r
77                 return err\r
78         }\r
79 \r
80         log.Info("all Kong routes and services deleted successfully")\r
81         return err\r
82 }\r
83 \r
84 \r
85 func deleteAllRoutes(kongAdminApiUrl string) error {\r
86         routes, err := listRoutes(kongAdminApiUrl)\r
87         if err != nil {\r
88                 return err\r
89         }\r
90 \r
91         for _, route := range routes {\r
92                 if err := deleteRoute(kongAdminApiUrl, route.ID); err != nil {\r
93                         return err\r
94                 }\r
95         }\r
96 \r
97         return nil\r
98 }\r
99 \r
100 func deleteAllServices(kongAdminApiUrl string) error {\r
101         services, err := listServices(kongAdminApiUrl)\r
102         if err != nil {\r
103                 return err\r
104         }\r
105 \r
106         for _, service := range services {\r
107                 if err := deleteService(kongAdminApiUrl, service.ID); err != nil {\r
108                         return err\r
109                 }\r
110         }\r
111 \r
112         return nil\r
113 }\r
114 \r
115 func listServices(kongAdminApiUrl string) ([]KongService, error) {\r
116         client := resty.New()\r
117         resp, err := client.R().Get(kongAdminApiUrl + "services")\r
118 \r
119         if err != nil {\r
120                 return nil, err\r
121         }\r
122 \r
123         if resp.StatusCode() != http.StatusOK {\r
124                 err := fmt.Errorf("failed to list services, status code %d", resp.StatusCode())\r
125                 return nil, err\r
126         }\r
127 \r
128         var serviceResponse ServiceResponse\r
129         err = json.Unmarshal(resp.Body(), &serviceResponse)\r
130         if err != nil {\r
131                 return nil, err\r
132         }\r
133 \r
134         log.Infof("kong services %v", serviceResponse.Data)\r
135         return serviceResponse.Data, nil\r
136 }\r
137 \r
138 func listRoutes(kongAdminApiUrl string) ([]KongRoute, error) {\r
139         client := resty.New()\r
140         resp, err := client.R().\r
141                 Get(kongAdminApiUrl + "routes")\r
142 \r
143         if err != nil {\r
144                 return nil, err\r
145         }\r
146 \r
147         if resp.StatusCode() != http.StatusOK {\r
148                 err := fmt.Errorf("failed to list routes, status code %d", resp.StatusCode())\r
149                 return nil, err\r
150         }\r
151 \r
152         var routeResponse RouteResponse\r
153         err = json.Unmarshal(resp.Body(), &routeResponse)\r
154         if err != nil {\r
155                 return nil, err\r
156         }\r
157 \r
158         log.Infof("kong routes %v", routeResponse.Data)\r
159         return routeResponse.Data, nil\r
160 }\r
161 \r
162 func deleteService(kongAdminApiUrl string, serviceID string) error {\r
163         log.Tracef("entering deleteService service ID %v", serviceID)\r
164         client := resty.New()\r
165         resp, err := client.R().Delete(kongAdminApiUrl + "services/" + serviceID)\r
166 \r
167         if err != nil {\r
168                 return err\r
169         }\r
170 \r
171         if resp.StatusCode() != http.StatusNoContent {\r
172                 err := fmt.Errorf("failed to delete service %s, status code %d", serviceID, resp.StatusCode())\r
173                 return err\r
174         }\r
175 \r
176         return nil\r
177 }\r
178 \r
179 func deleteRoute(kongAdminApiUrl string, routeID string) error {\r
180         log.Infof("kong route id %v", routeID)\r
181         client := resty.New()\r
182         resp, err := client.R().Delete(kongAdminApiUrl + "routes/" + routeID)\r
183 \r
184         if err != nil {\r
185                 return err\r
186         }\r
187 \r
188         if resp.StatusCode() != http.StatusNoContent {\r
189                 err := fmt.Errorf("failed to delete route %s, status code %d", routeID, resp.StatusCode())\r
190                 return err\r
191         }\r
192 \r
193         return nil\r
194 }