Skip to content
Open
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
88 changes: 88 additions & 0 deletions pkg/ctl/namespace/bundles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 namespace

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
)

func getBundles(vc *cmdutils.VerbCmd) {
desc := cmdutils.LongDescription{}
desc.CommandUsedFor = "Get the list of bundles for a namespace"
desc.CommandPermission = "This command requires tenant admin permissions."

var examples []cmdutils.Example
example := cmdutils.Example{
Desc: "Get the list of bundles for a namespace",
Command: "pulsarctl namespaces bundles (tenant/namespace)",
}
examples = append(examples, example)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "normal output",
Out: "{\n" +
" \"boundaries\": [\n" +
" \"0x00000000\",\n" +
" \"0xffffffff\"\n" +
" ],\n" +
" \"numBundles\": 1\n" +
"}",
}

noNamespaceName := cmdutils.Output{
Desc: "you must specify a tenant/namespace name, please check if the tenant/namespace name is provided",
Out: "[✖] the namespace name is not specified or the namespace name is specified more than one",
}

tenantNotExistError := cmdutils.Output{
Desc: "the tenant does not exist",
Out: "[✖] code: 404 reason: Tenant does not exist",
}

nsNotExistError := cmdutils.Output{
Desc: "the namespace does not exist",
Out: "[✖] code: 404 reason: Namespace (tenant/namespace) does not exist",
}

out = append(out, successOut, noNamespaceName, tenantNotExistError, nsNotExistError)
desc.CommandOutput = out

vc.SetDescription(
"bundles",
"Get the list of bundles for a namespace",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doGetBundles(vc)
}, "the namespace name is not specified or the namespace name is specified more than one")

vc.EnableOutputFlagSet()
}

func doGetBundles(vc *cmdutils.VerbCmd) error {
admin := cmdutils.NewPulsarClient()
policies, err := admin.Namespaces().GetPolicies(vc.NameArg)
if err == nil {
oc := cmdutils.NewOutputContent().WithObject(policies.Bundles)
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc)
}
return err
}
51 changes: 51 additions & 0 deletions pkg/ctl/namespace/bundles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 namespace

import (
"encoding/json"
"testing"

"github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils"
"github.com/stretchr/testify/assert"
)

func TestBundlesCommandArgsError(t *testing.T) {
args := []string{"bundles"}
_, _, nameErr, _ := TestNamespaceCommands(getBundles, args)
assert.Equal(t, "the namespace name is not specified or the namespace name is specified more than one",
nameErr.Error())
}

func TestBundlesCommand(t *testing.T) {
ns := "public/test-bundles-namespace"

args := []string{"create", ns}
_, execErr, _, _ := TestNamespaceCommands(createNs, args)
assert.Nil(t, execErr)

args = []string{"bundles", ns}
out, execErr, _, _ := TestNamespaceCommands(getBundles, args)
assert.Nil(t, execErr)

var bundles utils.BundlesData
err := json.Unmarshal(out.Bytes(), &bundles)
assert.Nil(t, err)
assert.True(t, bundles.NumBundles > 0)
assert.True(t, len(bundles.Boundaries) >= 2)
}
86 changes: 86 additions & 0 deletions pkg/ctl/namespace/get_deduplication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 namespace

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
)

func getDeduplication(vc *cmdutils.VerbCmd) {
desc := cmdutils.LongDescription{}
desc.CommandUsedFor = "Get deduplication for a namespace"
desc.CommandPermission = "This command requires tenant admin permissions."

var examples []cmdutils.Example
example := cmdutils.Example{
Desc: "Get deduplication for a namespace",
Command: "pulsarctl namespaces get-deduplication tenant/namespace",
}
examples = append(examples, example)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "normal output",
Out: "true",
}

noNamespaceName := cmdutils.Output{
Desc: "you must specify a tenant/namespace name, please check if the tenant/namespace name is provided",
Out: "[✖] the namespace name is not specified or the namespace name is specified more than one",
}

tenantNotExistError := cmdutils.Output{
Desc: "the tenant does not exist",
Out: "[✖] code: 404 reason: Tenant does not exist",
}

nsNotExistError := cmdutils.Output{
Desc: "the namespace does not exist",
Out: "[✖] code: 404 reason: Namespace (tenant/namespace) does not exist",
}

out = append(out, successOut, noNamespaceName, tenantNotExistError, nsNotExistError)
desc.CommandOutput = out

