Skip to content

Commit

Permalink
Add nonkube connector CLI command and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnemorrison committed Oct 11, 2024
1 parent b3d49ec commit fc6e23f
Show file tree
Hide file tree
Showing 10 changed files with 1,042 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/cmd/skupper/connector/kube/connector_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (cmd *CmdConnectorUpdate) ValidateInput(args []string) []error {
cmd.newSettings.connectorType = connector.Spec.Type
cmd.newSettings.selector = connector.Spec.Selector
cmd.newSettings.includeNotReady = connector.Spec.IncludeNotReady
cmd.newSettings.routingKey = connector.Spec.RoutingKey
}
}

Expand Down
150 changes: 142 additions & 8 deletions internal/cmd/skupper/connector/nonkube/connector_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,162 @@ package nonkube

import (
"fmt"
"strconv"

"github.com/skupperproject/skupper/internal/cmd/skupper/common"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
"github.com/skupperproject/skupper/internal/nonkube/client/fs"
"github.com/skupperproject/skupper/pkg/apis/skupper/v1alpha1"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CmdConnectorCreate struct {
CobraCmd *cobra.Command
Flags *common.CommandConnectorCreateFlags
Namespace string
siteName string
connectorHandler *fs.ConnectorHandler
CobraCmd *cobra.Command
Flags *common.CommandConnectorCreateFlags
namespace string
connectorName string
port int
output string
host string
routingKey string
connectorType string
tlsSecret string
}

func NewCmdConnectorCreate() *CmdConnectorCreate {
return &CmdConnectorCreate{}
}

func (cmd *CmdConnectorCreate) NewClient(cobraCommand *cobra.Command, args []string) {
//TODO
if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameNamespace) != nil && cmd.CobraCmd.Flag(common.FlagNameNamespace).Value.String() != "" {
cmd.namespace = cmd.CobraCmd.Flag(common.FlagNameNamespace).Value.String()
}

cmd.connectorHandler = fs.NewConnectorHandler(cmd.namespace)
}

func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
var validationErrors []error

if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameContext) != nil && cmd.CobraCmd.Flag(common.FlagNameContext).Value.String() != "" {
fmt.Println("Warning: --context flag is not supported on this platform")
}

if cmd.CobraCmd != nil && cmd.CobraCmd.Flag(common.FlagNameKubeconfig) != nil && cmd.CobraCmd.Flag(common.FlagNameKubeconfig).Value.String() != "" {
fmt.Println("Warning: --kubeconfig flag is not supported on this platform")
}

resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
connectorTypeValidator := validator.NewOptionValidator(common.ConnectorTypes)
outputTypeValidator := validator.NewOptionValidator(common.OutputTypes)

// Validate arguments name and port
if len(args) < 2 {
validationErrors = append(validationErrors, fmt.Errorf("connector name and port must be configured"))
} else if len(args) > 2 {
validationErrors = append(validationErrors, fmt.Errorf("only two arguments are allowed for this command"))
} else if args[0] == "" {
validationErrors = append(validationErrors, fmt.Errorf("connector name must not be empty"))
} else if args[1] == "" {
validationErrors = append(validationErrors, fmt.Errorf("connector port must not be empty"))
} else {
ok, err := resourceStringValidator.Evaluate(args[0])
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector name is not valid: %s", err))
} else {
cmd.connectorName = args[0]
}
cmd.port, err = strconv.Atoi(args[1])
if err != nil {
validationErrors = append(validationErrors, fmt.Errorf("connector port is not valid: %s", err))
}
ok, err = numberValidator.Evaluate(cmd.port)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector port is not valid: %s", err))
}
}

// Validate flags
if cmd.Flags.RoutingKey != "" {
ok, err := resourceStringValidator.Evaluate(cmd.Flags.RoutingKey)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("routing key is not valid: %s", err))
}
}
if cmd.Flags.ConnectorType != "" {
ok, err := connectorTypeValidator.Evaluate(cmd.Flags.ConnectorType)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("connector type is not valid: %s", err))
}
}
if cmd.Flags.Output != "" {
ok, err := outputTypeValidator.Evaluate(cmd.Flags.Output)
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("output type is not valid: %s", err))
}
}
if cmd.Flags.Host != "" {
// TBD what is valid host
}
if cmd.Flags.TlsSecret != "" {
// TBD what is valid TlsSecret
}

return validationErrors
}

func (cmd *CmdConnectorCreate) InputToOptions() {
// default routingkey to name of connector
if cmd.Flags.RoutingKey == "" {
cmd.routingKey = cmd.connectorName
} else {
cmd.routingKey = cmd.Flags.RoutingKey
}

if cmd.namespace == "" {
cmd.namespace = "default"
}

cmd.host = cmd.Flags.Host
cmd.connectorType = cmd.Flags.ConnectorType
cmd.output = cmd.Flags.Output
cmd.tlsSecret = cmd.Flags.TlsSecret
}

func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error { return nil }
func (cmd *CmdConnectorCreate) InputToOptions() {}
func (cmd *CmdConnectorCreate) Run() error {
return fmt.Errorf("command not supported by the selected platform")
connectorResource := v1alpha1.Connector{
TypeMeta: metav1.TypeMeta{
APIVersion: "skupper.io/v1alpha1",
Kind: "Connector",
},
ObjectMeta: metav1.ObjectMeta{
Name: cmd.connectorName,
Namespace: cmd.namespace,
},
Spec: v1alpha1.ConnectorSpec{
Host: cmd.host,
Port: cmd.port,
RoutingKey: cmd.routingKey,
TlsCredentials: cmd.tlsSecret,
Type: cmd.connectorType,
},
}

if cmd.output != "" {
encodedOutput, err := utils.Encode(cmd.output, connectorResource)
fmt.Println(encodedOutput)
return err
} else {
err := cmd.connectorHandler.Add(connectorResource)
if err != nil {
return err
}
}
return nil
}

func (cmd *CmdConnectorCreate) WaitUntil() error { return nil }
Loading

0 comments on commit fc6e23f

Please sign in to comment.