Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cmdutils/cmdutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func NewPulsarClientWithAPIVersion(version pulsar.APIVersion) pulsar.Client {
return PulsarCtlConfig.Client(version)
}

func NewBookieClient() pulsar.BookieClient {
return PulsarCtlConfig.BookieClient()
}

func PrintJSON(w io.Writer, obj interface{}) {
b, err := json.MarshalIndent(obj, "", " ")
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions pkg/cmdutils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type ClusterConfig struct {

AuthParams string

// the bookkeeper web service url that pulsarctl connects to.
BookieWebServiceURL string

// Token and TokenFile is used to config the pulsarctl using token to authentication
Token string
TokenFile string
Expand Down Expand Up @@ -77,6 +80,13 @@ func (c *ClusterConfig) FlagSet() *pflag.FlagSet {
"",
"Allow TLS trust cert file path")

flags.StringVar(
&c.BookieWebServiceURL,
"bookie-service-url",
pulsar.DefaultBookieWebServiceURL,
"The bookie web service url that pulsarctl connects to.",
)

flags.StringVar(
&c.Token,
"token",
Expand Down Expand Up @@ -148,3 +158,12 @@ func (c *ClusterConfig) Client(version pulsar.APIVersion) pulsar.Client {
}
return client
}

func (c *ClusterConfig) BookieClient() pulsar.BookieClient {
config := pulsar.DefaultConfig()
if len(c.BookieWebServiceURL) > 0 && c.BookieWebServiceURL != config.BookieWebServiceURL {
config.BookieWebServiceURL = c.BookieWebServiceURL
}

return pulsar.NewBookieClient(config)
}
38 changes: 38 additions & 0 deletions pkg/ctl/bk/bk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package bk

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/ctl/bk/ledger"

"github.com/spf13/cobra"
)

func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
resourceCmd := cmdutils.NewResourceCmd(
"bk",
"Operations about bookKeeper",
"",
"",
)

resourceCmd.AddCommand(ledger.Command(flagGrouping))

return resourceCmd
}
74 changes: 74 additions & 0 deletions pkg/ctl/bk/ledger/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package ledger

import (
"strconv"

"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/pkg/errors"
)

func DeleteCmd(vc *cmdutils.VerbCmd) {
var desc pulsar.LongDescription
desc.CommandUsedFor = "This command is used for deleting a ledger."
desc.CommandPermission = "none"

var examples []pulsar.Example
deleteLedger := pulsar.Example{
Desc: "Delete the specified ledger",
Command: "pulsarctl bookies ledger delete --ledger-id (ledger-id)",
}
examples = append(examples, deleteLedger)
desc.CommandExamples = examples

var out []pulsar.Output
successOut := pulsar.Output{
Desc: "normal output",
Out: "Successfully delete the ledger (ledger-id)",
}
out = append(out, successOut, argError)
desc.CommandOutput = out

vc.SetDescription(
"delete",
"Delete a ledger",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doDeleteCmd(vc)
}, "the ledger id is not specified or the ledger id is specified more than one")
}

func doDeleteCmd(vc *cmdutils.VerbCmd) error {
id, err := strconv.ParseInt(vc.NameArg, 10, 64)
if err != nil || id < 0 {
return errors.Errorf("invalid ledger id %s", vc.NameArg)
}

admin := cmdutils.NewBookieClient()
err = admin.Ledger().Delete(id)
if err == nil {
vc.Command.Printf("Successfully delete the ledger %d", id)
}

return err
}
42 changes: 42 additions & 0 deletions pkg/ctl/bk/ledger/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package ledger

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDeleteArgError(t *testing.T) {
args := []string{"delete"}
_, _, nameErr, _ := TestLedgerCommands(DeleteCmd, args)
assert.NotNil(t, nameErr)
assert.Equal(t, "the ledger id is not specified or the ledger id is specified more than one",
nameErr.Error())

args = []string{"delete", "a"}
_, execErr, _, _ := TestLedgerCommands(DeleteCmd, args)
assert.NotNil(t, execErr)
assert.Equal(t, "invalid ledger id a", execErr.Error())

args = []string{"delete", "--", "-1"}
_, execErr, _, _ = TestLedgerCommands(DeleteCmd, args)
assert.NotNil(t, execErr)
assert.Equal(t, "invalid ledger id -1", execErr.Error())
}
25 changes: 25 additions & 0 deletions pkg/ctl/bk/ledger/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package ledger

