configuration

package
v0.0.0-...-14e2bc2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

TODO: This library is imported from ror, should determine if its a public library or not the configuration package is responsible for loading and merging configurations it implements loaders and parsers

Example:

    teststring := `{"birdType":"Pigeon","what it does":"likes to eat seed", "age":12}`
    teststring2 := fmt.Sprintf("birdType: %s\nwhat it does: likes to eat seed\nage: %d\n", "Pigeon", 14)

    generator := configuration.NewConfigurationsGenerator()

    layer := configuration.NewConfigurationLayer("test1", 2, 250, configuration.NewStringLoader(configuration.ParserTypeJson, teststring))
    generator.AddConfiguration(layer)

    layer2 := configuration.NewConfigurationLayer("test2", 1, 251, configuration.NewStringLoader(configuration.ParserTypeYaml, teststring2))
    generator.AddConfiguration(layer2)

    generator.AddConfiguration(configuration.NewConfigurationLayer("test3", 3, 252, configuration.NewFuncLoader(configuration.ParserTypeYaml, testloaderz)))

	    ownerref := apiresourcecontracts.ResourceOwnerReference{
		   Scope:   "cluster",
	       Subject: "sdi-ror-dev-b40y",
     }

     generator.AddConfiguration(configuration.NewConfigurationLayer("test4", 3, 252, configuration.NewFileLoader(configuration.ParserTypeYaml, "../../assets/configs.yaml")))
     generator.AddConfiguration(configuration.NewConfigurationLayer("test5", 3, 254, configuration.NewFileLoader(configuration.ParserTypeJson, "../../assets/config1.json")))

     generator.AddConfiguration(configuration.NewConfigurationLayer("test6", 3, 253, configuration.NewResourceLoader(ownerref, "test")))

     merged, err := generator.GenerateConfig()

   	if err != nil {
	    	fmt.Println(err)
		}

	    fmt.Println(string(merged))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeB64

func DecodeB64(data string) string

func Merge

func Merge(input []byte, patch []byte) ([]byte, error)

Merge merges two jsons

Types

type ConfigGeneratorInterface

type ConfigGeneratorInterface interface {
	GenerateConfig() ([]byte, error)
	GenerateConfigYaml() ([]byte, error)
	AddConfiguration(configuration ConfigLayerInterface)
}

ConfigurationsGenerator is the main interface for generating configurations

func NewConfigurationsGenerator

func NewConfigurationsGenerator() ConfigGeneratorInterface

NewConfigurationsGenerator creates a new empty ConfigurationsGenerator

type ConfigLayerInterface

type ConfigLayerInterface interface {
	GetName() string
	GetTier() int
	GetOrder() int
	GetContent() []byte
	IsSecret() bool
}

ConfigLayerInterface is the interface for a configuration layer

func NewConfigurationLayer

func NewConfigurationLayer(name string, tier int, order int, content ConfigLoaderInterface) ConfigLayerInterface

NewConfigurationLayer creates a new configuration layer

Parameters:

name: the name of the layer
tier: the tier of the layer
order: the order of the layer
content: the content of the layer represented as a ConfigLoaderInterface

type ConfigLoaderInterface

type ConfigLoaderInterface interface {
	Parse() ([]byte, error)
	IsSecret() bool
}

ConfigLoaderInterface is the interface for loading a configuration

func NewFileLoader

func NewFileLoader(parser ParserType, path string) ConfigLoaderInterface

NewFileLoader creates a ConfigLoaderInterface from a file

Parameters:

parser: the parser to use
path: the path to the file
WARNING: this function should not be used with user input

func NewFuncLoader

func NewFuncLoader(parser ParserType, function func() ([]byte, error)) ConfigLoaderInterface

NewFuncLoader creates a ConfigLoaderInterface from a function

Parameters:

parser: the parser to use
function: the function to call
  - the function must return a byte array and an error

func NewHelsegitlabLoader

func NewHelsegitlabLoader(parser ParserType, projectid int, path string, branch string, vaultClient *vaultclient.VaultClient) ConfigLoaderInterface

NewHelsegitlabLoader creates a ConfigLoaderInterface from a git repo

Parameters:

	parser: the parser to use
 projectid: the id in helsegitlab
	path: the path to the file
 branch: the branch to download from

func NewMapStringLoader

func NewMapStringLoader(inn map[string]string) ConfigLoaderInterface

MapStringLoader is a ConfigLoaderInterface that loads a config from a map[string]string

Parameters:

Parser: the parser to use
Data: the string to parse

