Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ehanced parameter metadata to include concepts of required/optional, … #59

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions operator/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ type PluginConfig struct {
Pipeline []Config
}

// PluginParameter is a basic description of a plugin's parameter.
type PluginParameter struct {
Label string
Description string
Type string
}

// PluginRegistry is a registry of plugin templates.
type PluginRegistry map[string]*template.Template

Expand Down Expand Up @@ -63,6 +56,19 @@ func (r PluginRegistry) Render(pluginType string, params map[string]interface{})
)
}

for name, param := range config.Parameters {
if err := param.validate(); err != nil {
return PluginConfig{}, errors.NewError(
"invalid parameter description found in plugin config",
"ensure that all parameter descriptioins are valid for the plugin",
djaglowski marked this conversation as resolved.
Show resolved Hide resolved
"plugin_type", pluginType,
"plugin_parameter", name,
"rendered_config", writer.String(),
"error_message", err.Error(),
)
}
}

return config, nil
}

Expand Down
147 changes: 147 additions & 0 deletions operator/plugin_parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package operator

import (
"fmt"

"github.com/observiq/carbon/errors"
)

// PluginParameter is a basic description of a plugin's parameter.
type PluginParameter struct {
Label string
Description string
Required bool
Type string // "string", "int", "bool", "strings", or "enum"
ValidValues []string `yaml:"valid_values"` // only useable if Type == "enum"
Default interface{} // Must be valid according to Type & ValidValues
}

func (param PluginParameter) validate() error {
if param.Required && param.Default != nil {
return errors.NewError(
"required parameter cannot have a default value",
"ensure that required parameters do not have default values",
)
}

if err := param.validateType(); err != nil {
return err
}

if err := param.validateValidValues(); err != nil {
return err
}

if err := param.validateDefault(); err != nil {
return err
}

return nil
}

func (param PluginParameter) validateType() error {
switch param.Type {
case "string", "int", "bool", "strings", "enum": // ok
default:
return errors.NewError(
"invalid type for parameter",
"ensure that the type is one of 'string', 'int', 'bool', 'strings', or 'enum'",
)
}
return nil
}

func (param PluginParameter) validateValidValues() error {
switch param.Type {
case "string", "int", "bool", "strings":
if len(param.ValidValues) > 0 {
return errors.NewError(
fmt.Sprintf("valid_values is undefined for parameter of type '%s'", param.Type),
"remove 'valid_values' field or change type to 'enum'",
)
}
case "enum":
if len(param.ValidValues) == 0 {
return errors.NewError(
"parameter of type 'enum' must have 'valid_values' specified",
"specify an array that includes one or more valid values",
)
}
}
return nil
}

func (param PluginParameter) validateDefault() error {
if param.Default == nil {
return nil
}

// Validate that Default corresponds to Type
switch param.Type {
case "string":
if _, ok := param.Default.(string); !ok {
return errors.NewError(
"default value for a parameter of type 'string' must be a string",
"ensure that the default value is a string",
)
}
case "int":
switch param.Default.(type) {
case int, int32, int64: // ok
default:
return errors.NewError(
"default value for a parameter of type 'int' must be an integer",
"ensure that the default value is an integer",
)
}
case "bool":
if _, ok := param.Default.(bool); !ok {
return errors.NewError(
"default value for a parameter of type 'bool' must be a boolean",
"ensure that the default value is a boolean",
)
}
case "strings":
defaultList, ok := param.Default.([]interface{})
if !ok {
return errors.NewError(
"default value for a parameter of type 'strings' must be an array of strings",
"ensure that the default value is a string",
)
}
for _, s := range defaultList {
if _, ok := s.(string); !ok {
return errors.NewError(
"default value for a parameter of type 'strings' must be an array of strings",
"ensure that the default value is an array of strings",
)
}
}
case "enum":
if param.Default != nil {
// Validate that the default value is included in the enumeration
def, ok := param.Default.(string)
if !ok {
return errors.NewError(
"invalid default for enumerated parameter",
"ensure that the default value is a string",
)
}
for _, val := range param.ValidValues {
if val == def {
return nil
}
}
return errors.NewError(
"invalid default value for enumerated parameter",
"ensure default value is listed as a valid value",
)
}
default:
return errors.NewError(
"invalid type for parameter",
"ensure that the type is one of 'string', 'int', 'bool', 'strings', or 'enum'",
)
}
return nil
}
Loading