import "github.com/streamnative/pulsarctl/pkg/pulsar"

var argError = pulsar.Output{
Desc: "the ledger id is not specified or the ledger id is specified more than one",
Out: "[✖] the ledger id is not specified or the ledger id is specified more than one",
}
105 changes: 105 additions & 0 deletions pkg/ctl/bk/ledger/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package ledger

import (
"encoding/json"
"strconv"

"github.com/streamnative/pulsarctl/pkg/cmdutils"
"github.com/streamnative/pulsarctl/pkg/pulsar"

"github.com/pkg/errors"
)

func GetCmd(vc *cmdutils.VerbCmd) {
var desc pulsar.LongDescription
desc.CommandUsedFor = "This command is used for getting the metadata of a ledger."
desc.CommandPermission = "none"

var examples []pulsar.Example
get := pulsar.Example{
Desc: "Get the metadata of the specified ledger",
Command: "pulsarctl bookies ledger get (ledger-i)",
}
examples = append(examples, get)
desc.CommandExamples = examples

metadata := pulsar.LedgerMetadata{
MetadataFormatVersion: 1,
Ensemble: 1,
WriteQuorum: 1,
AckQuorum: 1,
Length: 1,
LastEntryID: 1,
Ctime: 1,
CToken: 0,
State: "CLOSED",
DigestType: "MAC",
Ensembles: map[int64][]pulsar.BookieSocketAddress{
1: {
pulsar.BookieSocketAddress{
HostName: "www.examples.com",
Port: 8080,
},
},
},
CurrentEnsemble: []pulsar.BookieSocketAddress{
{
HostName: "www.example.com",
Port: 8080,
},
},
Password: make([]byte, 0),
CustomMetadata: map[string][]byte{},
}
meta, _ := json.MarshalIndent(metadata, "", " ")

var out []pulsar.Output
successOut := pulsar.Output{
Desc: "normal output",
Out: string(meta),
}
out = append(out, successOut, argError)
desc.CommandOutput = out

vc.SetDescription(
"get",
"Get the metadata of a ledger",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doGet(vc)
}, "the ledger id is not specified or the ledger id is specified more than one")
}

func doGet(vc *cmdutils.VerbCmd) error {
id, err := strconv.ParseInt(vc.NameArg, 10, 64)
if err != nil || id < 0 {
return errors.Errorf("invalid ledger id %s", vc.NameArg)
}

admin := cmdutils.NewBookieClient()
metadata, err := admin.Ledger().Get(id)
if err == nil {
cmdutils.PrintJSON(vc.Command.OutOrStdout(), metadata)
}

return err
}
42 changes: 42 additions & 0 deletions pkg/ctl/bk/ledger/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package ledger

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetArgError(t *testing.T) {
args := []string{"get"}
_, _, nameErr, _ := TestLedgerCommands(GetCmd, args)
assert.NotNil(t, nameErr)
assert.Equal(t, "the ledger id is not specified or the ledger id is specified more than one",
nameErr.Error())

args = []string{"get", "a"}
_, execErr, _, _ := TestLedgerCommands(GetCmd, args)
assert.NotNil(t, execErr)
assert.Equal(t, "invalid ledger id a", execErr.Error())

args = []string{"get", "--", "-1"}
_, execErr, _, _ = TestLedgerCommands(GetCmd, args)
assert.NotNil(t, execErr)
assert.Equal(t, "invalid ledger id -1", execErr.Error())
}
Loading