Skip to content

Instantly share code, notes, and snippets.

@eriknelson
Last active August 9, 2017 18:40
Show Gist options
  • Save eriknelson/4c2c29ea107e6e197fb03ea7abe19a8d to your computer and use it in GitHub Desktop.
Save eriknelson/4c2c29ea107e6e197fb03ea7abe19a8d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type APBAction string
type COEType string
type Result string
const (
APBActionProvision APBAction = "provision"
APBActionDeprovision APBAction = "deprovision"
APBActionBind APBAction = "bind"
APBActionUnbind APBAction = "unbind"
)
const (
COETypeKubernetes COEType = "kubernetes"
COETypeOpenshift COEType = "openshift"
)
// In reality, this is a configuration field
const (
ConfiguredCOEType = COETypeOpenshift
)
type ProvisionRequest struct {
Context map[string]string
Parameters map[string]string
}
type InputBundle struct {
Action APBAction
Context map[string]string
Parameters map[string]string
}
type COEDelegate interface {
CreateSandbox()
Run(input InputBundle)
DestroySandbox()
}
type KubernetesDelegate struct {
state string
}
func (k KubernetesDelegate) CreateSandbox() {
fmt.Println("KubernetesDelegate::CreateSandbox")
}
func (k KubernetesDelegate) Run(input InputBundle) {
fmt.Printf("KubernetesDelegate::Run, state -> [ %s ]\n", k.state)
fmt.Printf("Doing some k8s stuff with inputs: %+v\n", input)
}
func (k KubernetesDelegate) DestroySandbox() {
fmt.Println("KubernetesDelegate::DestroySandbox")
}
type OpenshiftDelegate struct {
state string
}
func (o OpenshiftDelegate) CreateSandbox() {
fmt.Println("OpenshiftDelegate::CreateSandbox")
}
func (o OpenshiftDelegate) Run(input InputBundle) {
fmt.Printf("OpenshiftDelegate::Run, state -> [ %s ]\n", o.state)
fmt.Printf("Doing some openshift stuff with inputs: %+v\n", input)
}
func (o OpenshiftDelegate) DestroySandbox() {
fmt.Println("OpenshiftDelegate::DestroySandbox")
}
type COERunner struct {
state string
delegate COEDelegate
}
func NewCOERunner(coeType COEType) COERunner {
runner := COERunner{state: "runner data"}
switch coeType {
case COETypeKubernetes:
runner.delegate = &KubernetesDelegate{state: "kubernetes delegate data"}
case COETypeOpenshift:
runner.delegate = &OpenshiftDelegate{state: "openshift delegate data"}
default:
panic("A TERRIBLE THING OCCURED!")
}
return runner
}
func (r COERunner) Run(input InputBundle) error {
fmt.Printf("COERunner::Run, Action -> [ %s ]\n", input.Action)
r.delegate.CreateSandbox()
r.SharedLogic()
r.delegate.Run(input)
r.delegate.DestroySandbox()
return nil
}
func (r COERunner) SharedLogic() {
// Illustrating logic that is shared by all delegates belongs in the Runner
fmt.Println("COERunner::SharedLogic, stuff all runners need to do")
}
// Transforms ProvisionRequest -> APBInputBundle
func NewInputBundle(action APBAction, req ProvisionRequest) InputBundle {
return InputBundle{
Action: action,
Context: req.Context,
Parameters: req.Parameters,
}
}
func brokerProvision(req ProvisionRequest) {
fmt.Println("broker::Provision")
input := NewInputBundle(APBActionProvision, req)
// A bunch of broker.go junk
// Simulate apb.Provision, stuff will get wraped up into a work engine job
// in practice by broker.Provision. Point is, broker transforms request to inputbundle
// and passes it to apb.Provision. InputBundle probably should be in apb pkg
result, _ := ApbProvision(input)
fmt.Println(result)
}
func ApbProvision(input InputBundle) (Result, error) {
fmt.Println("apb::Provision")
runner := NewCOERunner(ConfiguredCOEType)
err := runner.Run(input)
if err != nil {
panic("Everything is on fire.")
}
return "Great success!", nil
}
func main() {
mockProvReq := ProvisionRequest{
Context: map[string]string{"namespace": "foo"},
Parameters: map[string]string{"database_name": "eriks_db"},
}
brokerProvision(mockProvReq) // broker.go Provision function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment