# README
Implementing a new Traffic Vault backend (e.g. Foo)
- Create a new directory in ./backends which will contain/define your new package (
foo
) which will provide all the functionality to support a newFoo
backend for Traffic Vault. - In this new
./backends/foo
directory, create a new file:foo.go
, withpackage foo
to define the package name. - In
foo.go
, define a struct (e.g.type Foo struct
) which will act as the method receiver for all the requiredTrafficVault
methods. This struct should contain any fields necessary to provide the required functionality. For instance, it should most likely contain all the required configuration to connect to and use theFoo
data store.
type Foo struct {
cfg Config
}
type Config struct {
user string
password string
}
- Implement all the methods required by the
TrafficVault
interface on your newFoo
struct. Initially, you may want to simply stub out the methods and implement them later:
func (f *Foo) GetDeliveryServiceSSLKeys(xmlID string, version string, tx *sql.Tx, ctx context.Context) (tc.DeliveryServiceSSLKeysV15, bool, error) {
return tc.DeliveryServiceSSLKeysV15{}, false, nil
}
... (snip)
func (f *Foo) Ping(tx *sql.Tx, ctx context.Context) (tc.TrafficVaultPingResponse, error) {
return tc.TrafficVaultPingResponse{}, nil
}
- Define a
trafficvault.LoadFunc
which will parse the given JSON config (from cdn.conf'straffic_vault_config
option) and return a pointer to an instance of theFoo
type:
func loadFoo(b json.RawMessage) (trafficvault.TrafficVault, error) {
// unmarshal the given JSON, validate it, return an error if any
// fooCfg, err := parseAndValidateConfig(b)
return &Foo{cfg: fooCfg}, nil
}
- Define a package
init
function which callstrafficvault.AddBackend
with your backend's name andLoadFunc
in order to register your new Traffic VaultFoo
backend for use:
func init() {
trafficvault.AddBackend("foo", loadFoo)
}
- In
./backends/backends.go
, import your new package:
import (
_ "github.com/apache/trafficcontrol/v8/traffic_ops/traffic_ops_golang/trafficvault/backends/foo"
)
This is required for the package init()
function to run and register the new backend.
8. You are now able to test your new Traffic Vault Foo
backend. First, in cdn.conf
, you need to set traffic_vault_backend
to "foo"
and include your desired Foo
configuration in traffic_vault_config
. Once that is done, Traffic Vault is enabled, and you can use Traffic Ops API routes that require Traffic Vault. At this point, you should go back and fully implement the stubbed out TrafficVault
interface methods on your Foo
type.
# Packages
Package backends is simply for importing the traffic vault backend packages so they can initialize.
# Functions
AddBackend should be called by each TrafficVault backend package's init() function in order to register its name and LoadFunc.
GetBackend is called with the contents of the traffic_vault_backend and traffic_vault_config options in cdn.conf, respectively, in order to lookup and load the chosen Traffic Vault backend to use.
# Interfaces
TrafficVault defines the methods necessary for a struct to implement in order to provide all the necessary functionality required of a Traffic Vault backend.
# Type aliases
A LoadFunc is a function that takes a json.RawMessage as input (the contents of traffic_vault_config in cdn.conf) and returns a valid TrafficVault as output.