func NewResourceLoader

func NewResourceLoader(ownerref apiresourcecontracts.ResourceOwnerReference, resourceName string) ConfigLoaderInterface

NewResourceLoader creates a ConfigLoaderInterface from a resource

Parameters:

ownerref: the owner reference (apiresourcecontracts.ResourceOwnerReference)
resourceName: the name of the resource (string)

The resource must have its data represented in spec.data but can be base64 encoded if spec.b64enc is true

func NewSecretLoader

func NewSecretLoader(vaultpath string, vaultkey string, jsonpath string, vaultClient *vaultclient.VaultClient) ConfigLoaderInterface

NewSecretLoader creates a ConfigLoaderInterface from a vault secret

Parameters:

vaultpath: the path in vault
vaultkey: the key to use
jsonpath: the json path to set "test.auth.username"

func NewSecretMapLoader

func NewSecretMapLoader(inn []SecretStruct, vaultClient *vaultclient.VaultClient) ConfigLoaderInterface

NewSecretMapLoader creates a ConfigLoaderInterface from a map of SecretStruct

func NewStringLoader

func NewStringLoader(parser ParserType, data string) ConfigLoaderInterface

StringLoader is a ConfigLoaderInterface that loads a config from a string

Parameters:

Parser: the parser to use
Data: the string to parse

type ConfigParserInterface

type ConfigParserInterface interface {
	Parse() ([]byte, error)
}

ConfigParserInterface is the interface for parsing a configuration to json

func NewJsonParser

func NewJsonParser(data []byte) ConfigParserInterface

NewJsonParser creates a new json parser as ConfigParserInterface

func NewYamlParser

func NewYamlParser(data []byte) ConfigParserInterface

NewYamlParser creates a new yaml parser as ConfigParserInterface

type ConfigurationsGenerator

type ConfigurationsGenerator struct {
	Configurations []ConfigLayerInterface
}

func (*ConfigurationsGenerator) AddConfiguration

func (c *ConfigurationsGenerator) AddConfiguration(config ConfigLayerInterface)

AddConfiguration adds a configuration layer to the ConfigurationsGenerator The configuration layer will be added to the list, but its position will be determined by the tier and order The lower tier, the higher priority The lower order, the higher priority within the same tier

func (*ConfigurationsGenerator) GenerateConfig

func (c *ConfigurationsGenerator) GenerateConfig() ([]byte, error)

GenerateConfig generates a json configuration from the configurations in the ConfigurationsGenerator

func (*ConfigurationsGenerator) GenerateConfigYaml

func (c *ConfigurationsGenerator) GenerateConfigYaml() ([]byte, error)

GenerateConfigYaml generates a yaml configuration from the configurations in the ConfigurationsGenerator

func (*ConfigurationsGenerator) SortConfigurations

func (c *ConfigurationsGenerator) SortConfigurations() []ConfigLayerInterface

SortConfigurations sorts the configurations by tier and order

type JsonParser

type JsonParser struct {
	Data []byte
}

func (JsonParser) Parse

func (c JsonParser) Parse() ([]byte, error)

type LayerConfig

type LayerConfig struct {
	Name    string
	Tier    int
	Order   int
	Content []byte
	Secret  bool
}

func (LayerConfig) GetContent

func (c LayerConfig) GetContent() []byte

GetContent returns the content of the layer

func (LayerConfig) GetName

func (c LayerConfig) GetName() string

GetName returns the name of the layer

func (LayerConfig) GetOrder

func (c LayerConfig) GetOrder() int

GetOrder returns the order of the layer

func (LayerConfig) GetTier

func (c LayerConfig) GetTier() int

GetTier returns the tier of the layer

func (LayerConfig) IsSecret

func (c LayerConfig) IsSecret() bool

IsSecret returns is the layer contains secrets

type Loader

type Loader struct {
	Parser ConfigParserInterface
	Secret bool
}

Common structure for configuration loaders

func (Loader) IsSecret

func (c Loader) IsSecret() bool

func (Loader) Parse

func (c Loader) Parse() ([]byte, error)

Parse runs the parser to return a json

type ParserType

type ParserType string
const (
	ParserTypeJson ParserType = "json"
	ParserTypeYaml ParserType = "yaml"
)

type SecretStruct

type SecretStruct struct {
	VaultPath string
	VaultKey  string
	JsonPath  string
}

type YamlParser

type YamlParser struct {
	Data []byte
}

func (YamlParser) Parse

func (c YamlParser) Parse() ([]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL