2 // ========================LICENSE_START=================================
\r
5 // Copyright (C) 2024: OpenInfra Foundation Europe
\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
11 // http://www.apache.org/licenses/LICENSE-2.0
\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
28 log "github.com/sirupsen/logrus"
\r
29 resty "github.com/go-resty/resty/v2"
\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
39 type KongServiceResponse struct {
\r
40 ID string `json:"id"`
\r
43 type ServiceResponse struct {
\r
44 Data []KongService `json:"data"`
\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
55 type RouteResponse struct {
\r
56 Data []KongRoute `json:"data"`
\r
59 type Service struct {
\r
60 ID string `json:"id"`
\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
66 kongAdminApiUrl := fmt.Sprintf("%s://%s:%d/", myEnv["KONG_PROTOCOL"], myEnv["KONG_IPV4"], myPorts["KONG_CONTROL_PLANE_PORT"])
\r
68 err := deleteAllRoutes(kongAdminApiUrl)
\r
70 log.Fatalf("error deleting routes %v", err)
\r
74 err = deleteAllServices(kongAdminApiUrl)
\r
76 log.Fatalf("error deleting services %v", err)
\r
80 log.Info("all Kong routes and services deleted successfully")
\r
85 func deleteAllRoutes(kongAdminApiUrl string) error {
\r
86 routes, err := listRoutes(kongAdminApiUrl)
\r
91 for _, route := range routes {
\r
92 if err := deleteRoute(kongAdminApiUrl, route.ID); err != nil {
\r
100 func deleteAllServices(kongAdminApiUrl string) error {
\r
101 services, err := listServices(kongAdminApiUrl)
\r
106 for _, service := range services {
\r
107 if err := deleteService(kongAdminApiUrl, service.ID); err != nil {
\r
115 func listServices(kongAdminApiUrl string) ([]KongService, error) {
\r
116 client := resty.New()
\r
117 resp, err := client.R().Get(kongAdminApiUrl + "services")
\r
123 if resp.StatusCode() != http.StatusOK {
\r
124 err := fmt.Errorf("failed to list services, status code %d", resp.StatusCode())
\r
128 var serviceResponse ServiceResponse
\r
129 err = json.Unmarshal(resp.Body(), &serviceResponse)
\r
134 log.Infof("kong services %v", serviceResponse.Data)
\r
135 return serviceResponse.Data, nil
\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
147 if resp.StatusCode() != http.StatusOK {
\r
148 err := fmt.Errorf("failed to list routes, status code %d", resp.StatusCode())
\r
152 var routeResponse RouteResponse
\r
153 err = json.Unmarshal(resp.Body(), &routeResponse)
\r
158 log.Infof("kong routes %v", routeResponse.Data)
\r
159 return routeResponse.Data, nil
\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
171 if resp.StatusCode() != http.StatusNoContent {
\r
172 err := fmt.Errorf("failed to delete service %s, status code %d", serviceID, resp.StatusCode())
\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
188 if resp.StatusCode() != http.StatusNoContent {
\r
189 err := fmt.Errorf("failed to delete route %s, status code %d", routeID, resp.StatusCode())
\r