mirror of
https://github.com/openziti/zrok.git
synced 2025-01-03 04:29:19 +01:00
admin bootstrap scaffolding (#131)
This commit is contained in:
parent
7458c625aa
commit
e0a734be2e
40
cmd/zrok/admin_bootstrap.go
Normal file
40
cmd/zrok/admin_bootstrap.go
Normal file
@ -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 <configPath>",
|
||||||
|
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!")
|
||||||
|
}
|
@ -16,6 +16,7 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().BoolVarP(&panicInstead, "panic", "p", false, "Panic instead of showing pretty errors")
|
rootCmd.PersistentFlags().BoolVarP(&panicInstead, "panic", "p", false, "Panic instead of showing pretty errors")
|
||||||
zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags())
|
zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags())
|
||||||
rootCmd.AddCommand(accessCmd)
|
rootCmd.AddCommand(accessCmd)
|
||||||
|
adminCreateCmd.AddCommand(adminCreateIdentity)
|
||||||
adminCmd.AddCommand(adminCreateCmd)
|
adminCmd.AddCommand(adminCreateCmd)
|
||||||
adminCmd.AddCommand(adminDeleteCmd)
|
adminCmd.AddCommand(adminDeleteCmd)
|
||||||
adminCmd.AddCommand(adminListCmd)
|
adminCmd.AddCommand(adminListCmd)
|
||||||
@ -53,6 +54,11 @@ var adminCreateCmd = &cobra.Command{
|
|||||||
Short: "Create global resources",
|
Short: "Create global resources",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var adminCreateIdentity = &cobra.Command{
|
||||||
|
Use: "identity",
|
||||||
|
Short: "Create global identities",
|
||||||
|
}
|
||||||
|
|
||||||
var adminDeleteCmd = &cobra.Command{
|
var adminDeleteCmd = &cobra.Command{
|
||||||
Use: "delete",
|
Use: "delete",
|
||||||
Short: "Delete global resources",
|
Short: "Delete global resources",
|
||||||
|
61
controller/bootstrap.go
Normal file
61
controller/bootstrap.go
Normal file
@ -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
|
||||||
|
}
|
@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/openziti-test-kitchen/zrok/model"
|
"github.com/openziti-test-kitchen/zrok/model"
|
||||||
"github.com/openziti/edge/rest_management_api_client"
|
"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/config"
|
||||||
"github.com/openziti/edge/rest_model"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"time"
|
"time"
|
||||||
@ -28,14 +27,14 @@ func inspectZiti() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error getting ziti edge client")
|
return errors.Wrap(err, "error getting ziti edge client")
|
||||||
}
|
}
|
||||||
if err := ensureZrokProxyConfigType(edge); err != nil {
|
if err := findZrokProxyConfigType(edge); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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)
|
filter := fmt.Sprintf("name=\"%v\"", model.ZrokProxyConfig)
|
||||||
limit := int64(100)
|
limit := int64(100)
|
||||||
offset := int64(0)
|
offset := int64(0)
|
||||||
@ -50,22 +49,11 @@ func ensureZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManageme
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(listResp.Payload.Data) < 1 {
|
if len(listResp.Payload.Data) != 1 {
|
||||||
name := model.ZrokProxyConfig
|
return errors.Errorf("expected 1 zrok proxy config type, found %d", len(listResp.Payload.Data))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
logrus.Infof("found '%v' config type with id '%v'", model.ZrokProxyConfig, *(listResp.Payload.Data[0].ID))
|
||||||
|
zrokProxyConfigId = *(listResp.Payload.Data[0].ID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user