diff --git a/README.md b/README.md index 52b1de0..f78bddc 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ setting up an organization and an admin user. Additionally, it generates Kuberne | TYK_K8SBOOTSTRAP_TYK_ORG_NAME | corresponds to the name for your organization that is going to be bootstrapped in Tyk. | | TYK_K8SBOOTSTRAP_TYK_ORG_CNAME | corresponds to the Organisation CNAME which is going to bind the Portal to. | | TYK_K8SBOOTSTRAP_TYK_ORG_ID | corresponds to the organisation ID that is being created. | +| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_ENABLED | specifies if the Hybrid organisation for MDCB Control Plane is enabled or not | +| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_KEYEVENT | corresponds to `key_event` of the event options (optional). | +| TYK_K8SBOOTSTRAP_TYK_ORG_HYBRID_HASHEDKEYEVENT | corresponds to `hashed_key_event` of the event options (optional). | | TYK_K8SBOOTSTRAP_TYK_DASHBOARDLICENSE | corresponds to the license key of Tyk Dashboard. | ## Bootstrapped Environments diff --git a/pkg/config/config.go b/pkg/config/config.go index 6b960b1..99e649b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,8 @@ package config import ( + "tyk/tyk/bootstrap/tyk/api" + "github.com/kelseyhightower/envconfig" ) @@ -74,6 +76,9 @@ type TykOrg struct { // ID corresponds to the organisation ID that is being created. ID string + + // Hybrid includes details of hybrid organisation while using MDCB Control Plane + Hybrid *HybridConf } type TykConf struct { @@ -86,6 +91,17 @@ type TykConf struct { DashboardLicense string } +type HybridConf struct { + // Enabled specified if the Hybrid organisation is enabled or not + Enabled bool + // KeyEvent corresponds to `key_event` of the event options which enables key events such as updates and deletes, + // to be propagated to the various instance zones. + KeyEvent *api.EventConfig + // HashedKeyEvent corresponds to `hashed_key_event` of the event options which enables key events such as updates + // and deletes, to be propagated to the various instance zones. + HashedKeyEvent *api.EventConfig `json:",omitempty"` +} + func NewConfig() (*Config, error) { conf := &Config{} if err := envconfig.Process(prefix, conf); err != nil { diff --git a/tyk/api/request.go b/tyk/api/request.go index a3a8167..1f709b8 100644 --- a/tyk/api/request.go +++ b/tyk/api/request.go @@ -1,9 +1,17 @@ package api +type EventConfig struct { + Webhook string `json:"webhook,omitempty"` + Email string `json:"email,omitempty"` + Redis bool `json:"redis,omitempty"` +} + type CreateOrgReq struct { - OwnerName string `json:"owner_name"` - CnameEnabled bool `json:"cname_enabled"` - Cname string `json:"cname"` + OwnerName string `json:"owner_name"` + CnameEnabled bool `json:"cname_enabled"` + Cname string `json:"cname"` + HybridEnabled bool `json:"hybrid_enabled"` + EventOptions map[string]EventConfig `json:"event_options,omitempty"` } type ResetPasswordReq struct { diff --git a/tyk/organisation.go b/tyk/organisation.go index f0be5d0..7dc1d06 100644 --- a/tyk/organisation.go +++ b/tyk/organisation.go @@ -4,6 +4,7 @@ import ( "bytes" "io" "net/http" + "tyk/tyk/bootstrap/pkg/config" "tyk/tyk/bootstrap/tyk/api" "tyk/tyk/bootstrap/tyk/internal/constants" @@ -79,6 +80,13 @@ func (s *Service) CreateOrganisation() error { Cname: s.appArgs.Tyk.Org.Cname, } + // Enable hybrid in the organisation while setting up the MDCB Control Plane. + // For reference: https://tyk.io/docs/tyk-multi-data-centre/setup-controller-data-centre/ + if s.appArgs.Tyk.Org.Hybrid != nil && s.appArgs.Tyk.Org.Hybrid.Enabled { + createOrgData.HybridEnabled = true + createOrgData.EventOptions = eventOptions(s.appArgs.Tyk.Org.Hybrid, s.appArgs.Tyk.Admin.EmailAddress) + } + reqBodyBytes, err := json.Marshal(createOrgData) if err != nil { return err @@ -121,3 +129,40 @@ func (s *Service) CreateOrganisation() error { return nil } + +func eventOptions(hconf *config.HybridConf, defaultEmail string) map[string]api.EventConfig { + if hconf == nil { + return nil + } + + m := make(map[string]api.EventConfig) + + const ( + hashedKeyEventKey = "hashed_key_event" + keyEventKey = "key_event" + ) + + hashedKeyEventConf := api.EventConfig{ + Email: defaultEmail, + Webhook: hconf.HashedKeyEvent.Webhook, + Redis: hconf.HashedKeyEvent.Redis, + } + keyEventConf := hashedKeyEventConf + + if hconf.HashedKeyEvent != nil { + if hconf.HashedKeyEvent.Email != "" { + hashedKeyEventConf.Email = hconf.HashedKeyEvent.Email + } + } + + if hconf.KeyEvent != nil { + if hconf.KeyEvent.Email != "" { + keyEventConf.Email = hconf.KeyEvent.Email + } + } + + m[hashedKeyEventKey] = hashedKeyEventConf + m[keyEventKey] = keyEventConf + + return m +}