From d8d3e777886a4f4c620d1d7f49aaf864f32d93ea Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 15 Aug 2022 15:30:26 -0400 Subject: [PATCH] destroy config when done (#12) --- controller/untunnel.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/controller/untunnel.go b/controller/untunnel.go index b649533c..1cc41acc 100644 --- a/controller/untunnel.go +++ b/controller/untunnel.go @@ -8,6 +8,7 @@ import ( "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/tunnel" "github.com/openziti/edge/rest_management_api_client" + "github.com/openziti/edge/rest_management_api_client/config" "github.com/openziti/edge/rest_management_api_client/edge_router_policy" "github.com/openziti/edge/rest_management_api_client/service" "github.com/openziti/edge/rest_management_api_client/service_edge_router_policy" @@ -98,6 +99,10 @@ func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *res logrus.Error(err) return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } + if err := self.deleteConfig(svcName, edge); err != nil { + logrus.Error(err) + return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } if err := self.deleteService(svcId, edge); err != nil { logrus.Error(err) return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) @@ -245,6 +250,36 @@ func (_ *untunnelHandler) deleteServicePolicy(filter string, edge *rest_manageme return nil } +func (_ *untunnelHandler) deleteConfig(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := fmt.Sprintf("name=\"%v\"", svcName) + limit := int64(0) + offset := int64(0) + listReq := &config.ListConfigsParams{ + Filter: &filter, + Limit: &limit, + Offset: &offset, + Context: context.Background(), + } + listReq.SetTimeout(30 * time.Second) + listResp, err := edge.Config.ListConfigs(listReq, nil) + if err != nil { + return err + } + for _, cfg := range listResp.Payload.Data { + deleteReq := &config.DeleteConfigParams{ + ID: *cfg.ID, + Context: context.Background(), + } + deleteReq.SetTimeout(30 * time.Second) + _, err := edge.Config.DeleteConfig(deleteReq, nil) + if err != nil { + return err + } + logrus.Infof("deleted config '%v'", *cfg.ID) + } + return nil +} + func (_ *untunnelHandler) deleteService(svcId string, edge *rest_management_api_client.ZitiEdgeManagement) error { req := &service.DeleteServiceParams{ ID: svcId,