diff --git a/.github/workflows/project.yml b/.github/workflows/project.yml new file mode 100644 index 00000000..b205d5e3 --- /dev/null +++ b/.github/workflows/project.yml @@ -0,0 +1,31 @@ +name: ProjectStyle +on: [pull_request] +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: InstallTool + run: | + wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.18.0 + ./bin/golangci-lint --version + + - name: Build + run: go build . + + - name: CheckStyle + run: | + ./bin/golangci-lint run -c ./golangci.yml ./... + + diff --git a/golangci.yml b/golangci.yml new file mode 100644 index 00000000..11de1c35 --- /dev/null +++ b/golangci.yml @@ -0,0 +1,27 @@ +run: + deadline: 6m + +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - gocritic + - goimports + - golint + - gosimple + - govet + - ineffassign + - interfacer + - misspell + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - lll + - prealloc + - maligned diff --git a/main.go b/main.go index 525be78d..7e572fbe 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,6 @@ import ( "fmt" "os" - "github.com/kris-nova/logger" - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/cluster" "github.com/streamnative/pulsarctl/pkg/ctl/completion" @@ -33,6 +31,9 @@ import ( "github.com/streamnative/pulsarctl/pkg/ctl/sources" "github.com/streamnative/pulsarctl/pkg/ctl/tenant" "github.com/streamnative/pulsarctl/pkg/ctl/topic" + + "github.com/kris-nova/logger" + "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ diff --git a/pkg/auth/tls.go b/pkg/auth/tls.go index faeead66..ebe33d68 100644 --- a/pkg/auth/tls.go +++ b/pkg/auth/tls.go @@ -19,13 +19,13 @@ package auth import "crypto/tls" -type TlsAuthProvider struct { +type TLSAuthProvider struct { certificatePath string privateKeyPath string } // NewAuthenticationTLSWithParams initialize the authentication provider with map param. -func NewAuthenticationTLSWithParams(params map[string]string) *TlsAuthProvider { +func NewAuthenticationTLSWithParams(params map[string]string) *TLSAuthProvider { return NewAuthenticationTLS( params["tlsCertFile"], params["tlsKeyFile"], @@ -33,24 +33,24 @@ func NewAuthenticationTLSWithParams(params map[string]string) *TlsAuthProvider { } // NewAuthenticationTLS initialize the authentication provider -func NewAuthenticationTLS(certificatePath string, privateKeyPath string) *TlsAuthProvider { - return &TlsAuthProvider{ +func NewAuthenticationTLS(certificatePath string, privateKeyPath string) *TLSAuthProvider { + return &TLSAuthProvider{ certificatePath: certificatePath, privateKeyPath: privateKeyPath, } } -func (p *TlsAuthProvider) Init() error { +func (p *TLSAuthProvider) Init() error { // Try to read certificates immediately to provide better error at startup _, err := p.GetTLSCertificate() return err } -func (p *TlsAuthProvider) Name() string { +func (p *TLSAuthProvider) Name() string { return "tls" } -func (p *TlsAuthProvider) GetTLSCertificate() (*tls.Certificate, error) { +func (p *TLSAuthProvider) GetTLSCertificate() (*tls.Certificate, error) { cert, err := tls.LoadX509KeyPair(p.certificatePath, p.privateKeyPath) return &cert, err } diff --git a/pkg/cmdutils/cmdutils.go b/pkg/cmdutils/cmdutils.go index f1844a1b..a6dce57b 100644 --- a/pkg/cmdutils/cmdutils.go +++ b/pkg/cmdutils/cmdutils.go @@ -25,9 +25,10 @@ import ( "os" "strings" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/pulsar" ) const IncompatibleFlags = "cannot be used at the same time" @@ -82,17 +83,17 @@ func NewPulsarClient() pulsar.Client { return PulsarCtlConfig.Client(pulsar.V2) } -func NewPulsarClientWithApiVersion(version pulsar.ApiVersion) pulsar.Client { +func NewPulsarClientWithAPIVersion(version pulsar.APIVersion) pulsar.Client { return PulsarCtlConfig.Client(version) } -func PrintJson(w io.Writer, obj interface{}) { +func PrintJSON(w io.Writer, obj interface{}) { b, err := json.MarshalIndent(obj, "", " ") if err != nil { - fmt.Fprintf(w, "unexpected response type: %v\n", err) + _, _ = fmt.Fprintf(w, "unexpected response type: %v\n", err) return } - fmt.Fprintln(w, string(b)) + _, _ = fmt.Fprintln(w, string(b)) } func PrintError(w io.Writer, err error) { diff --git a/pkg/cmdutils/config.go b/pkg/cmdutils/config.go index 5a5dbfa0..80130aaf 100644 --- a/pkg/cmdutils/config.go +++ b/pkg/cmdutils/config.go @@ -18,9 +18,11 @@ package cmdutils import ( - `log` - "github.com/spf13/pflag" + "log" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) var PulsarCtlConfig = ClusterConfig{} @@ -28,11 +30,11 @@ var PulsarCtlConfig = ClusterConfig{} // the configuration of the cluster that pulsarctl connects to type ClusterConfig struct { // the web service url that pulsarctl connects to. Default is http://localhost:8080 - WebServiceUrl string + WebServiceURL string // Set the path to the trusted TLS certificate file - TlsTrustCertsFilePath string + TLSTrustCertsFilePath string // Configure whether the Pulsar client accept untrusted TLS certificate from broker (default: false) - TlsAllowInsecureConnection bool + TLSAllowInsecureConnection bool AuthParams string } @@ -43,7 +45,7 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet { pflag.ContinueOnError) flags.StringVarP( - &c.WebServiceUrl, + &c.WebServiceURL, "admin-service-url", "s", pulsar.DefaultWebServiceURL, @@ -53,17 +55,17 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet { &c.AuthParams, "auth-params", "", - "Authentication parameters are used to configure the public and private key files required by tls\n" + + "Authentication parameters are used to configure the public and private key files required by tls\n"+ " For example: \"tlsCertFile:val1,tlsKeyFile:val2\"") flags.BoolVar( - &c.TlsAllowInsecureConnection, + &c.TLSAllowInsecureConnection, "tls-allow-insecure", false, "Allow TLS insecure connection") flags.StringVar( - &c.TlsTrustCertsFilePath, + &c.TLSTrustCertsFilePath, "tls-trust-cert-pat", "", "Allow TLS trust cert file path") @@ -71,26 +73,26 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet { return flags } -func (c *ClusterConfig) Client(version pulsar.ApiVersion) pulsar.Client { +func (c *ClusterConfig) Client(version pulsar.APIVersion) pulsar.Client { config := pulsar.DefaultConfig() - if len(c.WebServiceUrl) > 0 && c.WebServiceUrl != config.WebServiceUrl { - config.WebServiceUrl = c.WebServiceUrl + if len(c.WebServiceURL) > 0 && c.WebServiceURL != config.WebServiceURL { + config.WebServiceURL = c.WebServiceURL } - if len(c.TlsTrustCertsFilePath) > 0 && c.TlsTrustCertsFilePath != config.TlsOptions.TrustCertsFilePath { - config.TlsOptions.TrustCertsFilePath = c.TlsTrustCertsFilePath + if len(c.TLSTrustCertsFilePath) > 0 && c.TLSTrustCertsFilePath != config.TLSOptions.TrustCertsFilePath { + config.TLSOptions.TrustCertsFilePath = c.TLSTrustCertsFilePath } - if c.TlsAllowInsecureConnection { - config.TlsOptions.AllowInsecureConnection = true + if c.TLSAllowInsecureConnection { + config.TLSOptions.AllowInsecureConnection = true } if len(c.AuthParams) > 0 && c.AuthParams != config.AuthParams { config.AuthParams = c.AuthParams } - config.ApiVersion = version + config.APIVersion = version client, err := pulsar.New(config) if err != nil { diff --git a/pkg/cmdutils/group.go b/pkg/cmdutils/group.go index ba94e990..0a53cc31 100644 --- a/pkg/cmdutils/group.go +++ b/pkg/cmdutils/group.go @@ -19,10 +19,11 @@ package cmdutils import ( "fmt" - "github.com/spf13/cobra" - "github.com/spf13/pflag" "strings" "unicode" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) // FlagGrouping holds a superset of all flagsets for all commands @@ -64,8 +65,8 @@ func (n *NamedFlagSetGroup) InFlagSet(name string, cb func(*pflag.FlagSet)) { } nfs := namedFlagSet{ - name: name, - fs: &pflag.FlagSet{}, + name: name, + fs: &pflag.FlagSet{}, } cb(nfs.fs) n.list = append(n.list, nfs) @@ -96,7 +97,7 @@ func (g *FlagGrouping) Usage(cmd *cobra.Command) error { } if len(cmd.Aliases) > 0 { - usage = append(usage, "\nAliases: " + cmd.NameAndAliases()) + usage = append(usage, "\nAliases: "+cmd.NameAndAliases()) } if group != nil { @@ -114,10 +115,11 @@ func (g *FlagGrouping) Usage(cmd *cobra.Command) error { usage = append(usage, strings.TrimRightFunc(cmd.InheritedFlags().FlagUsages(), unicode.IsSpace)) } - usage = append(usage, fmt.Sprintf("\nUse '%s [command] --help' for more information about a command.\n", cmd.CommandPath())) + usage = append(usage, + fmt.Sprintf("\nUse '%s [command] --help' for more information about a command.\n", + cmd.CommandPath())) cmd.Println(strings.Join(usage, "\n")) return nil } - diff --git a/pkg/cmdutils/verb.go b/pkg/cmdutils/verb.go index b6e9271b..9e6475d7 100644 --- a/pkg/cmdutils/verb.go +++ b/pkg/cmdutils/verb.go @@ -18,9 +18,10 @@ package cmdutils import ( + "os" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "os" ) // VerbCmd holds attributes that most of the commands use @@ -31,7 +32,7 @@ type VerbCmd struct { NameArgs []string // for testing - NameError error + NameError error } // AddVerbCmd create a registers a new command under the given resource command @@ -45,7 +46,7 @@ func AddVerbCmd(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, ne parentResourceCmd.AddCommand(verb.Command) } -func AddVerbCmds(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, newVerbCmd ...func(cmd *VerbCmd)) { +func AddVerbCmds(flagGrouping *FlagGrouping, parentResourceCmd *cobra.Command, newVerbCmd ...func(cmd *VerbCmd)) { for _, cmd := range newVerbCmd { AddVerbCmd(flagGrouping, parentResourceCmd, cmd) } diff --git a/pkg/ctl/cluster/cluster.go b/pkg/ctl/cluster/cluster.go index 1c70bbb9..0ec4ac81 100644 --- a/pkg/ctl/cluster/cluster.go +++ b/pkg/ctl/cluster/cluster.go @@ -18,10 +18,11 @@ package cluster import ( - "errors" - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "github.com/spf13/cobra" ) var argsError = pulsar.Output{ diff --git a/pkg/ctl/cluster/create.go b/pkg/ctl/cluster/create.go index dd5c118d..ed663613 100644 --- a/pkg/ctl/cluster/create.go +++ b/pkg/ctl/cluster/create.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func CreateClusterCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/create_failure_domain.go b/pkg/ctl/cluster/create_failure_domain.go index 796115a5..ecdfbb20 100644 --- a/pkg/ctl/cluster/create_failure_domain.go +++ b/pkg/ctl/cluster/create_failure_domain.go @@ -18,10 +18,11 @@ package cluster import ( - "errors" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "github.com/spf13/pflag" ) func createFailureDomainCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/create_failure_domain_test.go b/pkg/ctl/cluster/create_failure_domain_test.go index 202f63f0..9f9ba8b3 100644 --- a/pkg/ctl/cluster/create_failure_domain_test.go +++ b/pkg/ctl/cluster/create_failure_domain_test.go @@ -18,8 +18,9 @@ package cluster import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateFailureDomainCmdSuccess(t *testing.T) { diff --git a/pkg/ctl/cluster/delete_failure_domain.go b/pkg/ctl/cluster/delete_failure_domain.go index 4a24da17..e563390f 100644 --- a/pkg/ctl/cluster/delete_failure_domain.go +++ b/pkg/ctl/cluster/delete_failure_domain.go @@ -24,7 +24,8 @@ import ( func deleteFailureDomainCmd(vc *cmdutils.VerbCmd) { var desc pulsar.LongDescription - desc.CommandUsedFor = "This command is used for deleting the failure domain of the cluster " + desc.CommandUsedFor = "This command is used for deleting the failure domain " + + " of the cluster " desc.CommandPermission = "This command requires super-user permissions." var examples []pulsar.Example @@ -81,7 +82,8 @@ func doDeleteFailureDomain(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() err := admin.Clusters().DeleteFailureDomain(failureDomain) if err == nil { - vc.Command.Printf("Delete failure domain [%s] for cluster [%s] succeed\n", failureDomain.DomainName, failureDomain.ClusterName) + vc.Command.Printf("Delete failure domain [%s] for cluster [%s] succeed\n", + failureDomain.DomainName, failureDomain.ClusterName) } return err diff --git a/pkg/ctl/cluster/delete_failure_domain_test.go b/pkg/ctl/cluster/delete_failure_domain_test.go index 48a0a27f..05e79bdd 100644 --- a/pkg/ctl/cluster/delete_failure_domain_test.go +++ b/pkg/ctl/cluster/delete_failure_domain_test.go @@ -18,8 +18,9 @@ package cluster import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteFailureDomainCmd(t *testing.T) { diff --git a/pkg/ctl/cluster/delete_test.go b/pkg/ctl/cluster/delete_test.go index 23f2c321..c0c6d182 100644 --- a/pkg/ctl/cluster/delete_test.go +++ b/pkg/ctl/cluster/delete_test.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteClusterCmd(t *testing.T) { diff --git a/pkg/ctl/cluster/get.go b/pkg/ctl/cluster/get.go index 53eba623..d278181b 100644 --- a/pkg/ctl/cluster/get.go +++ b/pkg/ctl/cluster/get.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" - "errors" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" ) func getClusterDataCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/get_failure_domain.go b/pkg/ctl/cluster/get_failure_domain.go index 60d7de24..8ad9c97f 100644 --- a/pkg/ctl/cluster/get_failure_domain.go +++ b/pkg/ctl/cluster/get_failure_domain.go @@ -72,7 +72,7 @@ func doGetFailureDomain(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() resFailureDomain, err := admin.Clusters().GetFailureDomain(clusterName, domainName) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), resFailureDomain) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), resFailureDomain) } return err diff --git a/pkg/ctl/cluster/get_failure_domain_test.go b/pkg/ctl/cluster/get_failure_domain_test.go index 01b054f0..ac196924 100644 --- a/pkg/ctl/cluster/get_failure_domain_test.go +++ b/pkg/ctl/cluster/get_failure_domain_test.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestGetFailureDomainSuccess(t *testing.T) { @@ -33,7 +35,8 @@ func TestGetFailureDomainSuccess(t *testing.T) { _, _, _, err = TestClusterCommands(CreateClusterCmd, args) assert.Nil(t, err) - args = []string{"create-failure-domain", "-b", "failure-broker-A", "-b", "failure-broker-B", "standalone", "failure-domain"} + args = []string{"create-failure-domain", + "-b", "failure-broker-A", "-b", "failure-broker-B", "standalone", "failure-domain"} _, _, _, err = TestClusterCommands(createFailureDomainCmd, args) assert.Nil(t, err) diff --git a/pkg/ctl/cluster/get_peer_clusters.go b/pkg/ctl/cluster/get_peer_clusters.go index b375f700..9a79b6fd 100644 --- a/pkg/ctl/cluster/get_peer_clusters.go +++ b/pkg/ctl/cluster/get_peer_clusters.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" ) func getPeerClustersCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/get_peer_clusters_test.go b/pkg/ctl/cluster/get_peer_clusters_test.go index 28fc93f1..50525a97 100644 --- a/pkg/ctl/cluster/get_peer_clusters_test.go +++ b/pkg/ctl/cluster/get_peer_clusters_test.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetPeerClustersCmd(t *testing.T) { diff --git a/pkg/ctl/cluster/get_test.go b/pkg/ctl/cluster/get_test.go index b087f274..7298b64a 100644 --- a/pkg/ctl/cluster/get_test.go +++ b/pkg/ctl/cluster/get_test.go @@ -19,12 +19,13 @@ package cluster import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "regexp" "testing" -) + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" +) func TestGetClusterData(t *testing.T) { args := []string{"get", "standalone"} @@ -38,20 +39,11 @@ func TestGetClusterData(t *testing.T) { t.Error(err) } - pulsarUrl, err := regexp.Compile("^pulsar://[a-z-A-Z0-9]*:6650$") - if err != nil { - t.Error(err) - } - - res := pulsarUrl.MatchString(c.BrokerServiceURL) + pulsarURL := regexp.MustCompile("^pulsar://[a-z-A-Z0-9]*:6650$") + res := pulsarURL.MatchString(c.BrokerServiceURL) assert.True(t, res) - httpUrl, err := regexp.Compile("^http://[a-z-A-Z0-9]*:8080$") - if err != nil { - t.Error(err) - } - - res = httpUrl.MatchString(c.ServiceURL) + httpURL := regexp.MustCompile("^http://[a-z-A-Z0-9]*:8080$") + res = httpURL.MatchString(c.ServiceURL) assert.True(t, res) } - diff --git a/pkg/ctl/cluster/list.go b/pkg/ctl/cluster/list.go index 651e009d..4d1c4d20 100644 --- a/pkg/ctl/cluster/list.go +++ b/pkg/ctl/cluster/list.go @@ -18,8 +18,9 @@ package cluster import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" + + "github.com/olekukonko/tablewriter" ) func listClustersCmd(vc *cmdutils.VerbCmd) { @@ -42,13 +43,13 @@ func doListClusters(vc *cmdutils.VerbCmd) error { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { table := tablewriter.NewWriter(vc.Command.OutOrStdout()) - table.SetHeader([]string{ "Cluster Name" }) + table.SetHeader([]string{"Cluster Name"}) for _, c := range clusters { - table.Append([]string { c }) + table.Append([]string{c}) } table.Render() } return err -} \ No newline at end of file +} diff --git a/pkg/ctl/cluster/list_failure_domain.go b/pkg/ctl/cluster/list_failure_domain.go index a2269db0..3dce0db5 100644 --- a/pkg/ctl/cluster/list_failure_domain.go +++ b/pkg/ctl/cluster/list_failure_domain.go @@ -38,7 +38,7 @@ func listFailureDomainCmd(vc *cmdutils.VerbCmd) { var out []pulsar.Output successOut := pulsar.Output{ Desc: "output example", - Out: "{\n" + + Out: "{\n" + " \"failure-domain\": {\n" + " \"brokers\": [\n" + " \"failure-broker-A\",\n" + @@ -74,7 +74,7 @@ func doListFailureDomain(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() domainData, err := admin.Clusters().ListFailureDomains(clusterName) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), domainData) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), domainData) } return err } diff --git a/pkg/ctl/cluster/list_failure_domain_test.go b/pkg/ctl/cluster/list_failure_domain_test.go index d2a3043a..65b4b7b6 100644 --- a/pkg/ctl/cluster/list_failure_domain_test.go +++ b/pkg/ctl/cluster/list_failure_domain_test.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestListFailureDomainsCmd(t *testing.T) { diff --git a/pkg/ctl/cluster/test.go b/pkg/ctl/cluster/test.go index 74fbd8cf..551b48db 100644 --- a/pkg/ctl/cluster/test.go +++ b/pkg/ctl/cluster/test.go @@ -19,13 +19,16 @@ package cluster import ( "bytes" + "os" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - "os" ) -func TestClusterCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, nameErr, err error) { +func TestClusterCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, + execErr, nameErr, err error) { var execError error cmdutils.ExecErrorHandler = func(err error) { execError = err @@ -68,7 +71,7 @@ var ( basePath string ) -func TestTlsHelp(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, err error) { +func TestTLSHelp(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, err error) { var rootCmd = &cobra.Command{ Use: "pulsarctl [command]", Short: "a CLI for Apache Pulsar", diff --git a/pkg/ctl/cluster/tls_test.go b/pkg/ctl/cluster/tls_test.go index 7fd85fbe..2ebf1b50 100644 --- a/pkg/ctl/cluster/tls_test.go +++ b/pkg/ctl/cluster/tls_test.go @@ -20,28 +20,29 @@ package cluster import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestTLS(t *testing.T) { args := []string{"clusters", "add", "tls"} - _, err := TestTlsHelp(CreateClusterCmd, args) + _, err := TestTLSHelp(CreateClusterCmd, args) assert.Nil(t, err) args = []string{"clusters", "list"} - out, err := TestTlsHelp(listClustersCmd, args) + out, err := TestTLSHelp(listClustersCmd, args) assert.Nil(t, err) clusters := out.String() assert.True(t, strings.Contains(clusters, "tls")) args = []string{"clusters", "delete", "tls"} - _, err = TestTlsHelp(deleteClusterCmd, args) + _, err = TestTLSHelp(deleteClusterCmd, args) assert.Nil(t, err) args = []string{"clusters", "list"} - out, err = TestTlsHelp(listClustersCmd, args) + out, err = TestTLSHelp(listClustersCmd, args) assert.Nil(t, err) clusters = out.String() assert.False(t, strings.Contains(clusters, "tls")) diff --git a/pkg/ctl/cluster/update.go b/pkg/ctl/cluster/update.go index 9a7cd742..36e9be0d 100644 --- a/pkg/ctl/cluster/update.go +++ b/pkg/ctl/cluster/update.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func UpdateClusterCmd(vc *cmdutils.VerbCmd) { @@ -30,29 +31,29 @@ func UpdateClusterCmd(vc *cmdutils.VerbCmd) { var examples []pulsar.Example - updateUrl := pulsar.Example{ + updateURL := pulsar.Example{ Desc: "updating the web service url of the ", Command: "pulsarctl clusters update --url http://example:8080 ", } - examples = append(examples, updateUrl) + examples = append(examples, updateURL) - updateUrlTls := pulsar.Example{ + updateURLTLS := pulsar.Example{ Desc: "updating the tls secured web service url of the ", Command: "pulsarctl clusters update --url-tls https://example:8080 ", } - examples = append(examples, updateUrlTls) + examples = append(examples, updateURLTLS) - updateBrokerUrl := pulsar.Example{ + updateBrokerURL := pulsar.Example{ Desc: "updating the broker service url of the ", Command: "pulsarctl clusters update --broker-url pulsar://example:6650 ", } - examples = append(examples, updateBrokerUrl) + examples = append(examples, updateBrokerURL) - updateBrokerUrlTls := pulsar.Example{ + updateBrokerURLTLS := pulsar.Example{ Desc: "updating the tls secured web service url of the ", Command: "pulsarctl clusters update --broker-url-tls pulsar+ssl://example:6650 ", } - examples = append(examples, updateBrokerUrlTls) + examples = append(examples, updateBrokerURLTLS) updatePeerCluster := pulsar.Example{ Desc: "registered as a peer-cluster of the clusters", diff --git a/pkg/ctl/cluster/update_failure_domain.go b/pkg/ctl/cluster/update_failure_domain.go index c44c1b8b..613f1b57 100644 --- a/pkg/ctl/cluster/update_failure_domain.go +++ b/pkg/ctl/cluster/update_failure_domain.go @@ -18,10 +18,11 @@ package cluster import ( - "errors" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "github.com/spf13/pflag" ) func updateFailureDomainCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/update_failure_domain_test.go b/pkg/ctl/cluster/update_failure_domain_test.go index b743fcd0..96c1fb4f 100644 --- a/pkg/ctl/cluster/update_failure_domain_test.go +++ b/pkg/ctl/cluster/update_failure_domain_test.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestUpdateFailureDomain(t *testing.T) { @@ -32,10 +34,10 @@ func TestUpdateFailureDomain(t *testing.T) { assert.Nil(t, err) args = []string{"get-failure-domain", "standalone", "standalone-failure-domain"} - out, execErr, _, _ := TestClusterCommands(getFailureDomainCmd, args) + out, execErr, _, _ := TestClusterCommands(getFailureDomainCmd, args) assert.Nil(t, execErr) - var failureDomain pulsar.FailureDomainData + var failureDomain pulsar.FailureDomainData err = json.Unmarshal(out.Bytes(), &failureDomain) if err != nil { t.Fatal(err) @@ -43,12 +45,12 @@ func TestUpdateFailureDomain(t *testing.T) { assert.Equal(t, 1, len(failureDomain.BrokerList)) assert.Equal(t, "127.0.0.1:6650", failureDomain.BrokerList[0]) - args = []string{"update-failure-domain", "-b", "192.168.0.1:6650", "standalone", "standalone-failure-domain"} + args = []string{"update-failure-domain", "-b", "192.168.0.1:6650", "standalone", "standalone-failure-domain"} _, execErr, _, _ = TestClusterCommands(updateFailureDomainCmd, args) assert.Nil(t, execErr) args = []string{"get-failure-domain", "standalone", "standalone-failure-domain"} - out, execErr, _, _ = TestClusterCommands(getFailureDomainCmd, args) + out, execErr, _, _ = TestClusterCommands(getFailureDomainCmd, args) assert.Nil(t, execErr) err = json.Unmarshal(out.Bytes(), &failureDomain) @@ -65,7 +67,7 @@ func TestUpdateFailureDomainArgsError(t *testing.T) { assert.Equal(t, "broker list must be specified", execErr.Error()) } -func TestUpdateFailureDomainWithNonExistTopic(t *testing.T) { +func TestUpdateFailureDomainWithNonExistTopic(t *testing.T) { args := []string{"update-failure-domain", "-b", "192.168.0.1:6650", "non-exist-cluster", "failure-domain"} _, execErr, _, _ := TestClusterCommands(updateFailureDomainCmd, args) assert.NotNil(t, execErr) diff --git a/pkg/ctl/cluster/update_peer_clusters.go b/pkg/ctl/cluster/update_peer_clusters.go index a961c786..449f2036 100644 --- a/pkg/ctl/cluster/update_peer_clusters.go +++ b/pkg/ctl/cluster/update_peer_clusters.go @@ -18,9 +18,10 @@ package cluster import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func updatePeerClustersCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/cluster/update_peer_clusters_test.go b/pkg/ctl/cluster/update_peer_clusters_test.go index d2ab375f..066c475d 100644 --- a/pkg/ctl/cluster/update_peer_clusters_test.go +++ b/pkg/ctl/cluster/update_peer_clusters_test.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestUpdatePeerClusters(t *testing.T) { @@ -38,7 +40,7 @@ func TestUpdatePeerClusters(t *testing.T) { } args = []string{"get", "standalone"} - out, _, _, err := TestClusterCommands(getClusterDataCmd, args) + out, _, _, _ := TestClusterCommands(getClusterDataCmd, args) var clusterData pulsar.ClusterData err = json.Unmarshal(out.Bytes(), &clusterData) @@ -46,6 +48,6 @@ func TestUpdatePeerClusters(t *testing.T) { t.Fatal(err) } - peer_cluster := clusterData.PeerClusterNames[0] - assert.Equal(t, "test_peer_cluster", peer_cluster) + peerCluster := clusterData.PeerClusterNames[0] + assert.Equal(t, "test_peer_cluster", peerCluster) } diff --git a/pkg/ctl/cluster/update_test.go b/pkg/ctl/cluster/update_test.go index 733ee8cb..98ec5625 100644 --- a/pkg/ctl/cluster/update_test.go +++ b/pkg/ctl/cluster/update_test.go @@ -19,9 +19,11 @@ package cluster import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestUpdateCluster(t *testing.T) { @@ -42,7 +44,8 @@ func TestUpdateCluster(t *testing.T) { } args = []string{"get", "standalone"} - out, _, _, err := TestClusterCommands(getClusterDataCmd, args) + out, execErr, _, _ := TestClusterCommands(getClusterDataCmd, args) + assert.Nil(t, execErr) var data pulsar.ClusterData err = json.Unmarshal(out.Bytes(), &data) diff --git a/pkg/ctl/completion/completion.go b/pkg/ctl/completion/completion.go index 3fccc8c4..e3e3eb18 100644 --- a/pkg/ctl/completion/completion.go +++ b/pkg/ctl/completion/completion.go @@ -18,16 +18,17 @@ package completion import ( + "os" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "os" ) func Command(rootCmd *cobra.Command) *cobra.Command { var bashCompletionCmd = &cobra.Command{ - Use: "bash", - Short: "Generates bash completion scripts", - Long: `To load completion run + Use: "bash", + Short: "Generates bash completion scripts", + Long: `To load completion run . <(pulsarctl completion bash) @@ -46,9 +47,9 @@ source /dev/stdin <<<"$(pulsarctl completion bash)" } var zshCompletionCmd = &cobra.Command{ - Use: "zsh", - Short: "Generates zsh completion scripts", - Long: `To configure your zsh shell, run: + Use: "zsh", + Short: "Generates zsh completion scripts", + Long: `To configure your zsh shell, run: mkdir -p ~/.zsh/completion/ pulsarctl completion zsh > ~/.zsh/completion/_pulsarctl @@ -63,8 +64,8 @@ fpath=($fpath ~/.zsh/completion) } cmd := &cobra.Command{ - Use: "completion", - Short: "Generates shell completion scripts", + Use: "completion", + Short: "Generates shell completion scripts", Run: func(cmd *cobra.Command, args []string) { if err := cmd.Help(); err != nil { logger.Debug("ignoring error %q", err.Error()) diff --git a/pkg/ctl/functions/create.go b/pkg/ctl/functions/create.go index 7ed81cfe..58e99095 100644 --- a/pkg/ctl/functions/create.go +++ b/pkg/ctl/functions/create.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - `github.com/streamnative/pulsarctl/pkg/ctl/utils` + "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func createFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -419,10 +420,10 @@ func doCreateFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) erro return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) - if utils.IsPackageUrlSupported(funcData.Jar) { - err = admin.Functions().CreateFuncWithUrl(funcData.FuncConf, funcData.Jar) + if utils.IsPackageURLSupported(funcData.Jar) { + err = admin.Functions().CreateFuncWithURL(funcData.FuncConf, funcData.Jar) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { diff --git a/pkg/ctl/functions/create_test.go b/pkg/ctl/functions/create_test.go index 43f8f0cb..29bb91ed 100644 --- a/pkg/ctl/functions/create_test.go +++ b/pkg/ctl/functions/create_test.go @@ -18,9 +18,10 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateFunctions(t *testing.T) { @@ -69,7 +70,7 @@ func TestCreateFunctions(t *testing.T) { _, _, err = TestFunctionsCommands(createFunctionsCmd, argsWithConf) assert.Nil(t, err) - argsWithFileUrl := []string{"create", + argsWithFileURL := []string{"create", "--tenant", "public", "--namespace", "default", "--name", "test-functions-create-file", @@ -80,6 +81,6 @@ func TestCreateFunctions(t *testing.T) { "--processing-guarantees", "EFFECTIVELY_ONCE", } - _, _, err = TestFunctionsCommands(createFunctionsCmd, argsWithFileUrl) + _, _, err = TestFunctionsCommands(createFunctionsCmd, argsWithFileURL) assert.Nil(t, err) } diff --git a/pkg/ctl/functions/delete.go b/pkg/ctl/functions/delete.go index 70f1f978..1418c678 100644 --- a/pkg/ctl/functions/delete.go +++ b/pkg/ctl/functions/delete.go @@ -18,9 +18,10 @@ package functions import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func deleteFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -124,7 +125,7 @@ func doDeleteFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) erro vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) err = admin.Functions().DeleteFunction(funcData.Tenant, funcData.Namespace, funcData.FuncName) if err != nil { return err diff --git a/pkg/ctl/functions/delete_test.go b/pkg/ctl/functions/delete_test.go index 6b06f220..f56eefbc 100644 --- a/pkg/ctl/functions/delete_test.go +++ b/pkg/ctl/functions/delete_test.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/functions.go b/pkg/ctl/functions/functions.go index f9c7a384..e18ef5c2 100644 --- a/pkg/ctl/functions/functions.go +++ b/pkg/ctl/functions/functions.go @@ -18,9 +18,11 @@ package functions import ( - `errors` - "github.com/spf13/cobra" + "errors" + "github.com/streamnative/pulsarctl/pkg/cmdutils" + + "github.com/spf13/cobra" ) var checkPutStateArgs = func(args []string) error { diff --git a/pkg/ctl/functions/get.go b/pkg/ctl/functions/get.go index adccd07b..4289c4d3 100644 --- a/pkg/ctl/functions/get.go +++ b/pkg/ctl/functions/get.go @@ -18,9 +18,10 @@ package functions import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func getFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -129,12 +130,12 @@ func doGetFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) functionConfig, err := admin.Functions().GetFunction(funcData.Tenant, funcData.Namespace, funcData.FuncName) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionConfig) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionConfig) } return err diff --git a/pkg/ctl/functions/get_test.go b/pkg/ctl/functions/get_test.go index 2a7c94b5..3c4ce796 100644 --- a/pkg/ctl/functions/get_test.go +++ b/pkg/ctl/functions/get_test.go @@ -19,11 +19,13 @@ package functions import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestGetFunction(t *testing.T) { diff --git a/pkg/ctl/functions/list.go b/pkg/ctl/functions/list.go index b0090155..f23091ba 100644 --- a/pkg/ctl/functions/list.go +++ b/pkg/ctl/functions/list.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/olekukonko/tablewriter" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" + "github.com/spf13/pflag" ) func listFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -86,7 +87,7 @@ func listFunctionsCmd(vc *cmdutils.VerbCmd) { func doListFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error { processNamespaceCmd(funcData) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) functions, err := admin.Functions().GetFunctions(funcData.Tenant, funcData.Namespace) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) diff --git a/pkg/ctl/functions/list_test.go b/pkg/ctl/functions/list_test.go index b04fa60f..1e0100e1 100644 --- a/pkg/ctl/functions/list_test.go +++ b/pkg/ctl/functions/list_test.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestListFunctions(t *testing.T) { @@ -55,7 +56,7 @@ func TestListFunctions(t *testing.T) { } _, _, err = TestFunctionsCommands(deleteFunctionsCmd, deleteArgs) - assert.Nil(t, nil) + assert.Nil(t, err) listArgsAgain := []string{"list", "--tenant", "public", diff --git a/pkg/ctl/functions/putstate.go b/pkg/ctl/functions/putstate.go index 4655125d..4c07674f 100644 --- a/pkg/ctl/functions/putstate.go +++ b/pkg/ctl/functions/putstate.go @@ -18,12 +18,14 @@ package functions import ( - "github.com/pkg/errors" - "github.com/spf13/pflag" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - "github.com/streamnative/pulsarctl/pkg/pulsar" "io/ioutil" "strings" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "github.com/spf13/pflag" ) func putstateFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -138,22 +140,23 @@ func doPutStateFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) err vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) var state pulsar.FunctionState state.Key = vc.NameArgs[0] value := vc.NameArgs[1] - if value == "-" { + switch value { + case "-": state.StringValue = strings.Join(vc.NameArgs[2:], " ") - } else if value == "=" { + case "=": contents, err := ioutil.ReadFile(vc.NameArgs[2]) if err != nil { return err } state.ByteValue = contents - } else { + default: return errors.New("error input format") } diff --git a/pkg/ctl/functions/putstate_test.go b/pkg/ctl/functions/putstate_test.go index 6053dd25..3cadeda3 100644 --- a/pkg/ctl/functions/putstate_test.go +++ b/pkg/ctl/functions/putstate_test.go @@ -29,6 +29,7 @@ import ( "testing" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) diff --git a/pkg/ctl/functions/querystate.go b/pkg/ctl/functions/querystate.go index dc5b22d0..80174e92 100644 --- a/pkg/ctl/functions/querystate.go +++ b/pkg/ctl/functions/querystate.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "time" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "time" + + "github.com/spf13/pflag" ) func querystateFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -154,14 +156,15 @@ func doQueryStateFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) e vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) for { - functionState, err := admin.Functions().GetFunctionState(funcData.Tenant, funcData.Namespace, funcData.FuncName, funcData.Key) + functionState, err := admin.Functions().GetFunctionState( + funcData.Tenant, funcData.Namespace, funcData.FuncName, funcData.Key) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionState) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionState) } if funcData.Watch { diff --git a/pkg/ctl/functions/restart.go b/pkg/ctl/functions/restart.go index e570b602..83d0255f 100644 --- a/pkg/ctl/functions/restart.go +++ b/pkg/ctl/functions/restart.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func restartFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -136,7 +138,7 @@ func doRestartFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) err vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.InstanceID != "" { instanceID, err := strconv.Atoi(funcData.InstanceID) if err != nil { diff --git a/pkg/ctl/functions/restart_test.go b/pkg/ctl/functions/restart_test.go index 25f99fdc..284bb17e 100644 --- a/pkg/ctl/functions/restart_test.go +++ b/pkg/ctl/functions/restart_test.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestRestartFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/start.go b/pkg/ctl/functions/start.go index ceff2cb6..8be06651 100644 --- a/pkg/ctl/functions/start.go +++ b/pkg/ctl/functions/start.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func startFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -137,7 +139,7 @@ func doStartFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.InstanceID != "" { instanceID, err := strconv.Atoi(funcData.InstanceID) if err != nil { @@ -147,7 +149,8 @@ func doStartFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error if err != nil { return err } - vc.Command.Printf("Started instanceID[%s] of Pulsar Functions[%s] successfully ", funcData.InstanceID, funcData.FuncName) + vc.Command.Printf("Started instanceID[%s] of Pulsar Functions[%s] successfully ", + funcData.InstanceID, funcData.FuncName) } else { err = admin.Functions().StartFunction(funcData.Tenant, funcData.Namespace, funcData.FuncName) if err != nil { diff --git a/pkg/ctl/functions/start_test.go b/pkg/ctl/functions/start_test.go index 38100a48..ca99229d 100644 --- a/pkg/ctl/functions/start_test.go +++ b/pkg/ctl/functions/start_test.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestStartFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/stats.go b/pkg/ctl/functions/stats.go index 87045027..2aa0956f 100644 --- a/pkg/ctl/functions/stats.go +++ b/pkg/ctl/functions/stats.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func statsFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -177,24 +179,25 @@ func doStatsFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.InstanceID != "" { instanceID, err := strconv.Atoi(funcData.InstanceID) if err != nil { return err } - functionInstanceStatsData, err := admin.Functions().GetFunctionStatsWithInstanceID(funcData.Tenant, funcData.Namespace, funcData.FuncName, instanceID) + functionInstanceStatsData, err := admin.Functions().GetFunctionStatsWithInstanceID( + funcData.Tenant, funcData.Namespace, funcData.FuncName, instanceID) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionInstanceStatsData) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionInstanceStatsData) } else { functionStats, err := admin.Functions().GetFunctionStats(funcData.Tenant, funcData.Namespace, funcData.FuncName) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionStats) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionStats) } return err diff --git a/pkg/ctl/functions/stats_test.go b/pkg/ctl/functions/stats_test.go index 8642b850..f141f5ba 100644 --- a/pkg/ctl/functions/stats_test.go +++ b/pkg/ctl/functions/stats_test.go @@ -19,10 +19,12 @@ package functions import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestStatsFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/status.go b/pkg/ctl/functions/status.go index 89427806..7320596f 100644 --- a/pkg/ctl/functions/status.go +++ b/pkg/ctl/functions/status.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func statusFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -147,23 +149,24 @@ func doStatusFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.InstanceID != "" { instanceID, err := strconv.Atoi(funcData.InstanceID) if err != nil { return err } - functionInstanceStatusData, err := admin.Functions().GetFunctionStatusWithInstanceID(funcData.Tenant, funcData.Namespace, funcData.FuncName, instanceID) + functionInstanceStatusData, err := admin.Functions().GetFunctionStatusWithInstanceID( + funcData.Tenant, funcData.Namespace, funcData.FuncName, instanceID) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionInstanceStatusData) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionInstanceStatusData) } else { functionStatus, err := admin.Functions().GetFunctionStatus(funcData.Tenant, funcData.Namespace, funcData.FuncName) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), functionStatus) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), functionStatus) } return err diff --git a/pkg/ctl/functions/status_test.go b/pkg/ctl/functions/status_test.go index a5896ddd..698acf41 100644 --- a/pkg/ctl/functions/status_test.go +++ b/pkg/ctl/functions/status_test.go @@ -23,10 +23,12 @@ package functions import ( "bytes" "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestStatusFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/stop.go b/pkg/ctl/functions/stop.go index c6e6e1d7..d4fcea41 100644 --- a/pkg/ctl/functions/stop.go +++ b/pkg/ctl/functions/stop.go @@ -18,10 +18,12 @@ package functions import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func stopFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -136,7 +138,7 @@ func doStopFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) error vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.InstanceID != "" { instanceID, err := strconv.Atoi(funcData.InstanceID) if err != nil { diff --git a/pkg/ctl/functions/stop_test.go b/pkg/ctl/functions/stop_test.go index 908160fd..015e42db 100644 --- a/pkg/ctl/functions/stop_test.go +++ b/pkg/ctl/functions/stop_test.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestStopFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/test_help.go b/pkg/ctl/functions/test_help.go index 9d81d2a4..efe10b18 100644 --- a/pkg/ctl/functions/test_help.go +++ b/pkg/ctl/functions/test_help.go @@ -19,10 +19,12 @@ package functions import ( "bytes" + "os" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - "os" ) func TestFunctionsCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, err error) { @@ -59,7 +61,7 @@ func TestFunctionsCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) ( } var ( - flag bool + flag bool basePath string ) diff --git a/pkg/ctl/functions/trigger.go b/pkg/ctl/functions/trigger.go index 1533796e..f4d2b2d7 100644 --- a/pkg/ctl/functions/trigger.go +++ b/pkg/ctl/functions/trigger.go @@ -19,9 +19,11 @@ package functions import ( "errors" - "github.com/spf13/pflag" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func triggerFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -158,17 +160,19 @@ func doTriggerFunction(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) erro vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if funcData.TriggerValue == "" && funcData.TriggerFile == "" { return errors.New("either a trigger value or a trigger filepath needs to be specified") } if funcData.TriggerValue != "" && funcData.TriggerFile != "" { - return errors.New("either a triggerValue or a triggerFile needs to specified for the function, cannot specify both") + return errors.New("either a triggerValue or a triggerFile needs to specified for the" + + " function, cannot specify both") } - retval, err := admin.Functions().TriggerFunction(funcData.Tenant, funcData.Namespace, funcData.FuncName, funcData.Topic, funcData.TriggerValue, funcData.TriggerFile) + retval, err := admin.Functions().TriggerFunction(funcData.Tenant, funcData.Namespace, + funcData.FuncName, funcData.Topic, funcData.TriggerValue, funcData.TriggerFile) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { diff --git a/pkg/ctl/functions/trigger_test.go b/pkg/ctl/functions/trigger_test.go index 48c145fd..a2216cc9 100644 --- a/pkg/ctl/functions/trigger_test.go +++ b/pkg/ctl/functions/trigger_test.go @@ -23,10 +23,12 @@ package functions import ( "bytes" "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "testing" "time" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestTriggerFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/update.go b/pkg/ctl/functions/update.go index 13353fe1..b9eb0dca 100644 --- a/pkg/ctl/functions/update.go +++ b/pkg/ctl/functions/update.go @@ -18,10 +18,11 @@ package functions import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - `github.com/streamnative/pulsarctl/pkg/ctl/utils` + "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func updateFunctionsCmd(vc *cmdutils.VerbCmd) { @@ -361,17 +362,18 @@ func doUpdateFunctions(vc *cmdutils.VerbCmd, funcData *pulsar.FunctionData) erro return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) updateOptions := pulsar.NewUpdateOptions() updateOptions.UpdateAuthData = funcData.UpdateAuthData - if utils.IsPackageUrlSupported(funcData.Jar) { - err = admin.Functions().UpdateFunctionWithUrl(funcData.FuncConf, funcData.Jar, updateOptions) + if utils.IsPackageURLSupported(funcData.Jar) { + err = admin.Functions().UpdateFunctionWithURL(funcData.FuncConf, funcData.Jar, updateOptions) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - vc.Command.Printf("Updated instanceID[%s] of Pulsar Functions[%s] successfully ", funcData.InstanceID, funcData.FuncName) + vc.Command.Printf("Updated instanceID[%s] of Pulsar Functions[%s] successfully ", + funcData.InstanceID, funcData.FuncName) } } else { err = admin.Functions().UpdateFunction(funcData.FuncConf, funcData.UserCodeFile, updateOptions) diff --git a/pkg/ctl/functions/update_test.go b/pkg/ctl/functions/update_test.go index da416e60..3d26d171 100644 --- a/pkg/ctl/functions/update_test.go +++ b/pkg/ctl/functions/update_test.go @@ -19,11 +19,13 @@ package functions import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestUpdateFunctions(t *testing.T) { diff --git a/pkg/ctl/functions/util.go b/pkg/ctl/functions/util.go index 18df0ab7..4bef120b 100644 --- a/pkg/ctl/functions/util.go +++ b/pkg/ctl/functions/util.go @@ -18,358 +18,372 @@ package functions import ( - `encoding/json` - `fmt` - `github.com/pkg/errors` - `github.com/streamnative/pulsarctl/pkg/ctl/utils` - `github.com/streamnative/pulsarctl/pkg/pulsar` - `gopkg.in/yaml.v2` - `io/ioutil` - `os` - `strings` + "encoding/json" + "fmt" + "io/ioutil" + "os" + "strings" + + "github.com/streamnative/pulsarctl/pkg/ctl/utils" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "gopkg.in/yaml.v2" ) func parseFullyQualifiedFunctionName(fqfn string, functionConfig *pulsar.FunctionConfig) error { - args := strings.Split(fqfn, "/") - if len(args) != 3 { - return errors.New("fully qualified function names (FQFNs) must be of the form tenant/namespace/name") - } + args := strings.Split(fqfn, "/") + if len(args) != 3 { + return errors.New("fully qualified function names (FQFNs) must be of the form tenant/namespace/name") + } - functionConfig.Tenant = args[0] - functionConfig.Namespace = args[1] - functionConfig.Name = args[2] + functionConfig.Tenant = args[0] + functionConfig.Namespace = args[1] + functionConfig.Name = args[2] - return nil + return nil } func processArgs(funcData *pulsar.FunctionData) error { - // Initialize config builder either from a supplied YAML config file or from scratch - if funcData.FuncConf != nil { - // no-op - } else { - funcData.FuncConf = new(pulsar.FunctionConfig) - } - - if funcData.FunctionConfigFile != "" { - yamlFile, err := ioutil.ReadFile(funcData.FunctionConfigFile) - if err == nil { - err = yaml.Unmarshal(yamlFile, funcData.FuncConf) - if err != nil { - return fmt.Errorf("unmarshal yaml file error:%s", err.Error()) - } - } else if err != nil && !os.IsNotExist(err) { - return fmt.Errorf("load conf file failed, err:%s", err.Error()) - } - } - - if funcData.FQFN != "" { - err := parseFullyQualifiedFunctionName(funcData.FQFN, funcData.FuncConf) - if err != nil { - return err - } - } else { - if funcData.Tenant != "" { - funcData.FuncConf.Tenant = funcData.Tenant - } - if funcData.Namespace != "" { - funcData.FuncConf.Namespace = funcData.Namespace - } - if funcData.FuncName != "" { - funcData.FuncConf.Name = funcData.FuncName - } - } - - if funcData.Inputs != "" { - inputTopics := strings.Split(funcData.Inputs, ",") - funcData.FuncConf.Inputs = inputTopics - } - - if funcData.CustomSerDeInputs != "" { - customSerdeInputMap := make(map[string]string) - err := json.Unmarshal([]byte(funcData.CustomSerDeInputs), &customSerdeInputMap) - if err != nil { - return err - } - funcData.FuncConf.CustomSerdeInputs = customSerdeInputMap - } - - if funcData.ProcessingGuarantees != "" { - funcData.FuncConf.ProcessingGuarantees = funcData.ProcessingGuarantees - } - - if funcData.CustomSchemaInput != "" { - customSchemaInputMap := make(map[string]string) - err := json.Unmarshal([]byte(funcData.CustomSchemaInput), &customSchemaInputMap) - if err != nil { - return err - } - funcData.FuncConf.CustomSchemaInputs = customSchemaInputMap - } - - if funcData.TopicsPattern != "" { - funcData.FuncConf.TopicsPattern = &funcData.TopicsPattern - } - - if funcData.Output != "" { - funcData.FuncConf.Output = funcData.Output - } - - if funcData.LogTopic != "" { - funcData.FuncConf.LogTopic = funcData.LogTopic - } - - if funcData.ClassName != "" { - funcData.FuncConf.ClassName = funcData.ClassName - } - - if funcData.OutputSerDeClassName != "" { - funcData.FuncConf.OutputSerdeClassName = funcData.OutputSerDeClassName - } - - if funcData.SchemaType != "" { - funcData.FuncConf.OutputSchemaType = funcData.SchemaType - } - - if funcData.RetainOrdering { - funcData.FuncConf.RetainOrdering = funcData.RetainOrdering - } - - if funcData.SubsName != "" { - funcData.FuncConf.SubName = funcData.SubsName - } - - if funcData.UserConfig != "" { - userConfigMap := make(map[string]interface{}) - err := json.Unmarshal([]byte(funcData.UserConfig), &userConfigMap) - if err != nil { - return err - } - - funcData.FuncConf.UserConfig = userConfigMap - } - - if funcData.FuncConf.UserConfig == nil { - funcData.FuncConf.UserConfig = make(map[string]interface{}) - } - - if funcData.Parallelism != 0 { - funcData.FuncConf.Parallelism = funcData.Parallelism - } else { - funcData.FuncConf.Parallelism = 1 - } - - if funcData.CPU != 0 { - if funcData.FuncConf.Resources == nil { - funcData.FuncConf.Resources = pulsar.NewDefaultResources() - } - - funcData.FuncConf.Resources.CPU = funcData.CPU - } - - if funcData.Disk != 0 { - if funcData.FuncConf.Resources == nil { - funcData.FuncConf.Resources = pulsar.NewDefaultResources() - } - - funcData.FuncConf.Resources.Disk = funcData.Disk - } - - if funcData.RAM != 0 { - if funcData.FuncConf.Resources == nil { - funcData.FuncConf.Resources = pulsar.NewDefaultResources() - } - - funcData.FuncConf.Resources.Ram = funcData.RAM - } - - if funcData.TimeoutMs != 0 { - funcData.FuncConf.TimeoutMs = &funcData.TimeoutMs - } - - // window configs - if funcData.WindowLengthCount != 0 { - if funcData.FuncConf.WindowConfig == nil { - funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() - } - - funcData.FuncConf.WindowConfig.WindowLengthCount = funcData.WindowLengthCount - } - - if funcData.WindowLengthDurationMs != 0 { - if funcData.FuncConf.WindowConfig == nil { - funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() - } - - funcData.FuncConf.WindowConfig.WindowLengthDurationMs = funcData.WindowLengthDurationMs - } - - if funcData.SlidingIntervalCount != 0 { - if funcData.FuncConf.WindowConfig == nil { - funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() - } - - funcData.FuncConf.WindowConfig.SlidingIntervalCount = funcData.SlidingIntervalCount - } - - if funcData.SlidingIntervalDurationMs != 0 { - if funcData.FuncConf.WindowConfig == nil { - funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() - } - - funcData.FuncConf.WindowConfig.SlidingIntervalDurationMs = funcData.SlidingIntervalDurationMs - } - - if funcData.AutoAck { - funcData.FuncConf.AutoAck = funcData.AutoAck - } else { - funcData.FuncConf.AutoAck = true - } - - if funcData.MaxMessageRetries != 0 { - funcData.FuncConf.MaxMessageRetries = funcData.MaxMessageRetries - } - - if funcData.DeadLetterTopic != "" { - funcData.FuncConf.DeadLetterTopic = funcData.DeadLetterTopic - } - - if funcData.Jar != "" { - funcData.FuncConf.Jar = funcData.Jar - } - - if funcData.Py != "" { - funcData.FuncConf.Py = funcData.Py - } - - if funcData.Go != "" { - funcData.FuncConf.Go = funcData.Go - } - - if funcData.FuncConf.Jar != "" { - funcData.UserCodeFile = funcData.FuncConf.Jar - } else if funcData.FuncConf.Py != "" { - funcData.UserCodeFile = funcData.FuncConf.Py - } else if funcData.FuncConf.Go != "" { - funcData.UserCodeFile = funcData.FuncConf.Go - } - - return nil + // Initialize config builder either from a supplied YAML config file or from scratch + if funcData.FuncConf != nil { + // no-op + } else { + funcData.FuncConf = new(pulsar.FunctionConfig) + } + + if funcData.FunctionConfigFile != "" { + yamlFile, err := ioutil.ReadFile(funcData.FunctionConfigFile) + if err == nil { + err = yaml.Unmarshal(yamlFile, funcData.FuncConf) + if err != nil { + return fmt.Errorf("unmarshal yaml file error:%s", err.Error()) + } + } else if !os.IsNotExist(err) { + return fmt.Errorf("load conf file failed, err:%s", err.Error()) + } + } + + if funcData.FQFN != "" { + err := parseFullyQualifiedFunctionName(funcData.FQFN, funcData.FuncConf) + if err != nil { + return err + } + } else { + if funcData.Tenant != "" { + funcData.FuncConf.Tenant = funcData.Tenant + } + if funcData.Namespace != "" { + funcData.FuncConf.Namespace = funcData.Namespace + } + if funcData.FuncName != "" { + funcData.FuncConf.Name = funcData.FuncName + } + } + + if funcData.Inputs != "" { + inputTopics := strings.Split(funcData.Inputs, ",") + funcData.FuncConf.Inputs = inputTopics + } + + if funcData.CustomSerDeInputs != "" { + customSerdeInputMap := make(map[string]string) + err := json.Unmarshal([]byte(funcData.CustomSerDeInputs), &customSerdeInputMap) + if err != nil { + return err + } + funcData.FuncConf.CustomSerdeInputs = customSerdeInputMap + } + + if funcData.ProcessingGuarantees != "" { + funcData.FuncConf.ProcessingGuarantees = funcData.ProcessingGuarantees + } + + if funcData.CustomSchemaInput != "" { + customSchemaInputMap := make(map[string]string) + err := json.Unmarshal([]byte(funcData.CustomSchemaInput), &customSchemaInputMap) + if err != nil { + return err + } + funcData.FuncConf.CustomSchemaInputs = customSchemaInputMap + } + + if funcData.TopicsPattern != "" { + funcData.FuncConf.TopicsPattern = &funcData.TopicsPattern + } + + if funcData.Output != "" { + funcData.FuncConf.Output = funcData.Output + } + + if funcData.LogTopic != "" { + funcData.FuncConf.LogTopic = funcData.LogTopic + } + + if funcData.ClassName != "" { + funcData.FuncConf.ClassName = funcData.ClassName + } + + if funcData.OutputSerDeClassName != "" { + funcData.FuncConf.OutputSerdeClassName = funcData.OutputSerDeClassName + } + + if funcData.SchemaType != "" { + funcData.FuncConf.OutputSchemaType = funcData.SchemaType + } + + if funcData.RetainOrdering { + funcData.FuncConf.RetainOrdering = funcData.RetainOrdering + } + + if funcData.SubsName != "" { + funcData.FuncConf.SubName = funcData.SubsName + } + + if funcData.UserConfig != "" { + userConfigMap := make(map[string]interface{}) + err := json.Unmarshal([]byte(funcData.UserConfig), &userConfigMap) + if err != nil { + return err + } + + funcData.FuncConf.UserConfig = userConfigMap + } + + if funcData.FuncConf.UserConfig == nil { + funcData.FuncConf.UserConfig = make(map[string]interface{}) + } + + if funcData.Parallelism != 0 { + funcData.FuncConf.Parallelism = funcData.Parallelism + } else { + funcData.FuncConf.Parallelism = 1 + } + + if funcData.CPU != 0 { + if funcData.FuncConf.Resources == nil { + funcData.FuncConf.Resources = pulsar.NewDefaultResources() + } + + funcData.FuncConf.Resources.CPU = funcData.CPU + } + + if funcData.Disk != 0 { + if funcData.FuncConf.Resources == nil { + funcData.FuncConf.Resources = pulsar.NewDefaultResources() + } + + funcData.FuncConf.Resources.Disk = funcData.Disk + } + + if funcData.RAM != 0 { + if funcData.FuncConf.Resources == nil { + funcData.FuncConf.Resources = pulsar.NewDefaultResources() + } + + funcData.FuncConf.Resources.RAM = funcData.RAM + } + + if funcData.TimeoutMs != 0 { + funcData.FuncConf.TimeoutMs = &funcData.TimeoutMs + } + + // window configs + if funcData.WindowLengthCount != 0 { + if funcData.FuncConf.WindowConfig == nil { + funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() + } + + funcData.FuncConf.WindowConfig.WindowLengthCount = funcData.WindowLengthCount + } + + if funcData.WindowLengthDurationMs != 0 { + if funcData.FuncConf.WindowConfig == nil { + funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() + } + + funcData.FuncConf.WindowConfig.WindowLengthDurationMs = funcData.WindowLengthDurationMs + } + + if funcData.SlidingIntervalCount != 0 { + if funcData.FuncConf.WindowConfig == nil { + funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() + } + + funcData.FuncConf.WindowConfig.SlidingIntervalCount = funcData.SlidingIntervalCount + } + + if funcData.SlidingIntervalDurationMs != 0 { + if funcData.FuncConf.WindowConfig == nil { + funcData.FuncConf.WindowConfig = pulsar.NewDefaultWindowConfing() + } + + funcData.FuncConf.WindowConfig.SlidingIntervalDurationMs = funcData.SlidingIntervalDurationMs + } + + if funcData.AutoAck { + funcData.FuncConf.AutoAck = funcData.AutoAck + } else { + funcData.FuncConf.AutoAck = true + } + + if funcData.MaxMessageRetries != 0 { + funcData.FuncConf.MaxMessageRetries = funcData.MaxMessageRetries + } + + if funcData.DeadLetterTopic != "" { + funcData.FuncConf.DeadLetterTopic = funcData.DeadLetterTopic + } + + if funcData.Jar != "" { + funcData.FuncConf.Jar = funcData.Jar + } + + if funcData.Py != "" { + funcData.FuncConf.Py = funcData.Py + } + + if funcData.Go != "" { + funcData.FuncConf.Go = funcData.Go + } + + if funcData.FuncConf.Go != "" { + funcData.UserCodeFile = funcData.FuncConf.Go + } + + if funcData.FuncConf.Py != "" { + funcData.UserCodeFile = funcData.FuncConf.Py + } + + if funcData.FuncConf.Jar != "" { + funcData.UserCodeFile = funcData.FuncConf.Jar + } + + return nil } func validateFunctionConfigs(functionConfig *pulsar.FunctionConfig) error { - if functionConfig.Name == "" { - utils.InferMissingFunctionName(functionConfig) - } - - if functionConfig.Tenant == "" { - utils.InferMissingTenant(functionConfig) - } - - if functionConfig.Namespace == "" { - utils.InferMissingNamespace(functionConfig) - } - - if functionConfig.Jar != "" && functionConfig.Py != "" && functionConfig.Go != "" { - return errors.New("either a Java jar or a Python file or a Go executable binary needs to " + - "be specified for the function, Cannot specify both") - } - - if functionConfig.Jar == "" && functionConfig.Py == "" && functionConfig.Go == "" { - return errors.New("either a Java jar or a Python file or a Go executable binary needs to " + - "be specified for the function. Please specify one") - } - - if functionConfig.Jar != "" && !utils.IsPackageUrlSupported(functionConfig.Jar) && - !utils.IsFileExist(functionConfig.Jar) { - return errors.New("the specified jar file does not exist") - } - - if functionConfig.Py != "" && !utils.IsPackageUrlSupported(functionConfig.Py) && - !utils.IsFileExist(functionConfig.Py) { - return errors.New("the specified py file does not exist") - } - - if functionConfig.Go != "" && !utils.IsPackageUrlSupported(functionConfig.Go) && - !utils.IsFileExist(functionConfig.Go) { - return errors.New("the specified go file does not exist") - } - - if functionConfig.Jar != "" { - functionConfig.Runtime = pulsar.JavaRuntime - } else if functionConfig.Py != "" { - functionConfig.Runtime = pulsar.PythonRuntime - } else if functionConfig.Go != "" { - functionConfig.Runtime = pulsar.GoRuntime - } - - // go doesn't need className - if functionConfig.Runtime == pulsar.JavaRuntime || functionConfig.Runtime == pulsar.PythonRuntime { - if functionConfig.ClassName == "" { - return errors.New("no Function Classname specified") - } - } - - return nil + if functionConfig.Name == "" { + utils.InferMissingFunctionName(functionConfig) + } + + if functionConfig.Tenant == "" { + utils.InferMissingTenant(functionConfig) + } + + if functionConfig.Namespace == "" { + utils.InferMissingNamespace(functionConfig) + } + + if functionConfig.Jar != "" && functionConfig.Py != "" && functionConfig.Go != "" { + return errors.New("either a Java jar or a Python file or a Go executable binary needs to " + + "be specified for the function, Cannot specify both") + } + + if functionConfig.Jar == "" && functionConfig.Py == "" && functionConfig.Go == "" { + return errors.New("either a Java jar or a Python file or a Go executable binary needs to " + + "be specified for the function. Please specify one") + } + + if functionConfig.Jar != "" && !utils.IsPackageURLSupported(functionConfig.Jar) && + !utils.IsFileExist(functionConfig.Jar) { + return errors.New("the specified jar file does not exist") + } + + if functionConfig.Py != "" && !utils.IsPackageURLSupported(functionConfig.Py) && + !utils.IsFileExist(functionConfig.Py) { + return errors.New("the specified py file does not exist") + } + + if functionConfig.Go != "" && !utils.IsPackageURLSupported(functionConfig.Go) && + !utils.IsFileExist(functionConfig.Go) { + return errors.New("the specified go file does not exist") + } + + if functionConfig.Go != "" { + functionConfig.Runtime = pulsar.GoRuntime + } + + if functionConfig.Py != "" { + functionConfig.Runtime = pulsar.PythonRuntime + } + + if functionConfig.Jar != "" { + functionConfig.Runtime = pulsar.JavaRuntime + } + + // go doesn't need className + if functionConfig.Runtime == pulsar.JavaRuntime || functionConfig.Runtime == pulsar.PythonRuntime { + if functionConfig.ClassName == "" { + return errors.New("no Function Classname specified") + } + } + + return nil } func processBaseArguments(funcData *pulsar.FunctionData) error { - usesSetters := funcData.Tenant != "" || funcData.Namespace != "" || funcData.FuncName != "" - usesFqfn := funcData.FQFN != "" - - // return error if --fqfn is set alongside any combination of --tenant, --namespace, and --name - if usesFqfn && usesSetters { - return errors.New("you must specify either a Fully Qualified Function Name (FQFN) or tenant, namespace, and function name") - } else if usesFqfn { - // If the --fqfn flag is used, parse tenant, namespace, and name using that flag - fqfnParts := strings.Split(funcData.FQFN, "/") - if len(fqfnParts) != 3 { - return errors.New("fully qualified function names (FQFNs) must be of the form tenant/namespace/name") - } - - funcData.Tenant = fqfnParts[0] - funcData.Namespace = fqfnParts[1] - funcData.FuncName = fqfnParts[2] - } else { - if funcData.Tenant == "" { - funcData.Tenant = utils.PublicTenant - } - - if funcData.Namespace == "" { - funcData.Namespace = utils.DefaultNamespace - } - - if funcData.FuncName == "" { - return errors.New("you must specify a name for the function or a Fully Qualified Function Name (FQFN)") - } - } - - return nil + usesSetters := funcData.Tenant != "" || funcData.Namespace != "" || funcData.FuncName != "" + usesFqfn := funcData.FQFN != "" + + // return error if --fqfn is set alongside any combination of --tenant, --namespace, and --name + if usesFqfn && usesSetters { + return errors.New("you must specify either a Fully Qualified Function Name (FQFN)" + + " or tenant, namespace, and function name") + } + + if usesFqfn { + // If the --fqfn flag is used, parse tenant, namespace, and name using that flag + fqfnParts := strings.Split(funcData.FQFN, "/") + if len(fqfnParts) != 3 { + return errors.New("fully qualified function names (FQFNs) must be of the form" + + " tenant/namespace/name") + } + + funcData.Tenant = fqfnParts[0] + funcData.Namespace = fqfnParts[1] + funcData.FuncName = fqfnParts[2] + } else { + if funcData.Tenant == "" { + funcData.Tenant = utils.PublicTenant + } + + if funcData.Namespace == "" { + funcData.Namespace = utils.DefaultNamespace + } + + if funcData.FuncName == "" { + return errors.New("you must specify a name for the function or a Fully Qualified" + + " Function Name (FQFN)") + } + } + + return nil } func processNamespaceCmd(funcData *pulsar.FunctionData) { - if funcData.Tenant == "" || funcData.Namespace == "" { - funcData.Tenant = utils.PublicTenant - funcData.Namespace = utils.DefaultNamespace - } + if funcData.Tenant == "" || funcData.Namespace == "" { + funcData.Tenant = utils.PublicTenant + funcData.Namespace = utils.DefaultNamespace + } } func checkArgsForUpdate(functionConfig *pulsar.FunctionConfig) error { - if functionConfig.ClassName == "" { - if functionConfig.Name == "" { - return errors.New("function Name not provided") - } - } else if functionConfig.Name == "" { - utils.InferMissingFunctionName(functionConfig) - } - - if functionConfig.Tenant == "" { - utils.InferMissingTenant(functionConfig) - } - - if functionConfig.Namespace == "" { - utils.InferMissingNamespace(functionConfig) - } - - return nil + if functionConfig.ClassName == "" { + if functionConfig.Name == "" { + return errors.New("function Name not provided") + } + } else if functionConfig.Name == "" { + utils.InferMissingFunctionName(functionConfig) + } + + if functionConfig.Tenant == "" { + utils.InferMissingTenant(functionConfig) + } + + if functionConfig.Namespace == "" { + utils.InferMissingNamespace(functionConfig) + } + + return nil } - diff --git a/pkg/ctl/namespace/backlog_quota_test.go b/pkg/ctl/namespace/backlog_quota_test.go index 760fa9b7..15d754f2 100644 --- a/pkg/ctl/namespace/backlog_quota_test.go +++ b/pkg/ctl/namespace/backlog_quota_test.go @@ -19,9 +19,11 @@ package namespace import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestBacklogQuota(t *testing.T) { @@ -30,7 +32,8 @@ func TestBacklogQuota(t *testing.T) { assert.Nil(t, err) assert.Equal(t, createOut.String(), "Created public/test-backlog-namespace successfully") - args = []string{"set-backlog-quota", "public/test-backlog-namespace", "--limit", "2G", "--policy", "producer_request_hold"} + args = []string{"set-backlog-quota", "public/test-backlog-namespace", + "--limit", "2G", "--policy", "producer_request_hold"} setOut, execErr, _, _ := TestNamespaceCommands(setBacklogQuota, args) assert.Nil(t, execErr) assert.Equal(t, setOut.String(), "Set backlog quota successfully for [public/test-backlog-namespace]") @@ -55,7 +58,8 @@ func TestBacklogQuota(t *testing.T) { } func TestFailureBacklogQuota(t *testing.T) { - args := []string{"set-backlog-quota", "public/test-backlog-namespace", "--limit", "12M", "--policy", "no-support-policy"} + args := []string{"set-backlog-quota", "public/test-backlog-namespace", + "--limit", "12M", "--policy", "no-support-policy"} _, execErr, _, _ := TestNamespaceCommands(setBacklogQuota, args) assert.NotNil(t, execErr) assert.Equal(t, execErr.Error(), "invalid retention policy type: no-support-policy") diff --git a/pkg/ctl/namespace/clear_backlog.go b/pkg/ctl/namespace/clear_backlog.go index d83b9e1e..61c17f6e 100644 --- a/pkg/ctl/namespace/clear_backlog.go +++ b/pkg/ctl/namespace/clear_backlog.go @@ -22,41 +22,44 @@ import ( "fmt" "os" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func ClearBacklogCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for clearing backlog for all topics of a namespace." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - clear := Example{ + var examples []pulsar.Example + clear := pulsar.Example{ Desc: "Clear backlog for all topics of the namespace ", Command: "pulsarctl namespaces clear-backlog ", } - clearWithBundle := Example{ + clearWithBundle := pulsar.Example{ Desc: "Clear backlog for all topic of the namespace with a bundle range ", Command: "pulsarctl namespaces clear-backlog --bundle ", } - clearWithSubName := Example{ + clearWithSubName := pulsar.Example{ Desc: "Clear the specified subscription backlog for all topics of the " + "namespace ", Command: "pulsarctl namespaces clear-backlog --subscription ", } - desc.CommandExamples = append(examples, clear, clearWithBundle, clearWithSubName) + examples = append(examples, clear, clearWithBundle, clearWithSubName) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "Successfully clear backlog for all topics of the namespace ", } out = append(out, successOut, ArgError, NsNotExistError) - out = append(out, NsNotExistError) + out = append(out, NsErrors...) + desc.CommandOutput = out vc.SetDescription( "clear-backlog", @@ -85,19 +88,23 @@ func doClearBacklog(vc *cmdutils.VerbCmd, sName, bundle string, force bool) (err return nil } } - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } admin := cmdutils.NewPulsarClient() - if sName != "" && bundle != "" { - err = admin.Namespaces().ClearNamespaceBundleBacklogForSubscription(*ns, bundle, sName) - } else if sName != "" { - err = admin.Namespaces().ClearNamespaceBacklogForSubscription(*ns, sName) - } else if bundle != "" { + + switch { + case sName != "": + if bundle != "" { + err = admin.Namespaces().ClearNamespaceBundleBacklogForSubscription(*ns, bundle, sName) + } else { + err = admin.Namespaces().ClearNamespaceBacklogForSubscription(*ns, sName) + } + case bundle != "": err = admin.Namespaces().ClearNamespaceBundleBacklog(*ns, bundle) - } else { + default: err = admin.Namespaces().ClearNamespaceBacklog(*ns) } diff --git a/pkg/ctl/namespace/clusters_test.go b/pkg/ctl/namespace/clusters_test.go index d452b119..5df4f0a9 100644 --- a/pkg/ctl/namespace/clusters_test.go +++ b/pkg/ctl/namespace/clusters_test.go @@ -18,11 +18,13 @@ package namespace import ( + "strings" + "testing" + "github.com/streamnative/pulsarctl/pkg/ctl/cluster" "github.com/streamnative/pulsarctl/pkg/ctl/tenant" + "github.com/stretchr/testify/assert" - "strings" - "testing" ) func TestClusters(t *testing.T) { @@ -35,7 +37,8 @@ func TestClusters(t *testing.T) { _, _, _, err = cluster.TestClusterCommands(cluster.CreateClusterCmd, clusterArgs) assert.Nil(t, err) - updateTenantArgs := []string{"update", "--allowed-clusters", "test-replication-cluster", "--allowed-clusters", "standalone", "public"} + updateTenantArgs := []string{"update", "--allowed-clusters", "test-replication-cluster", + "--allowed-clusters", "standalone", "public"} _, execErr, _, err := tenant.TestTenantCommands(tenant.UpdateTenantCmd, updateTenantArgs) assert.Nil(t, err) assert.Nil(t, execErr) diff --git a/pkg/ctl/namespace/create.go b/pkg/ctl/namespace/create.go index e6411b15..44bf844e 100644 --- a/pkg/ctl/namespace/create.go +++ b/pkg/ctl/namespace/create.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/pkg/errors" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" + "github.com/spf13/pflag" ) const MaxBundles = int64(1) << 32 @@ -103,7 +104,7 @@ func doCreate(vc *cmdutils.VerbCmd, data pulsar.NamespacesData) error { admin := cmdutils.NewPulsarClient() if data.NumBundles < 0 || data.NumBundles > int(MaxBundles) { - return errors.New("Invalid number of bundles. Number of numBundles has to be in the range of (0, 2^32].") + return errors.New("invalid number of bundles. Number of numBundles has to be in the range of (0, 2^32]") } ns, err := pulsar.GetNamespaceName(tenantAndNamespace) diff --git a/pkg/ctl/namespace/create_test.go b/pkg/ctl/namespace/create_test.go index 6bd7f7c9..48788fb3 100644 --- a/pkg/ctl/namespace/create_test.go +++ b/pkg/ctl/namespace/create_test.go @@ -19,12 +19,14 @@ package namespace import ( "encoding/json" + "strings" + "testing" + "github.com/streamnative/pulsarctl/pkg/ctl/cluster" "github.com/streamnative/pulsarctl/pkg/ctl/tenant" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "strings" - "testing" ) func TestCreateNs(t *testing.T) { @@ -72,21 +74,22 @@ func TestCreateNsForNegativeBundles(t *testing.T) { args := []string{"create", "public/test-namespace-negative-bundles", "--bundles", "-1"} createOut, execErr, _, err := TestNamespaceCommands(createNs, args) assert.Nil(t, err) - exceptedErr := "Invalid number of bundles. Number of numBundles has to be in the range of (0, 2^32]." + exceptedErr := "invalid number of bundles. Number of numBundles has to be in the range of (0, 2^32]" t.Log(createOut.String()) assert.Equal(t, exceptedErr, execErr.Error()) } func TestCreateNsForPositiveBundles(t *testing.T) { args := []string{"create", "public/test-namespace-positive-bundles", "--bundles", "12"} - _, _, _, err := TestNamespaceCommands(createNs, args) + _, execErr, _, _ := TestNamespaceCommands(createNs, args) + assert.Nil(t, execErr) policiesArgs := []string{"policies", "public/test-namespace-positive-bundles"} out, execErr, _, _ := TestNamespaceCommands(getPolicies, policiesArgs) assert.Nil(t, execErr) var police pulsar.Policies - err = json.Unmarshal(out.Bytes(), &police) + err := json.Unmarshal(out.Bytes(), &police) assert.Nil(t, err) assert.Equal(t, 12, police.Bundles.NumBundles) } @@ -107,13 +110,15 @@ func TestCreateNsForCluster(t *testing.T) { _, _, _, err := cluster.TestClusterCommands(cluster.CreateClusterCmd, clusterArgs) assert.Nil(t, err) - updateTenantArgs := []string{"update", "--allowed-clusters", "test-cluster", "--allowed-clusters", "standalone", "public"} + updateTenantArgs := []string{"update", "--allowed-clusters", "test-cluster", + "--allowed-clusters", "standalone", "public"} _, execErr, _, err := tenant.TestTenantCommands(tenant.UpdateTenantCmd, updateTenantArgs) assert.Nil(t, err) assert.Nil(t, execErr) nsArgs := []string{"create", "public/test-namespace-cluster", "--clusters", "test-cluster"} - nsOut, _, _, err := TestNamespaceCommands(createNs, nsArgs) + nsOut, execErr, _, _ := TestNamespaceCommands(createNs, nsArgs) + assert.Nil(t, execErr) assert.Equal(t, "Created public/test-namespace-cluster successfully", nsOut.String()) policiesArgs := []string{"policies", "public/test-namespace-cluster"} diff --git a/pkg/ctl/namespace/delete_test.go b/pkg/ctl/namespace/delete_test.go index f98739e7..b9439a35 100644 --- a/pkg/ctl/namespace/delete_test.go +++ b/pkg/ctl/namespace/delete_test.go @@ -18,9 +18,10 @@ package namespace import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteNsCmd(t *testing.T) { diff --git a/pkg/ctl/namespace/dispatch_rate_test.go b/pkg/ctl/namespace/dispatch_rate_test.go index 79d9f1ef..1868e1fe 100644 --- a/pkg/ctl/namespace/dispatch_rate_test.go +++ b/pkg/ctl/namespace/dispatch_rate_test.go @@ -23,6 +23,7 @@ import ( "testing" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) @@ -53,7 +54,10 @@ func TestDispatchRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{-1, -1, 1}), + pulsar.DispatchRate{ + DispatchThrottlingRateInMsg: -1, + DispatchThrottlingRateInByte: -1, + RatePeriodInSecond: 1}), out.String()) args = []string{"get-dispatch-rate", ns} @@ -73,7 +77,11 @@ func TestDispatchRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{10, 10, 10}), + pulsar.DispatchRate{ + DispatchThrottlingRateInMsg: 10, + DispatchThrottlingRateInByte: 10, + RatePeriodInSecond: 10, + }), out.String()) args = []string{"get-dispatch-rate", ns} diff --git a/pkg/ctl/namespace/errors_ns.go b/pkg/ctl/namespace/errors_ns.go index 90ed671a..3ec66f56 100644 --- a/pkg/ctl/namespace/errors_ns.go +++ b/pkg/ctl/namespace/errors_ns.go @@ -17,19 +17,19 @@ package namespace -import . "github.com/streamnative/pulsarctl/pkg/pulsar" +import "github.com/streamnative/pulsarctl/pkg/pulsar" -var ArgError = Output{ +var ArgError = pulsar.Output{ Desc: "the namespace name is not specified", Out: "[✖] only one argument is allowed to be used as a name", } -var NsNotExistError = Output{ +var NsNotExistError = pulsar.Output{ Desc: "the specified namespace name does not exist", Out: "[✖] code: 404 reason: Namespace does not exist", } -var NsErrors = []Output{ +var NsErrors = []pulsar.Output{ { Desc: "the namespace name is not in the format of /", Out: "[✖] The complete name of namespace is invalid. complete name : ", diff --git a/pkg/ctl/namespace/get_anti_affinity_ns.go b/pkg/ctl/namespace/get_anti_affinity_ns.go index a7d78c29..5eef585f 100644 --- a/pkg/ctl/namespace/get_anti_affinity_ns.go +++ b/pkg/ctl/namespace/get_anti_affinity_ns.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func getAntiAffinityNamespaces(vc *cmdutils.VerbCmd) { @@ -40,7 +41,7 @@ func getAntiAffinityNamespaces(vc *cmdutils.VerbCmd) { var out []pulsar.Output successOut := pulsar.Output{ Desc: "normal output", - Out: "", + Out: "", } noNamespaceName := pulsar.Output{ @@ -102,7 +103,7 @@ func getAntiAffinityNamespaces(vc *cmdutils.VerbCmd) { } func doGetAntiAffinityNamespaces(vc *cmdutils.VerbCmd, data pulsar.NamespacesData) error { - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V1) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V1) strList, err := admin.Namespaces().GetAntiAffinityNamespaces(data.Tenant, data.Cluster, data.AntiAffinityGroup) if err == nil { vc.Command.Println(strList) diff --git a/pkg/ctl/namespace/get_backlog_quota.go b/pkg/ctl/namespace/get_backlog_quota.go index aa4a7206..c8268731 100644 --- a/pkg/ctl/namespace/get_backlog_quota.go +++ b/pkg/ctl/namespace/get_backlog_quota.go @@ -81,7 +81,7 @@ func doGetBacklogQuotas(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() backlogQuotasMap, err := admin.Namespaces().GetBacklogQuotaMap(ns) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), &backlogQuotasMap) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), &backlogQuotasMap) } return err } diff --git a/pkg/ctl/namespace/get_dispatch_rate.go b/pkg/ctl/namespace/get_dispatch_rate.go index a9569953..dff2e2bb 100644 --- a/pkg/ctl/namespace/get_dispatch_rate.go +++ b/pkg/ctl/namespace/get_dispatch_rate.go @@ -19,25 +19,30 @@ package namespace import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetDispatchRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the default message dispatch rate of a namespace." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the default message dispatch rate of namespace ", Command: "pulsarctl namespaces get-dispatch-rate ", Command: "pulsarctl namespaces get-replicator-dispatch-rate ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "{\n \"dispatchThrottlingRateInMsg\" : 0,\n \"dispatchThrottlingRateInByte\" : 0,\n \"ratePeriodInSecond\" : 1\n}", + Out: "{\n" + + " \"dispatchThrottlingRateInMsg\" : 0,\n" + + " \"dispatchThrottlingRateInByte\" : 0,\n" + + " \"ratePeriodInSecond\" : 1\n" + + "}", } out = append(out, successOut) out = append(out, NsErrors...) @@ -54,7 +59,7 @@ func GetReplicatorDispatchRateCmd(vc *cmdutils.VerbCmd) { } func doGetReplicatorDispatchRate(vc *cmdutils.VerbCmd) error { - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } @@ -62,7 +67,7 @@ func doGetReplicatorDispatchRate(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() rate, err := admin.Namespaces().GetReplicatorDispatchRate(*ns) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), rate) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), rate) } return err diff --git a/pkg/ctl/namespace/get_retention.go b/pkg/ctl/namespace/get_retention.go index bba45b0b..0b90bdf5 100644 --- a/pkg/ctl/namespace/get_retention.go +++ b/pkg/ctl/namespace/get_retention.go @@ -79,7 +79,7 @@ func doGetRetention(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() policy, err := admin.Namespaces().GetRetention(ns) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), &policy) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), &policy) } return err } diff --git a/pkg/ctl/namespace/get_subscribe_rate.go b/pkg/ctl/namespace/get_subscribe_rate.go index b041941e..7662a0fd 100644 --- a/pkg/ctl/namespace/get_subscribe_rate.go +++ b/pkg/ctl/namespace/get_subscribe_rate.go @@ -19,23 +19,24 @@ package namespace import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetSubscribeRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the default subscribe rate per consumer of a namespace." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the default subscribe rate per consumer of a namespace ", Command: "pulsarctl namespaces get-subscribe-rate ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "{\n \"subscribeThrottlingRatePerConsumer\" : 0,\n \"ratePeriodInSecond\" : 30\n}", } @@ -54,7 +55,7 @@ func GetSubscribeRateCmd(vc *cmdutils.VerbCmd) { } func doGetSubscribeRate(vc *cmdutils.VerbCmd) error { - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } @@ -62,7 +63,7 @@ func doGetSubscribeRate(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() rate, err := admin.Namespaces().GetSubscribeRate(*ns) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), rate) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), rate) } return err diff --git a/pkg/ctl/namespace/get_subscription_dispatch_rate.go b/pkg/ctl/namespace/get_subscription_dispatch_rate.go index 078a7f15..b2c5bdff 100644 --- a/pkg/ctl/namespace/get_subscription_dispatch_rate.go +++ b/pkg/ctl/namespace/get_subscription_dispatch_rate.go @@ -19,25 +19,30 @@ package namespace import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the default subscription message dispatch rate of a namespace." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the default subscription message dispatch rate of namespace ", Command: "pulsarctl namespaces get-subscription-dispatch-rate ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "{\n \"dispatchThrottlingRateInMsg\" : 0,\n \"dispatchThrottlingRateInByte\" : 0,\n \"ratePeriodInSecond\" : 1\n}", + Out: "{\n" + + " \"dispatchThrottlingRateInMsg\" : 0,\n" + + " \"dispatchThrottlingRateInByte\" : 0,\n" + + " \"ratePeriodInSecond\" : 1\n" + + "}", } out = append(out, successOut, ArgError, NsNotExistError) out = append(out, NsErrors...) @@ -54,7 +59,7 @@ func GetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { } func doGetSubscriptionDispatchRate(vc *cmdutils.VerbCmd) error { - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } @@ -62,7 +67,7 @@ func doGetSubscriptionDispatchRate(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() rate, err := admin.Namespaces().GetSubscriptionDispatchRate(*ns) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), rate) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), rate) } return err diff --git a/pkg/ctl/namespace/list.go b/pkg/ctl/namespace/list.go index c2a85c4c..db93a008 100644 --- a/pkg/ctl/namespace/list.go +++ b/pkg/ctl/namespace/list.go @@ -18,9 +18,10 @@ package namespace import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" ) func getNamespacesFromTenant(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/message_ttl_test.go b/pkg/ctl/namespace/message_ttl_test.go index 3f19a747..89c552a6 100644 --- a/pkg/ctl/namespace/message_ttl_test.go +++ b/pkg/ctl/namespace/message_ttl_test.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestMessageTTL(t *testing.T) { diff --git a/pkg/ctl/namespace/namespace.go b/pkg/ctl/namespace/namespace.go index cceca963..dbab24fb 100644 --- a/pkg/ctl/namespace/namespace.go +++ b/pkg/ctl/namespace/namespace.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" + + "github.com/spf13/cobra" ) func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { diff --git a/pkg/ctl/namespace/ns_anti_affinity_test.go b/pkg/ctl/namespace/ns_anti_affinity_test.go index a80b5ec6..59a5428e 100644 --- a/pkg/ctl/namespace/ns_anti_affinity_test.go +++ b/pkg/ctl/namespace/ns_anti_affinity_test.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestNsAntiAffinityGroup(t *testing.T) { diff --git a/pkg/ctl/namespace/persistence_test.go b/pkg/ctl/namespace/persistence_test.go index 7172071f..2ce640e7 100644 --- a/pkg/ctl/namespace/persistence_test.go +++ b/pkg/ctl/namespace/persistence_test.go @@ -19,9 +19,11 @@ package namespace import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestPersistence(t *testing.T) { diff --git a/pkg/ctl/namespace/policies.go b/pkg/ctl/namespace/policies.go index 68169f95..be55b2ff 100644 --- a/pkg/ctl/namespace/policies.go +++ b/pkg/ctl/namespace/policies.go @@ -80,7 +80,7 @@ func getPolicies(vc *cmdutils.VerbCmd) { " },\n" + " \"DeduplicationEnabled\": false,\n" + " \"LatencyStatsSampleRate\": null,\n" + - " \"MessageTtlInSeconds\": 0,\n" + + " \"MessageTTLInSeconds\": 0,\n" + " \"RetentionPolicies\": {\n" + " \"RetentionTimeInMinutes\": 0,\n" + " \"RetentionSizeInMB\": 0\n" + @@ -95,7 +95,7 @@ func getPolicies(vc *cmdutils.VerbCmd) { " \"CompactionThreshold\": 0,\n" + " \"OffloadThreshold\": 0,\n" + " \"OffloadDeletionLagMs\": 0,\n" + - " \"SchemaAutoUpdateCompatibilityStrategy\": \"\",\n" + + " \"SchemaCompatibilityStrategy\": \"\",\n" + " \"SchemaValidationEnforced\": false\n" + "}", } @@ -135,7 +135,7 @@ func doGetPolicies(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() policies, err := admin.Namespaces().GetPolicies(namespace) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), policies) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), policies) } return err } diff --git a/pkg/ctl/namespace/policies_test.go b/pkg/ctl/namespace/policies_test.go index ff3c4ee2..07e5535c 100644 --- a/pkg/ctl/namespace/policies_test.go +++ b/pkg/ctl/namespace/policies_test.go @@ -19,9 +19,11 @@ package namespace import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestPolicesCommand(t *testing.T) { diff --git a/pkg/ctl/namespace/replicator_dispatch_rate_test.go b/pkg/ctl/namespace/replicator_dispatch_rate_test.go index 1b052d8d..c15feac4 100644 --- a/pkg/ctl/namespace/replicator_dispatch_rate_test.go +++ b/pkg/ctl/namespace/replicator_dispatch_rate_test.go @@ -34,17 +34,22 @@ func TestReplicatorDispatchRateCmd(t *testing.T) { assert.Nil(t, execErr) args = []string{"get-replicator-dispatch-rate", ns} - out, execErr, _, _ := TestNamespaceCommands(GetReplicatorDispatchRateCmd, args) + _, execErr, _, _ = TestNamespaceCommands(GetReplicatorDispatchRateCmd, args) assert.NotNil(t, execErr) - assert.Equal(t, "code: 404 reason: replicator-Dispatch-rate is not configured for cluster standalone", execErr.Error()) + assert.Equal(t, "code: 404 reason: replicator-Dispatch-rate is not configured "+ + "for cluster standalone", execErr.Error()) args = []string{"set-replicator-dispatch-rate", ns} - out, execErr, _, _ = TestNamespaceCommands(SetReplicatorDispatchRateCmd, args) + out, execErr, _, _ := TestNamespaceCommands(SetReplicatorDispatchRateCmd, args) assert.Nil(t, execErr) assert.Equal(t, fmt.Sprintf("Success set the default replicator message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{-1, -1, 1}), + pulsar.DispatchRate{ + DispatchThrottlingRateInMsg: -1, + DispatchThrottlingRateInByte: -1, + RatePeriodInSecond: 1, + }), out.String()) args = []string{"get-replicator-dispatch-rate", ns} @@ -66,7 +71,11 @@ func TestReplicatorDispatchRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default replicator message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{10, 10, 10}), + pulsar.DispatchRate{ + DispatchThrottlingRateInMsg: 10, + DispatchThrottlingRateInByte: 10, + RatePeriodInSecond: 10, + }), out.String()) args = []string{"get-replicator-dispatch-rate", ns} diff --git a/pkg/ctl/namespace/retention_test.go b/pkg/ctl/namespace/retention_test.go index bd430739..998e1387 100644 --- a/pkg/ctl/namespace/retention_test.go +++ b/pkg/ctl/namespace/retention_test.go @@ -19,9 +19,11 @@ package namespace import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestRetention(t *testing.T) { diff --git a/pkg/ctl/namespace/set_backlog_quota.go b/pkg/ctl/namespace/set_backlog_quota.go index 0da2c89f..f27f613d 100644 --- a/pkg/ctl/namespace/set_backlog_quota.go +++ b/pkg/ctl/namespace/set_backlog_quota.go @@ -19,11 +19,13 @@ package namespace import ( "fmt" - "github.com/spf13/cobra" - "github.com/spf13/pflag" + "github.com/streamnative/pulsarctl/pkg/cmdutils" - `github.com/streamnative/pulsarctl/pkg/ctl/utils` + "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setBacklogQuota(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_deduplication_status.go b/pkg/ctl/namespace/set_deduplication_status.go index 52a2be91..7e2cecd9 100644 --- a/pkg/ctl/namespace/set_deduplication_status.go +++ b/pkg/ctl/namespace/set_deduplication_status.go @@ -18,9 +18,10 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func setDeduplication(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_deduplication_status_test.go b/pkg/ctl/namespace/set_deduplication_status_test.go index 2f3156cd..7a39e7aa 100644 --- a/pkg/ctl/namespace/set_deduplication_status_test.go +++ b/pkg/ctl/namespace/set_deduplication_status_test.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeduplicationStatus(t *testing.T) { diff --git a/pkg/ctl/namespace/set_dispatch_rate.go b/pkg/ctl/namespace/set_dispatch_rate.go index 759d8532..3c3156a4 100644 --- a/pkg/ctl/namespace/set_dispatch_rate.go +++ b/pkg/ctl/namespace/set_dispatch_rate.go @@ -18,37 +18,39 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func SetDispatchRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for setting the default message dispatch rate of a namespace." desc.CommandPermission = "This command requires super-user permissions." - var examples []Example - setByMsg := Example{ + var examples []pulsar.Example + setByMsg := pulsar.Example{ Desc: "Set the default message dispatch rate by message of the namespace to ", Command: "pulsarctl namespaces set-dispatch-rate --msg-rate ", } - setByByte := Example{ + setByByte := pulsar.Example{ Desc: "Set the default message dispatch rate by byte of the namespace to ", Command: "pulsarctl namespaces set-dispatch-rate --byte-rate ", } - setByTime := Example{ + setByTime := pulsar.Example{ Desc: "Set the default message dispatch rate by time of the namespace to ", Command: "pulsarctl namespaces set-dispatch-rate --period to ", + Out: "Success set the default message dispatch rate of the namespace to ", } out = append(out, successOut) out = append(out, NsErrors...) @@ -59,7 +61,7 @@ func SetDispatchRateCmd(vc *cmdutils.VerbCmd) { "Set the default message dispatch rate of a namespace", desc.ToString()) - var rate DispatchRate + var rate pulsar.DispatchRate vc.SetRunFuncWithNameArg(func() error { return doSetDispatchRate(vc, rate) @@ -75,8 +77,8 @@ func SetDispatchRateCmd(vc *cmdutils.VerbCmd) { }) } -func doSetDispatchRate(vc *cmdutils.VerbCmd, rate DispatchRate) error { - ns, err := GetNamespaceName(vc.NameArg) +func doSetDispatchRate(vc *cmdutils.VerbCmd, rate pulsar.DispatchRate) error { + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } @@ -84,7 +86,7 @@ func doSetDispatchRate(vc *cmdutils.VerbCmd, rate DispatchRate) error { admin := cmdutils.NewPulsarClient() err = admin.Namespaces().SetDispatchRate(*ns, rate) if err == nil { - vc.Command.Printf("Success set the default message dispatch rate " + + vc.Command.Printf("Success set the default message dispatch rate "+ "of the namespace %s to %+v", ns.String(), rate) } diff --git a/pkg/ctl/namespace/set_encryption_require.go b/pkg/ctl/namespace/set_encryption_require.go index a151ae46..ee7ae0e1 100644 --- a/pkg/ctl/namespace/set_encryption_require.go +++ b/pkg/ctl/namespace/set_encryption_require.go @@ -18,33 +18,35 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func SetEncryptionRequiredCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for enabling or disabling messages encryption for a namespace." desc.CommandPermission = "This command requires tenant admin and " + "a broker needs the read-write operations of the global zookeeper." - var examples []Example - enable := Example{ + var examples []pulsar.Example + enable := pulsar.Example{ Desc: "Enable messages encryption for the namespace ", Command: "pulsarctl namespaces messages-encryption ", } - disable := Example{ + disable := pulsar.Example{ Desc: "Disable messages encryption for the namespace ", Command: "pulsarct. namespaces messages-encryption --disable ", } - desc.CommandExamples = append(examples, enable, disable) + examples = append(examples, enable, disable) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "Enable/Disable message encryption for the namespace ", + Out: "Enable/Disable message encryption for the namespace ", } out = append(out, successOut, ArgError, NsNotExistError) out = append(out, NsErrors...) @@ -67,7 +69,7 @@ func SetEncryptionRequiredCmd(vc *cmdutils.VerbCmd) { } func doSetEncryptionRequired(vc *cmdutils.VerbCmd, disable bool) error { - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/namespace/set_message_ttl.go b/pkg/ctl/namespace/set_message_ttl.go index 4498b173..752d4546 100644 --- a/pkg/ctl/namespace/set_message_ttl.go +++ b/pkg/ctl/namespace/set_message_ttl.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setMessageTTL(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_ns_anti_affinity_group.go b/pkg/ctl/namespace/set_ns_anti_affinity_group.go index 0c6ee882..27923f50 100644 --- a/pkg/ctl/namespace/set_ns_anti_affinity_group.go +++ b/pkg/ctl/namespace/set_ns_anti_affinity_group.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setAntiAffinityGroup(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_persistence.go b/pkg/ctl/namespace/set_persistence.go index 26aa4525..a105a15e 100644 --- a/pkg/ctl/namespace/set_persistence.go +++ b/pkg/ctl/namespace/set_persistence.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setPersistence(vc *cmdutils.VerbCmd) { @@ -104,7 +105,7 @@ func setPersistence(vc *cmdutils.VerbCmd) { "ack-quorum-size", "a", 0, - "Number of acks (garanteed copies) to wait for each entry") + "Number of acks (guaranteed copies) to wait for each entry") flagSet.Float64VarP( &data.ManagedLedgerMaxMarkDeleteRate, diff --git a/pkg/ctl/namespace/set_replication_clusters.go b/pkg/ctl/namespace/set_replication_clusters.go index 1b1d6b21..be4b8035 100644 --- a/pkg/ctl/namespace/set_replication_clusters.go +++ b/pkg/ctl/namespace/set_replication_clusters.go @@ -18,11 +18,13 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" + "strings" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strings" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setReplicationClusters(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_replicator_dispatch_rate.go b/pkg/ctl/namespace/set_replicator_dispatch_rate.go index 9fc6006b..f39f47c2 100644 --- a/pkg/ctl/namespace/set_replicator_dispatch_rate.go +++ b/pkg/ctl/namespace/set_replicator_dispatch_rate.go @@ -18,40 +18,42 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func SetReplicatorDispatchRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for setting the default replicator message dispatch rate of a namespace." desc.CommandPermission = "This command requires super-user permissions." - var examples []Example - setByMsg := Example{ + var examples []pulsar.Example + setByMsg := pulsar.Example{ Desc: "Set the default replicator message dispatch rate by message of the namespace to ", Command: "pulsarctl namespaces set-replicator-dispatch-rate --msg-rate to ", Command: "pulsarctl namespaces set-replicator-dispatch-rate --byte-rate to ", Command: "pulsarctl namespaces set-replicator-dispatch-rate --period to ", + Out: "Success set the default replicator message dispatch rate of the namespace to ", } - notConfigured := Output{ + notConfigured := pulsar.Output{ Desc: "the replicator-dispatch-rate is not configured", Out: "[✖] code: 404 reason: replicator-Dispatch-rate is not configured for cluster standalone", } @@ -65,7 +67,7 @@ func SetReplicatorDispatchRateCmd(vc *cmdutils.VerbCmd) { "Set the default replicator message dispatch rate of a namespace", desc.ToString()) - var rate DispatchRate + var rate pulsar.DispatchRate vc.SetRunFuncWithNameArg(func() error { return doSetReplicatorDispatchRate(vc, rate) @@ -81,8 +83,8 @@ func SetReplicatorDispatchRateCmd(vc *cmdutils.VerbCmd) { }) } -func doSetReplicatorDispatchRate(vc *cmdutils.VerbCmd, rate DispatchRate) error { - ns, err := GetNamespaceName(vc.NameArg) +func doSetReplicatorDispatchRate(vc *cmdutils.VerbCmd, rate pulsar.DispatchRate) error { + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/namespace/set_retention.go b/pkg/ctl/namespace/set_retention.go index ff5e534e..cfc8efbe 100644 --- a/pkg/ctl/namespace/set_retention.go +++ b/pkg/ctl/namespace/set_retention.go @@ -18,11 +18,12 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - `github.com/streamnative/pulsarctl/pkg/ctl/utils` + "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func setRetention(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/set_subscribe_rate.go b/pkg/ctl/namespace/set_subscribe_rate.go index 91b141b7..2bf2862a 100644 --- a/pkg/ctl/namespace/set_subscribe_rate.go +++ b/pkg/ctl/namespace/set_subscribe_rate.go @@ -18,30 +18,32 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func SetSubscribeRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for setting the default subscribe rate per consumer of a namespace." desc.CommandPermission = "This command requires super-user permissions." - var examples []Example - setBySub := Example{ + var examples []pulsar.Example + setBySub := pulsar.Example{ Desc: "Set the default subscribe rate by subscribe of the namespace ", Command: "pulsarctl namespaces set-subscribe-rate --subscribe-rate ", } - setByTime := Example{ + setByTime := pulsar.Example{ Desc: "Set the default subscribe rate by time of the namespace ", Command: "pulsarctl namespaces set-subscribe-rate --period to ", } @@ -54,7 +56,7 @@ func SetSubscribeRateCmd(vc *cmdutils.VerbCmd) { "Set the default subscribe rate per consumer of a namespace", desc.ToString()) - var rate SubscribeRate + var rate pulsar.SubscribeRate vc.SetRunFuncWithNameArg(func() error { return doSetSubscribeRate(vc, rate) @@ -68,8 +70,8 @@ func SetSubscribeRateCmd(vc *cmdutils.VerbCmd) { }) } -func doSetSubscribeRate(vc *cmdutils.VerbCmd, rate SubscribeRate) error { - ns, err := GetNamespaceName(vc.NameArg) +func doSetSubscribeRate(vc *cmdutils.VerbCmd, rate pulsar.SubscribeRate) error { + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/namespace/set_subscription_auth_mode.go b/pkg/ctl/namespace/set_subscription_auth_mode.go index 59a2a6f7..6498699a 100644 --- a/pkg/ctl/namespace/set_subscription_auth_mode.go +++ b/pkg/ctl/namespace/set_subscription_auth_mode.go @@ -18,27 +18,29 @@ package namespace import ( + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/spf13/cobra" "github.com/spf13/pflag" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" ) func SetSubscriptionAuthModeCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for setting the default subscription auth mode of a namespace." desc.CommandPermission = "This command requires tenant admin and " + "a broker needs the read-write operations of the global zookeeper." - var examples []Example - set := Example{ + var examples []pulsar.Example + set := pulsar.Example{ Desc: "Set the default subscription auth mode of the namespace ", Command: "pulsarctl namespaces set-subscription-auth-mode --mode ", } - desc.CommandExamples = append(examples, set) + examples = append(examples, set) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "Successfully set the default subscription auth mode of namespace to ", } @@ -65,12 +67,12 @@ func SetSubscriptionAuthModeCmd(vc *cmdutils.VerbCmd) { } func doSetSubscriptionAuthMode(vc *cmdutils.VerbCmd, mode string) error { - ns, err := GetNamespaceName(vc.NameArg) + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } - m, err := ParseSubscriptionAuthMode(mode) + m, err := pulsar.ParseSubscriptionAuthMode(mode) if err != nil { return err } diff --git a/pkg/ctl/namespace/set_subscription_dispatch_rate.go b/pkg/ctl/namespace/set_subscription_dispatch_rate.go index 275d42bc..ec072e13 100644 --- a/pkg/ctl/namespace/set_subscription_dispatch_rate.go +++ b/pkg/ctl/namespace/set_subscription_dispatch_rate.go @@ -18,37 +18,39 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func SetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for setting the default subscription message dispatch rate of a namespace." desc.CommandPermission = "This command requires super-user permissions." - var examples []Example - setByMsg := Example{ + var examples []pulsar.Example + setByMsg := pulsar.Example{ Desc: "Set the default subscription message dispatch rate by message of the namespace to ", Command: "pulsarctl namespaces set-subscription-dispatch-rate --msg-rate to ", Command: "pulsarctl namespaces set-subscription-dispatch-rate --byte-rate to ", Command: "pulsarctl namespaces set-subscription-dispatch-rate --period to ", + Out: "Success set the default subscription message dispatch rate of the namespace to ", } out = append(out, successOut, ArgError, NsNotExistError) out = append(out, NsErrors...) @@ -59,7 +61,7 @@ func SetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { "Set the default subscription message dispatch rate of a namespace", desc.ToString()) - var rate DispatchRate + var rate pulsar.DispatchRate vc.SetRunFuncWithNameArg(func() error { return doSetSubscriptionDispatchRate(vc, rate) @@ -75,8 +77,8 @@ func SetSubscriptionDispatchRateCmd(vc *cmdutils.VerbCmd) { }) } -func doSetSubscriptionDispatchRate(vc *cmdutils.VerbCmd, rate DispatchRate) error { - ns, err := GetNamespaceName(vc.NameArg) +func doSetSubscriptionDispatchRate(vc *cmdutils.VerbCmd, rate pulsar.DispatchRate) error { + ns, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/namespace/split_bundle.go b/pkg/ctl/namespace/split_bundle.go index 160e6088..ba2b94d1 100644 --- a/pkg/ctl/namespace/split_bundle.go +++ b/pkg/ctl/namespace/split_bundle.go @@ -18,10 +18,11 @@ package namespace import ( - "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func splitBundle(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/splite_bundle_test.go b/pkg/ctl/namespace/splite_bundle_test.go index ce37f3f9..8de7f786 100644 --- a/pkg/ctl/namespace/splite_bundle_test.go +++ b/pkg/ctl/namespace/splite_bundle_test.go @@ -24,6 +24,7 @@ import ( "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" "github.com/streamnative/pulsarctl/pkg/ctl/topic/lookup" topic "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/stretchr/testify/assert" ) diff --git a/pkg/ctl/namespace/subscribe_rate_test.go b/pkg/ctl/namespace/subscribe_rate_test.go index 2b4909a3..6039fbdb 100644 --- a/pkg/ctl/namespace/subscribe_rate_test.go +++ b/pkg/ctl/namespace/subscribe_rate_test.go @@ -52,7 +52,10 @@ func TestSubscribeRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default subscribe rate "+ "of the namespace %s to %+v", ns, - pulsar.SubscribeRate{-1, 30}), + pulsar.SubscribeRate{ + SubscribeThrottlingRatePerConsumer: -1, + RatePeriodInSecond: 30, + }), out.String()) args = []string{"get-subscribe-rate", ns} @@ -70,7 +73,10 @@ func TestSubscribeRateCmd(t *testing.T) { assert.Nil(t, execErr) assert.Equal(t, fmt.Sprintf("Success set the default subscribe rate of the namespace %s to %+v", ns, - pulsar.SubscribeRate{10, 10}), + pulsar.SubscribeRate{ + SubscribeThrottlingRatePerConsumer: 10, + RatePeriodInSecond: 10, + }), out.String()) args = []string{"get-subscribe-rate", ns} diff --git a/pkg/ctl/namespace/subscription_dispatch_rate_test.go b/pkg/ctl/namespace/subscription_dispatch_rate_test.go index 8596aeda..894d97ab 100644 --- a/pkg/ctl/namespace/subscription_dispatch_rate_test.go +++ b/pkg/ctl/namespace/subscription_dispatch_rate_test.go @@ -53,7 +53,11 @@ func TestSubscriptionDispatchRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default subscription message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{-1, -1, 1}), + pulsar.DispatchRate{ + DispatchThrottlingRateInMsg: -1, + DispatchThrottlingRateInByte: -1, + RatePeriodInSecond: 1, + }), out.String()) args = []string{"get-subscription-dispatch-rate", ns} @@ -73,7 +77,11 @@ func TestSubscriptionDispatchRateCmd(t *testing.T) { assert.Equal(t, fmt.Sprintf("Success set the default subscription message dispatch rate "+ "of the namespace %s to %+v", ns, - pulsar.DispatchRate{10, 10, 10}), + pulsar.DispatchRate{ + DispatchThrottlingRateInByte: 10, + DispatchThrottlingRateInMsg: 10, + RatePeriodInSecond: 10, + }), out.String()) args = []string{"get-subscription-dispatch-rate", ns} diff --git a/pkg/ctl/namespace/test_help.go b/pkg/ctl/namespace/test_help.go index 805dd827..b5725dc4 100644 --- a/pkg/ctl/namespace/test_help.go +++ b/pkg/ctl/namespace/test_help.go @@ -19,12 +19,15 @@ package namespace import ( "bytes" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" ) -func TestNamespaceCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, nameErr, err error) { +func TestNamespaceCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, + execErr, nameErr, err error) { var execError error cmdutils.ExecErrorHandler = func(err error) { execError = err diff --git a/pkg/ctl/namespace/topics.go b/pkg/ctl/namespace/topics.go index 02a20cd4..7e4d5b1d 100644 --- a/pkg/ctl/namespace/topics.go +++ b/pkg/ctl/namespace/topics.go @@ -18,9 +18,10 @@ package namespace import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" ) func getTopics(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/topics_test.go b/pkg/ctl/namespace/topics_test.go index ae7ad012..cf4b743b 100644 --- a/pkg/ctl/namespace/topics_test.go +++ b/pkg/ctl/namespace/topics_test.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestListNsTopicsCmd(t *testing.T) { diff --git a/pkg/ctl/namespace/unload.go b/pkg/ctl/namespace/unload.go index 6f23c863..e29e0a9f 100644 --- a/pkg/ctl/namespace/unload.go +++ b/pkg/ctl/namespace/unload.go @@ -18,9 +18,10 @@ package namespace import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func unload(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/namespace/unsubscribe.go b/pkg/ctl/namespace/unsubscribe.go index 394cc79c..6a384353 100644 --- a/pkg/ctl/namespace/unsubscribe.go +++ b/pkg/ctl/namespace/unsubscribe.go @@ -18,37 +18,43 @@ package namespace import ( + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/pkg/errors" "github.com/spf13/pflag" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/pulsar" ) func UnsubscribeCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription - desc.CommandUsedFor = "This command is used for unsubscribing the specified subscription for all topics of a namespace." + var desc pulsar.LongDescription + desc.CommandUsedFor = "This command is used for unsubscribing the specified " + + "subscription for all topics of a namespace." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - unsub := Example{ - Desc: "Unsubscribe the specified subscription for all topic of the namespace ", + var examples []pulsar.Example + unsub := pulsar.Example{ + Desc: "Unsubscribe the specified subscription for " + + "all topic of the namespace ", Command: "pulsarctl namespaces unsubscribe ", } - unsubWithBundle := Example{ - Desc: "Unsubscribe the specified subscription for all topic of the namespace " + + unsubWithBundle := pulsar.Example{ + Desc: "Unsubscribe the specified subscription for " + + "all topic of the namespace " + "with bundle range ", Command: "pulsarctl namespaces unsubscribe --bundle ", } - desc.CommandExamples = append(examples, unsub, unsubWithBundle) + examples = append(examples, unsub, unsubWithBundle) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "Successfully unsubscribe the subscription for all topics of the namespace ", + Out: "Successfully unsubscribe the subscription for " + + "all topics of the namespace ", } - argsError := Output{ + argsError := pulsar.Output{ Desc: "the namespace name is not specified or the subscription name is not specified", Out: "[✖] need two arguments apply to the command", } @@ -79,7 +85,7 @@ func UnsubscribeCmd(vc *cmdutils.VerbCmd) { } func doUnsubscribe(vc *cmdutils.VerbCmd, bundle string) (err error) { - ns, err := GetNamespaceName(vc.NameArgs[0]) + ns, err := pulsar.GetNamespaceName(vc.NameArgs[0]) if err != nil { return err } diff --git a/pkg/ctl/namespace/upload_test.go b/pkg/ctl/namespace/upload_test.go index b5426990..31d7401d 100644 --- a/pkg/ctl/namespace/upload_test.go +++ b/pkg/ctl/namespace/upload_test.go @@ -18,8 +18,9 @@ package namespace import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestUpload(t *testing.T) { @@ -36,7 +37,8 @@ func TestUpload(t *testing.T) { argsWithBundle := []string{"unload", "public/test-unload-namespace", "--bundle", "0x40000000_0x80000000"} unloadOut, execErr, _, _ = TestNamespaceCommands(unload, argsWithBundle) assert.Nil(t, execErr) - assert.Equal(t, unloadOut.String(), "Unload namespace public/test-unload-namespace with bundle 0x40000000_0x80000000 successfully") + assert.Equal(t, unloadOut.String(), + "Unload namespace public/test-unload-namespace with bundle 0x40000000_0x80000000 successfully") // test invalid upper boundary for bundle argsWithInvalidBundle := []string{"unload", "public/test-unload-namespace", "--bundle", "0x00000000_0x60000000"} diff --git a/pkg/ctl/schemas/get.go b/pkg/ctl/schemas/get.go index 9d2c2c15..0047f37d 100644 --- a/pkg/ctl/schemas/get.go +++ b/pkg/ctl/schemas/get.go @@ -18,9 +18,10 @@ package schemas import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func getSchema(vc *cmdutils.VerbCmd) { @@ -121,7 +122,7 @@ func doGetSchema(vc *cmdutils.VerbCmd, schemaData *pulsar.SchemaData) error { } info, err := admin.Schemas().GetSchemaInfoByVersion(topic, schemaData.Version) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), info) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), info) } return err diff --git a/pkg/ctl/schemas/schema_test.go b/pkg/ctl/schemas/schema_test.go index 11b60a02..89139e0d 100644 --- a/pkg/ctl/schemas/schema_test.go +++ b/pkg/ctl/schemas/schema_test.go @@ -18,10 +18,11 @@ package schemas import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestSchema(t *testing.T) { @@ -32,7 +33,13 @@ func TestSchema(t *testing.T) { _, err = f.WriteString("{\n" + " \"type\": \"AVRO\",\n" + - " \"schema\": \"{\\\"type\\\":\\\"record\\\",\\\"name\\\":\\\"Test\\\",\\\"fields\\\":[{\\\"name\\\":\\\"id\\\",\\\"type\\\":[\\\"null\\\",\\\"int\\\"]},{\\\"name\\\":\\\"name\\\",\\\"type\\\":[\\\"null\\\",\\\"string\\\"]}]}\",\n" + + " \"schema\": " + + "\"{\\\"type\\\":\\\"record\\\"," + + "\\\"name\\\":\\\"Test\\\"," + + "\\\"fields\\\":[{" + + "\\\"name\\\":\\\"id\\\"," + + "\\\"type\\\":[\\\"null\\\",\\\"int\\\"]}," + + "{\\\"name\\\":\\\"name\\\",\\\"type\\\":[\\\"null\\\",\\\"string\\\"]}]}\",\n" + " \"properties\": {}\n" + "}\n") assert.Nil(t, err) diff --git a/pkg/ctl/schemas/schemas.go b/pkg/ctl/schemas/schemas.go index 670a020b..71a3687a 100644 --- a/pkg/ctl/schemas/schemas.go +++ b/pkg/ctl/schemas/schemas.go @@ -21,10 +21,12 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/spf13/cobra" + "io" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "io" + + "github.com/spf13/cobra" ) func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { @@ -45,24 +47,24 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { func PrintSchema(w io.Writer, schema *pulsar.SchemaInfoWithVersion) { name, err := json.MarshalIndent(schema.SchemaInfo.Name, "", " ") if err != nil { - fmt.Fprintf(w, "unexpected response type: %v\n", err) + _, _ = fmt.Fprintf(w, "unexpected response type: %v\n", err) return } schemaType, err := json.MarshalIndent(schema.SchemaInfo.Type, "", " ") if err != nil { - fmt.Fprintf(w, "unexpected response type: %v\n", err) + _, _ = fmt.Fprintf(w, "unexpected response type: %v\n", err) return } properties, err := json.MarshalIndent(schema.SchemaInfo.Properties, "", " ") if err != nil { - fmt.Fprintf(w, "unexpected response type: %v\n", err) + _, _ = fmt.Fprintf(w, "unexpected response type: %v\n", err) return } - s, err := prettyPrint(schema.SchemaInfo.Schema) - fmt.Fprintf(w, "{\n name: %s \n schema: %s\n type: %s \n properties: %s\n}", string(name), string(s), - string(schemaType), string(properties)) + s, _ := prettyPrint(schema.SchemaInfo.Schema) + _, _ = fmt.Fprintf(w, "{\n name: %s \n schema: %s\n type: %s \n properties: %s\n}", + string(name), string(s), string(schemaType), string(properties)) } func prettyPrint(b []byte) ([]byte, error) { diff --git a/pkg/ctl/schemas/test_help.go b/pkg/ctl/schemas/test_help.go index 50539cf7..f3f59445 100644 --- a/pkg/ctl/schemas/test_help.go +++ b/pkg/ctl/schemas/test_help.go @@ -19,9 +19,11 @@ package schemas import ( "bytes" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" ) func TestSchemasCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, err error) { diff --git a/pkg/ctl/schemas/upload.go b/pkg/ctl/schemas/upload.go index 6c8fbd70..e7996cf5 100644 --- a/pkg/ctl/schemas/upload.go +++ b/pkg/ctl/schemas/upload.go @@ -19,10 +19,12 @@ package schemas import ( "encoding/json" - "github.com/spf13/pflag" + "io/ioutil" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "io/ioutil" + + "github.com/spf13/pflag" ) func uploadSchema(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/sinks/create.go b/pkg/ctl/sinks/create.go index 4bc24dfd..40b923ce 100644 --- a/pkg/ctl/sinks/create.go +++ b/pkg/ctl/sinks/create.go @@ -18,10 +18,11 @@ package sinks import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func createSinksCmd(vc *cmdutils.VerbCmd) { @@ -278,8 +279,8 @@ func doCreateSinks(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) - if utils.IsPackageUrlSupported(sinkData.Archive) { + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) + if utils.IsPackageURLSupported(sinkData.Archive) { err = admin.Sinks().CreateSinkWithURL(sinkData.SinkConf, sinkData.Archive) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) diff --git a/pkg/ctl/sinks/create_test.go b/pkg/ctl/sinks/create_test.go index db063c8b..f6f1926b 100644 --- a/pkg/ctl/sinks/create_test.go +++ b/pkg/ctl/sinks/create_test.go @@ -19,10 +19,11 @@ package sinks import ( "fmt" - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateSinks(t *testing.T) { diff --git a/pkg/ctl/sinks/delete.go b/pkg/ctl/sinks/delete.go index 624d5577..382134db 100644 --- a/pkg/ctl/sinks/delete.go +++ b/pkg/ctl/sinks/delete.go @@ -18,9 +18,10 @@ package sinks import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func deleteSinksCmd(vc *cmdutils.VerbCmd) { @@ -96,7 +97,7 @@ func doDeleteSink(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) err = admin.Sinks().DeleteSink(sinkData.Tenant, sinkData.Namespace, sinkData.Name) if err != nil { return err diff --git a/pkg/ctl/sinks/delete_test.go b/pkg/ctl/sinks/delete_test.go index d2080f3f..ef2172b5 100644 --- a/pkg/ctl/sinks/delete_test.go +++ b/pkg/ctl/sinks/delete_test.go @@ -19,9 +19,10 @@ package sinks import ( "fmt" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteSinks(t *testing.T) { diff --git a/pkg/ctl/sinks/get.go b/pkg/ctl/sinks/get.go index 8dd3d14c..acd7bcee 100644 --- a/pkg/ctl/sinks/get.go +++ b/pkg/ctl/sinks/get.go @@ -18,9 +18,10 @@ package sinks import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func getSinksCmd(vc *cmdutils.VerbCmd) { @@ -116,12 +117,12 @@ func doGetSinks(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) sinkConfig, err := admin.Sinks().GetSink(sinkData.Tenant, sinkData.Namespace, sinkData.Name) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - cmdutils.PrintJson(vc.Command.OutOrStdout(), sinkConfig) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sinkConfig) } return err diff --git a/pkg/ctl/sinks/get_test.go b/pkg/ctl/sinks/get_test.go index 7f9195a3..1e5e122b 100644 --- a/pkg/ctl/sinks/get_test.go +++ b/pkg/ctl/sinks/get_test.go @@ -19,10 +19,11 @@ package sinks import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) func TestGetSink(t *testing.T) { @@ -50,7 +51,8 @@ func TestGetSink(t *testing.T) { "--name", "test-sink-get", } - out, _, err := TestSinksCommands(getSinksCmd, getArgs) + out, execErr, _ := TestSinksCommands(getSinksCmd, getArgs) + assert.Nil(t, execErr) var sinkConf pulsar.SinkConfig err = json.Unmarshal(out.Bytes(), &sinkConf) diff --git a/pkg/ctl/sinks/list.go b/pkg/ctl/sinks/list.go index aeece437..0a8c4d8c 100644 --- a/pkg/ctl/sinks/list.go +++ b/pkg/ctl/sinks/list.go @@ -18,10 +18,11 @@ package sinks import ( - "github.com/olekukonko/tablewriter" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" + "github.com/spf13/pflag" ) func listSinksCmd(vc *cmdutils.VerbCmd) { @@ -86,7 +87,7 @@ func listSinksCmd(vc *cmdutils.VerbCmd) { func doListSinks(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { processNamespaceCmd(sinkData) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) sinks, err := admin.Sinks().ListSinks(sinkData.Tenant, sinkData.Namespace) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) diff --git a/pkg/ctl/sinks/list_test.go b/pkg/ctl/sinks/list_test.go index ac5c146a..dbb96dec 100644 --- a/pkg/ctl/sinks/list_test.go +++ b/pkg/ctl/sinks/list_test.go @@ -18,9 +18,10 @@ package sinks import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestListSinks(t *testing.T) { diff --git a/pkg/ctl/sinks/restart.go b/pkg/ctl/sinks/restart.go index c365151c..506d8a3d 100644 --- a/pkg/ctl/sinks/restart.go +++ b/pkg/ctl/sinks/restart.go @@ -18,10 +18,12 @@ package sinks import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func restartSinksCmd(vc *cmdutils.VerbCmd) { @@ -113,7 +115,7 @@ func doRestartSink(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sinkData.InstanceID != "" { instanceID, err := strconv.Atoi(sinkData.InstanceID) if err != nil { diff --git a/pkg/ctl/sinks/restart_test.go b/pkg/ctl/sinks/restart_test.go index 3f311bef..c49f0647 100644 --- a/pkg/ctl/sinks/restart_test.go +++ b/pkg/ctl/sinks/restart_test.go @@ -18,9 +18,10 @@ package sinks import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestRestartSink(t *testing.T) { diff --git a/pkg/ctl/sinks/sinks.go b/pkg/ctl/sinks/sinks.go index fc32efbe..0caa7b28 100644 --- a/pkg/ctl/sinks/sinks.go +++ b/pkg/ctl/sinks/sinks.go @@ -18,8 +18,9 @@ package sinks import ( - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" + + "github.com/spf13/cobra" ) func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { diff --git a/pkg/ctl/sinks/start.go b/pkg/ctl/sinks/start.go index aac26948..1986024c 100644 --- a/pkg/ctl/sinks/start.go +++ b/pkg/ctl/sinks/start.go @@ -18,10 +18,12 @@ package sinks import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func startSinksCmd(vc *cmdutils.VerbCmd) { @@ -112,7 +114,7 @@ func doStartSink(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sinkData.InstanceID != "" { instanceID, err := strconv.Atoi(sinkData.InstanceID) if err != nil { diff --git a/pkg/ctl/sinks/start_test.go b/pkg/ctl/sinks/start_test.go index 4bb610f6..8ff55000 100644 --- a/pkg/ctl/sinks/start_test.go +++ b/pkg/ctl/sinks/start_test.go @@ -18,9 +18,10 @@ package sinks import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestStartAndStopSink(t *testing.T) { diff --git a/pkg/ctl/sinks/status.go b/pkg/ctl/sinks/status.go index 318e2ca0..30d62af0 100644 --- a/pkg/ctl/sinks/status.go +++ b/pkg/ctl/sinks/status.go @@ -18,10 +18,12 @@ package sinks import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func statusSinksCmd(vc *cmdutils.VerbCmd) { @@ -125,23 +127,24 @@ func doStatusSink(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sinkData.InstanceID != "" { instanceID, err := strconv.Atoi(sinkData.InstanceID) if err != nil { return err } - sinkInstanceStatusData, err := admin.Sinks().GetSinkStatusWithID(sinkData.Tenant, sinkData.Namespace, sinkData.Name, instanceID) + sinkInstanceStatusData, err := admin.Sinks().GetSinkStatusWithID( + sinkData.Tenant, sinkData.Namespace, sinkData.Name, instanceID) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), sinkInstanceStatusData) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sinkInstanceStatusData) } else { sinkStatus, err := admin.Sinks().GetSinkStatus(sinkData.Tenant, sinkData.Namespace, sinkData.Name) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), sinkStatus) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sinkStatus) } return err diff --git a/pkg/ctl/sinks/status_test.go b/pkg/ctl/sinks/status_test.go index 4a1a223a..b99059b6 100644 --- a/pkg/ctl/sinks/status_test.go +++ b/pkg/ctl/sinks/status_test.go @@ -20,10 +20,12 @@ package sinks import ( "bytes" "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestStatusSink(t *testing.T) { @@ -52,7 +54,7 @@ func TestStatusSink(t *testing.T) { "--name", "test-sink-status", } - outStatus := new(bytes.Buffer) + var outStatus *bytes.Buffer var status pulsar.SinkStatus for { diff --git a/pkg/ctl/sinks/stop.go b/pkg/ctl/sinks/stop.go index aad2332e..80bec87a 100644 --- a/pkg/ctl/sinks/stop.go +++ b/pkg/ctl/sinks/stop.go @@ -18,10 +18,12 @@ package sinks import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func stopSinksCmd(vc *cmdutils.VerbCmd) { @@ -112,7 +114,7 @@ func doStopSinks(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sinkData.InstanceID != "" { instanceID, err := strconv.Atoi(sinkData.InstanceID) if err != nil { diff --git a/pkg/ctl/sinks/test_help.go b/pkg/ctl/sinks/test_help.go index d4cd2d12..dc9625c0 100644 --- a/pkg/ctl/sinks/test_help.go +++ b/pkg/ctl/sinks/test_help.go @@ -19,10 +19,12 @@ package sinks import ( "bytes" + "os" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - "os" ) func TestSinksCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, err error) { diff --git a/pkg/ctl/sinks/update.go b/pkg/ctl/sinks/update.go index 6f3ed38f..fb650cba 100644 --- a/pkg/ctl/sinks/update.go +++ b/pkg/ctl/sinks/update.go @@ -18,10 +18,11 @@ package sinks import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func updateSinksCmd(vc *cmdutils.VerbCmd) { @@ -251,13 +252,13 @@ func doUpdateSink(vc *cmdutils.VerbCmd, sinkData *pulsar.SinkData) error { checkArgsForUpdate(sinkData.SinkConf) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) updateOptions := pulsar.NewUpdateOptions() updateOptions.UpdateAuthData = sinkData.UpdateAuthData - if utils.IsPackageUrlSupported(sinkData.Archive) { - err = admin.Sinks().UpdateSinkWithUrl(sinkData.SinkConf, sinkData.Archive, updateOptions) + if utils.IsPackageURLSupported(sinkData.Archive) { + err = admin.Sinks().UpdateSinkWithURL(sinkData.SinkConf, sinkData.Archive, updateOptions) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { diff --git a/pkg/ctl/sinks/update_test.go b/pkg/ctl/sinks/update_test.go index 31d0f3ed..4cc74412 100644 --- a/pkg/ctl/sinks/update_test.go +++ b/pkg/ctl/sinks/update_test.go @@ -20,10 +20,12 @@ package sinks import ( "encoding/json" "fmt" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestUpdateSink(t *testing.T) { diff --git a/pkg/ctl/sinks/util.go b/pkg/ctl/sinks/util.go index 20e118c7..377a24f3 100644 --- a/pkg/ctl/sinks/util.go +++ b/pkg/ctl/sinks/util.go @@ -20,14 +20,17 @@ package sinks import ( "encoding/json" "fmt" - "github.com/pkg/errors" + "io/ioutil" + "log" + "os" + "strings" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" "gopkg.in/yaml.v2" - "io/ioutil" - "os" - "strings" ) func processArguments(sinkData *pulsar.SinkData) error { @@ -45,7 +48,7 @@ func processArguments(sinkData *pulsar.SinkData) error { if err != nil { return fmt.Errorf("unmarshal yaml file error:%s", err.Error()) } - } else if err != nil && !os.IsNotExist(err) { + } else if !os.IsNotExist(err) { return fmt.Errorf("load conf file failed, err:%s", err.Error()) } } @@ -127,7 +130,6 @@ func processArguments(sinkData *pulsar.SinkData) error { if sinkData.SinkConf.Resources == nil { sinkData.SinkConf.Resources = pulsar.NewDefaultResources() } - fmt.Println("====cpu====") sinkData.SinkConf.Resources.CPU = sinkData.CPU } @@ -136,7 +138,6 @@ func processArguments(sinkData *pulsar.SinkData) error { if sinkData.SinkConf.Resources == nil { sinkData.SinkConf.Resources = pulsar.NewDefaultResources() } - fmt.Println("====disk====") sinkData.SinkConf.Resources.Disk = sinkData.Disk } @@ -145,9 +146,8 @@ func processArguments(sinkData *pulsar.SinkData) error { if sinkData.SinkConf.Resources == nil { sinkData.SinkConf.Resources = pulsar.NewDefaultResources() } - fmt.Println("====ram====") - sinkData.SinkConf.Resources.Ram = sinkData.RAM + sinkData.SinkConf.Resources.RAM = sinkData.RAM } if sinkData.SinkConfigString != "" { @@ -167,10 +167,10 @@ func processArguments(sinkData *pulsar.SinkData) error { func validateSinkType(sinkType string) string { availableSinks := make([]string, 0, 10) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) connectorDefinition, err := admin.Sinks().GetBuiltInSinks() if err != nil { - fmt.Errorf("get builtin sinks error: %s", err.Error()) + log.Printf("get builtin sinks error: %s\n", err.Error()) return "" } @@ -180,7 +180,7 @@ func validateSinkType(sinkType string) string { availableSinksString := strings.Join(availableSinks, " ") if !strings.Contains(availableSinksString, sinkType) { - fmt.Errorf("invalid sink type [%s] -- Available sinks are: %s", sinkType, availableSinks) + log.Printf("invalid sink type [%s] -- Available sinks are: %s", sinkType, availableSinks) return "" } @@ -206,7 +206,7 @@ func validateSinkConfigs(sinkConf *pulsar.SinkConfig) error { utils.InferMissingSinkeArguments(sinkConf) - if utils.IsPackageUrlSupported(sinkConf.Archive) && strings.HasPrefix(sinkConf.Archive, utils.BUILTIN) { + if utils.IsPackageURLSupported(sinkConf.Archive) && strings.HasPrefix(sinkConf.Archive, utils.BUILTIN) { if !utils.IsFileExist(sinkConf.Archive) { return fmt.Errorf("sink Archive %s does not exist", sinkConf.Archive) } diff --git a/pkg/ctl/sources/create.go b/pkg/ctl/sources/create.go index e83c0517..ae040e01 100644 --- a/pkg/ctl/sources/create.go +++ b/pkg/ctl/sources/create.go @@ -18,10 +18,11 @@ package sources import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func createSourcesCmd(vc *cmdutils.VerbCmd) { @@ -193,7 +194,8 @@ func createSourcesCmd(vc *cmdutils.VerbCmd) { "a", "", "The path to the NAR archive for the Source. It also supports url-path [http/https/file \n"+ - "(file protocol assumes that file already exists on worker host)] from which worker can download the package") + "(file protocol assumes that file already exists on worker host)] from which worker can download"+ + " the package") flagSet.StringVar( &sourceData.ClassName, @@ -249,13 +251,14 @@ func doCreateSources(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) - if utils.IsPackageUrlSupported(sourceData.Archive) { + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) + if utils.IsPackageURLSupported(sourceData.Archive) { err = admin.Sources().CreateSourceWithURL(sourceData.SourceConf, sourceData.Archive) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - vc.Command.Printf("Created instanceID[%s] of Pulsar Sources[%s] successfully", sourceData.InstanceID, sourceData.Name) + vc.Command.Printf("Created instanceID[%s] of Pulsar Sources[%s] successfully", + sourceData.InstanceID, sourceData.Name) } } else { err = admin.Sources().CreateSource(sourceData.SourceConf, sourceData.Archive) diff --git a/pkg/ctl/sources/create_test.go b/pkg/ctl/sources/create_test.go index 4da75244..dc9a48f6 100644 --- a/pkg/ctl/sources/create_test.go +++ b/pkg/ctl/sources/create_test.go @@ -18,10 +18,11 @@ package sources import ( - "github.com/stretchr/testify/assert" "os" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateSources(t *testing.T) { diff --git a/pkg/ctl/sources/delete.go b/pkg/ctl/sources/delete.go index 1c7a2121..baa525be 100644 --- a/pkg/ctl/sources/delete.go +++ b/pkg/ctl/sources/delete.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func deleteSourcesCmd(vc *cmdutils.VerbCmd) { @@ -96,7 +97,7 @@ func doDeleteSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) err = admin.Sources().DeleteSource(sourceData.Tenant, sourceData.Namespace, sourceData.Name) if err != nil { return err diff --git a/pkg/ctl/sources/delete_test.go b/pkg/ctl/sources/delete_test.go index 4e2e3a4a..5edda1bf 100644 --- a/pkg/ctl/sources/delete_test.go +++ b/pkg/ctl/sources/delete_test.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteSources(t *testing.T) { diff --git a/pkg/ctl/sources/get.go b/pkg/ctl/sources/get.go index c821a192..f62edf1d 100644 --- a/pkg/ctl/sources/get.go +++ b/pkg/ctl/sources/get.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func getSourcesCmd(vc *cmdutils.VerbCmd) { @@ -111,12 +112,12 @@ func doGetSources(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) sourceConfig, err := admin.Sources().GetSource(sourceData.Tenant, sourceData.Namespace, sourceData.Name) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - cmdutils.PrintJson(vc.Command.OutOrStdout(), sourceConfig) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sourceConfig) } return err diff --git a/pkg/ctl/sources/get_test.go b/pkg/ctl/sources/get_test.go index e38fb2b3..7afdffb3 100644 --- a/pkg/ctl/sources/get_test.go +++ b/pkg/ctl/sources/get_test.go @@ -19,9 +19,11 @@ package sources import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestGetSource(t *testing.T) { @@ -50,7 +52,7 @@ func TestGetSource(t *testing.T) { "--name", "test-source-get", } - out, _, err := TestSourcesCommands(getSourcesCmd, getArgs) + out, _, _ := TestSourcesCommands(getSourcesCmd, getArgs) var sourceConf pulsar.SourceConfig err = json.Unmarshal(out.Bytes(), &sourceConf) diff --git a/pkg/ctl/sources/list.go b/pkg/ctl/sources/list.go index ba85c53d..b360e58b 100644 --- a/pkg/ctl/sources/list.go +++ b/pkg/ctl/sources/list.go @@ -18,10 +18,11 @@ package sources import ( - "github.com/olekukonko/tablewriter" - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" + "github.com/spf13/pflag" ) func listSourcesCmd(vc *cmdutils.VerbCmd) { @@ -86,7 +87,7 @@ func listSourcesCmd(vc *cmdutils.VerbCmd) { func doListSources(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { processNamespaceCmd(sourceData) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) sources, err := admin.Sources().ListSources(sourceData.Tenant, sourceData.Namespace) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) diff --git a/pkg/ctl/sources/list_test.go b/pkg/ctl/sources/list_test.go index bebc482c..a323b72b 100644 --- a/pkg/ctl/sources/list_test.go +++ b/pkg/ctl/sources/list_test.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestListSources(t *testing.T) { diff --git a/pkg/ctl/sources/restart.go b/pkg/ctl/sources/restart.go index 79ee4b13..94e449d2 100644 --- a/pkg/ctl/sources/restart.go +++ b/pkg/ctl/sources/restart.go @@ -18,10 +18,12 @@ package sources import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func restartSourcesCmd(vc *cmdutils.VerbCmd) { @@ -113,7 +115,7 @@ func doRestartSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sourceData.InstanceID != "" { instanceID, err := strconv.Atoi(sourceData.InstanceID) if err != nil { @@ -123,7 +125,8 @@ func doRestartSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error if err != nil { return err } - vc.Command.Printf("Restarted instanceID[%s] of Pulsar Sources[%s] successfully", sourceData.InstanceID, sourceData.Name) + vc.Command.Printf("Restarted instanceID[%s] of Pulsar Sources[%s] successfully", + sourceData.InstanceID, sourceData.Name) } else { err = admin.Sources().RestartSource(sourceData.Tenant, sourceData.Namespace, sourceData.Name) if err != nil { diff --git a/pkg/ctl/sources/restart_test.go b/pkg/ctl/sources/restart_test.go index 54c25126..32db8880 100644 --- a/pkg/ctl/sources/restart_test.go +++ b/pkg/ctl/sources/restart_test.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestRestartSource(t *testing.T) { diff --git a/pkg/ctl/sources/sources.go b/pkg/ctl/sources/sources.go index 04407a93..0815bc42 100644 --- a/pkg/ctl/sources/sources.go +++ b/pkg/ctl/sources/sources.go @@ -18,8 +18,9 @@ package sources import ( - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" + + "github.com/spf13/cobra" ) func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { diff --git a/pkg/ctl/sources/start.go b/pkg/ctl/sources/start.go index 3ba21fdd..3226d2f8 100644 --- a/pkg/ctl/sources/start.go +++ b/pkg/ctl/sources/start.go @@ -18,10 +18,12 @@ package sources import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func startSourcesCmd(vc *cmdutils.VerbCmd) { @@ -112,7 +114,7 @@ func doStartSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sourceData.InstanceID != "" { instanceID, err := strconv.Atoi(sourceData.InstanceID) if err != nil { diff --git a/pkg/ctl/sources/start_test.go b/pkg/ctl/sources/start_test.go index d255d39b..844a5316 100644 --- a/pkg/ctl/sources/start_test.go +++ b/pkg/ctl/sources/start_test.go @@ -18,9 +18,10 @@ package sources import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestStartAndStopSource(t *testing.T) { diff --git a/pkg/ctl/sources/status.go b/pkg/ctl/sources/status.go index c70e0699..0be6ee80 100644 --- a/pkg/ctl/sources/status.go +++ b/pkg/ctl/sources/status.go @@ -18,10 +18,12 @@ package sources import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func statusSourcesCmd(vc *cmdutils.VerbCmd) { @@ -125,23 +127,24 @@ func doStatusSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sourceData.InstanceID != "" { instanceID, err := strconv.Atoi(sourceData.InstanceID) if err != nil { return err } - sourceInstanceStatusData, err := admin.Sources().GetSourceStatusWithID(sourceData.Tenant, sourceData.Namespace, sourceData.Name, instanceID) + sourceInstanceStatusData, err := admin.Sources().GetSourceStatusWithID( + sourceData.Tenant, sourceData.Namespace, sourceData.Name, instanceID) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), sourceInstanceStatusData) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sourceInstanceStatusData) } else { sourceStatus, err := admin.Sources().GetSourceStatus(sourceData.Tenant, sourceData.Namespace, sourceData.Name) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } - cmdutils.PrintJson(vc.Command.OutOrStdout(), sourceStatus) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), sourceStatus) } return err diff --git a/pkg/ctl/sources/status_test.go b/pkg/ctl/sources/status_test.go index ffb74845..5518b455 100644 --- a/pkg/ctl/sources/status_test.go +++ b/pkg/ctl/sources/status_test.go @@ -23,10 +23,12 @@ package sources import ( "bytes" "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestStatusSource(t *testing.T) { diff --git a/pkg/ctl/sources/stop.go b/pkg/ctl/sources/stop.go index e4132160..8f8799eb 100644 --- a/pkg/ctl/sources/stop.go +++ b/pkg/ctl/sources/stop.go @@ -18,10 +18,12 @@ package sources import ( - "github.com/spf13/pflag" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/spf13/pflag" ) func stopSourcesCmd(vc *cmdutils.VerbCmd) { @@ -112,7 +114,7 @@ func doStopSources(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { vc.Command.Help() return err } - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) if sourceData.InstanceID != "" { instanceID, err := strconv.Atoi(sourceData.InstanceID) if err != nil { diff --git a/pkg/ctl/sources/test_help.go b/pkg/ctl/sources/test_help.go index ff9de4e1..d8e2f123 100644 --- a/pkg/ctl/sources/test_help.go +++ b/pkg/ctl/sources/test_help.go @@ -19,10 +19,12 @@ package sources import ( "bytes" + "os" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - "os" ) func TestSourcesCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, err error) { diff --git a/pkg/ctl/sources/update.go b/pkg/ctl/sources/update.go index f3d2997f..1d49d28f 100644 --- a/pkg/ctl/sources/update.go +++ b/pkg/ctl/sources/update.go @@ -18,10 +18,11 @@ package sources import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func updateSourcesCmd(vc *cmdutils.VerbCmd) { @@ -73,7 +74,8 @@ func updateSourcesCmd(vc *cmdutils.VerbCmd) { "\t# Other source parameters ", } - examples = append(examples, update, updateWithSourceConfig, updateWithResource, updateWithParallelism, updateWithSchema) + examples = append(examples, update, updateWithSourceConfig, + updateWithResource, updateWithParallelism, updateWithSchema) desc.CommandExamples = examples var out []pulsar.Output @@ -167,7 +169,8 @@ func updateSourcesCmd(vc *cmdutils.VerbCmd) { "a", "", "The path to the NAR archive for the Source. It also supports url-path [http/https/file \n"+ - "(file protocol assumes that file already exists on worker host)] from which worker can download the package") + "(file protocol assumes that file already exists on worker host)] from which worker can"+ + " download the package") flagSet.StringVar( &sourceData.ClassName, @@ -219,17 +222,18 @@ func doUpdateSource(vc *cmdutils.VerbCmd, sourceData *pulsar.SourceData) error { checkArgsForUpdate(sourceData.SourceConf) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) updateOptions := pulsar.NewUpdateOptions() updateOptions.UpdateAuthData = sourceData.UpdateAuthData - if utils.IsPackageUrlSupported(sourceData.Archive) { - err = admin.Sources().UpdateSourceWithUrl(sourceData.SourceConf, sourceData.Archive, updateOptions) + if utils.IsPackageURLSupported(sourceData.Archive) { + err = admin.Sources().UpdateSourceWithURL(sourceData.SourceConf, sourceData.Archive, updateOptions) if err != nil { cmdutils.PrintError(vc.Command.OutOrStderr(), err) } else { - vc.Command.Printf("Updated instanceID[%s] of Pulsar Source[%s] successfully ", sourceData.InstanceID, sourceData.Name) + vc.Command.Printf("Updated instanceID[%s] of Pulsar Source[%s] successfully ", + sourceData.InstanceID, sourceData.Name) } } else { err = admin.Sources().UpdateSource(sourceData.SourceConf, sourceData.Archive, updateOptions) diff --git a/pkg/ctl/sources/update_test.go b/pkg/ctl/sources/update_test.go index 45d9c1ed..fe382cf4 100644 --- a/pkg/ctl/sources/update_test.go +++ b/pkg/ctl/sources/update_test.go @@ -19,10 +19,12 @@ package sources import ( "encoding/json" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestUpdateSource(t *testing.T) { diff --git a/pkg/ctl/sources/util.go b/pkg/ctl/sources/util.go index 7e371ec0..74052a22 100644 --- a/pkg/ctl/sources/util.go +++ b/pkg/ctl/sources/util.go @@ -20,14 +20,17 @@ package sources import ( "encoding/json" "fmt" - "github.com/pkg/errors" + "io/ioutil" + "log" + "os" + "strings" + "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/utils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" "gopkg.in/yaml.v2" - "io/ioutil" - "os" - "strings" ) func processArguments(sourceData *pulsar.SourceData) error { @@ -45,7 +48,7 @@ func processArguments(sourceData *pulsar.SourceData) error { if err != nil { return fmt.Errorf("unmarshal yaml file error:%s", err.Error()) } - } else if err != nil && !os.IsNotExist(err) { + } else if !os.IsNotExist(err) { return fmt.Errorf("load conf file failed, err:%s", err.Error()) } } @@ -121,7 +124,7 @@ func processArguments(sourceData *pulsar.SourceData) error { sourceData.SourceConf.Resources = pulsar.NewDefaultResources() } - sourceData.SourceConf.Resources.Ram = sourceData.RAM + sourceData.SourceConf.Resources.RAM = sourceData.RAM } if sourceData.SourceConfigString != "" { @@ -133,10 +136,10 @@ func processArguments(sourceData *pulsar.SourceData) error { func validateSourceType(sourceType string) string { availableSources := make([]string, 0, 10) - admin := cmdutils.NewPulsarClientWithApiVersion(pulsar.V3) + admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V3) connectorDefinition, err := admin.Sources().GetBuiltInSources() if err != nil { - fmt.Errorf("get builtin sources error: %s", err.Error()) + log.Printf("get builtin sources error: %s", err.Error()) return "" } @@ -146,7 +149,7 @@ func validateSourceType(sourceType string) string { availableSourcesString := strings.Join(availableSources, " ") if !strings.Contains(availableSourcesString, sourceType) { - fmt.Errorf("invalid source type [%s] -- Available sources are: %s", sourceType, availableSources) + log.Printf("invalid source type [%s] -- Available sources are: %s", sourceType, availableSources) return "" } @@ -172,7 +175,7 @@ func validateSourceConfigs(sourceConfig *pulsar.SourceConfig) error { utils.InferMissingSourceArguments(sourceConfig) - if utils.IsPackageUrlSupported(sourceConfig.Archive) && strings.HasPrefix(sourceConfig.Archive, utils.BUILTIN) { + if utils.IsPackageURLSupported(sourceConfig.Archive) && strings.HasPrefix(sourceConfig.Archive, utils.BUILTIN) { if !utils.IsFileExist(sourceConfig.Archive) { return fmt.Errorf("source Archive %s does not exist", sourceConfig.Archive) } diff --git a/pkg/ctl/tenant/create.go b/pkg/ctl/tenant/create.go index cce0af4e..94b8b5fc 100644 --- a/pkg/ctl/tenant/create.go +++ b/pkg/ctl/tenant/create.go @@ -18,19 +18,20 @@ package tenant import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) -func createTenantCmd(vc *cmdutils.VerbCmd) { +func createTenantCmd(vc *cmdutils.VerbCmd) { var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for creating a new tenant." desc.CommandPermission = "This command requires super-user permissions." var examples []pulsar.Example create := pulsar.Example{ - Desc: "create a tenant named ", + Desc: "create a tenant named ", Command: "pulsarctl tenants create ", } examples = append(examples, create) @@ -39,7 +40,7 @@ func createTenantCmd(vc *cmdutils.VerbCmd) { var out []pulsar.Output successOut := pulsar.Output{ Desc: "normal output", - Out: "Create tenant successfully", + Out: "Create tenant successfully", } out = append(out, successOut) out = append(out, tenantNameArgsError, tenantAlreadyExistError) diff --git a/pkg/ctl/tenant/create_test.go b/pkg/ctl/tenant/create_test.go index ffea5fd7..58b9360e 100644 --- a/pkg/ctl/tenant/create_test.go +++ b/pkg/ctl/tenant/create_test.go @@ -18,9 +18,10 @@ package tenant import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestCreateTenantCmd(t *testing.T) { @@ -41,7 +42,7 @@ func TestCreateTenantArgsError(t *testing.T) { assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } -func TestCreateTenantAlreadyExistError(t *testing.T) { +func TestCreateTenantAlreadyExistError(t *testing.T) { args := []string{"create", "--allowed-clusters", "standalone", "create-tenant-duplicate"} _, execErr, _, _ := TestTenantCommands(createTenantCmd, args) assert.Nil(t, execErr) diff --git a/pkg/ctl/tenant/delete.go b/pkg/ctl/tenant/delete.go index 8d1626ff..9249c142 100644 --- a/pkg/ctl/tenant/delete.go +++ b/pkg/ctl/tenant/delete.go @@ -45,7 +45,7 @@ func deleteTenantCmd(vc *cmdutils.VerbCmd) { NonEmptyError := pulsar.Output{ Desc: "there has namespace(s) under the tenant ", - Out: "code: 409 reason: The tenant still has active namespaces", + Out: "code: 409 reason: The tenant still has active namespaces", } out = append(out, tenantNameArgsError, tenantNotExistError, NonEmptyError) desc.CommandOutput = out diff --git a/pkg/ctl/tenant/delete_test.go b/pkg/ctl/tenant/delete_test.go index f84db69b..84ff618b 100644 --- a/pkg/ctl/tenant/delete_test.go +++ b/pkg/ctl/tenant/delete_test.go @@ -18,9 +18,10 @@ package tenant import ( - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/stretchr/testify/assert" ) func TestDeleteTenantCmd(t *testing.T) { @@ -55,7 +56,7 @@ func TestDeleteNonExistTenant(t *testing.T) { } func TestDeleteNonEmptyTenant(t *testing.T) { - args := []string{"delete", "public"} + args := []string{"delete", "public"} _, execErr, _, _ := TestTenantCommands(deleteTenantCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 409 reason: The tenant still has active namespaces", execErr.Error()) diff --git a/pkg/ctl/tenant/get.go b/pkg/ctl/tenant/get.go index e0e4503f..590fee14 100644 --- a/pkg/ctl/tenant/get.go +++ b/pkg/ctl/tenant/get.go @@ -73,7 +73,7 @@ func doGetTenant(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() data, err := admin.Tenants().Get(vc.NameArg) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), data) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), data) } return err } diff --git a/pkg/ctl/tenant/get_test.go b/pkg/ctl/tenant/get_test.go index 8d42ffe5..9b1c5225 100644 --- a/pkg/ctl/tenant/get_test.go +++ b/pkg/ctl/tenant/get_test.go @@ -19,9 +19,11 @@ package tenant import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestGetTenantCmd(t *testing.T) { @@ -49,7 +51,7 @@ func TestGetTenantArgsError(t *testing.T) { assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } -func TestGetNonExistTenant(t *testing.T) { +func TestGetNonExistTenant(t *testing.T) { args := []string{"get", "non-existent-tenant"} _, execErr, _, _ := TestTenantCommands(getTenantCmd, args) assert.NotNil(t, execErr) diff --git a/pkg/ctl/tenant/list.go b/pkg/ctl/tenant/list.go index 6d4b96e3..9caee541 100644 --- a/pkg/ctl/tenant/list.go +++ b/pkg/ctl/tenant/list.go @@ -18,9 +18,10 @@ package tenant import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" ) func listTenantCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/tenant/tenant.go b/pkg/ctl/tenant/tenant.go index 6da43216..2f6552d2 100644 --- a/pkg/ctl/tenant/tenant.go +++ b/pkg/ctl/tenant/tenant.go @@ -18,9 +18,10 @@ package tenant import ( - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/cobra" ) var tenantNameArgsError = pulsar.Output{ diff --git a/pkg/ctl/tenant/test.go b/pkg/ctl/tenant/test.go index afbd1e51..519c216a 100644 --- a/pkg/ctl/tenant/test.go +++ b/pkg/ctl/tenant/test.go @@ -19,12 +19,15 @@ package tenant import ( "bytes" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" ) -func TestTenantCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, nameErr, err error) { +func TestTenantCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, + execErr, nameErr, err error) { var execError error cmdutils.ExecErrorHandler = func(err error) { execError = err @@ -61,5 +64,4 @@ func TestTenantCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out err = rootCmd.Execute() return buf, execError, nameError, err - -} \ No newline at end of file +} diff --git a/pkg/ctl/tenant/update.go b/pkg/ctl/tenant/update.go index 912ecde3..048248bc 100644 --- a/pkg/ctl/tenant/update.go +++ b/pkg/ctl/tenant/update.go @@ -18,9 +18,10 @@ package tenant import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func UpdateTenantCmd(vc *cmdutils.VerbCmd) { diff --git a/pkg/ctl/tenant/update_test.go b/pkg/ctl/tenant/update_test.go index eed003f9..cf4831ed 100644 --- a/pkg/ctl/tenant/update_test.go +++ b/pkg/ctl/tenant/update_test.go @@ -19,9 +19,11 @@ package tenant import ( "encoding/json" + "testing" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestUpdateTenantCmd(t *testing.T) { diff --git a/pkg/ctl/topic/crud/create.go b/pkg/ctl/topic/crud/create.go index 3a30bebf..816d2374 100644 --- a/pkg/ctl/topic/crud/create.go +++ b/pkg/ctl/topic/crud/create.go @@ -18,12 +18,14 @@ package crud import ( - "github.com/pkg/errors" + "strconv" + "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/args" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/args" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" "github.com/streamnative/pulsarctl/pkg/pulsar" - "strconv" + + "github.com/pkg/errors" ) func CreateTopicCmd(vc *cmdutils.VerbCmd) { @@ -50,9 +52,9 @@ func CreateTopicCmd(vc *cmdutils.VerbCmd) { Desc: "normal output", Out: "Create topic with partitions successfully", } - out = append(out, successOut, ArgsError, TopicAlreadyExistError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgsError, e.TopicAlreadyExistError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -63,7 +65,7 @@ func CreateTopicCmd(vc *cmdutils.VerbCmd) { vc.SetRunFuncWithMultiNameArgs(func() error { return doCreateTopic(vc) - }, CheckTopicNameTwoArgs) + }, args.CheckTopicNameTwoArgs) } func doCreateTopic(vc *cmdutils.VerbCmd) error { diff --git a/pkg/ctl/topic/crud/create_test.go b/pkg/ctl/topic/crud/create_test.go index 26ebbcb7..3a34224e 100644 --- a/pkg/ctl/topic/crud/create_test.go +++ b/pkg/ctl/topic/crud/create_test.go @@ -18,14 +18,16 @@ package crud import ( - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" - "github.com/stretchr/testify/assert" "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + + "github.com/stretchr/testify/assert" ) func TestCreateTopicCmd(t *testing.T) { args := []string{"create", "test-create-topic", "2"} - _, execErr, argsErr, err := TestTopicCommands(CreateTopicCmd, args) + _, execErr, argsErr, err := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) assert.Nil(t, argsErr) assert.Nil(t, err) @@ -33,7 +35,7 @@ func TestCreateTopicCmd(t *testing.T) { func TestCreateNonPersistentTopic(t *testing.T) { args := []string{"create", "non-persistent://public/default/test-create-topic", "2"} - _, execErr, argsErr, err := TestTopicCommands(CreateTopicCmd, args) + _, execErr, argsErr, err := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) assert.Nil(t, argsErr) assert.Nil(t, err) @@ -41,36 +43,36 @@ func TestCreateNonPersistentTopic(t *testing.T) { func TestCreateTopicAlreadExists(t *testing.T) { args := []string{"create", "test-duplicate-topic", "2"} - _, _, _, err := TestTopicCommands(CreateTopicCmd, args) + _, _, _, err := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, err) - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 409 reason: Partitioned topic already exists", execErr.Error()) } func TestCreateTopicArgsError(t *testing.T) { args := []string{"create", "topic"} - _, _, nameErr, _ := TestTopicCommands(CreateTopicCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "need to specified the topic name and the partitions", nameErr.Error()) } func TestCreateTopicWithInvalidPartitions(t *testing.T) { args := []string{"create", "topic", "a"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "invalid partition number 'a'", execErr.Error()) args = []string{"create", "topic", "--", "-1"} - _, execErr, _, _ = TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(CreateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "invalid partition number '-1'", execErr.Error()) } func TestCreateNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-create-non-partitioned-topic", "0"} - _, execErr, argsErr, err := TestTopicCommands(CreateTopicCmd, args) + _, execErr, argsErr, err := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) assert.Nil(t, argsErr) assert.Nil(t, err) @@ -78,7 +80,7 @@ func TestCreateNonPartitionedTopic(t *testing.T) { func TestCreateNonPersistentNonPartitionedTopic(t *testing.T) { args := []string{"create", "non-persistent://public/default/test-create-non-partitioned-topic", "0"} - _, execErr, argsErr, err := TestTopicCommands(CreateTopicCmd, args) + _, execErr, argsErr, err := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) assert.Nil(t, argsErr) assert.Nil(t, err) diff --git a/pkg/ctl/topic/crud/delete.go b/pkg/ctl/topic/crud/delete.go index eac0696d..21aea6cf 100644 --- a/pkg/ctl/topic/crud/delete.go +++ b/pkg/ctl/topic/crud/delete.go @@ -18,10 +18,11 @@ package crud import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func DeleteTopicCmd(vc *cmdutils.VerbCmd) { @@ -40,7 +41,8 @@ func DeleteTopicCmd(vc *cmdutils.VerbCmd) { Command: "pulsarctl topics delete --non-partitioned ", } - desc.CommandExamples = append(examples, deleteTopic, deleteNonPartitionedTopic) + examples = append(examples, deleteTopic, deleteNonPartitionedTopic) + desc.CommandExamples = examples var out []pulsar.Output successOut := pulsar.Output{ Desc: "normal output", @@ -56,10 +58,10 @@ func DeleteTopicCmd(vc *cmdutils.VerbCmd) { Desc: "the non-partitioned topic does not exist", Out: "[✖] code: 404 reason: Topic not found", } - out = append(out, successOut, ArgError, + out = append(out, successOut, e.ArgError, partitionedTopicNotExistError, nonPartitionedTopicNotExistError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -88,6 +90,7 @@ func DeleteTopicCmd(vc *cmdutils.VerbCmd) { // TODO add delete schema func doDeleteTopic(vc *cmdutils.VerbCmd, force, deleteSchema, nonPartitioned bool) error { + _ = deleteSchema // for testing if vc.NameError != nil { return vc.NameError diff --git a/pkg/ctl/topic/crud/delete_test.go b/pkg/ctl/topic/crud/delete_test.go index 98ef32df..a850d0e3 100644 --- a/pkg/ctl/topic/crud/delete_test.go +++ b/pkg/ctl/topic/crud/delete_test.go @@ -18,69 +18,71 @@ package crud import ( - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" - "github.com/stretchr/testify/assert" "strings" "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + + "github.com/stretchr/testify/assert" ) func TestDeletePartitionedTopic(t *testing.T) { args := []string{"create", "test-delete-partitioned-topic", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"list", "public/default"} - out, execErr, _, _ := TestTopicCommands(ListTopicsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.Nil(t, execErr) assert.True(t, strings.Contains(out.String(), "test-delete-partitioned-topic")) args = []string{"delete", "test-delete-partitioned-topic"} - _, execErr, _, _ = TestTopicCommands(DeleteTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(DeleteTopicCmd, args) assert.Nil(t, execErr) args = []string{"list", "public/default"} - out, execErr, _, _ = TestTopicCommands(ListTopicsCmd, args) + out, execErr, _, _ = test.TestTopicCommands(ListTopicsCmd, args) assert.Nil(t, execErr) assert.False(t, strings.Contains(out.String(), "test-delete-partitioned-topic")) } func TestDeleteNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-delete-non-partitioned-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"list", "public/default"} - out, execErr, _, _ := TestTopicCommands(ListTopicsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.Nil(t, execErr) assert.True(t, strings.Contains(out.String(), "test-delete-non-partitioned-topic")) args = []string{"delete", "--non-partitioned", "test-delete-non-partitioned-topic"} - _, execErr, _, _ = TestTopicCommands(DeleteTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(DeleteTopicCmd, args) assert.Nil(t, execErr) args = []string{"list", "public/default"} - out, execErr, _, _ = TestTopicCommands(ListTopicsCmd, args) + out, execErr, _, _ = test.TestTopicCommands(ListTopicsCmd, args) assert.Nil(t, execErr) assert.False(t, strings.Contains(out.String(), "test-delete-non-partitioned-topic")) } func TestDeleteTopicArgError(t *testing.T) { args := []string{"delete"} - _, _, nameErr, _ := TestTopicCommands(DeleteTopicCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(DeleteTopicCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestDeleteNonExistPartitionedTopic(t *testing.T) { args := []string{"delete", "non-existent-partitioned-topic"} - _, execErr, _, _ := TestTopicCommands(DeleteTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(DeleteTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Partitioned topic does not exist", execErr.Error()) } func TestDeleteNonExistNonPartitionedTopic(t *testing.T) { args := []string{"delete", "--non-partitioned", "non-existent-non-partitioned-topic"} - _, execErr, _, _ := TestTopicCommands(DeleteTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(DeleteTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } diff --git a/pkg/ctl/topic/crud/get.go b/pkg/ctl/topic/crud/get.go index 13832f7f..65e24df9 100644 --- a/pkg/ctl/topic/crud/get.go +++ b/pkg/ctl/topic/crud/get.go @@ -19,32 +19,33 @@ package crud import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetTopicCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the metadata of an exist topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - getTopic := Example{ + var examples []pulsar.Example + getTopic := pulsar.Example{ Desc: "Get hte metadata of an exist topic metadata", Command: "pulsarctl topics get ", } - desc.CommandExamples = append(examples, getTopic) + examples = append(examples, getTopic) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "{\n" + + Out: "{\n" + " \"partitions\": \"\"\n" + "}", } - out = append(out, successOut, ArgError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -64,7 +65,7 @@ func doGetTopic(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -72,7 +73,7 @@ func doGetTopic(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() meta, err := admin.Topics().GetMetadata(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), meta) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), meta) } return err diff --git a/pkg/ctl/topic/crud/get_test.go b/pkg/ctl/topic/crud/get_test.go index 6a3b87e0..9e23edc5 100644 --- a/pkg/ctl/topic/crud/get_test.go +++ b/pkg/ctl/topic/crud/get_test.go @@ -21,18 +21,19 @@ import ( "encoding/json" "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) func TestGetTopicCmd(t *testing.T) { args := []string{"create", "test-get-topic", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get", "test-get-topic"} - out, execErr, _, _ := TestTopicCommands(GetTopicCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetTopicCmd, args) var partitions pulsar.PartitionedTopicMetadata err := json.Unmarshal(out.Bytes(), &partitions) if err != nil { @@ -45,11 +46,11 @@ func TestGetTopicCmd(t *testing.T) { func TestBetNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-get-non-partitioned-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get", "test-get-non-partitioned-topic"} - out, execErr, _, _ := TestTopicCommands(GetTopicCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetTopicCmd, args) assert.Nil(t, execErr) var partitions pulsar.PartitionedTopicMetadata err := json.Unmarshal(out.Bytes(), &partitions) @@ -61,14 +62,14 @@ func TestBetNonPartitionedTopic(t *testing.T) { func TestGetTopicArgsError(t *testing.T) { args := []string{"get"} - _, _, nameErr, _ := TestTopicCommands(GetTopicCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetTopicCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestGetNonExistTopic(t *testing.T) { args := []string{"get", "non-exist-topic"} - out, execErr, _, _ := TestTopicCommands(GetTopicCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetTopicCmd, args) assert.Nil(t, execErr) var partitions pulsar.PartitionedTopicMetadata diff --git a/pkg/ctl/topic/crud/list.go b/pkg/ctl/topic/crud/list.go index 860750fe..69639592 100644 --- a/pkg/ctl/topic/crud/list.go +++ b/pkg/ctl/topic/crud/list.go @@ -18,25 +18,26 @@ package crud import ( - "github.com/olekukonko/tablewriter" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/olekukonko/tablewriter" ) func ListTopicsCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for listing all exist topics under the specified namespace." desc.CommandPermission = "This command requires admin permissions." - listTopics := Example{ + listTopics := pulsar.Example{ Desc: "List all exist topics under the namespace ", Command: "pulsarctl topics list ", } - desc.CommandExamples = []Example{listTopics} + desc.CommandExamples = []pulsar.Example{listTopics} - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: `+----------------------------------------------------------+---------------+ | TOPIC NAME | PARTITIONED ? | @@ -44,12 +45,12 @@ func ListTopicsCmd(vc *cmdutils.VerbCmd) { +----------------------------------------------------------+---------------+`, } - argError := Output{ + argError := pulsar.Output{ Desc: "the namespace is not specified", Out: "[✖] only one argument is allowed to be used as a name", } - out = append(out, successOut, argError, TenantNotExistError, NamespaceNotExistError) - out = append(out, NamespaceErrors...) + out = append(out, successOut, argError, e.TenantNotExistError, e.NamespaceNotExistError) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -69,7 +70,7 @@ func doListTopics(vc *cmdutils.VerbCmd) error { return vc.NameError } - namespace, err := GetNamespaceName(vc.NameArg) + namespace, err := pulsar.GetNamespaceName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/topic/crud/list_test.go b/pkg/ctl/topic/crud/list_test.go index bb95404b..1de43994 100644 --- a/pkg/ctl/topic/crud/list_test.go +++ b/pkg/ctl/topic/crud/list_test.go @@ -20,33 +20,34 @@ package crud import ( "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/stretchr/testify/assert" ) func TestListTopicsCmd(t *testing.T) { args := []string{"list", "public/default"} - _, execErr, _, _ := TestTopicCommands(ListTopicsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.Nil(t, execErr) } func TestListTopicArgError(t *testing.T) { args := []string{"list"} - _, _, nameErr, _ := TestTopicCommands(ListTopicsCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestListNonExistNamespace(t *testing.T) { args := []string{"list", "public/non-exist-namespace"} - _, execErr, _, _ := TestTopicCommands(ListTopicsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Namespace does not exist", execErr.Error()) } func TestListNonExistTenant(t *testing.T) { args := []string{"list", "non-exist-tenant/default"} - _, execErr, _, _ := TestTopicCommands(ListTopicsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(ListTopicsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Tenant does not exist", execErr.Error()) } diff --git a/pkg/ctl/topic/crud/update.go b/pkg/ctl/topic/crud/update.go index 043450e0..9e8683ef 100644 --- a/pkg/ctl/topic/crud/update.go +++ b/pkg/ctl/topic/crud/update.go @@ -18,39 +18,42 @@ package crud import ( - "github.com/pkg/errors" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/args" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" "strconv" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/args" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" ) func UpdateTopicCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for updating the partition number of an exist topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - updateTopic := Example{ + var examples []pulsar.Example + updateTopic := pulsar.Example{ Desc: "", Command: "pulsarctl topics update ", } - desc.CommandExamples = append(examples, updateTopic) + examples = append(examples, updateTopic) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "Update topic with partitions successfully", } - topicNotExist := Output{ + topicNotExist := pulsar.Output{ Desc: "the topic is not exist", Out: "[✖] code: 409 reason: Topic is not partitioned topic", } - out = append(out, successOut, ArgsError, InvalidPartitionsNumberError, topicNotExist) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgsError, e.InvalidPartitionsNumberError, topicNotExist) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -61,7 +64,7 @@ func UpdateTopicCmd(vc *cmdutils.VerbCmd) { vc.SetRunFuncWithMultiNameArgs(func() error { return doUpdateTopic(vc) - }, CheckTopicNameTwoArgs) + }, args.CheckTopicNameTwoArgs) } func doUpdateTopic(vc *cmdutils.VerbCmd) error { @@ -70,7 +73,7 @@ func doUpdateTopic(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArgs[0]) + topic, err := pulsar.GetTopicName(vc.NameArgs[0]) if err != nil { return err } diff --git a/pkg/ctl/topic/crud/update_test.go b/pkg/ctl/topic/crud/update_test.go index 5fc7997a..40c0e4b6 100644 --- a/pkg/ctl/topic/crud/update_test.go +++ b/pkg/ctl/topic/crud/update_test.go @@ -21,18 +21,19 @@ import ( "encoding/json" "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) func TestUpdateTopicCmd(t *testing.T) { args := []string{"create", "test-update-topic", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get", "test-update-topic"} - out, execErr, _, _ := TestTopicCommands(GetTopicCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetTopicCmd, args) assert.Nil(t, execErr) var partitions pulsar.PartitionedTopicMetadata err := json.Unmarshal(out.Bytes(), &partitions) @@ -42,11 +43,11 @@ func TestUpdateTopicCmd(t *testing.T) { assert.Equal(t, 2, partitions.Partitions) args = []string{"update", "test-update-topic", "3"} - _, execErr, _, _ = TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(UpdateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get", "test-update-topic"} - out, execErr, _, _ = TestTopicCommands(GetTopicCmd, args) + out, execErr, _, _ = test.TestTopicCommands(GetTopicCmd, args) assert.Nil(t, execErr) err = json.Unmarshal(out.Bytes(), &partitions) if err != nil { @@ -57,42 +58,42 @@ func TestUpdateTopicCmd(t *testing.T) { func TestUpdateTopicArgsError(t *testing.T) { args := []string{"update", "test-topic"} - _, _, nameErr, _ := TestTopicCommands(UpdateTopicCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "need to specified the topic name and the partitions", nameErr.Error()) } func TestUpdateTopicWithInvalidPartitions(t *testing.T) { args := []string{"update", "test-topic", "0"} - _, execErr, _, _ := TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "invalid partition number '0'", execErr.Error()) args = []string{"update", "test-topic", "a"} - _, execErr, _, _ = TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "invalid partition number 'a'", execErr.Error()) args = []string{"update", "test-topic", "--", "-1"} - _, execErr, _, _ = TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "invalid partition number '-1'", execErr.Error()) } func TestUpdateTopicNotExist(t *testing.T) { args := []string{"update", "non-exist-topic", "2"} - _, execErr, _, _ := TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 409 reason: Topic is not partitioned topic", execErr.Error()) } func TestUpdateNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-update-non-partitioned-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"update", "test-update-non-partitioned-topic", "3"} - _, execErr, _, _ = TestTopicCommands(UpdateTopicCmd, args) + _, execErr, _, _ = test.TestTopicCommands(UpdateTopicCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 409 reason: Topic is not partitioned topic", execErr.Error()) } diff --git a/pkg/ctl/topic/errors/errors_topic.go b/pkg/ctl/topic/errors/errors_topic.go index b52225a0..a06830bc 100644 --- a/pkg/ctl/topic/errors/errors_topic.go +++ b/pkg/ctl/topic/errors/errors_topic.go @@ -17,39 +17,39 @@ package errors -import . "github.com/streamnative/pulsarctl/pkg/pulsar" +import "github.com/streamnative/pulsarctl/pkg/pulsar" -var ArgError = Output{ +var ArgError = pulsar.Output{ Desc: "the topic name is not specified", Out: "[✖] only one argument is allowed to be used as a name", } -var ArgsError = Output{ +var ArgsError = pulsar.Output{ Desc: "the topic name and(or) the partitions is not specified", Out: "[✖] need to specified the topic name and the partitions", } -var TopicAlreadyExistError = Output{ +var TopicAlreadyExistError = pulsar.Output{ Desc: "the topic has been created", Out: "[✖] code: 409 reason: Partitioned topic already exists", } -var TenantNotExistError = Output{ +var TenantNotExistError = pulsar.Output{ Desc: "the tenant of the namespace is not exist", Out: "[✖] code: 404 reason: Tenant does not exist", } -var NamespaceNotExistError = Output{ +var NamespaceNotExistError = pulsar.Output{ Desc: "the namespace is not exist", Out: "[✖] code: 404 reason: Namespace does not exist", } -var InvalidPartitionsNumberError = Output{ +var InvalidPartitionsNumberError = pulsar.Output{ Desc: "the partitions number is invalid", Out: "[✖] invalid partition number ''", } -var TopicNameErrors = []Output{ +var TopicNameErrors = []pulsar.Output{ { Desc: "the topic name is not in the format of // or ", Out: "[✖] Invalid short topic name '', it should be " + @@ -67,7 +67,7 @@ var TopicNameErrors = []Output{ }, } -var NamespaceErrors = []Output{ +var NamespaceErrors = []pulsar.Output{ { Desc: "the namespace name is not in the format of /", Out: "[✖] The complete name of namespace is invalid. complete name : ", diff --git a/pkg/ctl/topic/info/internal.go b/pkg/ctl/topic/info/internal.go index d39a3221..9dcdf72d 100644 --- a/pkg/ctl/topic/info/internal.go +++ b/pkg/ctl/topic/info/internal.go @@ -19,12 +19,11 @@ package info import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) -var normalOutput = -`{ +var normalOutput = `{ "version": 1, "creationDate": "", "modificationData": "", @@ -57,34 +56,35 @@ var normalOutput = } ` -func GetInternalInfoCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription +func GetInternalInfoCmd(vc *cmdutils.VerbCmd) { + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the internal info of a topic " + "which has messages or subscriptions." desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - get := Example{ - Desc: "Get the internal info of a topic ", + var examples []pulsar.Example + get := pulsar.Example{ + Desc: "Get the internal info of a topic ", Command: "pulsarctl topic internal-info ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: normalOutput, + Out: normalOutput, } - failOut := Output{ + failOut := pulsar.Output{ Desc: "the specified topic does not exist", - Out: "[✖] code: 500 reason: Unknown pulsar error", + Out: "[✖] code: 500 reason: Unknown pulsar error", } - out = append(out, successOut, ArgError, failOut) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError, failOut) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out - + vc.SetDescription("internal-info", "Get the topic internal info", desc.ToString()) @@ -100,7 +100,7 @@ func doGetInternalInfo(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -108,7 +108,7 @@ func doGetInternalInfo(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() info, err := admin.Topics().GetInternalInfo(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), info) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), info) } return err diff --git a/pkg/ctl/topic/info/internal_test.go b/pkg/ctl/topic/info/internal_test.go index 652958da..fb737118 100644 --- a/pkg/ctl/topic/info/internal_test.go +++ b/pkg/ctl/topic/info/internal_test.go @@ -31,7 +31,7 @@ func TestGetInternalInfoArgError(t *testing.T) { assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } -func TestGetNonExistingTopicInternalInfo(t *testing.T) { +func TestGetNonExistingTopicInternalInfo(t *testing.T) { args := []string{"internal-info", "non-existing-topic"} _, execErr, _, _ := TestTopicCommands(GetInternalInfoCmd, args) assert.NotNil(t, execErr) diff --git a/pkg/ctl/topic/info/last_messageId.go b/pkg/ctl/topic/info/last_messageId.go index f09d0517..14f8ef6f 100644 --- a/pkg/ctl/topic/info/last_messageId.go +++ b/pkg/ctl/topic/info/last_messageId.go @@ -18,53 +18,55 @@ package info import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) -func GetLastMessageIdCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription +func GetLastMessageIDCmd(vc *cmdutils.VerbCmd) { + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the last message id of a topic (partition)." - desc.CommandPermission= "This command requires tenant admin permissions." + desc.CommandPermission = "This command requires tenant admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the last message id of a topic ", Command: "pulsarctl topic last-message-id ", } - getPartitionedTopic := Example{ - Desc: "Get the last message id of a partition of a partitioned topic ", + getPartitionedTopic := pulsar.Example{ + Desc: "Get the last message id of a partition of a partitioned topic ", Command: "pulsarctl topic last-message-id --partition ", } - desc.CommandExamples = append(examples, get, getPartitionedTopic) + examples = append(examples, get, getPartitionedTopic) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", - Out: "{\n" + - " \"LedgerId\": 0,\n" + - " \"EntryId\": 0,\n" + + Out: "{\n" + + " \"LedgerID\": 0,\n" + + " \"EntryID\": 0,\n" + " \"PartitionedIndex\": 0" + "\n}", } - out = append(out, successOut, ArgError) + out = append(out, successOut, e.ArgError) - topicNotFoundError := Output{ + topicNotFoundError := pulsar.Output{ Desc: "the topic does not exist in the cluster", - Out: "[✖] code: 404 reason: Topic not found", + Out: "[✖] code: 404 reason: Topic not found", } out = append(out, topicNotFoundError) - notAllowedError := Output{ + notAllowedError := pulsar.Output{ Desc: "the topic does not a persistent topic", - Out: "[✖] code: 405 reason: GetLastMessageId on a non-persistent topic is not allowed", + Out: "[✖] code: 405 reason: GetLastMessageID on a non-persistent topic is not allowed", } out = append(out, notAllowedError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out var partition int @@ -76,7 +78,7 @@ func GetLastMessageIdCmd(vc *cmdutils.VerbCmd) { "lmi") vc.SetRunFuncWithNameArg(func() error { - return doGetLastMessageId(vc, partition) + return doGetLastMessageID(vc, partition) }) vc.FlagSetGroup.InFlagSet("LastMessageId", func(set *pflag.FlagSet) { @@ -85,13 +87,13 @@ func GetLastMessageIdCmd(vc *cmdutils.VerbCmd) { }) } -func doGetLastMessageId(vc *cmdutils.VerbCmd, partition int) error { +func doGetLastMessageID(vc *cmdutils.VerbCmd, partition int) error { // for testing if vc.NameError != nil { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -104,9 +106,9 @@ func doGetLastMessageId(vc *cmdutils.VerbCmd, partition int) error { } admin := cmdutils.NewPulsarClient() - messageId, err := admin.Topics().GetLastMessageId(*topic) + messageID, err := admin.Topics().GetLastMessageID(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), messageId) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), messageID) } return err diff --git a/pkg/ctl/topic/info/last_messageId_test.go b/pkg/ctl/topic/info/last_messageId_test.go index 5de8b371..7e217cdd 100644 --- a/pkg/ctl/topic/info/last_messageId_test.go +++ b/pkg/ctl/topic/info/last_messageId_test.go @@ -18,22 +18,24 @@ package info import ( - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" - "github.com/stretchr/testify/assert" "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + + "github.com/stretchr/testify/assert" ) func TestGetLastMessageIdArgsError(t *testing.T) { args := []string{"last-message-id"} - _, _, nameErr, _ := TestTopicCommands(GetLastMessageIdCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetLastMessageIDCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestGetLastMessageIdTopicNotExistError(t *testing.T) { args := []string{"last-message-id", "not-existent-topic"} - _, execErr, _, _ := TestTopicCommands(GetLastMessageIdCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GetLastMessageIDCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } @@ -41,11 +43,11 @@ func TestGetLastMessageIdTopicNotExistError(t *testing.T) { func TestGetLastMessageIdNotAllowedError(t *testing.T) { args := []string{"create", "non-persistent://public/default/last-message-id-non-persistent-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"last-message-id", "non-persistent://public/default/last-message-id-non-persistent-topic"} - _, execErr, _, _ = TestTopicCommands(GetLastMessageIdCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GetLastMessageIDCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 405 reason: GetLastMessageId on a non-persistent topic is not allowed", diff --git a/pkg/ctl/topic/lookup/bundle_range.go b/pkg/ctl/topic/lookup/bundle_range.go index 60e72578..57d96030 100644 --- a/pkg/ctl/topic/lookup/bundle_range.go +++ b/pkg/ctl/topic/lookup/bundle_range.go @@ -19,30 +19,31 @@ package lookup import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetBundleRangeCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting namespace bundle range of a topic (partition)." desc.CommandPermission = "This command requires super-user permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get namespace bundle range of a topic ", Command: "pulsarctl topic bundle-range ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "The bundle range of the topic is: ", } - out = append(out, successOut, ArgError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -62,7 +63,7 @@ func doGetBundleRange(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/topic/lookup/bundle_range_test.go b/pkg/ctl/topic/lookup/bundle_range_test.go index 922564b3..d51ec18f 100644 --- a/pkg/ctl/topic/lookup/bundle_range_test.go +++ b/pkg/ctl/topic/lookup/bundle_range_test.go @@ -18,27 +18,29 @@ package lookup import ( - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" - "github.com/stretchr/testify/assert" "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + + "github.com/stretchr/testify/assert" ) func TestGetBundleRangeCmd(t *testing.T) { args := []string{"create", "test-get-topic-bundle-range", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"bundle-range", "test-get-topic-bundle-range"} - out, execErr, _, _ := TestTopicCommands(GetBundleRangeCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetBundleRangeCmd, args) assert.Nil(t, execErr) assert.Equal(t, "The bundle range of the topic "+ "persistent://public/default/test-get-topic-bundle-range is: 0xc0000000_0xffffffff", out.String()) } func TestGetBundleRangeArgError(t *testing.T) { - args := []string{"bundle-range"} - _, _, nameErr, _ := TestTopicCommands(GetBundleRangeCmd, args) + args := []string{"bundle-range"} + _, _, nameErr, _ := test.TestTopicCommands(GetBundleRangeCmd, args) assert.NotNil(t, nameErr) - assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) + assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } diff --git a/pkg/ctl/topic/lookup/topic.go b/pkg/ctl/topic/lookup/topic.go index 14c51d02..2dcf7dfe 100644 --- a/pkg/ctl/topic/lookup/topic.go +++ b/pkg/ctl/topic/lookup/topic.go @@ -19,24 +19,25 @@ package lookup import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) -func LookupTopicCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription +func TopicCmd(vc *cmdutils.VerbCmd) { + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for looking up the owner broker of a topic." desc.CommandPermission = "This command does not require permissions. " - var examples []Example - lookup := Example{ + var examples []pulsar.Example + lookup := pulsar.Example{ Desc: "Lookup the owner broker of the topic ", Command: "pulsarctl topic lookup ", } - desc.CommandExamples = append(examples, lookup) + examples = append(examples, lookup) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "", Out: "{\n" + " \"brokerUlr\": \"\",\n" + @@ -45,9 +46,9 @@ func LookupTopicCmd(vc *cmdutils.VerbCmd) { " \"httpUrlTls\": \"\",\n" + "}", } - out = append(out, successOut, ArgError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -67,7 +68,7 @@ func doLookupTopic(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -75,7 +76,7 @@ func doLookupTopic(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() lookup, err := admin.Topics().Lookup(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), lookup) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), lookup) } return err } diff --git a/pkg/ctl/topic/lookup/topic_test.go b/pkg/ctl/topic/lookup/topic_test.go index 6d665944..a158aae0 100644 --- a/pkg/ctl/topic/lookup/topic_test.go +++ b/pkg/ctl/topic/lookup/topic_test.go @@ -19,21 +19,23 @@ package lookup import ( "encoding/json" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" - "github.com/streamnative/pulsarctl/pkg/pulsar" - "github.com/stretchr/testify/assert" "regexp" "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/stretchr/testify/assert" ) func TestLookupTopicCmd(t *testing.T) { args := []string{"create", "test-lookup-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"lookup", "test-lookup-topic"} - out, execErr, _, _ := TestTopicCommands(LookupTopicCmd, args) + out, execErr, _, _ := test.TestTopicCommands(TopicCmd, args) assert.Nil(t, execErr) var data pulsar.LookupData @@ -42,24 +44,16 @@ func TestLookupTopicCmd(t *testing.T) { t.Fatal(err) } - brokerUrl, err := regexp.Compile("^pulsar://[a-z-A-Z0-9]*:6650$") - if err != nil { - t.Fatal(err) - } - - assert.True(t, brokerUrl.MatchString(data.BrokerUrl)) - - httpUrl, err := regexp.Compile("^http://[a-z-A-Z0-9]*:8080$") - if err != nil { - t.Fatal(err) - } + brokerURL := regexp.MustCompile("^pulsar://[a-z-A-Z0-9]*:6650$") + assert.True(t, brokerURL.MatchString(data.BrokerURL)) - assert.True(t, httpUrl.MatchString(data.HttpUrl)) + httpURL := regexp.MustCompile("^http://[a-z-A-Z0-9]*:8080$") + assert.True(t, httpURL.MatchString(data.HTTPURL)) } -func TestLookupTopicArgError(t *testing.T) { - args := []string{"lookup"} - _, _, nameErr, _ := TestTopicCommands(LookupTopicCmd, args) +func TestLookupTopicArgError(t *testing.T) { + args := []string{"lookup"} + _, _, nameErr, _ := test.TestTopicCommands(TopicCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } diff --git a/pkg/ctl/topic/permission/get.go b/pkg/ctl/topic/permission/get.go index db5916c2..c44b38aa 100644 --- a/pkg/ctl/topic/permission/get.go +++ b/pkg/ctl/topic/permission/get.go @@ -19,24 +19,25 @@ package permission import ( "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GetPermissionsCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the permissions of a topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the permissions of a topic ", Command: "pulsarctl topic get-permissions ", } - desc.CommandExamples = append(examples, get) + examples = append(examples, get) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "{\n" + " \"\": [\n" + @@ -44,9 +45,9 @@ func GetPermissionsCmd(vc *cmdutils.VerbCmd) { " ]" + "\n}", } - out = append(out, successOut, ArgError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -65,7 +66,7 @@ func doGetPermissions(vc *cmdutils.VerbCmd) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -73,7 +74,7 @@ func doGetPermissions(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() permissions, err := admin.Topics().GetPermissions(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), permissions) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), permissions) } return err diff --git a/pkg/ctl/topic/permission/get_test.go b/pkg/ctl/topic/permission/get_test.go index 7d390c69..15bf7083 100644 --- a/pkg/ctl/topic/permission/get_test.go +++ b/pkg/ctl/topic/permission/get_test.go @@ -20,13 +20,14 @@ package permission import ( "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/stretchr/testify/assert" ) func TestGetPermissionsArgError(t *testing.T) { args := []string{"get-permissions"} - _, _, nameErr, _ := TestTopicCommands(GetPermissionsCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetPermissionsCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } diff --git a/pkg/ctl/topic/permission/grant.go b/pkg/ctl/topic/permission/grant.go index 5da4f61b..8f528222 100644 --- a/pkg/ctl/topic/permission/grant.go +++ b/pkg/ctl/topic/permission/grant.go @@ -18,45 +18,47 @@ package permission import ( + "github.com/streamnative/pulsarctl/pkg/cmdutils" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" ) func GrantPermissionCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for granting permissions to a client role on a topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - grant := Example{ + var examples []pulsar.Example + grant := pulsar.Example{ Desc: "Grant permissions to a client on a single topic ", Command: "pulsarctl topic grant-permissions --role --actions --actions ", } - desc.CommandExamples = append(examples, grant) + examples = append(examples, grant) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "Grant role %s and actions %v to the topic %s successfully", } - flagError := Output{ + flagError := pulsar.Output{ Desc: "the specified role is empty", Out: "Invalid role name", } - actionsError := Output{ + actionsError := pulsar.Output{ Desc: "the specified actions is not allowed.", Out: "The auth action only can be specified as 'produce', " + "'consume', or 'functions'. Invalid auth action ''", } - out = append(out, successOut, ArgError, flagError, actionsError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, e.ArgError, flagError, actionsError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -88,7 +90,7 @@ func doGrantPermission(vc *cmdutils.VerbCmd, role string, actions []string) erro return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -113,10 +115,10 @@ func doGrantPermission(vc *cmdutils.VerbCmd, role string, actions []string) erro return err } -func getAuthActions(actions []string) ([]AuthAction, error) { - var authActions []AuthAction +func getAuthActions(actions []string) ([]pulsar.AuthAction, error) { + authActions := make([]pulsar.AuthAction, 0) for _, v := range actions { - a, err := ParseAuthAction(v) + a, err := pulsar.ParseAuthAction(v) if err != nil { return nil, err } diff --git a/pkg/ctl/topic/permission/grant_test.go b/pkg/ctl/topic/permission/grant_test.go index 80e6ded7..0e672386 100644 --- a/pkg/ctl/topic/permission/grant_test.go +++ b/pkg/ctl/topic/permission/grant_test.go @@ -21,19 +21,20 @@ import ( "encoding/json" "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) func TestGrantPermissionToNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-grant-permission-non-partitioned-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get-permissions", "test-grant-permission-non-partitioned-topic"} - out, execErr, _, _ := TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) var permissions map[string][]pulsar.AuthAction @@ -49,11 +50,11 @@ func TestGrantPermissionToNonPartitionedTopic(t *testing.T) { "--actions", "produce", "test-grant-permission-non-partitioned-topic", } - _, execErr, _, _ = TestTopicCommands(GrantPermissionCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GrantPermissionCmd, args) assert.Nil(t, execErr) args = []string{"get-permissions", "test-grant-permission-non-partitioned-topic"} - out, execErr, _, _ = TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ = test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) err = json.Unmarshal(out.Bytes(), &permissions) @@ -66,11 +67,11 @@ func TestGrantPermissionToNonPartitionedTopic(t *testing.T) { func TestGrantPermissionToPartitionedTopic(t *testing.T) { args := []string{"create", "test-grant-permission-partitioned-topic", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"get-permissions", "test-grant-permission-partitioned-topic"} - out, execErr, _, _ := TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) var permissions map[string][]pulsar.AuthAction @@ -86,11 +87,11 @@ func TestGrantPermissionToPartitionedTopic(t *testing.T) { "--actions", "consume", "test-grant-permission-partitioned-topic", } - _, execErr, _, _ = TestTopicCommands(GrantPermissionCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GrantPermissionCmd, args) assert.Nil(t, execErr) args = []string{"get-permissions", "test-grant-permission-partitioned-topic"} - out, execErr, _, _ = TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ = test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) err = json.Unmarshal(out.Bytes(), &permissions) @@ -103,17 +104,17 @@ func TestGrantPermissionToPartitionedTopic(t *testing.T) { func TestGrantPermissionArgError(t *testing.T) { args := []string{"grant-permissions", "--role", "test-arg-error-role", "--actions", "produce"} - _, _, nameErr, _ := TestTopicCommands(GrantPermissionCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GrantPermissionCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) args = []string{"grant-permissions", "args-error-topic"} - _, _, _, err := TestTopicCommands(GrantPermissionCmd, args) + _, _, _, err := test.TestTopicCommands(GrantPermissionCmd, args) assert.NotNil(t, err) assert.Equal(t, "required flag(s) \"actions\", \"role\" not set", err.Error()) args = []string{"grant-permissions", "--role", "", "--actions", "produce", "role-empty-topic"} - _, execErr, _, _ := TestTopicCommands(GrantPermissionCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GrantPermissionCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "Invalid role name", execErr.Error()) @@ -122,7 +123,7 @@ func TestGrantPermissionArgError(t *testing.T) { "--actions", "args-error-action", "invalid-actions-topic", } - _, execErr, _, _ = TestTopicCommands(GrantPermissionCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GrantPermissionCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "The auth action only can be specified as 'produce', "+ "'consume', or 'functions'. Invalid auth action 'args-error-action'", execErr.Error()) diff --git a/pkg/ctl/topic/permission/revoke.go b/pkg/ctl/topic/permission/revoke.go index 8a57422c..53ec0cf7 100644 --- a/pkg/ctl/topic/permission/revoke.go +++ b/pkg/ctl/topic/permission/revoke.go @@ -18,39 +18,41 @@ package permission import ( + "github.com/streamnative/pulsarctl/pkg/cmdutils" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" - "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" ) func RevokePermissions(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for revoking a client role permissions on a topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - revoke := Example{ + var examples []pulsar.Example + revoke := pulsar.Example{ Desc: "Revoke permissions of a topic ", Command: "pulsarctl topic revoke-permissions --role ", } examples = append(examples, revoke) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: "Revoke permissions for the role of the topic successfully\n", } - flagError := Output{ + flagError := pulsar.Output{ Desc: "the specified role is empty", Out: "Invalid role name", } - out = append(out, successOut, flagError, ArgError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, successOut, flagError, e.ArgError) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -80,7 +82,7 @@ func doRevokePermissions(vc *cmdutils.VerbCmd, role string) error { if role == "" { return errors.New("Invalid role name") } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } diff --git a/pkg/ctl/topic/permission/revoke_test.go b/pkg/ctl/topic/permission/revoke_test.go index 598df83c..bff9df79 100644 --- a/pkg/ctl/topic/permission/revoke_test.go +++ b/pkg/ctl/topic/permission/revoke_test.go @@ -21,22 +21,23 @@ import ( "encoding/json" "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) func TestRevokePermissionsOnPartitionedTopic(t *testing.T) { args := []string{"create", "test-revoke-partitioned-topic", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) testRevokePermission(t, "test-revoke-partitioned-topic") } func TestRevokePermissionsOnNonPartitionedTopic(t *testing.T) { args := []string{"create", "test-revoke-non-partitioned-topic", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) testRevokePermission(t, "test-revoke-non-partitioned-topic") } @@ -47,11 +48,11 @@ func testRevokePermission(t *testing.T, topic string) { "--actions", "produce", topic, } - _, execErr, _, _ := TestTopicCommands(GrantPermissionCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GrantPermissionCmd, args) assert.Nil(t, execErr) args = []string{"get-permissions", topic} - out, execErr, _, _ := TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) var permissions map[string][]pulsar.AuthAction @@ -64,11 +65,11 @@ func testRevokePermission(t *testing.T, topic string) { assert.Equal(t, "produce", permissions["revoke-test-role"][0].String()) args = []string{"revoke-permissions", "--role", "revoke-test-role", topic} - _, execErr, _, _ = TestTopicCommands(RevokePermissions, args) + _, execErr, _, _ = test.TestTopicCommands(RevokePermissions, args) assert.Nil(t, execErr) args = []string{"get-permissions", topic} - out, execErr, _, _ = TestTopicCommands(GetPermissionsCmd, args) + out, execErr, _, _ = test.TestTopicCommands(GetPermissionsCmd, args) assert.Nil(t, execErr) var emptyPermissions map[string][]pulsar.AuthAction @@ -82,17 +83,17 @@ func testRevokePermission(t *testing.T, topic string) { func TestRevokePermissionsArgError(t *testing.T) { args := []string{"revoke-permissions", "--role", "args-error-role"} - _, _, nameErr, _ := TestTopicCommands(RevokePermissions, args) + _, _, nameErr, _ := test.TestTopicCommands(RevokePermissions, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) args = []string{"revoke-permissions", "--role", "", "empty-role-topic"} - _, execErr, _, _ := TestTopicCommands(RevokePermissions, args) + _, execErr, _, _ := test.TestTopicCommands(RevokePermissions, args) assert.NotNil(t, execErr) assert.Equal(t, "Invalid role name", execErr.Error()) args = []string{"revoke-permissions", "not-specified-role-topic"} - _, _, _, err := TestTopicCommands(RevokePermissions, args) + _, _, _, err := test.TestTopicCommands(RevokePermissions, args) assert.NotNil(t, err) assert.Equal(t, "required flag(s) \"role\" not set", err.Error()) } diff --git a/pkg/ctl/topic/stats/stats.go b/pkg/ctl/topic/stats/stats.go index e0ff1d92..c150e07b 100644 --- a/pkg/ctl/topic/stats/stats.go +++ b/pkg/ctl/topic/stats/stats.go @@ -18,39 +18,40 @@ package stats import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func GetStatsCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the stats for an existing topic and its " + "connected producers and consumers. (All the rates are computed over a 1 minute window " + "and are relative the last completed 1 minute period)" desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get the non-partitioned topic stats", Command: "pulsarctl topic stats ", } - getPartition := Example{ + getPartition := pulsar.Example{ Desc: "Get the partitioned topic stats", Command: "pulsarctl topic stats --partitioned-topic ", } - getPerPartition := Example{ + getPerPartition := pulsar.Example{ Desc: "Get the partitioned topic stats and per partition stats", Command: "pulsarctl topic stats --partitioned-topic --per-partition ", } + examples = append(examples, get, getPartition, getPerPartition) + desc.CommandExamples = examples - desc.CommandExamples = append(examples, get, getPartition, getPerPartition) - - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "Get the non-partitioned topic stats", Out: `{ "msgRateIn": 0, @@ -66,7 +67,7 @@ func GetStatsCmd(vc *cmdutils.VerbCmd) { }`, } - partitionOutput := Output{ + partitionOutput := pulsar.Output{ Desc: "Get the partitioned topic stats", Out: `{ "msgRateIn": 0, @@ -86,7 +87,7 @@ func GetStatsCmd(vc *cmdutils.VerbCmd) { }`, } - perPartitionOutput := Output{ + perPartitionOutput := pulsar.Output{ Desc: "Get the partitioned topic stats and per partition topic stats", Out: `{ "msgRateIn": 0, @@ -118,17 +119,17 @@ func GetStatsCmd(vc *cmdutils.VerbCmd) { } }`, } - out = append(out, successOut, partitionOutput, perPartitionOutput, ArgError) + out = append(out, successOut, partitionOutput, perPartitionOutput, e.ArgError) - topicNotFoundError := Output{ + topicNotFoundError := pulsar.Output{ Desc: "the specified topic does not exist " + "or the specified topic is a partitioned-topic and you don't specified --partitioned-topic " + "or the specified topic is a non-partitioned topic and you specified --partitioned-topic", Out: "code: 404 reason: Topic not found", } out = append(out, topicNotFoundError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -156,7 +157,7 @@ func doGetStats(vc *cmdutils.VerbCmd, partitionedTopic, perPartition bool) error return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -166,14 +167,14 @@ func doGetStats(vc *cmdutils.VerbCmd, partitionedTopic, perPartition bool) error if partitionedTopic { stats, err := admin.Topics().GetPartitionedStats(*topic, perPartition) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), stats) - } - return err - } else { - topicStats, err := admin.Topics().GetStats(*topic) - if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), topicStats) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), stats) } return err } + + topicStats, err := admin.Topics().GetStats(*topic) + if err == nil { + cmdutils.PrintJSON(vc.Command.OutOrStdout(), topicStats) + } + return err } diff --git a/pkg/ctl/topic/stats/stats_internal.go b/pkg/ctl/topic/stats/stats_internal.go index c3d73459..7f6967f9 100644 --- a/pkg/ctl/topic/stats/stats_internal.go +++ b/pkg/ctl/topic/stats/stats_internal.go @@ -18,32 +18,34 @@ package stats import ( - "github.com/spf13/pflag" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" - . "github.com/streamnative/pulsarctl/pkg/pulsar" + e "github.com/streamnative/pulsarctl/pkg/ctl/topic/errors" + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/spf13/pflag" ) func GetInternalStatsCmd(vc *cmdutils.VerbCmd) { - var desc LongDescription + var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for getting the internal stats for a non-partitioned topic or a " + "partition of a partitioned topic." desc.CommandPermission = "This command requires namespace admin permissions." - var examples []Example - get := Example{ + var examples []pulsar.Example + get := pulsar.Example{ Desc: "Get internal stats for an existing non-partitioned-topic ", Command: "pulsarctl topic internal-stats ", } - getPartition := Example{ + getPartition := pulsar.Example{ Desc: "Get internal stats for a partition of a partitioned topic", Command: "pulsarctl topic internal-stats --partition ", } - desc.CommandExamples = append(examples, get, getPartition) + examples = append(examples, get, getPartition) + desc.CommandExamples = examples - var out []Output - successOut := Output{ + var out []pulsar.Output + successOut := pulsar.Output{ Desc: "normal output", Out: `{ "entriesAddedCounter": 0, @@ -68,15 +70,15 @@ func GetInternalStatsCmd(vc *cmdutils.VerbCmd) { "cursors": {} }`, } - out = append(out, successOut, ArgError) + out = append(out, successOut, e.ArgError) - partitionedTopicInternalStatsError := Output{ + partitionedTopicInternalStatsError := pulsar.Output{ Desc: "the specified topic is not exist or the specified topic is a partitioned topic", Out: "[✖] code: 404 reason: Topic not found", } out = append(out, partitionedTopicInternalStatsError) - out = append(out, TopicNameErrors...) - out = append(out, NamespaceErrors...) + out = append(out, e.TopicNameErrors...) + out = append(out, e.NamespaceErrors...) desc.CommandOutput = out vc.SetDescription( @@ -103,7 +105,7 @@ func doGetInternalStats(vc *cmdutils.VerbCmd, partition int) error { return vc.NameError } - topic, err := GetTopicName(vc.NameArg) + topic, err := pulsar.GetTopicName(vc.NameArg) if err != nil { return err } @@ -118,7 +120,7 @@ func doGetInternalStats(vc *cmdutils.VerbCmd, partition int) error { admin := cmdutils.NewPulsarClient() stats, err := admin.Topics().GetInternalStats(*topic) if err == nil { - cmdutils.PrintJson(vc.Command.OutOrStdout(), stats) + cmdutils.PrintJSON(vc.Command.OutOrStdout(), stats) } return err diff --git a/pkg/ctl/topic/stats/stats_internal_test.go b/pkg/ctl/topic/stats/stats_internal_test.go index d83a6213..38c3bff3 100644 --- a/pkg/ctl/topic/stats/stats_internal_test.go +++ b/pkg/ctl/topic/stats/stats_internal_test.go @@ -19,20 +19,22 @@ package stats import ( "encoding/json" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "testing" + + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" - "testing" ) func TestGetInternalStatsCmd(t *testing.T) { args := []string{"create", "test-topic-internal-stats", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"internal-stats", "test-topic-internal-stats"} - out, execErr, _, _ := TestTopicCommands(GetInternalStatsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetInternalStatsCmd, args) assert.Nil(t, execErr) var stats pulsar.PersistentTopicInternalStats @@ -64,25 +66,25 @@ func TestGetInternalStatsCmd(t *testing.T) { func TestGetPartitionedTopicInternalStatsError(t *testing.T) { args := []string{"create", "test-partitioned-topic-internal-stats-error", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"internal-stats", "test-partitioned-topic-internal-stats-error"} - _, execErr, _, _ = TestTopicCommands(GetInternalStatsCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GetInternalStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } func TestGetInternalStatsArgsError(t *testing.T) { args := []string{"internal-stats"} - _, _, nameErr, _ := TestTopicCommands(GetInternalStatsCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetInternalStatsCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestGetNonExistingTopic(t *testing.T) { args := []string{"internal-stats", "non-existing-topic"} - _, execErr, _, _ := TestTopicCommands(GetInternalStatsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GetInternalStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } diff --git a/pkg/ctl/topic/stats/stats_test.go b/pkg/ctl/topic/stats/stats_test.go index 7acb0e11..c0517372 100644 --- a/pkg/ctl/topic/stats/stats_test.go +++ b/pkg/ctl/topic/stats/stats_test.go @@ -21,9 +21,10 @@ import ( "encoding/json" "testing" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/test" "github.com/streamnative/pulsarctl/pkg/pulsar" + "github.com/stretchr/testify/assert" ) @@ -42,11 +43,11 @@ var defaultStats = pulsar.TopicStats{ func TestGetStatsCmd(t *testing.T) { args := []string{"create", "test-non-partitioned-topic-stats", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"stats", "test-non-partitioned-topic-stats"} - out, execErr, _, _ := TestTopicCommands(GetStatsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetStatsCmd, args) assert.Nil(t, execErr) var stats pulsar.TopicStats @@ -60,36 +61,36 @@ func TestGetStatsCmd(t *testing.T) { func TestGetPartitionedStats(t *testing.T) { args := []string{"create", "test-partitioned-topic-stats", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"stats", "test-partitioned-topic-stats"} - _, execErr, _, _ = TestTopicCommands(GetStatsCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } func TestGetStatsArgsError(t *testing.T) { args := []string{"stats"} - _, _, nameErr, _ := TestTopicCommands(GetStatsCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestGetNonExistingTopicStats(t *testing.T) { args := []string{"stats", "non-existing-topic"} - _, execErr, _, _ := TestTopicCommands(GetStatsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Topic not found", execErr.Error()) } func TestGetPartitionedStatsCmd(t *testing.T) { args := []string{"create", "test-topic-partitioned-stats", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"stats", "--partitioned-topic", "test-topic-partitioned-stats"} - out, execErr, _, _ := TestTopicCommands(GetStatsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetStatsCmd, args) assert.Nil(t, execErr) var stats pulsar.PartitionedTopicStats @@ -115,11 +116,11 @@ func TestGetPartitionedStatsCmd(t *testing.T) { func TestGetPerPartitionedStatsCmd(t *testing.T) { args := []string{"create", "test-topic-per-partitioned-stats", "2"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"stats", "--partitioned-topic", "--per-partition", "test-topic-per-partitioned-stats"} - out, execErr, _, _ := TestTopicCommands(GetStatsCmd, args) + out, execErr, _, _ := test.TestTopicCommands(GetStatsCmd, args) assert.Nil(t, execErr) var stats pulsar.PartitionedTopicStats @@ -161,25 +162,25 @@ func TestGetPerPartitionedStatsCmd(t *testing.T) { func TestGetPartitionedStatsArgError(t *testing.T) { args := []string{"stats", "--partitioned-topic"} - _, _, nameErr, _ := TestTopicCommands(GetStatsCmd, args) + _, _, nameErr, _ := test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, nameErr) assert.Equal(t, "only one argument is allowed to be used as a name", nameErr.Error()) } func TestGetNonExistingTopicStatsError(t *testing.T) { args := []string{"stats", "--partitioned-topic", "non-existing-topic"} - _, execErr, _, _ := TestTopicCommands(GetStatsCmd, args) + _, execErr, _, _ := test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Partitioned Topic not found", execErr.Error()) } func TestGetNonPartitionedTopicStatsError(t *testing.T) { args := []string{"create", "test-non-partitioned-topic-partitioned-stats", "0"} - _, execErr, _, _ := TestTopicCommands(CreateTopicCmd, args) + _, execErr, _, _ := test.TestTopicCommands(crud.CreateTopicCmd, args) assert.Nil(t, execErr) args = []string{"stats", "--partitioned-topic", "test-non-partitioned-topic-partitioned-stats"} - _, execErr, _, _ = TestTopicCommands(GetStatsCmd, args) + _, execErr, _, _ = test.TestTopicCommands(GetStatsCmd, args) assert.NotNil(t, execErr) assert.Equal(t, "code: 404 reason: Partitioned Topic not found", execErr.Error()) } diff --git a/pkg/ctl/topic/test/test.go b/pkg/ctl/topic/test/test.go index 47b6a23f..60ac5b56 100644 --- a/pkg/ctl/topic/test/test.go +++ b/pkg/ctl/topic/test/test.go @@ -19,12 +19,15 @@ package test import ( "bytes" + + "github.com/streamnative/pulsarctl/pkg/cmdutils" + "github.com/kris-nova/logger" "github.com/spf13/cobra" - "github.com/streamnative/pulsarctl/pkg/cmdutils" ) -func TestTopicCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, execErr, nameErr, err error) { +func TestTopicCommands(newVerb func(cmd *cmdutils.VerbCmd), args []string) (out *bytes.Buffer, + execErr, nameErr, err error) { var execError error cmdutils.ExecErrorHandler = func(err error) { execError = err diff --git a/pkg/ctl/topic/topic.go b/pkg/ctl/topic/topic.go index 723a7301..f5fc07cc 100644 --- a/pkg/ctl/topic/topic.go +++ b/pkg/ctl/topic/topic.go @@ -18,13 +18,14 @@ package topic import ( - "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/info" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/lookup" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/permission" - . "github.com/streamnative/pulsarctl/pkg/ctl/topic/stats" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/crud" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/info" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/lookup" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/permission" + "github.com/streamnative/pulsarctl/pkg/ctl/topic/stats" + + "github.com/spf13/cobra" ) func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { @@ -35,20 +36,19 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { "topic") commands := []func(*cmdutils.VerbCmd){ - CreateTopicCmd, - DeleteTopicCmd, - GetTopicCmd, - ListTopicsCmd, - UpdateTopicCmd, - GetInternalInfoCmd, - GrantPermissionCmd, - RevokePermissions, - GetPermissionsCmd, - LookupTopicCmd, - GetBundleRangeCmd, - GetLastMessageIdCmd, - GetStatsCmd, - GetInternalStatsCmd, + crud.CreateTopicCmd, + crud.DeleteTopicCmd, + crud.GetTopicCmd, + crud.ListTopicsCmd, + crud.UpdateTopicCmd, + permission.GrantPermissionCmd, + permission.RevokePermissions, + permission.GetPermissionsCmd, + lookup.TopicCmd, + lookup.GetBundleRangeCmd, + info.GetLastMessageIDCmd, + stats.GetStatsCmd, + stats.GetInternalStatsCmd, } cmdutils.AddVerbCmds(flagGrouping, resourceCmd, commands...) diff --git a/pkg/ctl/utils/util.go b/pkg/ctl/utils/util.go index 5be76eb0..a978155e 100644 --- a/pkg/ctl/utils/util.go +++ b/pkg/ctl/utils/util.go @@ -19,12 +19,14 @@ package utils import ( "fmt" - "github.com/pkg/errors" - "github.com/streamnative/pulsarctl/pkg/pulsar" "os" "strconv" "strings" "time" + + "github.com/streamnative/pulsarctl/pkg/pulsar" + + "github.com/pkg/errors" ) const ( @@ -36,9 +38,9 @@ const ( DefaultNamespace = "default" ) -func IsPackageUrlSupported(functionPkgUrl string) bool { - return functionPkgUrl != "" && strings.HasPrefix(functionPkgUrl, HTTP) || - strings.HasPrefix(functionPkgUrl, FILE) +func IsPackageURLSupported(functionPkgURL string) bool { + return functionPkgURL != "" && strings.HasPrefix(functionPkgURL, HTTP) || + strings.HasPrefix(functionPkgURL, FILE) } func InferMissingFunctionName(funcConf *pulsar.FunctionConfig) { @@ -128,14 +130,14 @@ func ValidateSizeString(s string) (int64, error) { func ParseRelativeTimeInSeconds(relativeTime string) (time.Duration, error) { if relativeTime == "" { - return -1, errors.New("Time can not be empty.") + return -1, errors.New("time can not be empty") } unitTime := relativeTime[len(relativeTime)-1:] t := relativeTime[:len(relativeTime)-1] timeValue, err := strconv.ParseInt(t, 10, 64) if err != nil { - return -1, errors.Errorf("Invalid time '%s'", t) + return -1, errors.Errorf("invalid time '%s'", t) } switch strings.ToLower(unitTime) { @@ -152,6 +154,6 @@ func ParseRelativeTimeInSeconds(relativeTime string) (time.Duration, error) { case "y": return time.Duration(timeValue) * time.Hour * 24 * 7 * 365, nil default: - return -1, errors.Errorf("Invalid time unit '%s'", unitTime) + return -1, errors.Errorf("invalid time unit '%s'", unitTime) } } diff --git a/pkg/pulsar/Tenant.go b/pkg/pulsar/Tenant.go index 1b3a67e0..53d9ba55 100644 --- a/pkg/pulsar/Tenant.go +++ b/pkg/pulsar/Tenant.go @@ -26,30 +26,30 @@ type Tenants interface { } type tenants struct { - client *client + client *client basePath string } func (c *client) Tenants() Tenants { return &tenants{ - client: c, + client: c, basePath: "/tenants", } } func (c *tenants) Create(data TenantData) error { endpoint := c.client.endpoint(c.basePath, data.Name) - return c.client.put(endpoint, &data, nil) + return c.client.put(endpoint, &data) } func (c *tenants) Delete(name string) error { endpoint := c.client.endpoint(c.basePath, name) - return c.client.delete(endpoint, nil) + return c.client.delete(endpoint) } func (c *tenants) Update(data TenantData) error { endpoint := c.client.endpoint(c.basePath, data.Name) - return c.client.post(endpoint, &data, nil) + return c.client.post(endpoint, &data) } func (c *tenants) List() ([]string, error) { diff --git a/pkg/pulsar/WindowConfing.go b/pkg/pulsar/WindowConfing.go index cf96ff0c..d1235f8d 100644 --- a/pkg/pulsar/WindowConfing.go +++ b/pkg/pulsar/WindowConfing.go @@ -17,23 +17,22 @@ package pulsar -const WindowConfigKey = "__WINDOWCONFIGS__" +const WindowConfigKey = "__WINDOWCONFIGS__" type WindowConfig struct { - WindowLengthCount int - WindowLengthDurationMs int64 - SlidingIntervalCount int - SlidingIntervalDurationMs int64 - LateDataTopic string - MaxLagMs int64 - WatermarkEmitIntervalMs int64 - TimestampExtractorClassName string - ActualWindowFunctionClassName string + WindowLengthCount int + WindowLengthDurationMs int64 + SlidingIntervalCount int + SlidingIntervalDurationMs int64 + LateDataTopic string + MaxLagMs int64 + WatermarkEmitIntervalMs int64 + TimestampExtractorClassName string + ActualWindowFunctionClassName string } func NewDefaultWindowConfing() *WindowConfig { - windowConfig := &WindowConfig{} + windowConfig := &WindowConfig{} - return windowConfig + return windowConfig } - diff --git a/pkg/pulsar/admin.go b/pkg/pulsar/admin.go index 93e542a1..f36cbb3d 100644 --- a/pkg/pulsar/admin.go +++ b/pkg/pulsar/admin.go @@ -40,13 +40,13 @@ const ( // Config is used to configure the admin client type Config struct { - WebServiceUrl string - HttpClient *http.Client - ApiVersion ApiVersion + WebServiceURL string + HTTPClient *http.Client + APIVersion APIVersion - Auth *auth.TlsAuthProvider + Auth *auth.TLSAuthProvider AuthParams string - TlsOptions *TLSOptions + TLSOptions *TLSOptions } type TLSOptions struct { @@ -57,10 +57,10 @@ type TLSOptions struct { // DefaultConfig returns a default configuration for the pulsar admin client func DefaultConfig() *Config { config := &Config{ - WebServiceUrl: DefaultWebServiceURL, - HttpClient: http.DefaultClient, + WebServiceURL: DefaultWebServiceURL, + HTTPClient: http.DefaultClient, - TlsOptions: &TLSOptions{ + TLSOptions: &TLSOptions{ AllowInsecureConnection: false, }, } @@ -80,12 +80,12 @@ type Client interface { } type client struct { - webServiceUrl string + webServiceURL string apiVersion string httpClient *http.Client // TLS config - auth *auth.TlsAuthProvider + auth *auth.TLSAuthProvider authParams string tlsOptions *TLSOptions transport *http.Transport @@ -93,18 +93,18 @@ type client struct { // New returns a new client func New(config *Config) (Client, error) { - if len(config.WebServiceUrl) == 0 { - config.WebServiceUrl = DefaultWebServiceURL + if len(config.WebServiceURL) == 0 { + config.WebServiceURL = DefaultWebServiceURL } c := &client{ - apiVersion: config.ApiVersion.String(), - webServiceUrl: config.WebServiceUrl, + apiVersion: config.APIVersion.String(), + webServiceURL: config.WebServiceURL, } - if strings.HasPrefix(c.webServiceUrl, "https://") { + if strings.HasPrefix(c.webServiceURL, "https://") { c.authParams = config.AuthParams - c.tlsOptions = config.TlsOptions + c.tlsOptions = config.TLSOptions mapAuthParams := make(map[string]string) err := json.Unmarshal([]byte(c.authParams), &mapAuthParams) @@ -158,13 +158,14 @@ func (c *client) getTLSConfig() (*tls.Config, error) { } func (c *client) endpoint(componentPath string, parts ...string) string { - return path.Join(makeHttpPath(c.apiVersion, componentPath), endpoint(parts...)) + return path.Join(makeHTTPPath(c.apiVersion, componentPath), endpoint(parts...)) } // get is used to do a GET request against an endpoint // and deserialize the response into an interface -func (c *client) getWithQueryParams(endpoint string, obj interface{}, params map[string]string, decode bool) ([]byte, error) { +func (c *client) getWithQueryParams(endpoint string, obj interface{}, params map[string]string, + decode bool) ([]byte, error) { req, err := c.newRequest(http.MethodGet, endpoint) if err != nil { @@ -186,7 +187,7 @@ func (c *client) getWithQueryParams(endpoint string, obj interface{}, params map defer safeRespClose(resp) if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { + if err := decodeJSONBody(resp, &obj); err != nil { return nil, err } } else if !decode { @@ -205,8 +206,8 @@ func (c *client) get(endpoint string, obj interface{}) error { return err } -func (c *client) put(endpoint string, in, obj interface{}) error { - return c.putWithQueryParams(endpoint, in, obj, nil) +func (c *client) put(endpoint string, in interface{}) error { + return c.putWithQueryParams(endpoint, in, nil, nil) } func (c *client) putWithQueryParams(endpoint string, in, obj interface{}, params map[string]string) error { @@ -231,7 +232,7 @@ func (c *client) putWithQueryParams(endpoint string, in, obj interface{}, params defer safeRespClose(resp) if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { + if err := decodeJSONBody(resp, &obj); err != nil { return err } } @@ -239,8 +240,8 @@ func (c *client) putWithQueryParams(endpoint string, in, obj interface{}, params return nil } -func (c *client) delete(endpoint string, obj interface{}) error { - return c.deleteWithQueryParams(endpoint, obj, nil) +func (c *client) delete(endpoint string) error { + return c.deleteWithQueryParams(endpoint, nil, nil) } func (c *client) deleteWithQueryParams(endpoint string, obj interface{}, params map[string]string) error { @@ -264,7 +265,7 @@ func (c *client) deleteWithQueryParams(endpoint string, obj interface{}, params defer safeRespClose(resp) if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { + if err := decodeJSONBody(resp, &obj); err != nil { return err } } @@ -272,50 +273,42 @@ func (c *client) deleteWithQueryParams(endpoint string, obj interface{}, params return nil } -func (c *client) post(endpoint string, in, obj interface{}) error { +func (c *client) post(endpoint string, in interface{}) error { req, err := c.newRequest(http.MethodPost, endpoint) if err != nil { return err } req.obj = in + + // nolint resp, err := checkSuccessful(c.doRequest(req)) if err != nil { return err } defer safeRespClose(resp) - if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { - return err - } - } + return nil } -func (c *client) putWithMultiPart(endpoint string, in, obj interface{}, body io.Reader, contentType string) error { +func (c *client) putWithMultiPart(endpoint string, body io.Reader, contentType string) error { req, err := c.newRequest(http.MethodPut, endpoint) if err != nil { return err } - req.obj = in req.body = body req.contentType = contentType + // nolint resp, err := checkSuccessful(c.doRequest(req)) if err != nil { return err } defer safeRespClose(resp) - if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { - return err - } - } - return nil } -func (c *client) postWithMultiPart(endpoint string, in, obj interface{}, body io.Reader, contentType string) error { +func (c *client) postWithMultiPart(endpoint string, in interface{}, body io.Reader, contentType string) error { req, err := c.newRequest(http.MethodPost, endpoint) if err != nil { return err @@ -324,18 +317,13 @@ func (c *client) postWithMultiPart(endpoint string, in, obj interface{}, body io req.body = body req.contentType = contentType + // nolint resp, err := checkSuccessful(c.doRequest(req)) if err != nil { return err } defer safeRespClose(resp) - if obj != nil { - if err := decodeJsonBody(resp, &obj); err != nil { - return err - } - } - return nil } @@ -354,7 +342,7 @@ func (r *request) toHTTP() (*http.Request, error) { // add a request body if there is one if r.body == nil && r.obj != nil { - body, err := encodeJsonBody(r.obj) + body, err := encodeJSONBody(r.obj) if err != nil { return nil, err } @@ -373,7 +361,7 @@ func (r *request) toHTTP() (*http.Request, error) { } func (c *client) newRequest(method, path string) (*request, error) { - base, _ := url.Parse(c.webServiceUrl) + base, _ := url.Parse(c.webServiceURL) u, err := url.Parse(path) if err != nil { return nil, err @@ -424,8 +412,8 @@ func (c *client) doRequest(r *request) (*http.Response, error) { return hc.Do(req) } -// decodeJsonBody is used to JSON encode a body -func encodeJsonBody(obj interface{}) (io.Reader, error) { +// encodeJSONBody is used to JSON encode a body +func encodeJSONBody(obj interface{}) (io.Reader, error) { buf := bytes.NewBuffer(nil) enc := json.NewEncoder(buf) if err := enc.Encode(obj); err != nil { @@ -434,18 +422,17 @@ func encodeJsonBody(obj interface{}) (io.Reader, error) { return buf, nil } -// decodeJsonBody is used to JSON decode a body -func decodeJsonBody(resp *http.Response, out interface{}) error { +// decodeJSONBody is used to JSON decode a body +func decodeJSONBody(resp *http.Response, out interface{}) error { dec := json.NewDecoder(resp.Body) return dec.Decode(out) } -// safeRespClose is used to close a respone body +// safeRespClose is used to close a response body func safeRespClose(resp *http.Response) { if resp != nil { - if err := resp.Body.Close(); err != nil { - // ignore error since it is closing a response body - } + // ignore error since it is closing a response body + _ = resp.Body.Close() } } diff --git a/pkg/pulsar/api_version.go b/pkg/pulsar/api_version.go index 07c52972..70963d94 100644 --- a/pkg/pulsar/api_version.go +++ b/pkg/pulsar/api_version.go @@ -17,17 +17,17 @@ package pulsar -type ApiVersion int +type APIVersion int const ( - V1 ApiVersion = iota + V1 APIVersion = iota V2 V3 ) -const DefaultApiVersion = "v2" +const DefaultAPIVersion = "v2" -func (v ApiVersion) String() string { +func (v APIVersion) String() string { switch v { case V1: return "" @@ -37,5 +37,5 @@ func (v ApiVersion) String() string { return "v3" } - return DefaultApiVersion + return DefaultAPIVersion } diff --git a/pkg/pulsar/api_version_test.go b/pkg/pulsar/api_version_test.go index 2a0c3a88..adcddccf 100644 --- a/pkg/pulsar/api_version_test.go +++ b/pkg/pulsar/api_version_test.go @@ -18,12 +18,13 @@ package pulsar import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestApiVersion_String(t *testing.T) { assert.Equal(t, "", V1.String()) assert.Equal(t, "v2", V2.String()) assert.Equal(t, "v3", V3.String()) -} \ No newline at end of file +} diff --git a/pkg/pulsar/backlog_quota.go b/pkg/pulsar/backlog_quota.go index 8d83b787..c3ece4cf 100644 --- a/pkg/pulsar/backlog_quota.go +++ b/pkg/pulsar/backlog_quota.go @@ -36,7 +36,7 @@ type BacklogQuotaType string const DestinationStorage BacklogQuotaType = "destination_storage" const ( - ProducerRequestHold RetentionPolicy = "producer_request_hold" - ProducerException RetentionPolicy = "producer_exception" + ProducerRequestHold RetentionPolicy = "producer_request_hold" + ProducerException RetentionPolicy = "producer_exception" ConsumerBacklogEviction RetentionPolicy = "consumer_backlog_eviction" ) diff --git a/pkg/pulsar/cluster.go b/pkg/pulsar/cluster.go index fcd3cd70..9b486825 100644 --- a/pkg/pulsar/cluster.go +++ b/pkg/pulsar/cluster.go @@ -61,17 +61,17 @@ func (c *clusters) Get(name string) (ClusterData, error) { func (c *clusters) Create(cdata ClusterData) error { endpoint := c.client.endpoint(c.basePath, cdata.Name) - return c.client.put(endpoint, &cdata, nil) + return c.client.put(endpoint, &cdata) } func (c *clusters) Delete(name string) error { endpoint := c.client.endpoint(c.basePath, name) - return c.client.delete(endpoint, nil) + return c.client.delete(endpoint) } func (c *clusters) Update(cdata ClusterData) error { endpoint := c.client.endpoint(c.basePath, cdata.Name) - return c.client.post(endpoint, &cdata, nil) + return c.client.post(endpoint, &cdata) } func (c *clusters) GetPeerClusters(name string) ([]string, error) { @@ -83,12 +83,12 @@ func (c *clusters) GetPeerClusters(name string) ([]string, error) { func (c *clusters) UpdatePeerClusters(cluster string, peerClusters []string) error { endpoint := c.client.endpoint(c.basePath, cluster, "peers") - return c.client.post(endpoint, peerClusters, nil) + return c.client.post(endpoint, peerClusters) } func (c *clusters) CreateFailureDomain(data FailureDomainData) error { endpoint := c.client.endpoint(c.basePath, data.ClusterName, "failureDomains", data.DomainName) - return c.client.post(endpoint, &data, nil) + return c.client.post(endpoint, &data) } func (c *clusters) GetFailureDomain(clusterName string, domainName string) (FailureDomainData, error) { @@ -107,9 +107,9 @@ func (c *clusters) ListFailureDomains(clusterName string) (FailureDomainMap, err func (c *clusters) DeleteFailureDomain(data FailureDomainData) error { endpoint := c.client.endpoint(c.basePath, data.ClusterName, "failureDomains", data.DomainName) - return c.client.delete(endpoint, nil) + return c.client.delete(endpoint) } func (c *clusters) UpdateFailureDomain(data FailureDomainData) error { endpoint := c.client.endpoint(c.basePath, data.ClusterName, "failureDomains", data.DomainName) - return c.client.post(endpoint, &data, nil) + return c.client.post(endpoint, &data) } diff --git a/pkg/pulsar/connectorDefinition.go b/pkg/pulsar/connectorDefinition.go index 4585e355..71b3aad2 100644 --- a/pkg/pulsar/connectorDefinition.go +++ b/pkg/pulsar/connectorDefinition.go @@ -19,17 +19,17 @@ package pulsar // Basic information about a Pulsar connector type ConnectorDefinition struct { - // The name of the connector type - Name string `json:"name"` + // The name of the connector type + Name string `json:"name"` - // Description to be used for user help - Description string `json:"description"` + // Description to be used for user help + Description string `json:"description"` - // The class name for the connector source implementation - //

If not defined, it will be assumed this connector cannot act as a data source - SourceClass string `json:"sourceClass"` + // The class name for the connector source implementation + //

If not defined, it will be assumed this connector cannot act as a data source + SourceClass string `json:"sourceClass"` - // The class name for the connector sink implementation - //

If not defined, it will be assumed this connector cannot act as a data sink - SinkClass string `json:"sinkClass"` + // The class name for the connector sink implementation + //

If not defined, it will be assumed this connector cannot act as a data sink + SinkClass string `json:"sinkClass"` } diff --git a/pkg/pulsar/consumerConfig.go b/pkg/pulsar/consumerConfig.go index 6d334aff..5cf84971 100644 --- a/pkg/pulsar/consumerConfig.go +++ b/pkg/pulsar/consumerConfig.go @@ -18,8 +18,8 @@ package pulsar type ConsumerConfig struct { - SchemaType string - SerdeClassName string - IsRegexPattern bool - ReceiverQueueSize int + SchemaType string + SerdeClassName string + IsRegexPattern bool + ReceiverQueueSize int } diff --git a/pkg/pulsar/data.go b/pkg/pulsar/data.go index 18cbf299..ea40c19d 100644 --- a/pkg/pulsar/data.go +++ b/pkg/pulsar/data.go @@ -29,52 +29,51 @@ type ClusterData struct { // FunctionData information for a Pulsar Function type FunctionData struct { - FQFN string `json:"fqfn"` - Tenant string `json:"tenant"` - Namespace string `json:"namespace"` - FuncName string `json:"functionName"` - InstanceID string `json:"instance_id"` - ClassName string `json:"className"` - Jar string `json:"jarFile"` - Py string `json:"pyFile"` - Go string `json:"goFile"` - Inputs string `json:"inputs"` - TopicsPattern string `json:"topicsPattern"` - Output string `json:"output"` - LogTopic string `json:"logTopic"` - SchemaType string `json:"schemaType"` - CustomSerDeInputs string `json:"customSerdeInputString"` - CustomSchemaInput string `json:"customSchemaInputString"` - OutputSerDeClassName string `json:"outputSerdeClassName"` - FunctionConfigFile string `json:"fnConfigFile"` - ProcessingGuarantees string `json:"processingGuarantees"` - UserConfig string `json:"userConfigString"` - RetainOrdering bool `json:"retainOrdering"` - SubsName string `json:"subsName"` - Parallelism int `json:"parallelism"` - CPU float64 `json:"cpu"` + UpdateAuthData bool `json:"updateAuthData"` + RetainOrdering bool `json:"retainOrdering"` + Watch bool `json:"watch"` + AutoAck bool `json:"autoAck"` + Parallelism int `json:"parallelism"` + WindowLengthCount int `json:"windowLengthCount"` + SlidingIntervalCount int `json:"slidingIntervalCount"` + MaxMessageRetries int `json:"maxMessageRetries"` + + TimeoutMs int64 `json:"timeoutMs"` + SlidingIntervalDurationMs int64 `json:"slidingIntervalDurationMs"` + WindowLengthDurationMs int64 `json:"windowLengthDurationMs"` RAM int64 `json:"ram"` Disk int64 `json:"disk"` - WindowLengthCount int `json:"windowLengthCount"` - WindowLengthDurationMs int64 `json:"windowLengthDurationMs"` - SlidingIntervalCount int `json:"slidingIntervalCount"` - SlidingIntervalDurationMs int64 `json:"slidingIntervalDurationMs"` - AutoAck bool `json:"autoAck"` - TimeoutMs int64 `json:"timeoutMs"` - MaxMessageRetries int `json:"maxMessageRetries"` + CPU float64 `json:"cpu"` + SubsName string `json:"subsName"` DeadLetterTopic string `json:"deadLetterTopic"` + Key string `json:"key"` + State string `json:"state"` + TriggerValue string `json:"triggerValue"` + TriggerFile string `json:"triggerFile"` + Topic string `json:"topic"` - Key string `json:"key"` - Watch bool `json:"watch"` - State string `json:"state"` - TriggerValue string `json:"triggerValue"` - TriggerFile string `json:"triggerFile"` - Topic string `json:"topic"` - - UpdateAuthData bool `json:"updateAuthData"` - - FuncConf *FunctionConfig `json:"-"` - UserCodeFile string `json:"-"` + UserCodeFile string `json:"-"` + FQFN string `json:"fqfn"` + Tenant string `json:"tenant"` + Namespace string `json:"namespace"` + FuncName string `json:"functionName"` + InstanceID string `json:"instance_id"` + ClassName string `json:"className"` + Jar string `json:"jarFile"` + Py string `json:"pyFile"` + Go string `json:"goFile"` + Inputs string `json:"inputs"` + TopicsPattern string `json:"topicsPattern"` + Output string `json:"output"` + LogTopic string `json:"logTopic"` + SchemaType string `json:"schemaType"` + CustomSerDeInputs string `json:"customSerdeInputString"` + CustomSchemaInput string `json:"customSchemaInputString"` + OutputSerDeClassName string `json:"outputSerdeClassName"` + FunctionConfigFile string `json:"fnConfigFile"` + ProcessingGuarantees string `json:"processingGuarantees"` + UserConfig string `json:"userConfigString"` + FuncConf *FunctionConfig `json:"-"` } // Failure Domain information @@ -118,32 +117,30 @@ type SourceData struct { } type SinkData struct { - Tenant string `json:"tenant"` - Namespace string `json:"namespace"` - Name string `json:"name"` - SinkType string `json:"sinkType"` - Inputs string `json:"inputs"` - TopicsPattern string `json:"topicsPattern"` - SubsName string `json:"subsName"` - CustomSerdeInputString string `json:"customSerdeInputString"` - CustomSchemaInputString string `json:"customSchemaInputString"` - ProcessingGuarantees string `json:"processingGuarantees"` - RetainOrdering bool `json:"retainOrdering"` - Parallelism int `json:"parallelism"` - Archive string `json:"archive"` - ClassName string `json:"className"` - SinkConfigFile string `json:"sinkConfigFile"` - CPU float64 `json:"cpu"` - RAM int64 `json:"ram"` - Disk int64 `json:"disk"` - SinkConfigString string `json:"sinkConfigString"` - AutoAck bool `json:"autoAck"` - TimeoutMs int64 `json:"timeoutMs"` - - SinkConf *SinkConfig `json:"-"` - InstanceID string `json:"instanceId"` - - UpdateAuthData bool `json:"updateAuthData"` + UpdateAuthData bool `json:"updateAuthData"` + RetainOrdering bool `json:"retainOrdering"` + AutoAck bool `json:"autoAck"` + Parallelism int `json:"parallelism"` + RAM int64 `json:"ram"` + Disk int64 `json:"disk"` + TimeoutMs int64 `json:"timeoutMs"` + CPU float64 `json:"cpu"` + Tenant string `json:"tenant"` + Namespace string `json:"namespace"` + Name string `json:"name"` + SinkType string `json:"sinkType"` + Inputs string `json:"inputs"` + TopicsPattern string `json:"topicsPattern"` + SubsName string `json:"subsName"` + CustomSerdeInputString string `json:"customSerdeInputString"` + CustomSchemaInputString string `json:"customSchemaInputString"` + ProcessingGuarantees string `json:"processingGuarantees"` + Archive string `json:"archive"` + ClassName string `json:"className"` + SinkConfigFile string `json:"sinkConfigFile"` + SinkConfigString string `json:"sinkConfigString"` + InstanceID string `json:"instanceId"` + SinkConf *SinkConfig `json:"-"` } // Topic data @@ -159,24 +156,25 @@ type ManagedLedgerInfo struct { TerminatedPosition PositionInfo `json:"terminatedPosition"` Cursors map[string]CursorInfo `json:"cursors"` } - type NamespacesData struct { + +type NamespacesData struct { + Enable bool `json:"enable"` + Unload bool `json:"unload"` NumBundles int `json:"numBundles"` - Clusters []string `json:"clusters"` - ClusterIds string `json:"clusterIds"` + BookkeeperEnsemble int `json:"bookkeeperEnsemble"` + BookkeeperWriteQuorum int `json:"bookkeeperWriteQuorum"` MessageTTL int `json:"messageTTL"` + BookkeeperAckQuorum int `json:"bookkeeperAckQuorum"` + ManagedLedgerMaxMarkDeleteRate float64 `json:"managedLedgerMaxMarkDeleteRate"` + ClusterIds string `json:"clusterIds"` RetentionTimeStr string `json:"retentionTimeStr"` LimitStr string `json:"limitStr"` PolicyStr string `json:"policyStr"` AntiAffinityGroup string `json:"antiAffinityGroup"` Tenant string `json:"tenant"` Cluster string `json:"cluster"` - BookkeeperEnsemble int `json:"bookkeeperEnsemble"` - BookkeeperWriteQuorum int `json:"bookkeeperWriteQuorum"` - BookkeeperAckQuorum int `json:"bookkeeperAckQuorum"` - ManagedLedgerMaxMarkDeleteRate float64 `json:"managedLedgerMaxMarkDeleteRate"` - Enable bool `json:"enable"` Bundle string `json:"bundle"` - Unload bool `json:"unload"` + Clusters []string `json:"clusters"` } type TopicStats struct { @@ -193,7 +191,7 @@ type TopicStats struct { } type PublisherStats struct { - ProducerId int64 `json:"producerId"` + ProducerID int64 `json:"producerId"` MsgRateIn float64 `json:"msgRateIn"` MsgThroughputIn float64 `json:"msgThroughputIn"` AverageMsgSize float64 `json:"averageMsgSize"` @@ -201,39 +199,39 @@ type PublisherStats struct { } type SubscriptionStats struct { + BlockedSubscriptionOnUnackedMsgs bool `json:"blockedSubscriptionOnUnackedMsgs"` + IsReplicated bool `json:"isReplicated"` MsgRateOut float64 `json:"msgRateOut"` MsgThroughputOut float64 `json:"msgThroughputOut"` MsgRateRedeliver float64 `json:"msgRateRedeliver"` MsgRateExpired float64 `json:"msgRateExpired"` MsgBacklog int64 `json:"msgBacklog"` - BlockedSubscriptionOnUnackedMsgs bool `json:"blockedSubscriptionOnUnackedMsgs"` MsgDelayed int64 `json:"msgDelayed"` - unAckedMessages int64 `json:"unackedMessages"` + UnAckedMessages int64 `json:"unackedMessages"` SubType string `json:"type"` ActiveConsumerName string `json:"activeConsumerName"` Consumers []ConsumerStats `json:"consumers"` - IsReplicated bool `json:"isReplicated"` } type ConsumerStats struct { - ConsumerName string `json:"consumerName"` + BlockedConsumerOnUnAckedMsgs bool `json:"blockedConsumerOnUnackedMsgs"` + AvailablePermits int `json:"availablePermits"` + UnAckedMessages int `json:"unackedMessages"` MsgRateOut float64 `json:"msgRateOut"` MsgThroughputOut float64 `json:"msgThroughputOut"` MsgRateRedeliver float64 `json:"msgRateRedeliver"` - AvailablePermits int `json:"availablePermits"` - UnAckedMessages int `json:"unackedMessages"` - BlockedConsumerOnUnAckedMsgs bool `json:"blockedConsumerOnUnackedMsgs"` + ConsumerName string `json:"consumerName"` Metadata map[string]string `json:"metadata"` } type ReplicatorStats struct { + Connected bool `json:"connected"` MsgRateIn float64 `json:"msgRateIn"` MsgRateOut float64 `json:"msgRateOut"` MsgThroughputIn float64 `json:"msgThroughputIn"` MsgThroughputOut float64 `json:"msgThroughputOut"` MsgRateExpired float64 `json:"msgRateExpired"` ReplicationBacklog int64 `json:"replicationBacklog"` - Connected bool `json:"connected"` ReplicationDelayInSeconds int64 `json:"replicationDelayInSeconds"` InboundConnection string `json:"inboundConnection"` InboundConnectedSince string `json:"inboundConnectedSince"` @@ -242,6 +240,8 @@ type ReplicatorStats struct { } type PersistentTopicInternalStats struct { + WaitingCursorsCount int `json:"waitingCursorsCount"` + PendingAddEntriesCount int `json:"pendingAddEntriesCount"` EntriesAddedCounter int64 `json:"entriesAddedCounter"` NumberOfEntries int64 `json:"numberOfEntries"` TotalSize int64 `json:"totalSize"` @@ -249,8 +249,6 @@ type PersistentTopicInternalStats struct { CurrentLedgerSize int64 `json:"currentLedgerSize"` LastLedgerCreatedTimestamp string `json:"lastLedgerCreatedTimestamp"` LastLedgerCreationFailureTimestamp string `json:"lastLedgerCreationFailureTimestamp"` - WaitingCursorsCount int `json:"waitingCursorsCount"` - PendingAddEntriesCount int `json:"pendingAddEntriesCount"` LastConfirmedEntry string `json:"lastConfirmedEntry"` State string `json:"state"` Ledgers []LedgerInfo `json:"ledgers"` @@ -258,7 +256,7 @@ type PersistentTopicInternalStats struct { } type LedgerInfo struct { - LedgerId int64 `json:"ledgerId"` + LedgerID int64 `json:"ledgerId"` Entries int64 `json:"entries"` Size int64 `json:"size"` Timestamp int64 `json:"timestamp"` @@ -268,21 +266,21 @@ type CursorInfo struct { Version int `json:"version"` CreationDate string `json:"creationDate"` ModificationDate string `json:"modificationDate"` - CursorsLedgerId int64 `json:"cursorsLedgerId"` + CursorsLedgerID int64 `json:"cursorsLedgerId"` MarkDelete PositionInfo `json:"markDelete"` IndividualDeletedMessages []MessageRangeInfo `json:"individualDeletedMessages"` Properties map[string]int64 } type PositionInfo struct { - LedgerId int64 `json:"ledgerId"` - EntryId int64 `json:"entryId"` + LedgerID int64 `json:"ledgerId"` + EntryID int64 `json:"entryId"` } type MessageRangeInfo struct { - From PositionInfo `json:"from"` - To PositionInfo `json:"to"` - Offloaded bool `json:"offloaded"` + From PositionInfo `json:"from"` + To PositionInfo `json:"to"` + Offloaded bool `json:"offloaded"` } type CursorStats struct { @@ -327,8 +325,8 @@ type SchemaData struct { } type LookupData struct { - BrokerUrl string `json:"brokerUrl"` - BrokerUrlTls string `json:"brokerUrlTls"` - HttpUrl string `json:"httpUrl"` - HttpUrlTls string `json:"httpUrlTls"` + BrokerURL string `json:"brokerUrl"` + BrokerURLTLS string `json:"brokerUrlTls"` + HTTPURL string `json:"httpUrl"` + HTTPURLTLS string `json:"httpUrlTls"` } diff --git a/pkg/pulsar/descriptions.go b/pkg/pulsar/descriptions.go index 991108ed..9a5a3e16 100644 --- a/pkg/pulsar/descriptions.go +++ b/pkg/pulsar/descriptions.go @@ -20,7 +20,7 @@ package pulsar import "strings" var SPACES = " " -var USED_FOR = "USED FOR:" +var USEDFOR = "USED FOR:" var PERMISSION = "REQUIRED PERMISSION:" var EXAMPLES = "EXAMPLES:" var OUTPUT = "OUTPUT:" @@ -43,7 +43,7 @@ type Output struct { } func (desc *LongDescription) ToString() string { - return USED_FOR + "\n" + + return USEDFOR + "\n" + SPACES + desc.CommandUsedFor + "\n\n" + PERMISSION + "\n" + SPACES + desc.CommandPermission + "\n\n" + diff --git a/pkg/pulsar/descriptions_test.go b/pkg/pulsar/descriptions_test.go index 7a0d65f0..23ba0d82 100644 --- a/pkg/pulsar/descriptions_test.go +++ b/pkg/pulsar/descriptions_test.go @@ -18,8 +18,9 @@ package pulsar import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestLongDescription_exampleToString(t *testing.T) { diff --git a/pkg/pulsar/errors.go b/pkg/pulsar/errors.go index ae3a317c..10ba9503 100644 --- a/pkg/pulsar/errors.go +++ b/pkg/pulsar/errors.go @@ -22,8 +22,8 @@ import "fmt" const unknownErrorReason = "Unknown pulsar error" type Error struct { - Reason string `json:"reason"` - Code int + Reason string `json:"reason"` + Code int } func (e Error) Error() string { diff --git a/pkg/pulsar/functionConfg.go b/pkg/pulsar/functionConfg.go index 13eb356e..0590ec11 100644 --- a/pkg/pulsar/functionConfg.go +++ b/pkg/pulsar/functionConfg.go @@ -18,60 +18,60 @@ package pulsar const ( - JavaRuntime = "JAVA" - PythonRuntime = "PYTHON" - GoRuntime = "GO" + JavaRuntime = "JAVA" + PythonRuntime = "PYTHON" + GoRuntime = "GO" ) type FunctionConfig struct { - // Any flags that you want to pass to the runtime. - // note that in thread mode, these flags will have no impact - RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` + TimeoutMs *int64 `json:"timeoutMs" yaml:"timeoutMs"` + TopicsPattern *string `json:"topicsPattern" yaml:"topicsPattern"` + // Whether the subscriptions the functions created/used should be deleted when the functions is deleted + CleanupSubscription bool `json:"cleanupSubscription" yaml:"cleanupSubscription"` + RetainOrdering bool `json:"retainOrdering" yaml:"retainOrdering"` + AutoAck bool `json:"autoAck" yaml:"autoAck"` + Parallelism int `json:"parallelism" yaml:"parallelism"` + MaxMessageRetries int `json:"maxMessageRetries" yaml:"maxMessageRetries"` - Tenant string `json:"tenant" yaml:"tenant"` - Namespace string `json:"namespace" yaml:"namespace"` - Name string `json:"name" yaml:"name"` - ClassName string `json:"className" yaml:"className"` + Output string `json:"output" yaml:"output"` - Inputs []string `json:"inputs" yaml:"inputs"` - CustomSerdeInputs map[string]string `json:"customSerdeInputs" yaml:"customSerdeInputs"` - TopicsPattern *string `json:"topicsPattern" yaml:"topicsPattern"` - CustomSchemaInputs map[string]string `json:"customSchemaInputs" yaml:"customSchemaInputs"` + OutputSerdeClassName string `json:"outputSerdeClassName" yaml:"outputSerdeClassName"` + LogTopic string `json:"logTopic" yaml:"logTopic"` + ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` - // A generalized way of specifying inputs - InputSpecs map[string]ConsumerConfig `json:"inputSpecs" yaml:"inputSpecs"` + // Represents either a builtin schema type (eg: 'avro', 'json', etc) or the class name for a Schema implementation + OutputSchemaType string `json:"outputSchemaType" yaml:"outputSchemaType"` - Output string `json:"output" yaml:"output"` + Runtime string `json:"runtime" yaml:"runtime"` + DeadLetterTopic string `json:"deadLetterTopic" yaml:"deadLetterTopic"` + SubName string `json:"subName" yaml:"subName"` + FQFN string `json:"fqfn" yaml:"fqfn"` + Jar string `json:"jar" yaml:"jar"` + Py string `json:"py" yaml:"py"` + Go string `json:"go" yaml:"go"` + // Any flags that you want to pass to the runtime. + // note that in thread mode, these flags will have no impact + RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` - // Represents either a builtin schema type (eg: 'avro', 'json', ect) or the class name for a Schema implementation - OutputSchemaType string `json:"outputSchemaType" yaml:"outputSchemaType"` + Tenant string `json:"tenant" yaml:"tenant"` + Namespace string `json:"namespace" yaml:"namespace"` + Name string `json:"name" yaml:"name"` + ClassName string `json:"className" yaml:"className"` - OutputSerdeClassName string `json:"outputSerdeClassName" yaml:"outputSerdeClassName"` - LogTopic string `json:"logTopic" yaml:"logTopic"` - ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` - RetainOrdering bool `json:"retainOrdering" yaml:"retainOrdering"` - UserConfig map[string]interface{} `json:"userConfig" yaml:"userConfig"` + Resources *Resources `json:"resources" yaml:"resources"` + WindowConfig *WindowConfig `json:"windowConfig" yaml:"windowConfig"` + Inputs []string `json:"inputs" yaml:"inputs"` + UserConfig map[string]interface{} `json:"userConfig" yaml:"userConfig"` + CustomSerdeInputs map[string]string `json:"customSerdeInputs" yaml:"customSerdeInputs"` + CustomSchemaInputs map[string]string `json:"customSchemaInputs" yaml:"customSchemaInputs"` - // This is a map of secretName(aka how the secret is going to be - // accessed in the function via context) to an object that - // encapsulates how the secret is fetched by the underlying - // secrets provider. The type of an value here can be found by the - // SecretProviderConfigurator.getSecretObjectType() method. - Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` + // A generalized way of specifying inputs + InputSpecs map[string]ConsumerConfig `json:"inputSpecs" yaml:"inputSpecs"` - Runtime string `json:"runtime" yaml:"runtime"` - AutoAck bool `json:"autoAck" yaml:"autoAck"` - MaxMessageRetries int `json:"maxMessageRetries" yaml:"maxMessageRetries"` - DeadLetterTopic string `json:"deadLetterTopic" yaml:"deadLetterTopic"` - SubName string `json:"subName" yaml:"subName"` - Parallelism int `json:"parallelism" yaml:"parallelism"` - Resources *Resources `json:"resources" yaml:"resources"` - FQFN string `json:"fqfn" yaml:"fqfn"` - WindowConfig *WindowConfig `json:"windowConfig" yaml:"windowConfig"` - TimeoutMs *int64 `json:"timeoutMs" yaml:"timeoutMs"` - Jar string `json:"jar" yaml:"jar"` - Py string `json:"py" yaml:"py"` - Go string `json:"go" yaml:"go"` - // Whether the subscriptions the functions created/used should be deleted when the functions is deleted - CleanupSubscription bool `json:"cleanupSubscription" yaml:"cleanupSubscription"` + // This is a map of secretName(aka how the secret is going to be + // accessed in the function via context) to an object that + // encapsulates how the secret is fetched by the underlying + // secrets provider. The type of an value here can be found by the + // SecretProviderConfigurator.getSecretObjectType() method. + Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` } diff --git a/pkg/pulsar/function_status.go b/pkg/pulsar/function_status.go index 1c699beb..b560eec6 100644 --- a/pkg/pulsar/function_status.go +++ b/pkg/pulsar/function_status.go @@ -24,7 +24,7 @@ type FunctionStatus struct { } type FunctionInstanceStatus struct { - InstanceId int `json:"instanceId"` + InstanceID int `json:"instanceId"` Status FunctionInstanceStatusData `json:"status"` } @@ -40,14 +40,10 @@ type FunctionInstanceStatusData struct { LatestSystemExceptions []ExceptionInformation `json:"latestSystemExceptions"` AverageLatency float64 `json:"averageLatency"` LastInvocationTime int64 `json:"lastInvocationTime"` - WorkerId string `json:"workerId"` + WorkerID string `json:"workerId"` } type ExceptionInformation struct { ExceptionString string `json:"exceptionString"` TimestampMs int64 `json:"timestampMs"` } - -func (fs *FunctionStatus) AddInstance(functionInstanceStatus FunctionInstanceStatus) { - fs.Instances = append(fs.Instances, functionInstanceStatus) -} diff --git a/pkg/pulsar/functions.go b/pkg/pulsar/functions.go index b02e29d1..514fd8e6 100644 --- a/pkg/pulsar/functions.go +++ b/pkg/pulsar/functions.go @@ -40,9 +40,9 @@ type Functions interface { // // @param functionConfig // the function configuration object - // @param pkgUrl + // @param pkgURL // url from which pkg can be downloaded - CreateFuncWithUrl(data *FunctionConfig, pkgUrl string) error + CreateFuncWithURL(data *FunctionConfig, pkgURL string) error // Stop all function instances StopFunction(tenant, namespace, name string) error @@ -104,7 +104,7 @@ type Functions interface { // eg: // File: file:/dir/fileName.jar // Http: http://www.repo.com/fileName.jar - UpdateFunctionWithUrl(functionConfig *FunctionConfig, pkgUrl string, updateOptions *UpdateOptions) error + UpdateFunctionWithURL(functionConfig *FunctionConfig, pkgURL string, updateOptions *UpdateOptions) error } type functions struct { @@ -184,7 +184,7 @@ func (f *functions) CreateFunc(funcConf *FunctionConfig, fileName string) error } contentType := multiPartWriter.FormDataContentType() - err = f.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) + err = f.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) if err != nil { return err } @@ -192,7 +192,7 @@ func (f *functions) CreateFunc(funcConf *FunctionConfig, fileName string) error return nil } -func (f *functions) CreateFuncWithUrl(funcConf *FunctionConfig, pkgUrl string) error { +func (f *functions) CreateFuncWithURL(funcConf *FunctionConfig, pkgURL string) error { endpoint := f.client.endpoint(f.basePath, funcConf.Tenant, funcConf.Namespace, funcConf.Name) // buffer to store our request as bytes bodyBuf := bytes.NewBufferString("") @@ -204,7 +204,7 @@ func (f *functions) CreateFuncWithUrl(funcConf *FunctionConfig, pkgUrl string) e return err } - _, err = textWriter.Write([]byte(pkgUrl)) + _, err = textWriter.Write([]byte(pkgURL)) if err != nil { return err } @@ -229,7 +229,7 @@ func (f *functions) CreateFuncWithUrl(funcConf *FunctionConfig, pkgUrl string) e } contentType := multiPartWriter.FormDataContentType() - err = f.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) + err = f.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) if err != nil { return err } @@ -238,44 +238,44 @@ func (f *functions) CreateFuncWithUrl(funcConf *FunctionConfig, pkgUrl string) e } func (f *functions) StopFunction(tenant, namespace, name string) error { - endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) - return f.client.post(endpoint+"/stop", "", nil) + endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) + return f.client.post(endpoint+"/stop", "") } -func (f *functions) StopFunctionWithID(tenant, namespace, name string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) +func (f *functions) StopFunctionWithID(tenant, namespace, name string, instanceID int) error { + id := fmt.Sprintf("%d", instanceID) + endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) - return f.client.post(endpoint+"/stop", "", nil) + return f.client.post(endpoint+"/stop", "") } func (f *functions) DeleteFunction(tenant, namespace, name string) error { endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) - return f.client.delete(endpoint, nil) + return f.client.delete(endpoint) } func (f *functions) StartFunction(tenant, namespace, name string) error { endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) - return f.client.post(endpoint+"/start", "", nil) + return f.client.post(endpoint+"/start", "") } -func (f *functions) StartFunctionWithID(tenant, namespace, name string, instanceID int) error { +func (f *functions) StartFunctionWithID(tenant, namespace, name string, instanceID int) error { id := fmt.Sprintf("%d", instanceID) endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) - return f.client.post(endpoint+"/start", "", nil) + return f.client.post(endpoint+"/start", "") } func (f *functions) RestartFunction(tenant, namespace, name string) error { endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) - return f.client.post(endpoint+"/restart", "", nil) + return f.client.post(endpoint+"/restart", "") } -func (f *functions) RestartFunctionWithID(tenant, namespace, name string, instanceID int) error { +func (f *functions) RestartFunctionWithID(tenant, namespace, name string, instanceID int) error { id := fmt.Sprintf("%d", instanceID) endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) - return f.client.post(endpoint+"/restart", "", nil) + return f.client.post(endpoint+"/restart", "") } func (f *functions) GetFunctions(tenant, namespace string) ([]string, error) { @@ -285,14 +285,15 @@ func (f *functions) GetFunctions(tenant, namespace string) ([]string, error) { return functions, err } -func (f *functions) GetFunction(tenant, namespace, name string) (FunctionConfig, error) { +func (f *functions) GetFunction(tenant, namespace, name string) (FunctionConfig, error) { var functionConfig FunctionConfig endpoint := f.client.endpoint(f.basePath, tenant, namespace, name) err := f.client.get(endpoint, &functionConfig) return functionConfig, err } -func (f *functions) UpdateFunction(functionConfig *FunctionConfig, fileName string, updateOptions *UpdateOptions) error { +func (f *functions) UpdateFunction(functionConfig *FunctionConfig, fileName string, + updateOptions *UpdateOptions) error { endpoint := f.client.endpoint(f.basePath, functionConfig.Tenant, functionConfig.Namespace, functionConfig.Name) // buffer to store our request as bytes bodyBuf := bytes.NewBufferString("") @@ -359,7 +360,7 @@ func (f *functions) UpdateFunction(functionConfig *FunctionConfig, fileName stri } contentType := multiPartWriter.FormDataContentType() - err = f.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) + err = f.client.putWithMultiPart(endpoint, bodyBuf, contentType) if err != nil { return err } @@ -367,7 +368,8 @@ func (f *functions) UpdateFunction(functionConfig *FunctionConfig, fileName stri return nil } -func (f *functions) UpdateFunctionWithUrl(functionConfig *FunctionConfig, pkgUrl string, updateOptions *UpdateOptions) error { +func (f *functions) UpdateFunctionWithURL(functionConfig *FunctionConfig, pkgURL string, + updateOptions *UpdateOptions) error { endpoint := f.client.endpoint(f.basePath, functionConfig.Tenant, functionConfig.Namespace, functionConfig.Name) // buffer to store our request as bytes bodyBuf := bytes.NewBufferString("") @@ -379,7 +381,7 @@ func (f *functions) UpdateFunctionWithUrl(functionConfig *FunctionConfig, pkgUrl return err } - _, err = textWriter.Write([]byte(pkgUrl)) + _, err = textWriter.Write([]byte(pkgURL)) if err != nil { return err } @@ -423,7 +425,7 @@ func (f *functions) UpdateFunctionWithUrl(functionConfig *FunctionConfig, pkgUrl } contentType := multiPartWriter.FormDataContentType() - err = f.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) + err = f.client.putWithMultiPart(endpoint, bodyBuf, contentType) if err != nil { return err } @@ -438,7 +440,8 @@ func (f *functions) GetFunctionStatus(tenant, namespace, name string) (FunctionS return functionStatus, err } -func (f *functions) GetFunctionStatusWithInstanceID(tenant, namespace, name string, instanceID int) (FunctionInstanceStatusData, error) { +func (f *functions) GetFunctionStatusWithInstanceID(tenant, namespace, name string, + instanceID int) (FunctionInstanceStatusData, error) { var functionInstanceStatusData FunctionInstanceStatusData id := fmt.Sprintf("%d", instanceID) endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) @@ -453,7 +456,8 @@ func (f *functions) GetFunctionStats(tenant, namespace, name string) (FunctionSt return functionStats, err } -func (f *functions) GetFunctionStatsWithInstanceID(tenant, namespace, name string, instanceID int) (FunctionInstanceStatsData, error) { +func (f *functions) GetFunctionStatsWithInstanceID(tenant, namespace, name string, + instanceID int) (FunctionInstanceStatsData, error) { var functionInstanceStatsData FunctionInstanceStatsData id := fmt.Sprintf("%d", instanceID) endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, id) @@ -461,7 +465,7 @@ func (f *functions) GetFunctionStatsWithInstanceID(tenant, namespace, name strin return functionInstanceStatsData, err } -func (f *functions)GetFunctionState(tenant, namespace, name, key string) (FunctionState, error) { +func (f *functions) GetFunctionState(tenant, namespace, name, key string) (FunctionState, error) { var functionState FunctionState endpoint := f.client.endpoint(f.basePath, tenant, namespace, name, "state", key) err := f.client.get(endpoint, &functionState) @@ -501,7 +505,7 @@ func (f *functions) PutFunctionState(tenant, namespace, name string, state Funct contentType := multiPartWriter.FormDataContentType() - err = f.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) + err = f.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) if err != nil { return err @@ -570,7 +574,7 @@ func (f *functions) TriggerFunction(tenant, namespace, name, topic, triggerValue contentType := multiPartWriter.FormDataContentType() var str string - err := f.client.postWithMultiPart(endpoint, &str, nil, bodyBuf, contentType) + err := f.client.postWithMultiPart(endpoint, &str, bodyBuf, contentType) if err != nil { return "", err } diff --git a/pkg/pulsar/functions_stats.go b/pkg/pulsar/functions_stats.go index 3f644388..3e6ecc4e 100644 --- a/pkg/pulsar/functions_stats.go +++ b/pkg/pulsar/functions_stats.go @@ -46,7 +46,7 @@ type FunctionStats struct { type FunctionInstanceStats struct { FunctionInstanceStatsDataBase - InstanceId int64 `json:"instanceId"` + InstanceID int64 `json:"instanceId"` Metrics FunctionInstanceStatsData `json:"metrics"` } @@ -129,14 +129,14 @@ func (fs *FunctionStats) CalculateOverall() *FunctionStats { // calculate average from sum if nonNullInstances > 0 { - fs.AvgProcessLatency = fs.AvgProcessLatency / float64(nonNullInstances) + fs.AvgProcessLatency /= float64(nonNullInstances) } else { fs.AvgProcessLatency = 0 } // calculate 1min average from sum if nonNullInstancesOneMin > 0 { - fs.OneMin.AvgProcessLatency = fs.OneMin.AvgProcessLatency / float64(nonNullInstancesOneMin) + fs.OneMin.AvgProcessLatency /= float64(nonNullInstancesOneMin) } else { fs.AvgProcessLatency = 0 } diff --git a/pkg/pulsar/message_id.go b/pkg/pulsar/message_id.go index a86a830c..42e3fc7f 100644 --- a/pkg/pulsar/message_id.go +++ b/pkg/pulsar/message_id.go @@ -17,8 +17,8 @@ package pulsar -type MessageId struct { - LedgerId int64 - EntryId int64 - PartitionedIndex int +type MessageID struct { + LedgerID int64 `json:"ledgerId"` + EntryID int64 `json:"entryId"` + PartitionedIndex int `json:"partitionedIndex"` } diff --git a/pkg/pulsar/namespace.go b/pkg/pulsar/namespace.go index f3c42a7d..d1e29a2f 100644 --- a/pkg/pulsar/namespace.go +++ b/pkg/pulsar/namespace.go @@ -142,16 +142,20 @@ type Namespaces interface { // Clear backlog for all topics on a namespace ClearNamespaceBacklog(namespace NameSpaceName) error - // Set replicator-message-dispatch-rate (Replicators under this namespace can dispatch this many messages per second) + // Set replicator-message-dispatch-rate (Replicators under this namespace + // can dispatch this many messages per second) SetReplicatorDispatchRate(namespace NameSpaceName, rate DispatchRate) error - // Get replicator-message-dispatch-rate (Replicators under this namespace can dispatch this many messages per second) + // Get replicator-message-dispatch-rate (Replicators under this namespace + // can dispatch this many messages per second) GetReplicatorDispatchRate(namespace NameSpaceName) (DispatchRate, error) - // Set subscription-message-dispatch-rate (subscriptions under this namespace can dispatch this many messages per second) + // Set subscription-message-dispatch-rate (subscriptions under this namespace + // can dispatch this many messages per second) SetSubscriptionDispatchRate(namespace NameSpaceName, rate DispatchRate) error - // Get subscription-message-dispatch-rate (subscriptions under this namespace can dispatch this many messages per second) + // Get subscription-message-dispatch-rate (subscriptions under this namespace + // can dispatch this many messages per second) GetSubscriptionDispatchRate(namespace NameSpaceName) (DispatchRate, error) // Set namespace-subscribe-rate (topics under this namespace will limit by subscribeRate) @@ -218,7 +222,7 @@ func (n *namespaces) CreateNsWithPolices(namespace string, policies Policies) er return err } endpoint := n.client.endpoint(n.basePath, ns.String()) - return n.client.put(endpoint, &policies, nil) + return n.client.put(endpoint, &policies) } func (n *namespaces) CreateNsWithBundlesData(namespace string, bundleData *BundlesData) error { @@ -230,7 +234,7 @@ func (n *namespaces) CreateNsWithBundlesData(namespace string, bundleData *Bundl polices := new(Policies) polices.Bundles = bundleData - return n.client.put(endpoint, &polices, nil) + return n.client.put(endpoint, &polices) } func (n *namespaces) CreateNamespace(namespace string) error { @@ -239,7 +243,7 @@ func (n *namespaces) CreateNamespace(namespace string) error { return err } endpoint := n.client.endpoint(n.basePath, ns.String()) - return n.client.put(endpoint, nil, nil) + return n.client.put(endpoint, nil) } func (n *namespaces) DeleteNamespace(namespace string) error { @@ -248,7 +252,7 @@ func (n *namespaces) DeleteNamespace(namespace string) error { return err } endpoint := n.client.endpoint(n.basePath, ns.String()) - return n.client.delete(endpoint, nil) + return n.client.delete(endpoint) } func (n *namespaces) DeleteNamespaceBundle(namespace string, bundleRange string) error { @@ -257,7 +261,7 @@ func (n *namespaces) DeleteNamespaceBundle(namespace string, bundleRange string) return err } endpoint := n.client.endpoint(n.basePath, ns.String(), bundleRange) - return n.client.delete(endpoint, nil) + return n.client.delete(endpoint) } func (n *namespaces) GetNamespaceMessageTTL(namespace string) (int, error) { @@ -278,7 +282,7 @@ func (n *namespaces) SetNamespaceMessageTTL(namespace string, ttlInSeconds int) } endpoint := n.client.endpoint(n.basePath, nsName.String(), "messageTTL") - return n.client.post(endpoint, &ttlInSeconds, nil) + return n.client.post(endpoint, &ttlInSeconds) } func (n *namespaces) SetRetention(namespace string, policy RetentionPolicies) error { @@ -287,7 +291,7 @@ func (n *namespaces) SetRetention(namespace string, policy RetentionPolicies) er return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "retention") - return n.client.post(endpoint, &policy, nil) + return n.client.post(endpoint, &policy) } func (n *namespaces) GetRetention(namespace string) (*RetentionPolicies, error) { @@ -318,7 +322,7 @@ func (n *namespaces) SetBacklogQuota(namespace string, backlogQuota BacklogQuota return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "backlogQuota") - return n.client.post(endpoint, &backlogQuota, nil) + return n.client.post(endpoint, &backlogQuota) } func (n *namespaces) RemoveBacklogQuota(namespace string) error { @@ -350,7 +354,7 @@ func (n *namespaces) SetNamespaceReplicationClusters(namespace string, clusterId return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "replication") - return n.client.post(endpoint, &clusterIds, nil) + return n.client.post(endpoint, &clusterIds) } func (n *namespaces) SetNamespaceAntiAffinityGroup(namespace string, namespaceAntiAffinityGroup string) error { @@ -359,7 +363,7 @@ func (n *namespaces) SetNamespaceAntiAffinityGroup(namespace string, namespaceAn return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "antiAffinity") - return n.client.post(endpoint, namespaceAntiAffinityGroup, nil) + return n.client.post(endpoint, namespaceAntiAffinityGroup) } func (n *namespaces) GetAntiAffinityNamespaces(tenant, cluster, namespaceAntiAffinityGroup string) ([]string, error) { @@ -388,7 +392,7 @@ func (n *namespaces) DeleteNamespaceAntiAffinityGroup(namespace string) error { return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "antiAffinity") - return n.client.delete(endpoint, nil) + return n.client.delete(endpoint) } func (n *namespaces) SetDeduplicationStatus(namespace string, enableDeduplication bool) error { @@ -397,7 +401,7 @@ func (n *namespaces) SetDeduplicationStatus(namespace string, enableDeduplicatio return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "deduplication") - return n.client.post(endpoint, enableDeduplication, nil) + return n.client.post(endpoint, enableDeduplication) } func (n *namespaces) SetPersistence(namespace string, persistence PersistencePolicies) error { @@ -406,7 +410,7 @@ func (n *namespaces) SetPersistence(namespace string, persistence PersistencePol return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "persistence") - return n.client.post(endpoint, &persistence, nil) + return n.client.post(endpoint, &persistence) } func (n *namespaces) SetBookieAffinityGroup(namespace string, bookieAffinityGroup BookieAffinityGroupData) error { @@ -415,7 +419,7 @@ func (n *namespaces) SetBookieAffinityGroup(namespace string, bookieAffinityGrou return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "persistence", "bookieAffinity") - return n.client.post(endpoint, &bookieAffinityGroup, nil) + return n.client.post(endpoint, &bookieAffinityGroup) } func (n *namespaces) DeleteBookieAffinityGroup(namespace string) error { @@ -424,7 +428,7 @@ func (n *namespaces) DeleteBookieAffinityGroup(namespace string) error { return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "persistence", "bookieAffinity") - return n.client.delete(endpoint, nil) + return n.client.delete(endpoint) } func (n *namespaces) GetBookieAffinityGroup(namespace string) (*BookieAffinityGroupData, error) { @@ -455,7 +459,7 @@ func (n *namespaces) Unload(namespace string) error { return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), "unload") - return n.client.put(endpoint, "", nil) + return n.client.put(endpoint, "") } func (n *namespaces) UnloadNamespaceBundle(namespace, bundle string) error { @@ -464,7 +468,7 @@ func (n *namespaces) UnloadNamespaceBundle(namespace, bundle string) error { return err } endpoint := n.client.endpoint(n.basePath, nsName.String(), bundle, "unload") - return n.client.put(endpoint, "", nil) + return n.client.put(endpoint, "") } func (n *namespaces) SplitNamespaceBundle(namespace, bundle string, unloadSplitBundles bool) error { @@ -481,47 +485,47 @@ func (n *namespaces) SplitNamespaceBundle(namespace, bundle string, unloadSplitB func (n *namespaces) SetSubscriptionAuthMode(namespace NameSpaceName, mode SubscriptionAuthMode) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "subscriptionAuthMode") - return n.client.post(endpoint, mode.String(), nil) + return n.client.post(endpoint, mode.String()) } func (n *namespaces) SetEncryptionRequiredStatus(namespace NameSpaceName, encrypt bool) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "encryptionRequired") - return n.client.post(endpoint, strconv.FormatBool(encrypt), nil) + return n.client.post(endpoint, strconv.FormatBool(encrypt)) } func (n *namespaces) UnsubscribeNamespace(namespace NameSpaceName, sName string) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "unsubscribe", url.QueryEscape(sName)) - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) UnsubscribeNamespaceBundle(namespace NameSpaceName, bundle, sName string) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), bundle, "unsubscribe", url.QueryEscape(sName)) - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) ClearNamespaceBundleBacklogForSubscription(namespace NameSpaceName, bundle, sName string) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), bundle, "clearBacklog", url.QueryEscape(sName)) - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) ClearNamespaceBundleBacklog(namespace NameSpaceName, bundle string) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), bundle, "clearBacklog") - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) ClearNamespaceBacklogForSubscription(namespace NameSpaceName, sName string) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "clearBacklog", url.QueryEscape(sName)) - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) ClearNamespaceBacklog(namespace NameSpaceName) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "clearBacklog") - return n.client.post(endpoint, "", nil) + return n.client.post(endpoint, "") } func (n *namespaces) SetReplicatorDispatchRate(namespace NameSpaceName, rate DispatchRate) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "replicatorDispatchRate") - return n.client.post(endpoint, rate, nil) + return n.client.post(endpoint, rate) } func (n *namespaces) GetReplicatorDispatchRate(namespace NameSpaceName) (DispatchRate, error) { @@ -533,7 +537,7 @@ func (n *namespaces) GetReplicatorDispatchRate(namespace NameSpaceName) (Dispatc func (n *namespaces) SetSubscriptionDispatchRate(namespace NameSpaceName, rate DispatchRate) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "subscriptionDispatchRate") - return n.client.post(endpoint, rate, nil) + return n.client.post(endpoint, rate) } func (n *namespaces) GetSubscriptionDispatchRate(namespace NameSpaceName) (DispatchRate, error) { @@ -545,7 +549,7 @@ func (n *namespaces) GetSubscriptionDispatchRate(namespace NameSpaceName) (Dispa func (n *namespaces) SetSubscribeRate(namespace NameSpaceName, rate SubscribeRate) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "subscribeRate") - return n.client.post(endpoint, rate, nil) + return n.client.post(endpoint, rate) } func (n *namespaces) GetSubscribeRate(namespace NameSpaceName) (SubscribeRate, error) { @@ -557,7 +561,7 @@ func (n *namespaces) GetSubscribeRate(namespace NameSpaceName) (SubscribeRate, e func (n *namespaces) SetDispatchRate(namespace NameSpaceName, rate DispatchRate) error { endpoint := n.client.endpoint(n.basePath, namespace.String(), "dispatchRate") - return n.client.post(endpoint, rate, nil) + return n.client.post(endpoint, rate) } func (n *namespaces) GetDispatchRate(namespace NameSpaceName) (DispatchRate, error) { diff --git a/pkg/pulsar/namespace_name.go b/pkg/pulsar/namespace_name.go index 3e1c2e5b..ac572146 100644 --- a/pkg/pulsar/namespace_name.go +++ b/pkg/pulsar/namespace_name.go @@ -19,9 +19,10 @@ package pulsar import ( "fmt" - "github.com/pkg/errors" "regexp" "strings" + + "github.com/pkg/errors" ) type NameSpaceName struct { @@ -30,14 +31,14 @@ type NameSpaceName struct { } func GetNameSpaceName(tenant, namespace string) (*NameSpaceName, error) { - return GetNamespaceName(fmt.Sprintf("%s/%s", tenant,namespace)) + return GetNamespaceName(fmt.Sprintf("%s/%s", tenant, namespace)) } func GetNamespaceName(completeName string) (*NameSpaceName, error) { var n NameSpaceName if completeName == "" { - return nil, errors.New("The namespace complete name is empty.") + return nil, errors.New("the namespace complete name is empty") } parts := strings.Split(completeName, "/") diff --git a/pkg/pulsar/namespace_name_test.go b/pkg/pulsar/namespace_name_test.go index fc7b04f8..4b5294af 100644 --- a/pkg/pulsar/namespace_name_test.go +++ b/pkg/pulsar/namespace_name_test.go @@ -18,8 +18,9 @@ package pulsar import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetNamespaceName(t *testing.T) { @@ -29,7 +30,7 @@ func TestGetNamespaceName(t *testing.T) { empty, err := GetNamespaceName("") assert.NotNil(t, err) - assert.Equal(t, "The namespace complete name is empty.", err.Error()) + assert.Equal(t, "the namespace complete name is empty", err.Error()) assert.Nil(t, empty) empty, err = GetNamespaceName("/") diff --git a/pkg/pulsar/persistence_policies.go b/pkg/pulsar/persistence_policies.go index a8739c1e..3c0a2229 100644 --- a/pkg/pulsar/persistence_policies.go +++ b/pkg/pulsar/persistence_policies.go @@ -24,16 +24,8 @@ type PersistencePolicies struct { ManagedLedgerMaxMarkDeleteRate float64 `json:"managedLedgerMaxMarkDeleteRate"` } -func NewDefaultPersistencePolicies() *PersistencePolicies { - return &PersistencePolicies{ - BookkeeperEnsemble: 2, - BookkeeperWriteQuorum: 2, - BookkeeperAckQuorum: 2, - ManagedLedgerMaxMarkDeleteRate: 0.0, - } -} - -func NewPersistencePolicies(bookkeeperEnsemble, bookkeeperWriteQuorum, bookkeeperAckQuorum int, managedLedgerMaxMarkDeleteRate float64) PersistencePolicies { +func NewPersistencePolicies(bookkeeperEnsemble, bookkeeperWriteQuorum, bookkeeperAckQuorum int, + managedLedgerMaxMarkDeleteRate float64) PersistencePolicies { return PersistencePolicies{ BookkeeperEnsemble: bookkeeperEnsemble, BookkeeperWriteQuorum: bookkeeperWriteQuorum, @@ -46,10 +38,3 @@ type BookieAffinityGroupData struct { BookkeeperAffinityGroupPrimary string `json:"bookkeeperAffinityGroupPrimary"` BookkeeperAffinityGroupSecondary string `json:"bookkeeperAffinityGroupSecondary"` } - -func NewBookieAffinityGroupData(bookkeeperAffinityGroupPrimary, bookkeeperAffinityGroupSecondary string) BookieAffinityGroupData { - return BookieAffinityGroupData{ - BookkeeperAffinityGroupPrimary: bookkeeperAffinityGroupPrimary, - BookkeeperAffinityGroupSecondary: bookkeeperAffinityGroupSecondary, - } -} diff --git a/pkg/pulsar/policies.go b/pkg/pulsar/policies.go index f6643351..392f416c 100644 --- a/pkg/pulsar/policies.go +++ b/pkg/pulsar/policies.go @@ -23,54 +23,53 @@ const ( ) type Policies struct { - AuthPolicies AuthPolicies `json:"auth_policies"` - ReplicationClusters []string `json:"replication_clusters"` - Bundles *BundlesData `json:"bundles"` - BacklogQuotaMap map[BacklogQuotaType]BacklogQuota `json:"backlog_quota_map"` - TopicDispatchRate map[string]DispatchRate `json:"topicDispatchRate"` - SubscriptionDispatchRate map[string]DispatchRate `json:"subscriptionDispatchRate"` - ReplicatorDispatchRate map[string]DispatchRate `json:"replicatorDispatchRate"` - ClusterSubscribeRate map[string]SubscribeRate `json:"clusterSubscribeRate"` - Persistence *PersistencePolicies `json:"persistence"` - DeduplicationEnabled bool `json:"deduplicationEnabled"` - LatencyStatsSampleRate map[string]int `json:"latency_stats_sample_rate"` - MessageTtlInSeconds int `json:"message_ttl_in_seconds"` - RetentionPolicies *RetentionPolicies `json:"retention_policies"` - Deleted bool `json:"deleted"` - AntiAffinityGroup string `json:"antiAffinityGroup"` - EncryptionRequired bool `json:"encryption_required"` - SubscriptionAuthMode SubscriptionAuthMode `json:"subscription_auth_mode"` - MaxProducersPerTopic int `json:"max_producers_per_topic"` - MaxConsumersPerTopic int `json:"max_consumers_per_topic"` - MaxConsumersPerSubscription int `json:"max_consumers_per_subscription"` - CompactionThreshold int64 `json:"compaction_threshold"` - OffloadThreshold int64 `json:"offload_threshold"` - OffloadDeletionLagMs int64 `json:"offload_deletion_lag_ms"` - SchemaAutoUpdateCompatibilityStrategy SchemaAutoUpdateCompatibilityStrategy `json:"schema_auto_update_compatibility_strategy"` - SchemaValidationEnforced bool `json:"schema_validation_enforced"` + Bundles *BundlesData `json:"bundles"` + Persistence *PersistencePolicies `json:"persistence"` + RetentionPolicies *RetentionPolicies `json:"retention_policies"` + SchemaValidationEnforced bool `json:"schema_validation_enforced"` + DeduplicationEnabled bool `json:"deduplicationEnabled"` + Deleted bool `json:"deleted"` + EncryptionRequired bool `json:"encryption_required"` + MessageTTLInSeconds int `json:"message_ttl_in_seconds"` + MaxProducersPerTopic int `json:"max_producers_per_topic"` + MaxConsumersPerTopic int `json:"max_consumers_per_topic"` + MaxConsumersPerSubscription int `json:"max_consumers_per_subscription"` + CompactionThreshold int64 `json:"compaction_threshold"` + OffloadThreshold int64 `json:"offload_threshold"` + OffloadDeletionLagMs int64 `json:"offload_deletion_lag_ms"` + AntiAffinityGroup string `json:"antiAffinityGroup"` + ReplicationClusters []string `json:"replication_clusters"` + LatencyStatsSampleRate map[string]int `json:"latency_stats_sample_rate"` + BacklogQuotaMap map[BacklogQuotaType]BacklogQuota `json:"backlog_quota_map"` + TopicDispatchRate map[string]DispatchRate `json:"topicDispatchRate"` + SubscriptionDispatchRate map[string]DispatchRate `json:"subscriptionDispatchRate"` + ReplicatorDispatchRate map[string]DispatchRate `json:"replicatorDispatchRate"` + ClusterSubscribeRate map[string]SubscribeRate `json:"clusterSubscribeRate"` + SchemaCompatibilityStrategy SchemaCompatibilityStrategy `json:"schema_auto_update_compatibility_strategy"` + AuthPolicies AuthPolicies `json:"auth_policies"` + SubscriptionAuthMode SubscriptionAuthMode `json:"subscription_auth_mode"` } func NewDefaultPolicies() *Policies { return &Policies{ - AuthPolicies: *NewAuthPolicies(), - ReplicationClusters: make([]string, 0, 10), - BacklogQuotaMap: make(map[BacklogQuotaType]BacklogQuota), - TopicDispatchRate: make(map[string]DispatchRate), - SubscriptionDispatchRate: make(map[string]DispatchRate), - ReplicatorDispatchRate: make(map[string]DispatchRate), - ClusterSubscribeRate: make(map[string]SubscribeRate), - LatencyStatsSampleRate: make(map[string]int), - MessageTtlInSeconds: 0, - Deleted: false, - EncryptionRequired: false, - SubscriptionAuthMode: None, - MaxProducersPerTopic: 0, - MaxConsumersPerSubscription: 0, - MaxConsumersPerTopic: 0, - CompactionThreshold: 0, - OffloadThreshold: -1, - SchemaAutoUpdateCompatibilityStrategy: Full, - SchemaValidationEnforced: false, + AuthPolicies: *NewAuthPolicies(), + ReplicationClusters: make([]string, 0, 10), + BacklogQuotaMap: make(map[BacklogQuotaType]BacklogQuota), + TopicDispatchRate: make(map[string]DispatchRate), + SubscriptionDispatchRate: make(map[string]DispatchRate), + ReplicatorDispatchRate: make(map[string]DispatchRate), + ClusterSubscribeRate: make(map[string]SubscribeRate), + LatencyStatsSampleRate: make(map[string]int), + MessageTTLInSeconds: 0, + Deleted: false, + EncryptionRequired: false, + SubscriptionAuthMode: None, + MaxProducersPerTopic: 0, + MaxConsumersPerSubscription: 0, + MaxConsumersPerTopic: 0, + CompactionThreshold: 0, + OffloadThreshold: -1, + SchemaCompatibilityStrategy: Full, + SchemaValidationEnforced: false, } } - diff --git a/pkg/pulsar/resources.go b/pkg/pulsar/resources.go index f1fd1431..bfe03375 100644 --- a/pkg/pulsar/resources.go +++ b/pkg/pulsar/resources.go @@ -18,39 +18,20 @@ package pulsar type Resources struct { - CPU float64 `json:"cpu"` - Disk int64 `json:"disk"` - Ram int64 `json:"ram"` + CPU float64 `json:"cpu"` + Disk int64 `json:"disk"` + RAM int64 `json:"ram"` } func NewDefaultResources() *Resources { - resources:= &Resources{ - //Default cpu is 1 core - CPU: 1, - // Default memory is 1GB - Disk: 1073741824, - // Default disk is 10GB - Ram: 10737418240, - } - - return resources -} - -func NewResources(resources *Resources) *Resources { - if resources == nil { - return NewDefaultResources() - } - - if resources.CPU == 0 { - resources.CPU = 1 - } - - if resources.Disk == 0 { - resources.Disk = 1073741824 - } - - if resources.Ram == 0 { - resources.Ram = 10737418240 - } - return resources + resources := &Resources{ + //Default cpu is 1 core + CPU: 1, + // Default memory is 1GB + Disk: 1073741824, + // Default disk is 10GB + RAM: 10737418240, + } + + return resources } diff --git a/pkg/pulsar/schema.go b/pkg/pulsar/schema.go index 494acea3..880992e7 100644 --- a/pkg/pulsar/schema.go +++ b/pkg/pulsar/schema.go @@ -117,7 +117,7 @@ func (s *schemas) DeleteSchema(topic string) error { fmt.Println(endpoint) - return s.client.delete(endpoint, nil) + return s.client.delete(endpoint) } func (s *schemas) CreateSchemaByPayload(topic string, schemaPayload PostSchemaPayload) error { @@ -129,5 +129,5 @@ func (s *schemas) CreateSchemaByPayload(topic string, schemaPayload PostSchemaPa endpoint := s.client.endpoint(s.basePath, topicName.tenant, topicName.namespace, topicName.GetEncodedTopic(), "schema") - return s.client.post(endpoint, &schemaPayload, nil) + return s.client.post(endpoint, &schemaPayload) } diff --git a/pkg/pulsar/schema_strategy.go b/pkg/pulsar/schema_strategy.go index 66b47525..e0f4f2cd 100644 --- a/pkg/pulsar/schema_strategy.go +++ b/pkg/pulsar/schema_strategy.go @@ -17,15 +17,15 @@ package pulsar -type SchemaAutoUpdateCompatibilityStrategy string +type SchemaCompatibilityStrategy string const ( - AutoUpdateDisabled SchemaAutoUpdateCompatibilityStrategy = "AutoUpdateDisabled" - Backward SchemaAutoUpdateCompatibilityStrategy = "Backward" - Forward SchemaAutoUpdateCompatibilityStrategy = "Forward" - Full SchemaAutoUpdateCompatibilityStrategy = "Full" - AlwaysCompatible SchemaAutoUpdateCompatibilityStrategy = "AlwaysCompatible" - BackwardTransitive SchemaAutoUpdateCompatibilityStrategy = "BackwardTransitive" - ForwardTransitive SchemaAutoUpdateCompatibilityStrategy = "ForwardTransitive" - FullTransitive SchemaAutoUpdateCompatibilityStrategy = "FullTransitive" + AutoUpdateDisabled SchemaCompatibilityStrategy = "AutoUpdateDisabled" + Backward SchemaCompatibilityStrategy = "Backward" + Forward SchemaCompatibilityStrategy = "Forward" + Full SchemaCompatibilityStrategy = "Full" + AlwaysCompatible SchemaCompatibilityStrategy = "AlwaysCompatible" + BackwardTransitive SchemaCompatibilityStrategy = "BackwardTransitive" + ForwardTransitive SchemaCompatibilityStrategy = "ForwardTransitive" + FullTransitive SchemaCompatibilityStrategy = "FullTransitive" ) diff --git a/pkg/pulsar/sinkConfig.go b/pkg/pulsar/sinkConfig.go index bf77168d..81fee992 100644 --- a/pkg/pulsar/sinkConfig.go +++ b/pkg/pulsar/sinkConfig.go @@ -18,34 +18,35 @@ package pulsar type SinkConfig struct { - Tenant string `json:"tenant" yaml:"tenant"` - Namespace string `json:"namespace" yaml:"namespace"` - Name string `json:"name" yaml:"name"` - ClassName string `json:"className" yaml:"className"` + TopicsPattern *string `json:"topicsPattern" yaml:"topicsPattern"` + Resources *Resources `json:"resources" yaml:"resources"` + TimeoutMs *int64 `json:"timeoutMs" yaml:"timeoutMs"` - SourceSubscriptionName string `json:"sourceSubscriptionName" yaml:"sourceSubscriptionName"` - Inputs []string `json:"inputs" yaml:"inputs"` - TopicToSerdeClassName map[string]string `json:"topicToSerdeClassName" yaml:"topicToSerdeClassName"` - TopicsPattern *string `json:"topicsPattern" yaml:"topicsPattern"` - TopicToSchemaType map[string]string `json:"topicToSchemaType" yaml:"topicToSchemaType"` - InputSpecs map[string]ConsumerConfig `json:"inputSpecs" yaml:"inputSpecs"` - Configs map[string]interface{} `json:"configs" yaml:"configs"` + // Whether the subscriptions the functions created/used should be deleted when the functions is deleted + CleanupSubscription bool `json:"cleanupSubscription" yaml:"cleanupSubscription"` - // This is a map of secretName(aka how the secret is going to be - // accessed in the function via context) to an object that - // encapsulates how the secret is fetched by the underlying - // secrets provider. The type of an value here can be found by the - // SecretProviderConfigurator.getSecretObjectType() method. - Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` - Parallelism int `json:"parallelism" yaml:"parallelism"` - ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` - RetainOrdering bool `json:"retainOrdering" yaml:"retainOrdering"` - Resources *Resources `json:"resources" yaml:"resources"` - AutoAck bool `json:"autoAck" yaml:"autoAck"` - TimeoutMs *int64 `json:"timeoutMs" yaml:"timeoutMs"` - Archive string `json:"archive" yaml:"archive"` + RetainOrdering bool `json:"retainOrdering" yaml:"retainOrdering"` + AutoAck bool `json:"autoAck" yaml:"autoAck"` + Parallelism int `json:"parallelism" yaml:"parallelism"` + Tenant string `json:"tenant" yaml:"tenant"` + Namespace string `json:"namespace" yaml:"namespace"` + Name string `json:"name" yaml:"name"` + ClassName string `json:"className" yaml:"className"` - // Whether the subscriptions the functions created/used should be deleted when the functions is deleted - CleanupSubscription bool `json:"cleanupSubscription" yaml:"cleanupSubscription"` - RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` + Archive string `json:"archive" yaml:"archive"` + ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` + SourceSubscriptionName string `json:"sourceSubscriptionName" yaml:"sourceSubscriptionName"` + RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` + Inputs []string `json:"inputs" yaml:"inputs"` + TopicToSerdeClassName map[string]string `json:"topicToSerdeClassName" yaml:"topicToSerdeClassName"` + TopicToSchemaType map[string]string `json:"topicToSchemaType" yaml:"topicToSchemaType"` + InputSpecs map[string]ConsumerConfig `json:"inputSpecs" yaml:"inputSpecs"` + Configs map[string]interface{} `json:"configs" yaml:"configs"` + + // This is a map of secretName(aka how the secret is going to be + // accessed in the function via context) to an object that + // encapsulates how the secret is fetched by the underlying + // secrets provider. The type of an value here can be found by the + // SecretProviderConfigurator.getSecretObjectType() method. + Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` } diff --git a/pkg/pulsar/sink_status.go b/pkg/pulsar/sink_status.go index 67c79be0..68fa27df 100644 --- a/pkg/pulsar/sink_status.go +++ b/pkg/pulsar/sink_status.go @@ -18,55 +18,50 @@ package pulsar type SinkStatus struct { - // The total number of sink instances that ought to be running - NumInstances int `json:"numInstances"` - // The number of source instances that are actually running - NumRunning int `json:"numRunning"` + // The total number of sink instances that ought to be running + NumInstances int `json:"numInstances"` - Instances []*SinkInstanceStatus `json:"instances"` + // The number of source instances that are actually running + NumRunning int `json:"numRunning"` + + Instances []*SinkInstanceStatus `json:"instances"` } type SinkInstanceStatus struct { - InstanceId int `json:"instanceId"` - - Status SourceInstanceStatusData `json:"status"` + InstanceID int `json:"instanceId"` + Status SourceInstanceStatusData `json:"status"` } type SinkInstanceStatusData struct { - // Is this instance running? - Running bool `json:"running"` + // Is this instance running? + Running bool `json:"running"` - // Do we have any error while running this instance - Err string `json:"error"` + // Do we have any error while running this instance + Err string `json:"error"` - // Number of times this instance has restarted - NumRestarts int64 `json:"numRestarts"` + // Number of times this instance has restarted + NumRestarts int64 `json:"numRestarts"` - // Number of messages read from Pulsar - NumReadFromPulsar int64 `json:"numReadFromPulsar"` + // Number of messages read from Pulsar + NumReadFromPulsar int64 `json:"numReadFromPulsar"` - // Number of times there was a system exception handling messages - NumSystemExceptions int64 `json:"numSystemExceptions"` + // Number of times there was a system exception handling messages + NumSystemExceptions int64 `json:"numSystemExceptions"` - // A list of the most recent system exceptions - LatestSystemExceptions []ExceptionInformation `json:"latestSystemExceptions"` + // A list of the most recent system exceptions + LatestSystemExceptions []ExceptionInformation `json:"latestSystemExceptions"` - // Number of times there was a sink exception - NumSinkExceptions int64 `json:"numSinkExceptions"` + // Number of times there was a sink exception + NumSinkExceptions int64 `json:"numSinkExceptions"` - // A list of the most recent sink exceptions - LatestSinkExceptions []ExceptionInformation `json:"latestSinkExceptions"` + // A list of the most recent sink exceptions + LatestSinkExceptions []ExceptionInformation `json:"latestSinkExceptions"` - // Number of messages written to sink - NumWrittenToSink int64 `json:"numWrittenToSink"` + // Number of messages written to sink + NumWrittenToSink int64 `json:"numWrittenToSink"` - // When was the last time we received a message from Pulsar - LastReceivedTime int64 `json:"lastReceivedTime"` + // When was the last time we received a message from Pulsar + LastReceivedTime int64 `json:"lastReceivedTime"` - WorkerId string `json:"workerId"` + WorkerID string `json:"workerId"` } - -func (ss *SinkStatus)AddInstance(sinkInstanceStatus *SinkInstanceStatus) { - ss.Instances = append(ss.Instances, sinkInstanceStatus) -} - diff --git a/pkg/pulsar/sinks.go b/pkg/pulsar/sinks.go index 6a317986..936bd4ac 100644 --- a/pkg/pulsar/sinks.go +++ b/pkg/pulsar/sinks.go @@ -18,417 +18,416 @@ package pulsar import ( - `bytes` - `encoding/json` - `fmt` - `io` - `mime/multipart` - `net/textproto` - `os` - `path/filepath` - `strings` + "bytes" + "encoding/json" + "fmt" + "io" + "mime/multipart" + "net/textproto" + "os" + "path/filepath" + "strings" ) type Sinks interface { - // Get the list of all the Pulsar Sinks. - ListSinks(tenant, namespace string) ([]string, error) + // Get the list of all the Pulsar Sinks. + ListSinks(tenant, namespace string) ([]string, error) - // Get the configuration for the specified sink - GetSink(tenant, namespace, Sink string) (SinkConfig, error) + // Get the configuration for the specified sink + GetSink(tenant, namespace, Sink string) (SinkConfig, error) - // Create a new sink - CreateSink(config *SinkConfig, fileName string) error + // Create a new sink + CreateSink(config *SinkConfig, fileName string) error - // Create a new sink by providing url from which fun-pkg can be downloaded. supported url: http/file - CreateSinkWithURL(config *SinkConfig, pkgUrl string) error + // Create a new sink by providing url from which fun-pkg can be downloaded. supported url: http/file + CreateSinkWithURL(config *SinkConfig, pkgURL string) error - // Update the configuration for a sink. - UpdateSink(config *SinkConfig, fileName string, options *UpdateOptions) error + // Update the configuration for a sink. + UpdateSink(config *SinkConfig, fileName string, options *UpdateOptions) error - // Update a sink by providing url from which fun-pkg can be downloaded. supported url: http/file - UpdateSinkWithUrl(config *SinkConfig, pkgUrl string, options *UpdateOptions) error + // Update a sink by providing url from which fun-pkg can be downloaded. supported url: http/file + UpdateSinkWithURL(config *SinkConfig, pkgURL string, options *UpdateOptions) error - // Delete an existing sink - DeleteSink(tenant, namespace, Sink string) error + // Delete an existing sink + DeleteSink(tenant, namespace, Sink string) error - // Gets the current status of a sink. - GetSinkStatus(tenant, namespace, Sink string) (SinkStatus, error) + // Gets the current status of a sink. + GetSinkStatus(tenant, namespace, Sink string) (SinkStatus, error) - // Gets the current status of a sink instance. - GetSinkStatusWithID(tenant, namespace, Sink string, id int) (SinkInstanceStatusData, error) + // Gets the current status of a sink instance. + GetSinkStatusWithID(tenant, namespace, Sink string, id int) (SinkInstanceStatusData, error) - // Restart all sink instances - RestartSink(tenant, namespace, Sink string) error + // Restart all sink instances + RestartSink(tenant, namespace, Sink string) error - // Restart sink instance - RestartSinkWithID(tenant, namespace, Sink string, id int) error + // Restart sink instance + RestartSinkWithID(tenant, namespace, Sink string, id int) error - // Stop all sink instances - StopSink(tenant, namespace, Sink string) error + // Stop all sink instances + StopSink(tenant, namespace, Sink string) error - // Stop sink instance - StopSinkWithID(tenant, namespace, Sink string, id int) error + // Stop sink instance + StopSinkWithID(tenant, namespace, Sink string, id int) error - // Start all sink instances - StartSink(tenant, namespace, Sink string) error + // Start all sink instances + StartSink(tenant, namespace, Sink string) error - // Start sink instance - StartSinkWithID(tenant, namespace, Sink string, id int) error + // Start sink instance + StartSinkWithID(tenant, namespace, Sink string, id int) error - // Fetches a list of supported Pulsar IO sinks currently running in cluster mode - GetBuiltInSinks() ([]*ConnectorDefinition, error) + // Fetches a list of supported Pulsar IO sinks currently running in cluster mode + GetBuiltInSinks() ([]*ConnectorDefinition, error) - // Reload the available built-in connectors, include Source and Sink - ReloadBuiltInSinks() error + // Reload the available built-in connectors, include Source and Sink + ReloadBuiltInSinks() error } type sinks struct { - client *client - basePath string + client *client + basePath string } func (c *client) Sinks() Sinks { - return &sinks{ - client: c, - basePath: "/sinks", - } + return &sinks{ + client: c, + basePath: "/sinks", + } } func (s *sinks) createStringFromField(w *multipart.Writer, value string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) - h.Set("Content-Type", "application/json") - return w.CreatePart(h) + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) + h.Set("Content-Type", "application/json") + return w.CreatePart(h) } func (s *sinks) createTextFromFiled(w *multipart.Writer, value string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) - h.Set("Content-Type", "text/plain") - return w.CreatePart(h) + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) + h.Set("Content-Type", "text/plain") + return w.CreatePart(h) } func (s *sinks) ListSinks(tenant, namespace string) ([]string, error) { - var sinks []string - endpoint := s.client.endpoint(s.basePath, tenant, namespace) - err := s.client.get(endpoint, &sinks) - return sinks, err + var sinks []string + endpoint := s.client.endpoint(s.basePath, tenant, namespace) + err := s.client.get(endpoint, &sinks) + return sinks, err } -func (s *sinks) GetSink(tenant, namespace, Sink string) (SinkConfig, error) { - var sinkConfig SinkConfig - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - err := s.client.get(endpoint, &sinkConfig) - return sinkConfig, err +func (s *sinks) GetSink(tenant, namespace, sink string) (SinkConfig, error) { + var sinkConfig SinkConfig + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + err := s.client.get(endpoint, &sinkConfig) + return sinkConfig, err } func (s *sinks) CreateSink(config *SinkConfig, fileName string) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { - // If the function code is built in, we don't need to submit here - file, err := os.Open(fileName) - if err != nil { - return err - } - defer file.Close() - - part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) - - if err != nil { - return err - } - - // copy the actual file content to the filed's writer - _, err = io.Copy(part, file) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { + // If the function code is built in, we don't need to submit here + file, err := os.Open(fileName) + if err != nil { + return err + } + defer file.Close() + + part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) + + if err != nil { + return err + } + + // copy the actual file content to the filed's writer + _, err = io.Copy(part, file) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } -func (s *sinks) CreateSinkWithURL(config *SinkConfig, pkgUrl string) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - textWriter, err := s.createTextFromFiled(multiPartWriter, "url") - if err != nil { - return err - } - - _, err = textWriter.Write([]byte(pkgUrl)) - if err != nil { - return err - } - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil +func (s *sinks) CreateSinkWithURL(config *SinkConfig, pkgURL string) error { + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + textWriter, err := s.createTextFromFiled(multiPartWriter, "url") + if err != nil { + return err + } + + _, err = textWriter.Write([]byte(pkgURL)) + if err != nil { + return err + } + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } func (s *sinks) UpdateSink(config *SinkConfig, fileName string, updateOptions *UpdateOptions) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if updateOptions != nil { - updateData, err := json.Marshal(updateOptions) - if err != nil { - return err - } - - updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") - if err != nil { - return err - } - - _, err = updateStrWriter.Write(updateData) - if err != nil { - return err - } - } - - if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { - // If the function code is built in, we don't need to submit here - file, err := os.Open(fileName) - if err != nil { - return err - } - defer file.Close() - - part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) - - if err != nil { - return err - } - - // copy the actual file content to the filed's writer - _, err = io.Copy(part, file) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if updateOptions != nil { + updateData, err := json.Marshal(updateOptions) + if err != nil { + return err + } + + updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") + if err != nil { + return err + } + + _, err = updateStrWriter.Write(updateData) + if err != nil { + return err + } + } + + if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { + // If the function code is built in, we don't need to submit here + file, err := os.Open(fileName) + if err != nil { + return err + } + defer file.Close() + + part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) + + if err != nil { + return err + } + + // copy the actual file content to the filed's writer + _, err = io.Copy(part, file) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.putWithMultiPart(endpoint, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } -func (s *sinks) UpdateSinkWithUrl(config *SinkConfig, pkgUrl string, updateOptions *UpdateOptions) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - textWriter, err := s.createTextFromFiled(multiPartWriter, "url") - if err != nil { - return err - } - - _, err = textWriter.Write([]byte(pkgUrl)) - if err != nil { - return err - } - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if updateOptions != nil { - updateData, err := json.Marshal(updateOptions) - if err != nil { - return err - } - - updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") - if err != nil { - return err - } - - _, err = updateStrWriter.Write(updateData) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil +func (s *sinks) UpdateSinkWithURL(config *SinkConfig, pkgURL string, updateOptions *UpdateOptions) error { + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + textWriter, err := s.createTextFromFiled(multiPartWriter, "url") + if err != nil { + return err + } + + _, err = textWriter.Write([]byte(pkgURL)) + if err != nil { + return err + } + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sinkConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if updateOptions != nil { + updateData, err := json.Marshal(updateOptions) + if err != nil { + return err + } + + updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") + if err != nil { + return err + } + + _, err = updateStrWriter.Write(updateData) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.putWithMultiPart(endpoint, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } -func (s *sinks) DeleteSink(tenant, namespace, Sink string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - return s.client.delete(endpoint, nil) +func (s *sinks) DeleteSink(tenant, namespace, sink string) error { + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + return s.client.delete(endpoint) } -func (s *sinks) GetSinkStatus(tenant, namespace, Sink string) (SinkStatus, error) { - var sinkStatus SinkStatus - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - err := s.client.get(endpoint+"/status", &sinkStatus) - return sinkStatus, err +func (s *sinks) GetSinkStatus(tenant, namespace, sink string) (SinkStatus, error) { + var sinkStatus SinkStatus + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + err := s.client.get(endpoint+"/status", &sinkStatus) + return sinkStatus, err } -func (s *sinks) GetSinkStatusWithID(tenant, namespace, Sink string, id int) (SinkInstanceStatusData, error) { - var sinkInstanceStatusData SinkInstanceStatusData - instanceID := fmt.Sprintf("%d", id) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink, instanceID) - err := s.client.get(endpoint+"/status", &sinkInstanceStatusData) - return sinkInstanceStatusData, err +func (s *sinks) GetSinkStatusWithID(tenant, namespace, sink string, id int) (SinkInstanceStatusData, error) { + var sinkInstanceStatusData SinkInstanceStatusData + instanceID := fmt.Sprintf("%d", id) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink, instanceID) + err := s.client.get(endpoint+"/status", &sinkInstanceStatusData) + return sinkInstanceStatusData, err } -func (s *sinks) RestartSink(tenant, namespace, Sink string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - return s.client.post(endpoint+"/restart", "", nil) +func (s *sinks) RestartSink(tenant, namespace, sink string) error { + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + return s.client.post(endpoint+"/restart", "") } -func (s *sinks) RestartSinkWithID(tenant, namespace, Sink string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink, id) +func (s *sinks) RestartSinkWithID(tenant, namespace, sink string, instanceID int) error { + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink, id) - return s.client.post(endpoint+"/restart", "", nil) + return s.client.post(endpoint+"/restart", "") } -func (s *sinks) StopSink(tenant, namespace, Sink string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - return s.client.post(endpoint+"/stop", "", nil) +func (s *sinks) StopSink(tenant, namespace, sink string) error { + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + return s.client.post(endpoint+"/stop", "") } -func (s *sinks) StopSinkWithID(tenant, namespace, Sink string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink, id) +func (s *sinks) StopSinkWithID(tenant, namespace, sink string, instanceID int) error { + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink, id) - return s.client.post(endpoint+"/stop", "", nil) + return s.client.post(endpoint+"/stop", "") } -func (s *sinks) StartSink(tenant, namespace, Sink string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink) - return s.client.post(endpoint+"/start", "", nil) +func (s *sinks) StartSink(tenant, namespace, sink string) error { + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink) + return s.client.post(endpoint+"/start", "") } -func (s *sinks) StartSinkWithID(tenant, namespace, Sink string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, Sink, id) +func (s *sinks) StartSinkWithID(tenant, namespace, sink string, instanceID int) error { + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, sink, id) - return s.client.post(endpoint+"/start", "", nil) + return s.client.post(endpoint+"/start", "") } func (s *sinks) GetBuiltInSinks() ([]*ConnectorDefinition, error) { - var connectorDefinition []*ConnectorDefinition - endpoint := s.client.endpoint(s.basePath, "builtinSinks") - err := s.client.get(endpoint, &connectorDefinition) - return connectorDefinition, err + var connectorDefinition []*ConnectorDefinition + endpoint := s.client.endpoint(s.basePath, "builtinSinks") + err := s.client.get(endpoint, &connectorDefinition) + return connectorDefinition, err } func (s *sinks) ReloadBuiltInSinks() error { - endpoint := s.client.endpoint(s.basePath, "reloadBuiltInSinks") - return s.client.post(endpoint, "", nil) + endpoint := s.client.endpoint(s.basePath, "reloadBuiltInSinks") + return s.client.post(endpoint, "") } - diff --git a/pkg/pulsar/sourceConfig.go b/pkg/pulsar/sourceConfig.go index 1405a5c5..debbc39c 100644 --- a/pkg/pulsar/sourceConfig.go +++ b/pkg/pulsar/sourceConfig.go @@ -18,28 +18,28 @@ package pulsar type SourceConfig struct { - Tenant string `json:"tenant" yaml:"tenant"` - Namespace string `json:"namespace" yaml:"namespace"` - Name string `json:"name" yaml:"name"` - ClassName string `json:"className" yaml:"className"` + Tenant string `json:"tenant" yaml:"tenant"` + Namespace string `json:"namespace" yaml:"namespace"` + Name string `json:"name" yaml:"name"` + ClassName string `json:"className" yaml:"className"` - TopicName string `json:"topicName" yaml:"topicName"` - SerdeClassName string `json:"serdeClassName" yaml:"serdeClassName"` - SchemaType string `json:"schemaType" yaml:"schemaType"` + TopicName string `json:"topicName" yaml:"topicName"` + SerdeClassName string `json:"serdeClassName" yaml:"serdeClassName"` + SchemaType string `json:"schemaType" yaml:"schemaType"` - Configs map[string]interface{} `json:"configs" yaml:"configs"` + Configs map[string]interface{} `json:"configs" yaml:"configs"` - // This is a map of secretName(aka how the secret is going to be - // accessed in the function via context) to an object that - // encapsulates how the secret is fetched by the underlying - // secrets provider. The type of an value here can be found by the - // SecretProviderConfigurator.getSecretObjectType() method. - Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` + // This is a map of secretName(aka how the secret is going to be + // accessed in the function via context) to an object that + // encapsulates how the secret is fetched by the underlying + // secrets provider. The type of an value here can be found by the + // SecretProviderConfigurator.getSecretObjectType() method. + Secrets map[string]interface{} `json:"secrets" yaml:"secrets"` - Parallelism int `json:"parallelism" yaml:"parallelism"` - ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` - Resources *Resources `json:"resources" yaml:"resources"` - Archive string `json:"archive" yaml:"archive"` - // Any flags that you want to pass to the runtime. - RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` + Parallelism int `json:"parallelism" yaml:"parallelism"` + ProcessingGuarantees string `json:"processingGuarantees" yaml:"processingGuarantees"` + Resources *Resources `json:"resources" yaml:"resources"` + Archive string `json:"archive" yaml:"archive"` + // Any flags that you want to pass to the runtime. + RuntimeFlags string `json:"runtimeFlags" yaml:"runtimeFlags"` } diff --git a/pkg/pulsar/source_status.go b/pkg/pulsar/source_status.go index 9c3833ae..1e147980 100644 --- a/pkg/pulsar/source_status.go +++ b/pkg/pulsar/source_status.go @@ -18,42 +18,26 @@ package pulsar type SourceStatus struct { - NumInstances int `json:"numInstances"` - NumRunning int `json:"numRunning"` - - Instances []*SourceInstanceStatus `json:"instances"` + NumInstances int `json:"numInstances"` + NumRunning int `json:"numRunning"` + Instances []*SourceInstanceStatus `json:"instances"` } type SourceInstanceStatus struct { - InstanceId int `json:"instanceId"` - - Status SourceInstanceStatusData `json:"status"` + InstanceID int `json:"instanceId"` + Status SourceInstanceStatusData `json:"status"` } type SourceInstanceStatusData struct { - Running bool `json:"running"` - - Err string `json:"error"` - - NumRestarts int64 `json:"numRestarts"` - - NumReceivedFromSource int64 `json:"numReceivedFromSource"` - - NumSystemExceptions int64 `json:"numSystemExceptions"` - - LatestSystemExceptions []ExceptionInformation `json:"latestSystemExceptions"` - - NumSourceExceptions int64 `json:"numSourceExceptions"` - - LatestSourceExceptions []ExceptionInformation `json:"latestSourceExceptions"` - - NumWritten int64 `json:"numWritten"` - - LastReceivedTime int64 `json:"lastReceivedTime"` - - WorkerId string `json:"workerId"` -} - -func (ss *SourceStatus)AddInstance(sourceInstanceStatus *SourceInstanceStatus) { - ss.Instances = append(ss.Instances, sourceInstanceStatus) + Running bool `json:"running"` + Err string `json:"error"` + NumRestarts int64 `json:"numRestarts"` + NumReceivedFromSource int64 `json:"numReceivedFromSource"` + NumSystemExceptions int64 `json:"numSystemExceptions"` + LatestSystemExceptions []ExceptionInformation `json:"latestSystemExceptions"` + NumSourceExceptions int64 `json:"numSourceExceptions"` + LatestSourceExceptions []ExceptionInformation `json:"latestSourceExceptions"` + NumWritten int64 `json:"numWritten"` + LastReceivedTime int64 `json:"lastReceivedTime"` + WorkerID string `json:"workerId"` } diff --git a/pkg/pulsar/sources.go b/pkg/pulsar/sources.go index 5d3f9a1d..ece4b546 100644 --- a/pkg/pulsar/sources.go +++ b/pkg/pulsar/sources.go @@ -18,416 +18,416 @@ package pulsar import ( - `bytes` - `encoding/json` - `fmt` - `io` - `mime/multipart` - `net/textproto` - `os` - `path/filepath` - `strings` + "bytes" + "encoding/json" + "fmt" + "io" + "mime/multipart" + "net/textproto" + "os" + "path/filepath" + "strings" ) type Sources interface { - // Get the list of all the Pulsar Sources. - ListSources(tenant, namespace string) ([]string, error) + // Get the list of all the Pulsar Sources. + ListSources(tenant, namespace string) ([]string, error) - // Get the configuration for the specified source - GetSource(tenant, namespace, source string) (SourceConfig, error) + // Get the configuration for the specified source + GetSource(tenant, namespace, source string) (SourceConfig, error) - // Create a new source - CreateSource(config *SourceConfig, fileName string) error + // Create a new source + CreateSource(config *SourceConfig, fileName string) error - // Create a new source by providing url from which fun-pkg can be downloaded. supported url: http/file - CreateSourceWithURL(config *SourceConfig, pkgUrl string) error + // Create a new source by providing url from which fun-pkg can be downloaded. supported url: http/file + CreateSourceWithURL(config *SourceConfig, pkgURL string) error - // Update the configuration for a source. - UpdateSource(config *SourceConfig, fileName string, options *UpdateOptions) error + // Update the configuration for a source. + UpdateSource(config *SourceConfig, fileName string, options *UpdateOptions) error - // Update a source by providing url from which fun-pkg can be downloaded. supported url: http/file - UpdateSourceWithUrl(config *SourceConfig, pkgUrl string, options *UpdateOptions) error + // Update a source by providing url from which fun-pkg can be downloaded. supported url: http/file + UpdateSourceWithURL(config *SourceConfig, pkgURL string, options *UpdateOptions) error - // Delete an existing source - DeleteSource(tenant, namespace, source string) error + // Delete an existing source + DeleteSource(tenant, namespace, source string) error - // Gets the current status of a source. - GetSourceStatus(tenant, namespace, source string) (SourceStatus, error) + // Gets the current status of a source. + GetSourceStatus(tenant, namespace, source string) (SourceStatus, error) - // Gets the current status of a source instance. - GetSourceStatusWithID(tenant, namespace, source string, id int) (SourceInstanceStatusData, error) + // Gets the current status of a source instance. + GetSourceStatusWithID(tenant, namespace, source string, id int) (SourceInstanceStatusData, error) - // Restart all source instances - RestartSource(tenant, namespace, source string) error + // Restart all source instances + RestartSource(tenant, namespace, source string) error - // Restart source instance - RestartSourceWithID(tenant, namespace, source string, id int) error + // Restart source instance + RestartSourceWithID(tenant, namespace, source string, id int) error - // Stop all source instances - StopSource(tenant, namespace, source string) error + // Stop all source instances + StopSource(tenant, namespace, source string) error - // Stop source instance - StopSourceWithID(tenant, namespace, source string, id int) error + // Stop source instance + StopSourceWithID(tenant, namespace, source string, id int) error - // Start all source instances - StartSource(tenant, namespace, source string) error + // Start all source instances + StartSource(tenant, namespace, source string) error - // Start source instance - StartSourceWithID(tenant, namespace, source string, id int) error + // Start source instance + StartSourceWithID(tenant, namespace, source string, id int) error - // Fetches a list of supported Pulsar IO sources currently running in cluster mode - GetBuiltInSources() ([]*ConnectorDefinition, error) + // Fetches a list of supported Pulsar IO sources currently running in cluster mode + GetBuiltInSources() ([]*ConnectorDefinition, error) - // Reload the available built-in connectors, include Source and Sink - ReloadBuiltInSources() error + // Reload the available built-in connectors, include Source and Sink + ReloadBuiltInSources() error } type sources struct { - client *client - basePath string + client *client + basePath string } func (c *client) Sources() Sources { - return &sources{ - client: c, - basePath: "/sources", - } + return &sources{ + client: c, + basePath: "/sources", + } } func (s *sources) createStringFromField(w *multipart.Writer, value string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) - h.Set("Content-Type", "application/json") - return w.CreatePart(h) + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) + h.Set("Content-Type", "application/json") + return w.CreatePart(h) } func (s *sources) createTextFromFiled(w *multipart.Writer, value string) (io.Writer, error) { - h := make(textproto.MIMEHeader) - h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) - h.Set("Content-Type", "text/plain") - return w.CreatePart(h) + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value)) + h.Set("Content-Type", "text/plain") + return w.CreatePart(h) } func (s *sources) ListSources(tenant, namespace string) ([]string, error) { - var sources []string - endpoint := s.client.endpoint(s.basePath, tenant, namespace) - err := s.client.get(endpoint, &sources) - return sources, err + var sources []string + endpoint := s.client.endpoint(s.basePath, tenant, namespace) + err := s.client.get(endpoint, &sources) + return sources, err } func (s *sources) GetSource(tenant, namespace, source string) (SourceConfig, error) { - var sourceConfig SourceConfig - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - err := s.client.get(endpoint, &sourceConfig) - return sourceConfig, err + var sourceConfig SourceConfig + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + err := s.client.get(endpoint, &sourceConfig) + return sourceConfig, err } func (s *sources) CreateSource(config *SourceConfig, fileName string) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { - // If the function code is built in, we don't need to submit here - file, err := os.Open(fileName) - if err != nil { - return err - } - defer file.Close() - - part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) - - if err != nil { - return err - } - - // copy the actual file content to the filed's writer - _, err = io.Copy(part, file) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { + // If the function code is built in, we don't need to submit here + file, err := os.Open(fileName) + if err != nil { + return err + } + defer file.Close() + + part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) + + if err != nil { + return err + } + + // copy the actual file content to the filed's writer + _, err = io.Copy(part, file) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } -func (s *sources) CreateSourceWithURL(config *SourceConfig, pkgUrl string) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - textWriter, err := s.createTextFromFiled(multiPartWriter, "url") - if err != nil { - return err - } - - _, err = textWriter.Write([]byte(pkgUrl)) - if err != nil { - return err - } - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.postWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil +func (s *sources) CreateSourceWithURL(config *SourceConfig, pkgURL string) error { + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + textWriter, err := s.createTextFromFiled(multiPartWriter, "url") + if err != nil { + return err + } + + _, err = textWriter.Write([]byte(pkgURL)) + if err != nil { + return err + } + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } func (s *sources) UpdateSource(config *SourceConfig, fileName string, updateOptions *UpdateOptions) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if updateOptions != nil { - updateData, err := json.Marshal(updateOptions) - if err != nil { - return err - } - - updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") - if err != nil { - return err - } - - _, err = updateStrWriter.Write(updateData) - if err != nil { - return err - } - } - - if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { - // If the function code is built in, we don't need to submit here - file, err := os.Open(fileName) - if err != nil { - return err - } - defer file.Close() - - part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) - - if err != nil { - return err - } - - // copy the actual file content to the filed's writer - _, err = io.Copy(part, file) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if updateOptions != nil { + updateData, err := json.Marshal(updateOptions) + if err != nil { + return err + } + + updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") + if err != nil { + return err + } + + _, err = updateStrWriter.Write(updateData) + if err != nil { + return err + } + } + + if fileName != "" && !strings.HasPrefix(fileName, "builtin://") { + // If the function code is built in, we don't need to submit here + file, err := os.Open(fileName) + if err != nil { + return err + } + defer file.Close() + + part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name())) + + if err != nil { + return err + } + + // copy the actual file content to the filed's writer + _, err = io.Copy(part, file) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.putWithMultiPart(endpoint, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } -func (s *sources) UpdateSourceWithUrl(config *SourceConfig, pkgUrl string, updateOptions *UpdateOptions) error { - endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) - // buffer to store our request as bytes - bodyBuf := bytes.NewBufferString("") - - multiPartWriter := multipart.NewWriter(bodyBuf) - - textWriter, err := s.createTextFromFiled(multiPartWriter, "url") - if err != nil { - return err - } - - _, err = textWriter.Write([]byte(pkgUrl)) - if err != nil { - return err - } - - jsonData, err := json.Marshal(config) - if err != nil { - return err - } - - stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") - if err != nil { - return err - } - - _, err = stringWriter.Write(jsonData) - if err != nil { - return err - } - - if updateOptions != nil { - updateData, err := json.Marshal(updateOptions) - if err != nil { - return err - } - - updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") - if err != nil { - return err - } - - _, err = updateStrWriter.Write(updateData) - if err != nil { - return err - } - } - - // In here, we completed adding the file and the fields, let's close the multipart writer - // So it writes the ending boundary - if err = multiPartWriter.Close(); err != nil { - return err - } - - contentType := multiPartWriter.FormDataContentType() - err = s.client.putWithMultiPart(endpoint, nil, nil, bodyBuf, contentType) - if err != nil { - return err - } - - return nil +func (s *sources) UpdateSourceWithURL(config *SourceConfig, pkgURL string, updateOptions *UpdateOptions) error { + endpoint := s.client.endpoint(s.basePath, config.Tenant, config.Namespace, config.Name) + // buffer to store our request as bytes + bodyBuf := bytes.NewBufferString("") + + multiPartWriter := multipart.NewWriter(bodyBuf) + + textWriter, err := s.createTextFromFiled(multiPartWriter, "url") + if err != nil { + return err + } + + _, err = textWriter.Write([]byte(pkgURL)) + if err != nil { + return err + } + + jsonData, err := json.Marshal(config) + if err != nil { + return err + } + + stringWriter, err := s.createStringFromField(multiPartWriter, "sourceConfig") + if err != nil { + return err + } + + _, err = stringWriter.Write(jsonData) + if err != nil { + return err + } + + if updateOptions != nil { + updateData, err := json.Marshal(updateOptions) + if err != nil { + return err + } + + updateStrWriter, err := s.createStringFromField(multiPartWriter, "updateOptions") + if err != nil { + return err + } + + _, err = updateStrWriter.Write(updateData) + if err != nil { + return err + } + } + + // In here, we completed adding the file and the fields, let's close the multipart writer + // So it writes the ending boundary + if err = multiPartWriter.Close(); err != nil { + return err + } + + contentType := multiPartWriter.FormDataContentType() + err = s.client.putWithMultiPart(endpoint, bodyBuf, contentType) + if err != nil { + return err + } + + return nil } func (s *sources) DeleteSource(tenant, namespace, source string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - return s.client.delete(endpoint, nil) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + return s.client.delete(endpoint) } func (s *sources) GetSourceStatus(tenant, namespace, source string) (SourceStatus, error) { - var sourceStatus SourceStatus - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - err := s.client.get(endpoint+"/status", &sourceStatus) - return sourceStatus, err + var sourceStatus SourceStatus + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + err := s.client.get(endpoint+"/status", &sourceStatus) + return sourceStatus, err } func (s *sources) GetSourceStatusWithID(tenant, namespace, source string, id int) (SourceInstanceStatusData, error) { - var sourceInstanceStatusData SourceInstanceStatusData - instanceID := fmt.Sprintf("%d", id) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, instanceID) - err := s.client.get(endpoint+"/status", &sourceInstanceStatusData) - return sourceInstanceStatusData, err + var sourceInstanceStatusData SourceInstanceStatusData + instanceID := fmt.Sprintf("%d", id) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, instanceID) + err := s.client.get(endpoint+"/status", &sourceInstanceStatusData) + return sourceInstanceStatusData, err } func (s *sources) RestartSource(tenant, namespace, source string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - return s.client.post(endpoint+"/restart", "", nil) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + return s.client.post(endpoint+"/restart", "") } func (s *sources) RestartSourceWithID(tenant, namespace, source string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) - return s.client.post(endpoint+"/restart", "", nil) + return s.client.post(endpoint+"/restart", "") } func (s *sources) StopSource(tenant, namespace, source string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - return s.client.post(endpoint+"/stop", "", nil) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + return s.client.post(endpoint+"/stop", "") } func (s *sources) StopSourceWithID(tenant, namespace, source string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) - return s.client.post(endpoint+"/stop", "", nil) + return s.client.post(endpoint+"/stop", "") } func (s *sources) StartSource(tenant, namespace, source string) error { - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) - return s.client.post(endpoint+"/start", "", nil) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source) + return s.client.post(endpoint+"/start", "") } func (s *sources) StartSourceWithID(tenant, namespace, source string, instanceID int) error { - id := fmt.Sprintf("%d", instanceID) - endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) + id := fmt.Sprintf("%d", instanceID) + endpoint := s.client.endpoint(s.basePath, tenant, namespace, source, id) - return s.client.post(endpoint+"/start", "", nil) + return s.client.post(endpoint+"/start", "") } func (s *sources) GetBuiltInSources() ([]*ConnectorDefinition, error) { - var connectorDefinition []*ConnectorDefinition - endpoint := s.client.endpoint(s.basePath, "builtinsources") - err := s.client.get(endpoint, &connectorDefinition) - return connectorDefinition, err + var connectorDefinition []*ConnectorDefinition + endpoint := s.client.endpoint(s.basePath, "builtinsources") + err := s.client.get(endpoint, &connectorDefinition) + return connectorDefinition, err } func (s *sources) ReloadBuiltInSources() error { - endpoint := s.client.endpoint(s.basePath, "reloadBuiltInSources") - return s.client.post(endpoint, "", nil) + endpoint := s.client.endpoint(s.basePath, "reloadBuiltInSources") + return s.client.post(endpoint, "") } diff --git a/pkg/pulsar/topic.go b/pkg/pulsar/topic.go index 0b1260a5..0533319b 100644 --- a/pkg/pulsar/topic.go +++ b/pkg/pulsar/topic.go @@ -34,7 +34,7 @@ type Topics interface { RevokePermission(TopicName, string) error Lookup(TopicName) (LookupData, error) GetBundleRange(TopicName) (string, error) - GetLastMessageId(TopicName) (MessageId, error) + GetLastMessageID(TopicName) (MessageID, error) GetStats(TopicName) (TopicStats, error) GetInternalStats(TopicName) (PersistentTopicInternalStats, error) GetPartitionedStats(TopicName, bool) (PartitionedTopicStats, error) @@ -63,7 +63,7 @@ func (t *topics) Create(topic TopicName, partitions int) error { if partitions == 0 { endpoint = t.client.endpoint(t.basePath, topic.GetRestPath()) } - return t.client.put(endpoint, partitions, nil) + return t.client.put(endpoint, partitions) } func (t *topics) Delete(topic TopicName, force bool, nonPartitioned bool) error { @@ -79,7 +79,7 @@ func (t *topics) Delete(topic TopicName, force bool, nonPartitioned bool) error func (t *topics) Update(topic TopicName, partitions int) error { endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "partitions") - return t.client.post(endpoint, partitions, nil) + return t.client.post(endpoint, partitions) } func (t *topics) GetMetadata(topic TopicName) (PartitionedTopicMetadata, error) { @@ -137,7 +137,7 @@ func (t *topics) GetInternalInfo(topic TopicName) (ManagedLedgerInfo, error) { endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "internal-info") var info ManagedLedgerInfo err := t.client.get(endpoint, &info) - return info, err + return info, err } func (t *topics) GetPermissions(topic TopicName) (map[string][]AuthAction, error) { @@ -149,16 +149,16 @@ func (t *topics) GetPermissions(topic TopicName) (map[string][]AuthAction, error func (t *topics) GrantPermission(topic TopicName, role string, action []AuthAction) error { endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "permissions", role) - var s []string + s := []string{} for _, v := range action { s = append(s, v.String()) } - return t.client.post(endpoint, s, nil) + return t.client.post(endpoint, s) } func (t *topics) RevokePermission(topic TopicName, role string) error { endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "permissions", role) - return t.client.delete(endpoint, nil) + return t.client.delete(endpoint) } func (t *topics) Lookup(topic TopicName) (LookupData, error) { @@ -174,11 +174,11 @@ func (t *topics) GetBundleRange(topic TopicName) (string, error) { return string(data), err } -func (t *topics) GetLastMessageId(topic TopicName) (MessageId, error) { - var messageId MessageId +func (t *topics) GetLastMessageID(topic TopicName) (MessageID, error) { + var messageID MessageID endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "lastMessageId") - err := t.client.get(endpoint, &messageId) - return messageId, err + err := t.client.get(endpoint, &messageID) + return messageID, err } func (t *topics) GetStats(topic TopicName) (TopicStats, error) { diff --git a/pkg/pulsar/topic_domain.go b/pkg/pulsar/topic_domain.go index a54d1a1a..9210a507 100644 --- a/pkg/pulsar/topic_domain.go +++ b/pkg/pulsar/topic_domain.go @@ -22,8 +22,8 @@ import "github.com/pkg/errors" type TopicDomain string const ( - persistent TopicDomain = "persistent" - non_persistent TopicDomain = "non-persistent" + persistent TopicDomain = "persistent" + nonPersistent TopicDomain = "non-persistent" ) func ParseTopicDomain(domain string) (TopicDomain, error) { @@ -31,9 +31,9 @@ func ParseTopicDomain(domain string) (TopicDomain, error) { case "persistent": return persistent, nil case "non-persistent": - return non_persistent, nil + return nonPersistent, nil default: - return "", errors.Errorf("The domain only can be specified as 'persistent' or " + + return "", errors.Errorf("The domain only can be specified as 'persistent' or "+ "'non-persistent'. Input domain is '%s'.", domain) } } diff --git a/pkg/pulsar/topic_name.go b/pkg/pulsar/topic_name.go index 5c27eb15..324e9d82 100644 --- a/pkg/pulsar/topic_name.go +++ b/pkg/pulsar/topic_name.go @@ -19,16 +19,17 @@ package pulsar import ( "fmt" - "github.com/pkg/errors" "net/url" "strconv" "strings" + + "github.com/pkg/errors" ) const ( - PUBLIC_TENANT = "public" - DEFAULT_NAMESPACE = "default" - PARTITIONED_TOPIC_SUFFIX = "-partition-" + PUBLICTENANT = "public" + DEFAULTNAMESPACE = "default" + PARTITIONEDTOPICSUFFIX = "-partition-" ) type TopicName struct { @@ -50,11 +51,12 @@ func GetTopicName(completeName string) (*TopicName, error) { // - // if !strings.Contains(completeName, "://") { parts := strings.Split(completeName, "/") - if len(parts) == 3 { + switch len(parts) { + case 3: completeName = persistent.String() + "://" + completeName - } else if len(parts) == 1 { - completeName = persistent.String() + "://" + PUBLIC_TENANT + "/" + DEFAULT_NAMESPACE + "/" + parts[0] - } else { + case 1: + completeName = persistent.String() + "://" + PUBLICTENANT + "/" + DEFAULTNAMESPACE + "/" + parts[0] + default: return nil, errors.Errorf("Invalid short topic name '%s', it should be "+ "in the format of // or ", completeName) } @@ -79,12 +81,12 @@ func GetTopicName(completeName string) (*TopicName, error) { topicname.topic = parts[2] topicname.partitionIndex = getPartitionIndex(completeName) } else { - return nil, errors.Errorf("Invalid topic name '%s', it should be in the format of "+ + return nil, errors.Errorf("invalid topic name '%s', it should be in the format of "+ "//", rest) } if topicname.topic == "" { - return nil, errors.New("Topic name can not be empty.") + return nil, errors.New("topic name can not be empty") } n, err := GetNameSpaceName(topicname.tenant, topicname.namespace) @@ -118,19 +120,19 @@ func (t *TopicName) GetLocalName() string { func (t *TopicName) GetPartition(index int) (*TopicName, error) { if index < 0 { - return nil, errors.New("Invalid partition index number.") + return nil, errors.New("invalid partition index number") } - if strings.Contains(t.String(), PARTITIONED_TOPIC_SUFFIX) { + if strings.Contains(t.String(), PARTITIONEDTOPICSUFFIX) { return t, nil } - topicNameWithPartition := t.String() + PARTITIONED_TOPIC_SUFFIX + strconv.Itoa(index) + topicNameWithPartition := t.String() + PARTITIONEDTOPICSUFFIX + strconv.Itoa(index) return GetTopicName(topicNameWithPartition) } func getPartitionIndex(topic string) int { - if strings.Contains(topic, PARTITIONED_TOPIC_SUFFIX) { + if strings.Contains(topic, PARTITIONEDTOPICSUFFIX) { parts := strings.Split(topic, "-") index, err := strconv.Atoi(parts[len(parts)-1]) if err == nil { diff --git a/pkg/pulsar/topic_name_test.go b/pkg/pulsar/topic_name_test.go index a7be0d03..55fc5090 100644 --- a/pkg/pulsar/topic_name_test.go +++ b/pkg/pulsar/topic_name_test.go @@ -18,9 +18,10 @@ package pulsar import ( - "github.com/stretchr/testify/assert" "net/url" "testing" + + "github.com/stretchr/testify/assert" ) func TestGetTopicName(t *testing.T) { @@ -40,11 +41,12 @@ func TestGetTopicName(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "non-persistent://tenant/namespace/success", success.String()) - fail, err := GetTopicName("://tenant.namespace.topic") + _, err = GetTopicName("://tenant.namespace.topic") assert.NotNil(t, err) - assert.Equal(t, "The domain only can be specified as 'persistent' or 'non-persistent'. Input domain is ''.", err.Error()) + assert.Equal(t, "The domain only can be specified as 'persistent' or 'non-persistent'."+ + " Input domain is ''.", err.Error()) - fail, err = GetTopicName("default/fail") + fail, err := GetTopicName("default/fail") assert.NotNil(t, err) assert.Equal(t, "Invalid short topic name 'default/fail', it should be in the "+ "format of // or ", err.Error()) @@ -63,13 +65,13 @@ func TestGetTopicName(t *testing.T) { fail, err = GetTopicName("persistent://tenant/namespace") assert.NotNil(t, err) - assert.Equal(t, "Invalid topic name 'tenant/namespace', it should be in the format "+ + assert.Equal(t, "invalid topic name 'tenant/namespace', it should be in the format "+ "of //", err.Error()) assert.Nil(t, fail) fail, err = GetTopicName("persistent://tenant/namespace/") assert.NotNil(t, err) - assert.Equal(t, "Topic name can not be empty.", err.Error()) + assert.Equal(t, "topic name can not be empty", err.Error()) assert.Nil(t, fail) } diff --git a/pkg/pulsar/utils.go b/pkg/pulsar/utils.go index e4087451..249f18d2 100644 --- a/pkg/pulsar/utils.go +++ b/pkg/pulsar/utils.go @@ -21,6 +21,6 @@ import ( "fmt" ) -func makeHttpPath(apiVersion string, componentPath string) string { +func makeHTTPPath(apiVersion string, componentPath string) string { return fmt.Sprintf("/admin/%s%s", apiVersion, componentPath) }