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

Deploy multiple #32

Merged
merged 10 commits into from
Jan 30, 2019
84 changes: 66 additions & 18 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package cmd

import (
"encoding/json"
"errors"
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
"path"
"strings"

"github.com/ricardo-ch/go-kafka-connect/lib/connectors"
Expand All @@ -40,42 +43,87 @@ var createCmd = &cobra.Command{

//RunECreate ...
func RunECreate(cmd *cobra.Command, args []string) error {
config, err := getCreateCmdConfig(cmd)
configs, err := getCreateCmdConfig(cmd)
if err != nil {
return err
}

resp, err := getClient().CreateConnector(config, sync)
if err != nil {
return err
//TODO was not expecting I would have to update CreateConnector when adding multiple file deployment feature
// will have to add properly later
for _, config := range configs {
resp, err := getClient().CreateConnector(config, sync)
printResponse(resp)
if err != nil {
return err
}
}

return printResponse(resp)
return nil
}

func getCreateCmdConfig(cmd *cobra.Command) (connectors.CreateConnectorRequest, error) {
config := connectors.CreateConnectorRequest{}
func getCreateCmdConfig(cmd *cobra.Command) ([]connectors.CreateConnectorRequest, error) {
var configs []connectors.CreateConnectorRequest

if cmd.Flag("file").Changed {
fileReader, err := os.Open(file)
fileInfo, err := os.Stat(file)
if err != nil {
return config, err
return nil, errors.Wrapf(err, "error while trying to find file or folder: %v", file)
}

err = json.NewDecoder(fileReader).Decode(&config)
if err != nil {
return config, err
if fileInfo.IsDir() {
configs, err = getConfigFromFolder(file)
if err != nil {
return nil, err
}
} else {
config, err := getConfigFromFile(file)
if err != nil {
return nil, err
}
configs = append(configs, config)
}

} else if cmd.Flag("string").Changed {
config := connectors.CreateConnectorRequest{}
err := json.NewDecoder(strings.NewReader(configString)).Decode(&config)
if err != nil {
return config, err
return nil, err
}
configs = append(configs, config)
} else {
return config, errors.New("neither file nor string was supplied")
return nil, errors.New("neither file nor string was supplied")
}
return configs, nil
}

func getConfigFromFolder(folderPath string) ([]connectors.CreateConnectorRequest, error) {
configs := []connectors.CreateConnectorRequest{}
configFiles, err := ioutil.ReadDir(folderPath)
if err != nil {
return configs, err
}
for _, fileInfo := range configFiles {
if fileInfo.IsDir() {
log.Printf("found unexpected subfolder in folder: %s. This command will not search through it.", file)
continue
}
config, err := getConfigFromFile(path.Join(folderPath, fileInfo.Name()))
if err != nil {
log.Printf("found unexpected not config file in folder: %s", file)
} else {
configs = append(configs, config)
}
}
return config, nil
return configs, nil
}

func getConfigFromFile(filePath string) (connectors.CreateConnectorRequest, error) {
config := connectors.CreateConnectorRequest{}
fileReader, err := os.Open(filePath)
if err != nil {
return config, err
}

err = json.NewDecoder(fileReader).Decode(&config)
return config, err
}

func init() {
Expand Down
10 changes: 5 additions & 5 deletions cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ import (
var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy a new connector",
Long: `Deploy a new connector or replace the old version if it alrerady exists.
Long: `Deploy a new connector or replace the old version if it already exists.
This command is executes all its steps synchronously.
flags:
--url -u: url of the kafka-connect server
--file -f: path to the config file
--file -f: path to the config file or folder containing config files
FrancoisPoinsot marked this conversation as resolved.
Show resolved Hide resolved
--string -s: literal configuration string`,
RunE: RunEDeploy,
}

func RunEDeploy(cmd *cobra.Command, args []string) error {
config, err := getCreateCmdConfig(cmd)
configs, err := getCreateCmdConfig(cmd)
if err != nil {
return err
}

return getClient().DeployConnector(config)
return getClient().DeployMultipleConnector(configs)
}

func init() {
RootCmd.AddCommand(deployCmd)

deployCmd.PersistentFlags().StringVarP(&file, "file", "f", "", "path to the config file")
deployCmd.PersistentFlags().StringVarP(&file, "file", "f", "", "path to the config file or folder")
deployCmd.MarkFlagFilename("file")
deployCmd.PersistentFlags().StringVarP(&configString, "string", "s", "", "JSON configuration string")
}
4 changes: 2 additions & 2 deletions cli/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func handleCmd(cmd *cobra.Command, args []string) error {

func validateArgs() error {
if connector == "" {
return errors.New("Please specify the target connector's name")
return errors.New("please specify the target connector's name")
}
if (status && config) || (status && tasks) || (config && tasks) {
return errors.New("More than one action were provided")
return errors.New("more than one action were provided")
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func printResponse(response interface{}) error {
return nil
}

func getClient() *connectors.Client {
func getClient() connectors.HighLevelClient {
client := connectors.NewClient(url)
if verbose {
client = client.WithDebug()
client.SetDebug()
}
if SSLInsecure {
client = client.WithInsecureSSL()
client.SetInsecureSSL()
}
return client
}
1 change: 0 additions & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var (
tasks bool
verbose bool
SSLInsecure bool
output string
)

var RootCmd = &cobra.Command{
Expand Down
14 changes: 13 additions & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
.PHONY: test-integration
test-integration:
make rundep
go test -v `go list ./... | grep -v /vendor/` -tags=integration
go test -tags=integration -count=1 ./...

.PHONY: rundep
rundep:
Expand All @@ -19,3 +19,15 @@ rundep:
sleep 2; \
done
@echo "up and running"

MOCKERY_PATH := $(shell [ -z "$${GOBIN}" ] && echo $${GOPATH}/bin/mockery || echo $${GOBIN}/mockery; )

.PHONY: update-mocks
update-mocks:
go get github.com/vektra/mockery/...
mockery -inpkg -case "underscore" -recursive -all -note "NOTE: run 'make update-mocks' from this project top folder to update this file and generate new ones."
FrancoisPoinsot marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: test-unit
test-unit:
go test -tags=unit ./...

Loading