2022-12-14 20:40:45 +01:00
|
|
|
package zrokEdgeSdk
|
2022-12-14 19:06:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-07 20:31:39 +01:00
|
|
|
"fmt"
|
2023-05-25 17:50:38 +02:00
|
|
|
"github.com/openziti/edge-api/rest_management_api_client"
|
|
|
|
edge_service "github.com/openziti/edge-api/rest_management_api_client/service"
|
|
|
|
"github.com/openziti/edge-api/rest_model"
|
2022-12-14 20:11:50 +01:00
|
|
|
"github.com/pkg/errors"
|
2022-12-14 19:06:26 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-05-25 20:59:39 +02:00
|
|
|
"time"
|
2022-12-14 19:06:26 +01:00
|
|
|
)
|
|
|
|
|
2023-03-07 20:31:39 +01:00
|
|
|
func FindShareService(svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) {
|
|
|
|
filter := fmt.Sprintf("id=\"%v\"", svcZId)
|
|
|
|
limit := int64(0)
|
|
|
|
offset := int64(0)
|
|
|
|
listReq := &edge_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 "", errors.Wrapf(err, "error listing service '%v'", svcZId)
|
|
|
|
}
|
|
|
|
if len(listResp.Payload.Data) == 1 {
|
|
|
|
return *listResp.Payload.Data[0].Name, nil
|
|
|
|
}
|
|
|
|
return "", errors.Errorf("service with ziti id '%v' not found", svcZId)
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
func CreateShareService(envZId, shrToken, cfgZId string, edge *rest_management_api_client.ZitiEdgeManagement) (shrZId string, err error) {
|
|
|
|
shrZId, err = CreateService(shrToken, []string{cfgZId}, map[string]interface{}{"zrokShareToken": shrToken}, edge)
|
2022-12-14 20:11:50 +01:00
|
|
|
if err != nil {
|
2023-01-04 20:21:23 +01:00
|
|
|
return "", errors.Wrapf(err, "error creating share '%v'", shrToken)
|
2022-12-14 20:11:50 +01:00
|
|
|
}
|
2023-01-04 20:21:23 +01:00
|
|
|
logrus.Infof("created share '%v' (with ziti id '%v') for environment '%v'", shrToken, shrZId, envZId)
|
|
|
|
return shrZId, nil
|
2022-12-14 20:11:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:21:23 +01:00
|
|
|
func CreateService(name string, cfgZIds []string, addlTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (shrZId string, err error) {
|
2022-12-14 19:06:26 +01:00
|
|
|
encryptionRequired := true
|
|
|
|
svc := &rest_model.ServiceCreate{
|
|
|
|
EncryptionRequired: &encryptionRequired,
|
|
|
|
Name: &name,
|
2022-12-14 21:47:14 +01:00
|
|
|
Tags: MergeTags(ZrokTags(), addlTags),
|
2022-12-14 19:06:26 +01:00
|
|
|
}
|
2022-12-14 21:47:14 +01:00
|
|
|
if cfgZIds != nil {
|
|
|
|
svc.Configs = cfgZIds
|
2022-12-14 19:06:26 +01:00
|
|
|
}
|
|
|
|
req := &edge_service.CreateServiceParams{
|
|
|
|
Service: svc,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
resp, err := edge.Service.CreateService(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Payload.Data.ID, nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:42:58 +01:00
|
|
|
func DeleteService(envZId, shrZId string, edge *rest_management_api_client.ZitiEdgeManagement) error {
|
2022-12-14 19:06:26 +01:00
|
|
|
req := &edge_service.DeleteServiceParams{
|
2023-01-04 20:42:58 +01:00
|
|
|
ID: shrZId,
|
2022-12-14 19:06:26 +01:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
req.SetTimeout(30 * time.Second)
|
|
|
|
_, err := edge.Service.DeleteService(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-04 20:42:58 +01:00
|
|
|
logrus.Infof("deleted service '%v' for environment '%v'", shrZId, envZId)
|
2022-12-14 19:06:26 +01:00
|
|
|
return nil
|
|
|
|
}
|