From e0a734be2e0e37f72d47f896150c7035d0594d13 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 5 Dec 2022 13:12:12 -0500 Subject: [PATCH] admin bootstrap scaffolding (#131) --- cmd/zrok/admin_bootstrap.go | 40 ++++++++++++++++++++++++ cmd/zrok/main.go | 6 ++++ controller/bootstrap.go | 61 +++++++++++++++++++++++++++++++++++++ controller/startup.go | 26 +++++----------- 4 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 cmd/zrok/admin_bootstrap.go create mode 100644 controller/bootstrap.go diff --git a/cmd/zrok/admin_bootstrap.go b/cmd/zrok/admin_bootstrap.go new file mode 100644 index 00000000..682dc763 --- /dev/null +++ b/cmd/zrok/admin_bootstrap.go @@ -0,0 +1,40 @@ +package main + +import ( + "github.com/michaelquigley/cf" + "github.com/openziti-test-kitchen/zrok/controller" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + adminCmd.AddCommand(newAdminBootstrap().cmd) +} + +type adminBootstrap struct { + cmd *cobra.Command +} + +func newAdminBootstrap() *adminBootstrap { + cmd := &cobra.Command{ + Use: "bootstrap ", + Short: "Bootstrap the underlying Ziti network for zrok", + Args: cobra.ExactArgs(1), + } + command := &adminBootstrap{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *adminBootstrap) run(_ *cobra.Command, args []string) { + configPath := args[0] + inCfg, err := controller.LoadConfig(configPath) + if err != nil { + panic(err) + } + logrus.Infof(cf.Dump(inCfg, cf.DefaultOptions())) + if err := controller.Bootstrap(inCfg); err != nil { + panic(err) + } + logrus.Info("bootstrap complete!") +} diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 3807c673..94ade4ed 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -16,6 +16,7 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&panicInstead, "panic", "p", false, "Panic instead of showing pretty errors") zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags()) rootCmd.AddCommand(accessCmd) + adminCreateCmd.AddCommand(adminCreateIdentity) adminCmd.AddCommand(adminCreateCmd) adminCmd.AddCommand(adminDeleteCmd) adminCmd.AddCommand(adminListCmd) @@ -53,6 +54,11 @@ var adminCreateCmd = &cobra.Command{ Short: "Create global resources", } +var adminCreateIdentity = &cobra.Command{ + Use: "identity", + Short: "Create global identities", +} + var adminDeleteCmd = &cobra.Command{ Use: "delete", Short: "Delete global resources", diff --git a/controller/bootstrap.go b/controller/bootstrap.go new file mode 100644 index 00000000..3820e56e --- /dev/null +++ b/controller/bootstrap.go @@ -0,0 +1,61 @@ +package controller + +import ( + "context" + "fmt" + "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_model" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "time" +) + +func Bootstrap(inCfg *Config) error { + cfg = inCfg + + edge, err := edgeClient() + if err != nil { + return err + } + + if err := assertZrokProxyConfigType(edge); err != nil { + return err + } + + return nil +} + +func assertZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManagement) error { + filter := fmt.Sprintf("name=\"%v\"", model.ZrokProxyConfig) + limit := int64(100) + offset := int64(0) + listReq := &config.ListConfigTypesParams{ + Filter: &filter, + Limit: &limit, + Offset: &offset, + Context: context.Background(), + } + listReq.SetTimeout(30 * time.Second) + listResp, err := edge.Config.ListConfigTypes(listReq, nil) + if err != nil { + return err + } + if len(listResp.Payload.Data) < 1 { + name := model.ZrokProxyConfig + ct := &rest_model.ConfigTypeCreate{Name: &name} + createReq := &config.CreateConfigTypeParams{ConfigType: ct} + createReq.SetTimeout(30 * time.Second) + createResp, err := edge.Config.CreateConfigType(createReq, nil) + if err != nil { + return err + } + logrus.Infof("created '%v' config type with id '%v'", model.ZrokProxyConfig, createResp.Payload.Data.ID) + } else if len(listResp.Payload.Data) > 1 { + return errors.Errorf("found %d '%v' config types; expected 0 or 1", len(listResp.Payload.Data), model.ZrokProxyConfig) + } else { + logrus.Infof("found '%v' config type with id '%v'", model.ZrokProxyConfig, *(listResp.Payload.Data[0].ID)) + } + return nil +} diff --git a/controller/startup.go b/controller/startup.go index 730550f5..fdeb96d9 100644 --- a/controller/startup.go +++ b/controller/startup.go @@ -6,7 +6,6 @@ import ( "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_model" "github.com/pkg/errors" "github.com/sirupsen/logrus" "time" @@ -28,14 +27,14 @@ func inspectZiti() error { if err != nil { return errors.Wrap(err, "error getting ziti edge client") } - if err := ensureZrokProxyConfigType(edge); err != nil { + if err := findZrokProxyConfigType(edge); err != nil { return err } return nil } -func ensureZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManagement) error { +func findZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManagement) error { filter := fmt.Sprintf("name=\"%v\"", model.ZrokProxyConfig) limit := int64(100) offset := int64(0) @@ -50,22 +49,11 @@ func ensureZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManageme if err != nil { return err } - if len(listResp.Payload.Data) < 1 { - name := model.ZrokProxyConfig - ct := &rest_model.ConfigTypeCreate{Name: &name} - createReq := &config.CreateConfigTypeParams{ConfigType: ct} - createReq.SetTimeout(30 * time.Second) - createResp, err := edge.Config.CreateConfigType(createReq, nil) - if err != nil { - return err - } - logrus.Infof("created '%v' config type with id '%v'", model.ZrokProxyConfig, createResp.Payload.Data.ID) - zrokProxyConfigId = createResp.Payload.Data.ID - } else if len(listResp.Payload.Data) > 1 { - return errors.Errorf("found %d '%v' config types; expected 0 or 1", len(listResp.Payload.Data), model.ZrokProxyConfig) - } else { - logrus.Infof("found '%v' config type with id '%v'", model.ZrokProxyConfig, *(listResp.Payload.Data[0].ID)) - zrokProxyConfigId = *(listResp.Payload.Data[0].ID) + if len(listResp.Payload.Data) != 1 { + return errors.Errorf("expected 1 zrok proxy config type, found %d", len(listResp.Payload.Data)) } + logrus.Infof("found '%v' config type with id '%v'", model.ZrokProxyConfig, *(listResp.Payload.Data[0].ID)) + zrokProxyConfigId = *(listResp.Payload.Data[0].ID) + return nil }