clean up edge router policy (#3, #9)

This commit is contained in:
Michael Quigley 2022-07-27 12:17:37 -04:00
parent b852518a0d
commit 91849caf25
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 76 additions and 3 deletions

View File

@ -61,4 +61,13 @@ func handleHttp(_ *cobra.Command, args []string) {
func cleanupHttp(cfg *http.Config, zrok *rest_client_zrok.Zrok) { func cleanupHttp(cfg *http.Config, zrok *rest_client_zrok.Zrok) {
logrus.Infof("shutting down '%v'", cfg.Service) 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)
}
} }

View File

@ -32,6 +32,7 @@ func Run(cfg *Config) error {
api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler) api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler)
api.IdentityEnableHandler = identity.EnableHandlerFunc(enableHandler) api.IdentityEnableHandler = identity.EnableHandlerFunc(enableHandler)
api.TunnelTunnelHandler = tunnel.TunnelHandlerFunc(tunnelHandler) api.TunnelTunnelHandler = tunnel.TunnelHandlerFunc(tunnelHandler)
api.TunnelUntunnelHandler = tunnel.UntunnelHandlerFunc(untunnelHandler)
server := rest_server_zrok.NewServer(api) server := rest_server_zrok.NewServer(api)
defer func() { _ = server.Shutdown() }() defer func() { _ = server.Shutdown() }()

View File

@ -173,12 +173,12 @@ func createEdgeRouterPolicy(svcName, envId string, edge *rest_management_api_cli
Name: &svcName, Name: &svcName,
Semantic: &semantic, Semantic: &semantic,
} }
erpParams := &edge_router_policy.CreateEdgeRouterPolicyParams{ req := &edge_router_policy.CreateEdgeRouterPolicyParams{
Policy: erp, Policy: erp,
Context: context.Background(), Context: context.Background(),
} }
erpParams.SetTimeout(30 * time.Second) req.SetTimeout(30 * time.Second)
_, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(erpParams, nil) _, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(req, nil)
if err != nil { if err != nil {
return err return err
} }

63
controller/untunnel.go Normal file
View File

@ -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
}