more reasonable allocation and deallocation of resources (#3, #9)

This commit is contained in:
Michael Quigley 2022-07-27 12:55:51 -04:00
parent 91849caf25
commit 6b4fcadc6b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 145 additions and 20 deletions

View File

@ -28,8 +28,6 @@ func tunnelHandler(params tunnel.TunnelParams) middleware.Responder {
logrus.Error(err)
return middleware.Error(500, err.Error())
}
logrus.Infof("allocated service '%v'", svcName)
svcId, err := createService(svcName, edge)
if err != nil {
logrus.Error(err)
@ -57,6 +55,8 @@ func tunnelHandler(params tunnel.TunnelParams) middleware.Responder {
return middleware.Error(500, err.Error())
}
logrus.Infof("allocated service '%v'", svcName)
resp := tunnel.NewTunnelCreated().WithPayload(&rest_model_zrok.TunnelResponse{
Service: svcName,
})
@ -104,11 +104,11 @@ func createServicePolicyBind(svcName, svcId, envId string, edge *rest_management
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
resp, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("created service policy '%v'", name)
logrus.Infof("created service policy '%v'", resp.Payload.Data.ID)
return nil
}
@ -132,11 +132,11 @@ func createServicePolicyDial(svcName, svcId string, edge *rest_management_api_cl
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
resp, err := edge.ServicePolicy.CreateServicePolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("created service policy '%v'", name)
logrus.Infof("created service policy '%v'", resp.Payload.Data.ID)
return nil
}
@ -155,11 +155,11 @@ func createServiceEdgeRouterPolicy(svcName, svcId string, edge *rest_management_
Context: context.Background(),
}
serpParams.SetTimeout(30 * time.Second)
_, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil)
resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil)
if err != nil {
return err
}
logrus.Infof("created service edge router policy '%v'", svcName)
logrus.Infof("created service edge router policy '%v'", resp.Payload.Data.ID)
return nil
}
@ -178,10 +178,10 @@ func createEdgeRouterPolicy(svcName, envId string, edge *rest_management_api_cli
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(req, nil)
resp, err := edge.EdgeRouterPolicy.CreateEdgeRouterPolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("created edge router policy '%v'", svcName)
logrus.Infof("created edge router policy '%v'", resp.Payload.Data.ID)
return nil
}

View File

@ -7,6 +7,9 @@ import (
"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/openziti/edge/rest_management_api_client/service"
"github.com/openziti/edge/rest_management_api_client/service_edge_router_policy"
"github.com/openziti/edge/rest_management_api_client/service_policy"
"github.com/sirupsen/logrus"
"time"
)
@ -18,20 +21,37 @@ func untunnelHandler(params tunnel.UntunnelParams) middleware.Responder {
return tunnel.NewUntunnelInternalServerError()
}
svcId := params.Body.Service
if err := deleteEdgeRouterPolicy(svcId, edge); err != nil {
svcName := params.Body.Service
if err := deleteEdgeRouterPolicy(svcName, edge); err != nil {
logrus.Error(err)
return middleware.Error(500, err.Error())
return tunnel.NewUntunnelInternalServerError()
}
if err := deleteServiceEdgeRouterPolicy(svcName, edge); err != nil {
logrus.Error(err)
return tunnel.NewUntunnelInternalServerError()
}
if err := deleteServicePolicyDial(svcName, edge); err != nil {
logrus.Error(err)
return tunnel.NewUntunnelInternalServerError()
}
if err := deleteServicePolicyBind(svcName, edge); err != nil {
logrus.Error(err)
return tunnel.NewUntunnelInternalServerError()
}
if err := deleteService(svcName, edge); err != nil {
logrus.Error(err)
return tunnel.NewUntunnelInternalServerError()
}
logrus.Infof("deallocated service '%v'", svcName)
return tunnel.NewUntunnelOK()
}
func deleteEdgeRouterPolicy(svcId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("name=\"%v\"", svcId)
func deleteEdgeRouterPolicy(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("name=\"%v\"", svcName)
limit := int64(1)
offset := int64(0)
listReq := &edge_router_policy.ListEdgeRouterPoliciesParams{
Filter: &filter,
Limit: &limit,
@ -45,13 +65,12 @@ func deleteEdgeRouterPolicy(svcId string, edge *rest_management_api_client.ZitiE
}
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{
req := &edge_router_policy.DeleteEdgeRouterPolicyParams{
ID: erpId,
Context: context.Background(),
}
deleteReq.SetTimeout(30 * time.Second)
_, err := edge.EdgeRouterPolicy.DeleteEdgeRouterPolicy(deleteReq, nil)
req.SetTimeout(30 * time.Second)
_, err := edge.EdgeRouterPolicy.DeleteEdgeRouterPolicy(req, nil)
if err != nil {
return err
}
@ -61,3 +80,109 @@ func deleteEdgeRouterPolicy(svcId string, edge *rest_management_api_client.ZitiE
}
return nil
}
func deleteServiceEdgeRouterPolicy(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("name=\"%v\"", svcName)
limit := int64(1)
offset := int64(0)
listReq := &service_edge_router_policy.ListServiceEdgeRouterPoliciesParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.ServiceEdgeRouterPolicy.ListServiceEdgeRouterPolicies(listReq, nil)
if err != nil {
return err
}
if len(listResp.Payload.Data) == 1 {
serpId := *(listResp.Payload.Data[0].ID)
req := &service_edge_router_policy.DeleteServiceEdgeRouterPolicyParams{
ID: serpId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServiceEdgeRouterPolicy.DeleteServiceEdgeRouterPolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("deleted service edge router policy '%v'", serpId)
} else {
logrus.Infof("did not find a service edge router policy")
}
return nil
}
func deleteServicePolicyBind(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
return deleteServicePolicy(fmt.Sprintf("name=\"%v-bind\"", svcName), edge)
}
func deleteServicePolicyDial(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
return deleteServicePolicy(fmt.Sprintf("name=\"%v-dial\"", svcName), edge)
}
func deleteServicePolicy(filter string, edge *rest_management_api_client.ZitiEdgeManagement) error {
limit := int64(1)
offset := int64(0)
listReq := &service_policy.ListServicePoliciesParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.ServicePolicy.ListServicePolicies(listReq, nil)
if err != nil {
return err
}
if len(listResp.Payload.Data) == 1 {
spId := *(listResp.Payload.Data[0].ID)
req := &service_policy.DeleteServicePolicyParams{
ID: spId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.ServicePolicy.DeleteServicePolicy(req, nil)
if err != nil {
return err
}
logrus.Infof("deleted service policy '%v'", spId)
} else {
logrus.Infof("did not find a service policy")
}
return nil
}
func deleteService(svcName string, edge *rest_management_api_client.ZitiEdgeManagement) error {
filter := fmt.Sprintf("name=\"%v\"", svcName)
limit := int64(1)
offset := int64(0)
listReq := &service.ListServicesParams{
Filter: &filter,
Limit: &limit,
Offset: &offset,
Context: context.Background(),
}
listReq.SetTimeout(30 * time.Second)
listResp, err := edge.Service.ListServices(listReq, nil)
if err != nil {
return err
}
if len(listResp.Payload.Data) == 1 {
svcId := *(listResp.Payload.Data[0].ID)
req := &service.DeleteServiceParams{
ID: svcId,
Context: context.Background(),
}
req.SetTimeout(30 * time.Second)
_, err := edge.Service.DeleteService(req, nil)
if err != nil {
return err
}
logrus.Infof("deleted service '%v'", svcId)
} else {
logrus.Infof("did not find a service")
}
return nil
}