vc.SetDescription(
"get-deduplication",
"Get deduplication for a namespace",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doGetDeduplication(vc)
}, "the namespace name is not specified or the namespace name is specified more than one")

vc.EnableOutputFlagSet()
}

func doGetDeduplication(vc *cmdutils.VerbCmd) error {
admin := cmdutils.NewPulsarClient()
policies, err := admin.Namespaces().GetPolicies(vc.NameArg)
if err == nil {
enabled := false
if policies.DeduplicationEnabled != nil {
enabled = *policies.DeduplicationEnabled
}
oc := cmdutils.NewOutputContent().WithObject(enabled)
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc)
}
return err
}
49 changes: 49 additions & 0 deletions pkg/ctl/namespace/get_deduplication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 namespace

import (
"encoding/json"
"testing"

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

func TestGetDeduplicationArgsError(t *testing.T) {
args := []string{"get-deduplication"}
_, _, nameErr, _ := TestNamespaceCommands(getDeduplication, args)
assert.Equal(t, "the namespace name is not specified or the namespace name is specified more than one",
nameErr.Error())
}

func TestGetDeduplication(t *testing.T) {
ns := "public/test-get-deduplication-ns"

args := []string{"create", ns}
_, execErr, _, _ := TestNamespaceCommands(createNs, args)
assert.Nil(t, execErr)

args = []string{"get-deduplication", ns}
out, execErr, _, _ := TestNamespaceCommands(getDeduplication, args)
assert.Nil(t, execErr)

var enabled bool
err := json.Unmarshal(out.Bytes(), &enabled)
assert.Nil(t, err)
assert.False(t, enabled)
}
67 changes: 67 additions & 0 deletions pkg/ctl/namespace/get_encryption_required.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 namespace

import (
"github.com/streamnative/pulsarctl/pkg/cmdutils"
)

func GetEncryptionRequiredCmd(vc *cmdutils.VerbCmd) {
var desc cmdutils.LongDescription
desc.CommandUsedFor = "This command is used for getting whether encryption is required for a namespace."
desc.CommandPermission = "This command requires tenant admin permissions."

var examples []cmdutils.Example
example := cmdutils.Example{
Desc: "Get whether encryption is required for the namespace (namespace-name)",
Command: "pulsarctl namespaces get-encryption-required (namespace-name)",
}
examples = append(examples, example)
desc.CommandExamples = examples

var out []cmdutils.Output
successOut := cmdutils.Output{
Desc: "normal output",
Out: "true",
}
out = append(out, successOut, ArgError, NsNotExistError)
out = append(out, NsErrors...)
desc.CommandOutput = out

vc.SetDescription(
"get-encryption-required",
"Get whether encryption is required for a namespace",
desc.ToString(),
desc.ExampleToString())

vc.SetRunFuncWithNameArg(func() error {
return doGetEncryptionRequired(vc)
}, "the namespace name is not specified or the namespace name is specified more than one")

vc.EnableOutputFlagSet()
}

func doGetEncryptionRequired(vc *cmdutils.VerbCmd) error {
admin := cmdutils.NewPulsarClient()
policies, err := admin.Namespaces().GetPolicies(vc.NameArg)
if err == nil {
oc := cmdutils.NewOutputContent().WithObject(policies.EncryptionRequired)
err = vc.OutputConfig.WriteOutput(vc.Command.OutOrStdout(), oc)
}
return err
}
49 changes: 49 additions & 0 deletions pkg/ctl/namespace/get_encryption_required_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 namespace

import (
"encoding/json"
"testing"

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

func TestGetEncryptionRequiredArgsError(t *testing.T) {
args := []string{"get-encryption-required"}
_, _, nameErr, _ := TestNamespaceCommands(GetEncryptionRequiredCmd, args)
assert.Equal(t, "the namespace name is not specified or the namespace name is specified more than one",
nameErr.Error())
}

func TestGetEncryptionRequiredCmd(t *testing.T) {
ns := "public/test-get-encryption-required-ns"

args := []string{"create", ns}
_, execErr, _, _ := TestNamespaceCommands(createNs, args)
assert.Nil(t, execErr)

args = []string{"get-encryption-required", ns}
out, execErr, _, _ := TestNamespaceCommands(GetEncryptionRequiredCmd, args)
assert.Nil(t, execErr)

var enabled bool
err := json.Unmarshal(out.Bytes(), &enabled)
assert.Nil(t, err)
assert.False(t, enabled)
}
Loading
Loading