Skip to content

Commit

Permalink
plugins validate: API call & result typing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejWilde committed Apr 21, 2020
1 parent 3e41660 commit a0f2788
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
26 changes: 23 additions & 3 deletions internal/ctl/plugins/validate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package plugins

import (
"encoding/json"
"fmt"
"os"

"github.com/90poe/connectctl/internal/ctl"
"github.com/90poe/connectctl/internal/version"
Expand Down Expand Up @@ -36,6 +38,11 @@ func validatePluginsCmd() *cobra.Command {
}

func doValidatePlugins(_ *cobra.Command, params *validatePluginsCmdParams) error {
var inputConfig connect.ConnectorConfig
if err := json.Unmarshal([]byte(params.Input), &inputConfig); err != nil {
return errors.Wrap(err, "error parsing input connector config")
}

config := &manager.Config{
ClusterURL: params.ClusterURL,
Version: version.Version,
Expand All @@ -53,10 +60,23 @@ func doValidatePlugins(_ *cobra.Command, params *validatePluginsCmdParams) error
return errors.Wrap(err, "error creating connectors manager")
}

//TODO remove
if mngr != nil {
return nil
validation, err := mngr.ValidatePlugins(inputConfig)
if err != nil {
return err
}

//TODO support different output types
printAsJSON(validation)

return nil
}

func printAsJSON(validation *connect.ConfigValidation) error {
b, err := json.MarshalIndent(validation, "", " ")
if err != nil {
return err
}

os.Stdout.Write(b)
return nil
}
52 changes: 45 additions & 7 deletions pkg/client/connect/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package connect

import (
"errors"
"fmt"
"net/http"
"strings"
)

// This is new and not from the original author
Expand All @@ -14,6 +16,40 @@ type Plugin struct {
Version string `json:"version"`
}

type FieldDefinition struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required"`
DefaultValue *string `json:"default_value"`
Importance string `json:"importance"`
Documentation string `json:"documentation"`
Group string `json:"group"`
Width string `json:"width"`
DisplayName string `json:"display_name"`
Dependents []map[string]interface{} `json:"dependents"` //unknown type
Order int `json:"order"`
}

type FieldValue struct {
Name string `json:"name"`
Value *string `json:"value"`
RecommendedValues []*string `json:"recommended_values"`
Errors []string `json:"errors"`
Visible bool `json:"visible"`
}

type FieldValidation struct {
Definition FieldDefinition `json:"definition"`
Value FieldValue `json:"value"`
}

type ConfigValidation struct {
Name string `json:"name"`
ErrorCount int `json:"error_count"`
Groups []string `json:"groups"`
Configs []FieldValidation `json:"configs"`
}

// ListPlugins retrieves a list of the installed plugins.
// Note that the API only checks for connectors on the worker
// that handles the request, which means it is possible to see
Expand All @@ -30,15 +66,17 @@ func (c *Client) ListPlugins() ([]*Plugin, *http.Response, error) {

// ValidatePlugins validates the provided configuration values against the configuration definition.
// See: https://docs.confluent.io/current/connect/references/restapi.html#put--connector-plugins-(string-name)-config-validate
func (c *Client) ValidatePlugins(config ConnectorConfig) ([]*Plugin, *http.Response, error) {
func (c *Client) ValidatePlugins(config ConnectorConfig) (*ConfigValidation, *http.Response, error) {
connectorClass, ok := config["connector.class"]
if !ok {
return nil, nil, errors.New("missing required key in config: connector.class")
return nil, nil, errors.New("missing required key in config: 'connector.class'")
}
// TODO
path := "connector-plugins"
var names []*Plugin

response, err := c.get(path, &names)
return names, response, err
tuple := strings.Split(connectorClass, ".")
path := fmt.Sprintf("connector-plugins/%s/config/validate", tuple[len(tuple)-1])

var validation ConfigValidation
response, err := c.doRequest("PUT", path, config, &validation)

return &validation, response, err
}
1 change: 1 addition & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type client interface {
ListConnectors() ([]string, *http.Response, error)
GetConnector(name string) (*connect.Connector, *http.Response, error)
ListPlugins() ([]*connect.Plugin, *http.Response, error)
ValidatePlugins(config connect.ConnectorConfig) (*connect.ConfigValidation, *http.Response, error)
GetConnectorStatus(name string) (*connect.ConnectorStatus, *http.Response, error)
DeleteConnector(name string) (*http.Response, error)
RestartConnectorTask(name string, taskID int) (*http.Response, error)
Expand Down
11 changes: 11 additions & 0 deletions pkg/manager/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ func (c *ConnectorManager) GetAllPlugins() ([]*connect.Plugin, error) {

return plugins, nil
}

// ValidatePlugins returns validation results of a connector config
func (c *ConnectorManager) ValidatePlugins(config connect.ConnectorConfig) (*connect.ConfigValidation, error) {
validation, _, err := c.client.ValidatePlugins(config)

if err != nil {
return nil, errors.Wrap(err, "error validating plugins")
}

return validation, nil
}

0 comments on commit a0f2788

Please sign in to comment.