2 // ========================LICENSE_START=================================
5 // Copyright (C) 2024: OpenInfra Foundation Europe
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
29 resty "github.com/go-resty/resty/v2"
30 log "github.com/sirupsen/logrus"
33 // Service represents the structure for Kong service creation
34 type KongService struct {
36 Name string `json:"name"`
37 URL string `json:"url"`
38 Tags []string `json:"tags"`
41 type KongServiceResponse struct {
45 type ServiceResponse struct {
46 Data []KongService `json:"data"`
49 type KongRoute struct {
51 Name string `json:"name"`
52 Paths []string `json:"paths"`
53 Service Service `json:"service"`
54 StripPath bool `json:"strip_path"`
55 Tags []string `json:"tags"`
58 type RouteResponse struct {
59 Data []KongRoute `json:"data"`
66 func KongClear(myEnv map[string]string, myPorts map[string]int) error {
67 log.Info("delete only ServiceManager Kong routes and services")
69 kongAdminApiUrl := fmt.Sprintf("%s://%s:%d/", myEnv["KONG_PROTOCOL"], myEnv["KONG_CONTROL_PLANE_IPV4"], myPorts["KONG_CONTROL_PLANE_PORT"])
71 err := deleteRoutes(kongAdminApiUrl)
73 log.Fatalf("error deleting routes %v", err)
77 err = deleteServices(kongAdminApiUrl)
79 log.Fatalf("error deleting services %v", err)
83 log.Info("finished deleting only ServiceManger Kong routes and services")
87 func deleteRoutes(kongAdminApiUrl string) error {
88 routes, err := listRoutes(kongAdminApiUrl)
93 for _, route := range routes {
94 if areServiceManagerTags(route.Tags) {
95 if err := deleteRoute(kongAdminApiUrl, route.ID); err != nil {
104 func deleteServices(kongAdminApiUrl string) error {
105 services, err := listServices(kongAdminApiUrl)
110 for _, service := range services {
111 if areServiceManagerTags(service.Tags) {
112 if err := deleteService(kongAdminApiUrl, service.ID); err != nil {
121 func listRoutes(kongAdminApiUrl string) ([]KongRoute, error) {
122 client := resty.New()
123 resp, err := client.R().
124 Get(kongAdminApiUrl + "routes")
130 if resp.StatusCode() != http.StatusOK {
131 err := fmt.Errorf("failed to list routes, status code %d", resp.StatusCode())
135 var routeResponse RouteResponse
136 err = json.Unmarshal(resp.Body(), &routeResponse)
141 log.Infof("kong routes %v", routeResponse.Data)
142 return routeResponse.Data, nil
145 func listServices(kongAdminApiUrl string) ([]KongService, error) {
146 client := resty.New()
147 resp, err := client.R().Get(kongAdminApiUrl + "services")
153 if resp.StatusCode() != http.StatusOK {
154 err := fmt.Errorf("failed to list services, status code %d", resp.StatusCode())
158 var serviceResponse ServiceResponse
159 err = json.Unmarshal(resp.Body(), &serviceResponse)
164 log.Infof("kong services %v", serviceResponse.Data)
165 return serviceResponse.Data, nil
168 func areServiceManagerTags(tags []string) bool {
169 tagMap := make(map[string]string)
171 for _, tag := range tags {
172 log.Debugf("found tag %s", tag)
173 tagSlice := strings.Split(tag, ":")
174 log.Debugf("tag slice %v", tagSlice)
175 if (len(tagSlice) > 0) && (tagSlice[0] != "") {
176 if (len(tagSlice) > 1) {
177 tagMap[tagSlice[0]] = tagSlice[1]
179 tagMap[tagSlice[0]] = ""
184 if tagMap["apfId"] == "" {
185 log.Debug("did NOT find apfId")
188 log.Debugf("found valid apfId %s", tagMap["apfId"])
190 if tagMap["aefId"] == "" {
191 log.Debug("did NOT find aefId")
194 log.Debugf("found valid aefId %s", tagMap["aefId"])
196 if tagMap["apiId"] == "" {
197 log.Debug("did NOT find apiId")
200 log.Debugf("found valid apiId %s", tagMap["apiId"])
205 func deleteRoute(kongAdminApiUrl string, routeID string) error {
206 log.Infof("delete kong route id %s", routeID)
207 client := resty.New()
208 resp, err := client.R().Delete(kongAdminApiUrl + "routes/" + routeID)
214 if resp.StatusCode() != http.StatusNoContent {
215 err := fmt.Errorf("failed to delete route %s, status code %d", routeID, resp.StatusCode())
222 func deleteService(kongAdminApiUrl string, serviceID string) error {
223 log.Infof("delete kong service id %s", serviceID)
224 client := resty.New()
225 resp, err := client.R().Delete(kongAdminApiUrl + "services/" + serviceID)
231 if resp.StatusCode() != http.StatusNoContent {
232 err := fmt.Errorf("failed to delete service %s, status code %d", serviceID, resp.StatusCode())