diff --git a/controller/access.go b/controller/access.go index f945af0d..6a51cbb0 100644 --- a/controller/access.go +++ b/controller/access.go @@ -4,8 +4,8 @@ import ( "context" "fmt" "github.com/go-openapi/runtime/middleware" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" "github.com/openziti/edge/rest_management_api_client" @@ -96,7 +96,7 @@ func (h *accessHandler) Handle(params service.AccessParams, principal *rest_mode } func createServicePolicyDialForEnvironment(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement, tags ...*rest_model.Tags) error { - allTags := edge_ctrl.ZrokServiceTags(svcToken) + allTags := zrok_edge_sdk.ZrokServiceTags(svcToken) for _, t := range tags { for k, v := range t.SubTags { allTags.SubTags[k] = v diff --git a/controller/bootstrap.go b/controller/bootstrap.go index 52c75992..a5c88cbb 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -5,8 +5,8 @@ import ( "context" "encoding/json" "fmt" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti/edge/rest_management_api_client" @@ -261,7 +261,7 @@ func assertMetricsService(cfg *Config, edge *rest_management_api_client.ZitiEdge var svcZId string if len(listResp.Payload.Data) != 1 { logrus.Infof("creating '%v' service", cfg.Metrics.ServiceName) - svcZId, err = edge_ctrl.CreateService("metrics", nil, nil, edge) + svcZId, err = zrok_edge_sdk.CreateService("metrics", nil, nil, edge) if err != nil { return "", errors.Wrapf(err, "error creating '%v' service", cfg.Metrics.ServiceName) } @@ -289,7 +289,7 @@ func assertMetricsSerp(metricsSvcZId string, cfg *Config, edge *rest_management_ } if len(listResp.Payload.Data) != 1 { logrus.Infof("creating '%v' serp", cfg.Metrics.ServiceName) - _, err := createServiceEdgeRouterPolicy(cfg.Metrics.ServiceName, metricsSvcZId, nil, edge) + _, err := zrok_edge_sdk.CreateServiceEdgeRouterPolicy(cfg.Metrics.ServiceName, metricsSvcZId, nil, edge) if err != nil { return errors.Wrapf(err, "error creating '%v' serp", cfg.Metrics.ServiceName) } @@ -314,7 +314,7 @@ func assertCtrlMetricsBind(ctrlZId, metricsSvcZId string, edge *rest_management_ } if len(listResp.Payload.Data) != 1 { logrus.Info("creating 'ctrl-metrics-bind' service policy") - if err := createNamedBindServicePolicy("ctrl-metrics-bind", metricsSvcZId, ctrlZId, edge, edge_ctrl.ZrokTags()); err != nil { + if err := createNamedBindServicePolicy("ctrl-metrics-bind", metricsSvcZId, ctrlZId, edge, zrok_edge_sdk.ZrokTags()); err != nil { return errors.Wrap(err, "error creating 'ctrl-metrics-bind' service policy") } } @@ -338,7 +338,7 @@ func assertFrontendMetricsDial(frontendZId, metricsSvcZId string, edge *rest_man } if len(listResp.Payload.Data) != 1 { logrus.Info("creating 'frontend-metrics-dial' service policy") - if err := createNamedDialServicePolicy("frontend-metrics-dial", metricsSvcZId, frontendZId, edge, edge_ctrl.ZrokTags()); err != nil { + if err := createNamedDialServicePolicy("frontend-metrics-dial", metricsSvcZId, frontendZId, edge, zrok_edge_sdk.ZrokTags()); err != nil { return errors.Wrap(err, "error creating 'frontend-metrics-dial' service policy") } } diff --git a/controller/disable.go b/controller/disable.go index f03fb056..bea4dc85 100644 --- a/controller/disable.go +++ b/controller/disable.go @@ -3,7 +3,7 @@ package controller import ( "github.com/go-openapi/runtime/middleware" "github.com/jmoiron/sqlx" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/environment" "github.com/openziti/edge/rest_management_api_client" @@ -87,7 +87,7 @@ func (h *disableHandler) removeServicesForEnvironment(envId int, tx *sqlx.Tx, ed for _, svc := range svcs { svcToken := svc.Token logrus.Infof("garbage collecting service '%v' for environment '%v'", svcToken, env.ZId) - if err := deleteServiceEdgeRouterPolicy(env.ZId, svcToken, edge); err != nil { + if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy(env.ZId, svcToken, edge); err != nil { logrus.Error(err) } if err := deleteServicePolicyDial(env.ZId, svcToken, edge); err != nil { @@ -99,7 +99,7 @@ func (h *disableHandler) removeServicesForEnvironment(envId int, tx *sqlx.Tx, ed if err := deleteConfig(env.ZId, svcToken, edge); err != nil { logrus.Error(err) } - if err := edge_ctrl.DeleteService(env.ZId, svc.ZId, edge); err != nil { + if err := zrok_edge_sdk.DeleteService(env.ZId, svc.ZId, edge); err != nil { logrus.Error(err) } logrus.Infof("removed service '%v' for environment '%v'", svc.Token, env.ZId) diff --git a/controller/edge.go b/controller/edge.go index c33f226b..85088c6e 100644 --- a/controller/edge.go +++ b/controller/edge.go @@ -3,13 +3,12 @@ package controller import ( "context" "fmt" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti/edge/rest_management_api_client" "github.com/openziti/edge/rest_management_api_client/config" "github.com/openziti/edge/rest_management_api_client/edge_router_policy" identity_edge "github.com/openziti/edge/rest_management_api_client/identity" - "github.com/openziti/edge/rest_management_api_client/service_edge_router_policy" "github.com/openziti/edge/rest_management_api_client/service_policy" "github.com/openziti/edge/rest_model" rest_model_edge "github.com/openziti/edge/rest_model" @@ -20,75 +19,6 @@ import ( "time" ) -func createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { - serpZId, err := createServiceEdgeRouterPolicy(svcToken, svcZId, edge_ctrl.ZrokServiceTags(svcToken).SubTags, edge) - if err != nil { - return err - } - logrus.Infof("created service edge router policy '%v' for service '%v' for environment '%v'", serpZId, svcZId, envZId) - return nil -} - -func createServiceEdgeRouterPolicy(name, svcZId string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { - edgeRouterRoles := []string{"#all"} - semantic := rest_model.SemanticAllOf - serviceRoles := []string{fmt.Sprintf("@%v", svcZId)} - tags := edge_ctrl.ZrokTags() - for k, v := range moreTags { - tags.SubTags[k] = v - } - serp := &rest_model.ServiceEdgeRouterPolicyCreate{ - EdgeRouterRoles: edgeRouterRoles, - Name: &name, - Semantic: &semantic, - ServiceRoles: serviceRoles, - Tags: tags, - } - serpParams := &service_edge_router_policy.CreateServiceEdgeRouterPolicyParams{ - Policy: serp, - Context: context.Background(), - } - serpParams.SetTimeout(30 * time.Second) - resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil) - if err != nil { - return "", errors.Wrapf(err, "error creating serp '%v' for service '%v'", name, svcZId) - } - return resp.Payload.Data.ID, nil -} - -func deleteServiceEdgeRouterPolicy(envZId, svcToken string, edge *rest_management_api_client.ZitiEdgeManagement) error { - filter := fmt.Sprintf("tags.zrokServiceToken=\"%v\"", svcToken) - 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' for environment '%v'", serpId, envZId) - } else { - logrus.Infof("did not find a service edge router policy") - } - return nil -} - func createServicePolicyBind(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { semantic := rest_model.SemanticAllOf identityRoles := []string{fmt.Sprintf("@%v", envZId)} @@ -103,7 +33,7 @@ func createServicePolicyBind(envZId, svcToken, svcZId string, edge *rest_managem Semantic: &semantic, ServiceRoles: serviceRoles, Type: &dialBind, - Tags: edge_ctrl.ZrokServiceTags(svcToken), + Tags: zrok_edge_sdk.ZrokServiceTags(svcToken), } req := &service_policy.CreateServicePolicyParams{ Policy: svcp, @@ -157,7 +87,7 @@ func deleteServicePolicyBind(envZId, svcToken string, edge *rest_management_api_ } func createServicePolicyDial(envZId, svcToken, svcZId string, dialZIds []string, edge *rest_management_api_client.ZitiEdgeManagement, tags ...*rest_model.Tags) error { - allTags := edge_ctrl.ZrokServiceTags(svcToken) + allTags := zrok_edge_sdk.ZrokServiceTags(svcToken) for _, t := range tags { for k, v := range t.SubTags { allTags.SubTags[k] = v @@ -284,7 +214,7 @@ func createConfig(envZId, svcToken string, authSchemeStr string, authUsers []*mo ConfigTypeID: &zrokProxyConfigId, Data: cfg, Name: &svcToken, - Tags: edge_ctrl.ZrokServiceTags(svcToken), + Tags: zrok_edge_sdk.ZrokServiceTags(svcToken), } cfgReq := &config.CreateConfigParams{ Config: cfgCrt, @@ -331,8 +261,8 @@ func deleteConfig(envZId, svcToken string, edge *rest_management_api_client.Ziti func createShareService(envZId, svcToken, cfgId string, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) { configs := []string{cfgId} - tags := edge_ctrl.ZrokServiceTags(svcToken) - svcZId, err = edge_ctrl.CreateService(svcToken, configs, tags.SubTags, edge) + tags := zrok_edge_sdk.ZrokServiceTags(svcToken) + svcZId, err = zrok_edge_sdk.CreateService(svcToken, configs, tags.SubTags, edge) if err != nil { return "", errors.Wrapf(err, "error creating service '%v'", svcToken) } @@ -349,7 +279,7 @@ func createEdgeRouterPolicy(name, zId string, edge *rest_management_api_client.Z IdentityRoles: identityRoles, Name: &name, Semantic: &semantic, - Tags: edge_ctrl.ZrokTags(), + Tags: zrok_edge_sdk.ZrokTags(), } req := &edge_router_policy.CreateEdgeRouterPolicyParams{ Policy: erp, @@ -408,7 +338,7 @@ func createEnvironmentIdentity(accountEmail string, client *rest_management_api_ func createIdentity(name string, identityType rest_model_edge.IdentityType, moreTags map[string]interface{}, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) { isAdmin := false - tags := edge_ctrl.ZrokTags() + tags := zrok_edge_sdk.ZrokTags() for k, v := range moreTags { tags.SubTags[k] = v } diff --git a/controller/gc.go b/controller/gc.go index 57cb86f1..5314d930 100644 --- a/controller/gc.go +++ b/controller/gc.go @@ -3,8 +3,8 @@ package controller import ( "context" "fmt" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti/edge/rest_management_api_client" "github.com/openziti/edge/rest_management_api_client/config" "github.com/openziti/edge/rest_management_api_client/service" @@ -72,7 +72,7 @@ func gcServices(edge *rest_management_api_client.ZitiEdgeManagement, liveMap map for _, svc := range listResp.Payload.Data { if _, found := liveMap[*svc.Name]; !found { logrus.Infof("garbage collecting, zitiSvcId='%v', zrokSvcId='%v'", *svc.ID, *svc.Name) - if err := deleteServiceEdgeRouterPolicy("gc", *svc.Name, edge); err != nil { + if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy("gc", *svc.Name, edge); err != nil { logrus.Errorf("error garbage collecting service edge router policy: %v", err) } if err := deleteServicePolicyDial("gc", *svc.Name, edge); err != nil { @@ -84,7 +84,7 @@ func gcServices(edge *rest_management_api_client.ZitiEdgeManagement, liveMap map if err := deleteConfig("gc", *svc.Name, edge); err != nil { logrus.Errorf("error garbage collecting config: %v", err) } - if err := edge_ctrl.DeleteService("gc", *svc.ID, edge); err != nil { + if err := zrok_edge_sdk.DeleteService("gc", *svc.ID, edge); err != nil { logrus.Errorf("error garbage collecting service: %v", err) } } else { @@ -109,7 +109,7 @@ func gcServiceEdgeRouterPolicies(edge *rest_management_api_client.ZitiEdgeManage for _, serp := range listResp.Payload.Data { if _, found := liveMap[*serp.Name]; !found { logrus.Infof("garbage collecting, svcId='%v'", *serp.Name) - if err := deleteServiceEdgeRouterPolicy("gc", *serp.Name, edge); err != nil { + if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy("gc", *serp.Name, edge); err != nil { logrus.Errorf("error garbage collecting service edge router policy: %v", err) } } else { diff --git a/controller/share_private.go b/controller/share_private.go index e59f9fc2..10e6e416 100644 --- a/controller/share_private.go +++ b/controller/share_private.go @@ -1,6 +1,7 @@ package controller import ( + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" "github.com/openziti/edge/rest_management_api_client" @@ -31,7 +32,7 @@ func (a *privateResourceAllocator) allocate(envZId, svcToken string, params serv return "", nil, err } - if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { + if err := zrok_edge_sdk.CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { return "", nil, err } diff --git a/controller/share_public.go b/controller/share_public.go index 5cef94c5..2626a159 100644 --- a/controller/share_public.go +++ b/controller/share_public.go @@ -1,6 +1,7 @@ package controller import ( + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" "github.com/openziti/edge/rest_management_api_client" @@ -35,7 +36,7 @@ func (a *publicResourceAllocator) allocate(envZId, svcToken string, frontendZIds return "", nil, err } - if err := createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { + if err := zrok_edge_sdk.CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId, edge); err != nil { return "", nil, err } diff --git a/controller/unshare.go b/controller/unshare.go index cc70e101..41a9a228 100644 --- a/controller/unshare.go +++ b/controller/unshare.go @@ -4,8 +4,8 @@ import ( "context" "fmt" "github.com/go-openapi/runtime/middleware" - "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/controller/zrok_edge_sdk" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" "github.com/openziti/edge/rest_management_api_client" @@ -78,7 +78,7 @@ func (h *unshareHandler) Handle(params service.UnshareParams, principal *rest_mo if ssvc.Reserved == params.Body.Reserved { // single tag-based service deallocator; should work regardless of sharing mode - if err := h.deallocateResources(senv, ssvc, svcToken, svcZId, edge); err != nil { + if err := h.deallocateResources(senv, svcToken, svcZId, edge); err != nil { logrus.Errorf("error unsharing ziti resources for '%v': %v", ssvc, err) return service.NewUnshareInternalServerError() } @@ -122,8 +122,8 @@ func (h *unshareHandler) findServiceZId(svcToken string, edge *rest_management_a return "", errors.Errorf("service '%v' not found", svcToken) } -func (h *unshareHandler) deallocateResources(senv *store.Environment, ssvc *store.Service, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { - if err := deleteServiceEdgeRouterPolicy(senv.ZId, svcToken, edge); err != nil { +func (h *unshareHandler) deallocateResources(senv *store.Environment, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { + if err := zrok_edge_sdk.DeleteServiceEdgeRouterPolicy(senv.ZId, svcToken, edge); err != nil { return err } if err := deleteServicePolicyDial(senv.ZId, svcToken, edge); err != nil { @@ -135,7 +135,7 @@ func (h *unshareHandler) deallocateResources(senv *store.Environment, ssvc *stor if err := deleteConfig(senv.ZId, svcToken, edge); err != nil { return err } - if err := edge_ctrl.DeleteService(senv.ZId, svcZId, edge); err != nil { + if err := zrok_edge_sdk.DeleteService(senv.ZId, svcZId, edge); err != nil { return err } return nil diff --git a/controller/zrok_edge_sdk/serp.go b/controller/zrok_edge_sdk/serp.go new file mode 100644 index 00000000..bfc86e62 --- /dev/null +++ b/controller/zrok_edge_sdk/serp.go @@ -0,0 +1,81 @@ +package zrok_edge_sdk + +import ( + "context" + "fmt" + "github.com/openziti/edge/rest_management_api_client" + "github.com/openziti/edge/rest_management_api_client/service_edge_router_policy" + "github.com/openziti/edge/rest_model" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "time" +) + +func CreateShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { + serpZId, err := CreateServiceEdgeRouterPolicy(svcToken, svcZId, ZrokServiceTags(svcToken).SubTags, edge) + if err != nil { + return err + } + logrus.Infof("created service edge router policy '%v' for service '%v' for environment '%v'", serpZId, svcZId, envZId) + return nil +} + +func CreateServiceEdgeRouterPolicy(name, svcZId string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (string, error) { + edgeRouterRoles := []string{"#all"} + semantic := rest_model.SemanticAllOf + serviceRoles := []string{fmt.Sprintf("@%v", svcZId)} + tags := ZrokTags() + for k, v := range moreTags { + tags.SubTags[k] = v + } + serp := &rest_model.ServiceEdgeRouterPolicyCreate{ + EdgeRouterRoles: edgeRouterRoles, + Name: &name, + Semantic: &semantic, + ServiceRoles: serviceRoles, + Tags: tags, + } + serpParams := &service_edge_router_policy.CreateServiceEdgeRouterPolicyParams{ + Policy: serp, + Context: context.Background(), + } + serpParams.SetTimeout(30 * time.Second) + resp, err := edge.ServiceEdgeRouterPolicy.CreateServiceEdgeRouterPolicy(serpParams, nil) + if err != nil { + return "", errors.Wrapf(err, "error creating serp '%v' for service '%v'", name, svcZId) + } + return resp.Payload.Data.ID, nil +} + +func DeleteServiceEdgeRouterPolicy(envZId, svcToken string, edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := fmt.Sprintf("tags.zrokServiceToken=\"%v\"", svcToken) + 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' for environment '%v'", serpId, envZId) + } else { + logrus.Infof("did not find a service edge router policy") + } + return nil +} diff --git a/controller/edge_ctrl/service.go b/controller/zrok_edge_sdk/service.go similarity index 98% rename from controller/edge_ctrl/service.go rename to controller/zrok_edge_sdk/service.go index bb5c4b5c..75922f53 100644 --- a/controller/edge_ctrl/service.go +++ b/controller/zrok_edge_sdk/service.go @@ -1,4 +1,4 @@ -package edge_ctrl +package zrok_edge_sdk import ( "context" diff --git a/controller/edge_ctrl/tags.go b/controller/zrok_edge_sdk/tags.go similarity index 94% rename from controller/edge_ctrl/tags.go rename to controller/zrok_edge_sdk/tags.go index 84fcfef5..e4d78fb3 100644 --- a/controller/edge_ctrl/tags.go +++ b/controller/zrok_edge_sdk/tags.go @@ -1,4 +1,4 @@ -package edge_ctrl +package zrok_edge_sdk import ( "github.com/openziti-test-kitchen/zrok/build"