Skip to content

Commit

Permalink
add retry for loading and validating connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktaylorsbg committed Jul 8, 2022
1 parent 01071e9 commit 335702a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

retryablehttp "github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -130,6 +132,10 @@ var loadCmd = &cobra.Command{
}
}

rhttp := retryablehttp.NewClient()
rhttp.RetryMax = 3
rhttp.RetryWaitMin = time.Duration(5 * time.Second)

// validate
var allValid = true
for i, file := range files {
Expand All @@ -139,7 +145,7 @@ var loadCmd = &cobra.Command{
continue
}

files[i].ValidationResp = ValidateConfig(host, port, file)
files[i].ValidationResp = ValidateConfig(rhttp, host, port, file)
if files[i].ValidationResp.ErrorCount > 0 {
allValid = false
}
Expand All @@ -149,7 +155,7 @@ var loadCmd = &cobra.Command{
if allValid {
fmt.Fprintf(cmd.OutOrStdout(), "All connectors are valid loading configs.\n")
for i, file := range files {
files[i].LoadResp = LoadConfig(host, port, file)
files[i].LoadResp = LoadConfig(rhttp, host, port, file)
}
}

Expand All @@ -167,33 +173,33 @@ var loadCmd = &cobra.Command{
},
}

func LoadConfig(host string, port string, configFile ConfigFile) *http.Response {
func LoadConfig(client *retryablehttp.Client, host string, port string, configFile ConfigFile) *http.Response {
validateUrl := fmt.Sprintf("http://%s:%s/connectors/%s/config", host, port, configFile.ConnectorName)

req, err := http.NewRequest(http.MethodPut, validateUrl, bytes.NewBuffer(configFile.ConfigBytes))
req, err := retryablehttp.NewRequest(http.MethodPut, validateUrl, bytes.NewBuffer(configFile.ConfigBytes))
cobra.CheckErr(err)

req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
cobra.CheckErr(err)
respBodyBytes, _ := ioutil.ReadAll(resp.Body)
log.Debug(string(respBodyBytes))
log.Debug("Put config for: ", configFile.ConnectorName, " got response status: ", resp.StatusCode)
log.Info("Put config for: ", configFile.ConnectorName, " got response status: ", resp.Status, " and code: ", resp.StatusCode)
return resp

}

func ValidateConfig(host string, port string, configFile ConfigFile) ValidationResponse {
func ValidateConfig(client *retryablehttp.Client, host string, port string, configFile ConfigFile) ValidationResponse {

validateUrl := fmt.Sprintf("http://%s:%s/connector-plugins/%s/config/validate", host, port, configFile.PluginClass)

req, err := http.NewRequest(http.MethodPut, validateUrl, bytes.NewBuffer(configFile.ConfigBytes))
req, err := retryablehttp.NewRequest(http.MethodPut, validateUrl, bytes.NewBuffer(configFile.ConfigBytes))
cobra.CheckErr(err)

req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
cobra.CheckErr(err)
respBodyBytes, _ := ioutil.ReadAll(resp.Body)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/fatih/color v1.7.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/hashicorp/go-retryablehttp v0.7.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down

0 comments on commit 335702a

Please sign in to comment.