From 06ce17ae8fabfa5e1aafb26e92830626c593cab9 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 14 Dec 2022 13:06:26 -0500 Subject: [PATCH] centralizing edge_ctrl sdk; tags, service (#112) --- controller/access.go | 3 +- controller/bootstrap.go | 7 +-- controller/disable.go | 3 +- controller/edge.go | 76 +++++---------------------------- controller/edge_ctrl/service.go | 50 ++++++++++++++++++++++ controller/edge_ctrl/tags.go | 20 +++++++++ controller/gc.go | 3 +- controller/unshare.go | 3 +- 8 files changed, 92 insertions(+), 73 deletions(-) create mode 100644 controller/edge_ctrl/service.go create mode 100644 controller/edge_ctrl/tags.go diff --git a/controller/access.go b/controller/access.go index 71cfba20..f945af0d 100644 --- a/controller/access.go +++ b/controller/access.go @@ -4,6 +4,7 @@ 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/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" @@ -95,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 := zrokServiceTags(svcToken) + allTags := edge_ctrl.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 ff23c057..52c75992 100644 --- a/controller/bootstrap.go +++ b/controller/bootstrap.go @@ -5,6 +5,7 @@ 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/model" "github.com/openziti-test-kitchen/zrok/zrokdir" @@ -260,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 = createService("metrics", nil, nil, edge) + svcZId, err = edge_ctrl.CreateService("metrics", nil, nil, edge) if err != nil { return "", errors.Wrapf(err, "error creating '%v' service", cfg.Metrics.ServiceName) } @@ -313,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, zrokTags()); err != nil { + if err := createNamedBindServicePolicy("ctrl-metrics-bind", metricsSvcZId, ctrlZId, edge, edge_ctrl.ZrokTags()); err != nil { return errors.Wrap(err, "error creating 'ctrl-metrics-bind' service policy") } } @@ -337,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, zrokTags()); err != nil { + if err := createNamedDialServicePolicy("frontend-metrics-dial", metricsSvcZId, frontendZId, edge, edge_ctrl.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 e300aa85..f03fb056 100644 --- a/controller/disable.go +++ b/controller/disable.go @@ -3,6 +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/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/environment" "github.com/openziti/edge/rest_management_api_client" @@ -98,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 := deleteService(env.ZId, svc.ZId, edge); err != nil { + if err := edge_ctrl.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 dc3c4079..c33f226b 100644 --- a/controller/edge.go +++ b/controller/edge.go @@ -3,14 +3,12 @@ package controller import ( "context" "fmt" - "github.com/openziti-test-kitchen/zrok/build" + "github.com/openziti-test-kitchen/zrok/controller/edge_ctrl" "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_service "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/openziti/edge/rest_model" @@ -23,7 +21,7 @@ import ( ) func createShareServiceEdgeRouterPolicy(envZId, svcToken, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { - serpZId, err := createServiceEdgeRouterPolicy(svcToken, svcZId, zrokServiceTags(svcToken).SubTags, edge) + serpZId, err := createServiceEdgeRouterPolicy(svcToken, svcZId, edge_ctrl.ZrokServiceTags(svcToken).SubTags, edge) if err != nil { return err } @@ -35,7 +33,7 @@ func createServiceEdgeRouterPolicy(name, svcZId string, moreTags map[string]inte edgeRouterRoles := []string{"#all"} semantic := rest_model.SemanticAllOf serviceRoles := []string{fmt.Sprintf("@%v", svcZId)} - tags := zrokTags() + tags := edge_ctrl.ZrokTags() for k, v := range moreTags { tags.SubTags[k] = v } @@ -105,7 +103,7 @@ func createServicePolicyBind(envZId, svcToken, svcZId string, edge *rest_managem Semantic: &semantic, ServiceRoles: serviceRoles, Type: &dialBind, - Tags: zrokServiceTags(svcToken), + Tags: edge_ctrl.ZrokServiceTags(svcToken), } req := &service_policy.CreateServicePolicyParams{ Policy: svcp, @@ -159,7 +157,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 := zrokServiceTags(svcToken) + allTags := edge_ctrl.ZrokServiceTags(svcToken) for _, t := range tags { for k, v := range t.SubTags { allTags.SubTags[k] = v @@ -286,7 +284,7 @@ func createConfig(envZId, svcToken string, authSchemeStr string, authUsers []*mo ConfigTypeID: &zrokProxyConfigId, Data: cfg, Name: &svcToken, - Tags: zrokServiceTags(svcToken), + Tags: edge_ctrl.ZrokServiceTags(svcToken), } cfgReq := &config.CreateConfigParams{ Config: cfgCrt, @@ -333,8 +331,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 := zrokServiceTags(svcToken) - svcZId, err = createService(svcToken, configs, tags.SubTags, edge) + tags := edge_ctrl.ZrokServiceTags(svcToken) + svcZId, err = edge_ctrl.CreateService(svcToken, configs, tags.SubTags, edge) if err != nil { return "", errors.Wrapf(err, "error creating service '%v'", svcToken) } @@ -342,46 +340,6 @@ func createShareService(envZId, svcToken, cfgId string, edge *rest_management_ap return svcZId, nil } -func createService(name string, cfgIds []string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) { - encryptionRequired := true - svc := &rest_model.ServiceCreate{ - EncryptionRequired: &encryptionRequired, - Name: &name, - } - if cfgIds != nil { - svc.Configs = cfgIds - } - tags := zrokTags() - for k, v := range moreTags { - tags.SubTags[k] = v - } - svc.Tags = tags - 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 -} - -func deleteService(envZId, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { - req := &service.DeleteServiceParams{ - ID: svcZId, - Context: context.Background(), - } - req.SetTimeout(30 * time.Second) - _, err := edge.Service.DeleteService(req, nil) - if err != nil { - return err - } - logrus.Infof("deleted service '%v' for environment '%v'", svcZId, envZId) - return nil -} - func createEdgeRouterPolicy(name, zId string, edge *rest_management_api_client.ZitiEdgeManagement) error { edgeRouterRoles := []string{"#all"} identityRoles := []string{fmt.Sprintf("@%v", zId)} @@ -391,7 +349,7 @@ func createEdgeRouterPolicy(name, zId string, edge *rest_management_api_client.Z IdentityRoles: identityRoles, Name: &name, Semantic: &semantic, - Tags: zrokTags(), + Tags: edge_ctrl.ZrokTags(), } req := &edge_router_policy.CreateEdgeRouterPolicyParams{ Policy: erp, @@ -450,7 +408,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 := zrokTags() + tags := edge_ctrl.ZrokTags() for k, v := range moreTags { tags.SubTags[k] = v } @@ -528,17 +486,3 @@ func deleteIdentity(id string, edge *rest_management_api_client.ZitiEdgeManageme logrus.Infof("deleted environment identity '%v'", id) return nil } - -func zrokTags() *rest_model.Tags { - return &rest_model.Tags{ - SubTags: map[string]interface{}{ - "zrok": build.String(), - }, - } -} - -func zrokServiceTags(svcToken string) *rest_model.Tags { - tags := zrokTags() - tags.SubTags["zrokServiceToken"] = svcToken - return tags -} diff --git a/controller/edge_ctrl/service.go b/controller/edge_ctrl/service.go new file mode 100644 index 00000000..bb5c4b5c --- /dev/null +++ b/controller/edge_ctrl/service.go @@ -0,0 +1,50 @@ +package edge_ctrl + +import ( + "context" + "github.com/openziti/edge/rest_management_api_client" + edge_service "github.com/openziti/edge/rest_management_api_client/service" + "github.com/openziti/edge/rest_model" + "github.com/sirupsen/logrus" + "time" +) + +func CreateService(name string, cfgIds []string, moreTags map[string]interface{}, edge *rest_management_api_client.ZitiEdgeManagement) (svcZId string, err error) { + encryptionRequired := true + svc := &rest_model.ServiceCreate{ + EncryptionRequired: &encryptionRequired, + Name: &name, + } + if cfgIds != nil { + svc.Configs = cfgIds + } + tags := ZrokTags() + for k, v := range moreTags { + tags.SubTags[k] = v + } + svc.Tags = tags + 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 +} + +func DeleteService(envZId, svcZId string, edge *rest_management_api_client.ZitiEdgeManagement) error { + req := &edge_service.DeleteServiceParams{ + ID: svcZId, + Context: context.Background(), + } + req.SetTimeout(30 * time.Second) + _, err := edge.Service.DeleteService(req, nil) + if err != nil { + return err + } + logrus.Infof("deleted service '%v' for environment '%v'", svcZId, envZId) + return nil +} diff --git a/controller/edge_ctrl/tags.go b/controller/edge_ctrl/tags.go new file mode 100644 index 00000000..84fcfef5 --- /dev/null +++ b/controller/edge_ctrl/tags.go @@ -0,0 +1,20 @@ +package edge_ctrl + +import ( + "github.com/openziti-test-kitchen/zrok/build" + "github.com/openziti/edge/rest_model" +) + +func ZrokTags() *rest_model.Tags { + return &rest_model.Tags{ + SubTags: map[string]interface{}{ + "zrok": build.String(), + }, + } +} + +func ZrokServiceTags(svcToken string) *rest_model.Tags { + tags := ZrokTags() + tags.SubTags["zrokServiceToken"] = svcToken + return tags +} diff --git a/controller/gc.go b/controller/gc.go index bca4d81c..57cb86f1 100644 --- a/controller/gc.go +++ b/controller/gc.go @@ -3,6 +3,7 @@ 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/edge/rest_management_api_client" "github.com/openziti/edge/rest_management_api_client/config" @@ -83,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 := deleteService("gc", *svc.ID, edge); err != nil { + if err := edge_ctrl.DeleteService("gc", *svc.ID, edge); err != nil { logrus.Errorf("error garbage collecting service: %v", err) } } else { diff --git a/controller/unshare.go b/controller/unshare.go index 8b3fdd19..cc70e101 100644 --- a/controller/unshare.go +++ b/controller/unshare.go @@ -4,6 +4,7 @@ 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/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service" @@ -134,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 := deleteService(senv.ZId, svcZId, edge); err != nil { + if err := edge_ctrl.DeleteService(senv.ZId, svcZId, edge); err != nil { return err } return nil