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
Changes from 1 commit
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
Next Next commit
Ehanced parameter metadata to include concepts of required/optional, …
…default values, and specific parameter types
djaglowski committed Jul 27, 2020
commit 798453248632fdf6cc5c65a83bdd276c1911362f
20 changes: 13 additions & 7 deletions operator/plugin.go
Original file line number Diff line number Diff line change
@@ -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

@@ -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
}

110 changes: 110 additions & 0 deletions operator/plugin_parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 interface{} // "string", "int", "bool" or array of strings
Default interface{} // Must fit Type
}

func (param PluginParameter) validate() error {
switch t := param.Type.(type) {
case string:
switch t {
case "string", "int", "bool": // ok
default:
return errors.NewError(
"invalid type for parameter",
"ensure that the type is one of 'string', 'int', 'bool', or an array containing only strings",
)
}

if param.Default == nil {
return nil
}

// Validate default corresponds to type
switch param.Default.(type) {
case string:
if param.Type != "string" {
return errors.NewError(
fmt.Sprintf("default value is a string but parameter type is %s", param.Type),
"ensure that the default value is a string",
)
}
case int, int32, int64:
if param.Type != "int" {
return errors.NewError(
fmt.Sprintf("default value is an int but parameter type is %s", param.Type),
"ensure that the default value is an int",
)
}
case bool:
if param.Type != "bool" {
return errors.NewError(
fmt.Sprintf("default value is a bool but parameter type is %s", param.Type),
"ensure that the default value is a bool",
)
}
default:
return errors.NewError(
"invalid default value",
"ensure that the default value corresponds to parameter type",
)
}

return nil
case []interface{}: // array represents enumerated values
for _, e := range t {
if _, ok := e.(string); !ok {
return errors.NewError(
"invalid value for enumerated parameter",
"ensure that all enumerated values are strings",
)
}
}

if param.Default == nil {
return 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",
)
}

validDef := false
for _, e := range t {
if str, ok := e.(string); ok && str == def {
validDef = true
}
}

if !validDef {
return errors.NewError(
"invalid default value for enumerated parameter",
"ensure default value is listed as a valid value",
)
}

return nil

default:
return errors.NewError(
"invalid type for parameter",
"supported types are 'string', 'int', 'bool', and array of strings",
)
}
}
Loading