From 6fcd952d682ef55126ab9b81c8e0f97d50dc8c96 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 15 Aug 2022 15:06:57 -0400 Subject: [PATCH] create config (#12) --- controller/startup.go | 4 ++++ controller/tunnel.go | 34 +++++++++++++++++++++++++++++++--- model/ZrokAuth.go | 5 +++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 model/ZrokAuth.go diff --git a/controller/startup.go b/controller/startup.go index a555d099..4f216529 100644 --- a/controller/startup.go +++ b/controller/startup.go @@ -10,6 +10,8 @@ import ( "time" ) +var zrokAuthV1Id string + func controllerStartup(cfg *Config) error { if err := inspectZiti(cfg); err != nil { return err @@ -56,10 +58,12 @@ func ensureZrokAuthConfigType(edge *rest_management_api_client.ZitiEdgeManagemen return err } logrus.Infof("created 'zrok.auth.v1' config type with id '%v'", createResp.Payload.Data.ID) + zrokAuthV1Id = createResp.Payload.Data.ID } else if len(listResp.Payload.Data) > 1 { return errors.Errorf("found %d 'zrok.auth.v1' config types; expected 0 or 1", len(listResp.Payload.Data)) } else { logrus.Infof("found 'zrok.auth.v1' config type with id '%v'", *(listResp.Payload.Data[0].ID)) + zrokAuthV1Id = *(listResp.Payload.Data[0].ID) } return nil } diff --git a/controller/tunnel.go b/controller/tunnel.go index 31ebec66..e0463e0e 100644 --- a/controller/tunnel.go +++ b/controller/tunnel.go @@ -5,9 +5,11 @@ import ( "fmt" "github.com/go-openapi/runtime/middleware" "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/openziti-test-kitchen/zrok/model" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" "github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/tunnel" "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" "github.com/openziti/edge/rest_management_api_client/service" "github.com/openziti/edge/rest_management_api_client/service_edge_router_policy" @@ -65,7 +67,12 @@ func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_mo logrus.Error(err) return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } - svcId, err := self.createService(svcName, edge) + cfgId, err := self.createConfig(edge) + if err != nil { + logrus.Error(err) + return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) + } + svcId, err := self.createService(svcName, cfgId, edge) if err != nil { logrus.Error(err) return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) @@ -110,8 +117,29 @@ func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_mo }) } -func (self *tunnelHandler) createService(name string, edge *rest_management_api_client.ZitiEdgeManagement) (serviceId string, err error) { - configs := make([]string, 0) +func (self *tunnelHandler) createConfig(edge *rest_management_api_client.ZitiEdgeManagement) (cfgID string, err error) { + cfg := &model.ZrokAuth{Hello: "World"} + name := "zrok.auth.v1" + cfgCrt := &rest_model.ConfigCreate{ + ConfigTypeID: &zrokAuthV1Id, + Data: cfg, + Name: &name, + } + cfgReq := &config.CreateConfigParams{ + Config: cfgCrt, + Context: context.Background(), + } + cfgReq.SetTimeout(30 * time.Second) + cfgResp, err := edge.Config.CreateConfig(cfgReq, nil) + if err != nil { + return "", err + } + logrus.Infof("created config '%v'", cfgResp.Payload.Data.ID) + return cfgResp.Payload.Data.ID, nil +} + +func (self *tunnelHandler) createService(name, cfgId string, edge *rest_management_api_client.ZitiEdgeManagement) (serviceId string, err error) { + configs := []string{cfgId} encryptionRequired := true svc := &rest_model.ServiceCreate{ Configs: configs, diff --git a/model/ZrokAuth.go b/model/ZrokAuth.go new file mode 100644 index 00000000..cb76e585 --- /dev/null +++ b/model/ZrokAuth.go @@ -0,0 +1,5 @@ +package model + +type ZrokAuth struct { + Hello string +}