From 91849caf2510e04bcd09e87c63d928e5446481f4 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 27 Jul 2022 12:17:37 -0400 Subject: [PATCH] clean up edge router policy (#3, #9) --- cmd/zrok/http.go | 9 ++++++ controller/controller.go | 1 + controller/tunnel.go | 6 ++-- controller/untunnel.go | 63 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 controller/untunnel.go diff --git a/cmd/zrok/http.go b/cmd/zrok/http.go index 86f4dca6..544846ea 100644 --- a/cmd/zrok/http.go +++ b/cmd/zrok/http.go @@ -61,4 +61,13 @@ func handleHttp(_ *cobra.Command, args []string) { func cleanupHttp(cfg *http.Config, zrok *rest_client_zrok.Zrok) { logrus.Infof("shutting down '%v'", cfg.Service) + req := tunnel.NewUntunnelParams() + req.Body = &rest_model_zrok.UntunnelRequest{ + Service: cfg.Service, + } + if _, err := zrok.Tunnel.Untunnel(req); err == nil { + logrus.Infof("shutdown complete") + } else { + logrus.Errorf("error shutting down: %v", err) + } } diff --git a/controller/controller.go b/controller/controller.go index d4e471d0..75b4e9ab 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -32,6 +32,7 @@ func Run(cfg *Config) error { api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler) api.IdentityEnableHandler = identity.EnableHandlerFunc(enableHandler) api.TunnelTunnelHandler = tunnel.TunnelHandlerFunc(tunnelHandler) + api.TunnelUntunnelHandler = tunnel.UntunnelHandlerFunc(untunnelHandler) server := rest_server_zrok.NewServer(api) defer func() { _ = server.Shutdown() }() diff --git a/controller/tunnel.go b/controller/tunnel.go index d65ec42e..341090cf 100644 --- a/controller/tunnel.go +++ b/controller/tunnel.go @@ -173,12 +173,12 @@ func createEdgeRouterPolicy(svcName, envId string, edge *rest_management_api_cli Name: &svcName, Semantic: &semantic, } - erpParams := &edge_router_policy.CreateEdgeRouterPolicyParams{ + req := &edge_router_policy.CreateEdgeRouterPolicyParams{ Policy: erp, Context: context.Background(), } - erpParams.SetTimeout(30 * time.Second) - _, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(erpParams, nil) + req.SetTimeout(30 * time.Second) + _, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(req, nil) if err != nil { return err } diff --git a/controller/untunnel.go b/controller/untunnel.go new file mode 100644 index 00000000..320f1acd --- /dev/null +++ b/controller/untunnel.go @@ -0,0 +1,63 @@ +package controller + +import ( + "context" + "fmt" + "github.com/go-openapi/runtime/middleware" + "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/edge_router_policy" + "github.com/sirupsen/logrus" + "time" +) + +func untunnelHandler(params tunnel.UntunnelParams) middleware.Responder { + edge, err := edgeClient() + if err != nil { + logrus.Error(err) + return tunnel.NewUntunnelInternalServerError() + } + + svcId := params.Body.Service + if err := deleteEdgeRouterPolicy(svcId, edge); err != nil { + logrus.Error(err) + return middleware.Error(500, err.Error()) + } + + return tunnel.NewUntunnelOK() +} + +func deleteEdgeRouterPolicy(svcId string, edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := fmt.Sprintf("name=\"%v\"", svcId) + limit := int64(1) + offset := int64(0) + + listReq := &edge_router_policy.ListEdgeRouterPoliciesParams{ + Filter: &filter, + Limit: &limit, + Offset: &offset, + Context: context.Background(), + } + listReq.SetTimeout(30 * time.Second) + listResp, err := edge.EdgeRouterPolicy.ListEdgeRouterPolicies(listReq, nil) + if err != nil { + return err + } + if len(listResp.Payload.Data) == 1 { + erpId := *(listResp.Payload.Data[0].ID) + logrus.Infof("found edge router policy '%v'", erpId) + deleteReq := &edge_router_policy.DeleteEdgeRouterPolicyParams{ + ID: erpId, + Context: context.Background(), + } + deleteReq.SetTimeout(30 * time.Second) + _, err := edge.EdgeRouterPolicy.DeleteEdgeRouterPolicy(deleteReq, nil) + if err != nil { + return err + } + logrus.Infof("deleted edge router policy '%v'", erpId) + } else { + logrus.Infof("did not find an edge router policy") + } + return nil +}