mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 08:44:14 +01:00
centralizing edge_ctrl sdk; tags, service (#112)
This commit is contained in:
parent
3ed31e60e0
commit
06ce17ae8f
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
50
controller/edge_ctrl/service.go
Normal file
50
controller/edge_ctrl/service.go
Normal file
@ -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
|
||||
}
|
20
controller/edge_ctrl/tags.go
Normal file
20
controller/edge_ctrl/tags.go
Normal file
@ -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
|
||||
}
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user