2022-10-06 20:52:52 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client/config"
|
2022-10-07 20:17:15 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client/edge_router_policy"
|
|
|
|
identity_edge "github.com/openziti/edge/rest_management_api_client/identity"
|
2022-10-06 20:52:52 +02:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-backend\"", 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
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:17:15 +02:00
|
|
|
func 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
|
|
|
|
}
|
|
|
|
|
2022-10-06 20:52:52 +02:00
|
|
|
func deleteService(svcId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
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)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:17:15 +02:00
|
|
|
func deleteEdgeRouterPolicy(id string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
filter := fmt.Sprintf("name=\"zrok-%v\"", id)
|
2022-10-06 20:52:52 +02:00
|
|
|
limit := int64(0)
|
|
|
|
offset := int64(0)
|
2022-10-07 20:17:15 +02:00
|
|
|
listReq := &edge_router_policy.ListEdgeRouterPoliciesParams{
|
2022-10-06 20:52:52 +02:00
|
|
|
Filter: &filter,
|
|
|
|
Limit: &limit,
|
|
|
|
Offset: &offset,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
listReq.SetTimeout(30 * time.Second)
|
2022-10-07 20:17:15 +02:00
|
|
|
listResp, err := edge.EdgeRouterPolicy.ListEdgeRouterPolicies(listReq, nil)
|
2022-10-06 20:52:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-07 20:17:15 +02:00
|
|
|
if len(listResp.Payload.Data) == 1 {
|
|
|
|
erpId := *(listResp.Payload.Data[0].ID)
|
|
|
|
req := &edge_router_policy.DeleteEdgeRouterPolicyParams{
|
|
|
|
ID: erpId,
|
2022-10-06 20:52:52 +02:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2022-10-07 20:17:15 +02:00
|
|
|
_, err := edge.EdgeRouterPolicy.DeleteEdgeRouterPolicy(req, nil)
|
2022-10-06 20:52:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-07 20:17:15 +02:00
|
|
|
logrus.Infof("deleted edge router policy '%v'", erpId)
|
|
|
|
} else {
|
|
|
|
logrus.Infof("found '%d' edge router policies, expected 1", len(listResp.Payload.Data))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteIdentity(id string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
req := &identity_edge.DeleteIdentityParams{
|
|
|
|
ID: id,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.Identity.DeleteIdentity(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-10-06 20:52:52 +02:00
|
|
|
}
|
2022-10-07 20:17:15 +02:00
|
|
|
logrus.Infof("deleted identity '%v'", id)
|
2022-10-06 20:52:52 +02:00
|
|
|
return nil
|
|
|
|
}
|