2022-08-15 20:25:50 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-15 23:29:31 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/openziti-test-kitchen/zrok/model"
|
2022-08-15 20:25:50 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
|
|
|
"github.com/openziti/edge/rest_management_api_client/config"
|
2022-08-15 20:36:17 +02:00
|
|
|
"github.com/openziti/edge/rest_model"
|
2022-08-15 20:25:50 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-08-15 23:29:31 +02:00
|
|
|
var zrokProxyConfigId string
|
2022-08-15 21:06:57 +02:00
|
|
|
|
2022-08-15 20:25:50 +02:00
|
|
|
func controllerStartup(cfg *Config) error {
|
|
|
|
if err := inspectZiti(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectZiti(cfg *Config) error {
|
|
|
|
logrus.Infof("inspecting ziti controller configuration")
|
|
|
|
|
|
|
|
edge, err := edgeClient(cfg.Ziti)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error getting ziti edge client")
|
|
|
|
}
|
2022-08-15 23:29:31 +02:00
|
|
|
if err := ensureZrokProxyConfigType(edge); err != nil {
|
2022-08-15 20:25:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:29:31 +02:00
|
|
|
func ensureZrokProxyConfigType(edge *rest_management_api_client.ZitiEdgeManagement) error {
|
|
|
|
filter := fmt.Sprintf("name=\"%v\"", model.ZrokProxyConfig)
|
2022-08-15 20:25:50 +02:00
|
|
|
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
|
|
|
|
}
|
2022-08-15 20:36:17 +02:00
|
|
|
if len(listResp.Payload.Data) < 1 {
|
2022-08-15 23:29:31 +02:00
|
|
|
name := model.ZrokProxyConfig
|
2022-08-15 20:36:17 +02:00
|
|
|
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
|
|
|
|
}
|
2022-08-15 23:29:31 +02:00
|
|
|
logrus.Infof("created '%v' config type with id '%v'", model.ZrokProxyConfig, createResp.Payload.Data.ID)
|
|
|
|
zrokProxyConfigId = createResp.Payload.Data.ID
|
2022-08-15 20:36:17 +02:00
|
|
|
} else if len(listResp.Payload.Data) > 1 {
|
2022-08-15 23:29:31 +02:00
|
|
|
return errors.Errorf("found %d '%v' config types; expected 0 or 1", len(listResp.Payload.Data), model.ZrokProxyConfig)
|
2022-08-15 20:36:17 +02:00
|
|
|
} else {
|
2022-08-15 23:29:31 +02:00
|
|
|
logrus.Infof("found '%v' config type with id '%v'", model.ZrokProxyConfig, *(listResp.Payload.Data[0].ID))
|
|
|
|
zrokProxyConfigId = *(listResp.Payload.Data[0].ID)
|
2022-08-15 20:36:17 +02:00
|
|
|
}
|
2022-08-15 20:25:50 +02:00
|
|
|
return nil
|
|
|
|
}
|