Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for subscription expiration time namespace settings #1254

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions pulsaradmin/pkg/admin/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@

// SetInactiveTopicPolicies sets the inactive topic policies on a namespace
SetInactiveTopicPolicies(namespace utils.NameSpaceName, data utils.InactiveTopicPolicies) error

// GetSubscriptionExpirationTime gets the subscription expiration time on a namespace. Returns -1 if not set
GetSubscriptionExpirationTime(namespace utils.NameSpaceName) (int, error)

// SetSubscriptionExpirationTime sets the subscription expiration time on a namespace
SetSubscriptionExpirationTime(namespace utils.NameSpaceName, expirationTimeInMinutes int) error

// RemoveSubscriptionExpirationTime removes subscription expiration time from a namespace, defaulting to broker settings
RemoveSubscriptionExpirationTime(namespace utils.NameSpaceName) error
}

type namespaces struct {
Expand Down Expand Up @@ -883,3 +892,21 @@
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "inactiveTopicPolicies")
return n.pulsar.Client.Post(endpoint, data)
}

func (n *namespaces) GetSubscriptionExpirationTime(namespace utils.NameSpaceName) (int, error) {
var result int = -1

Check failure on line 897 in pulsaradmin/pkg/admin/namespace.go

View workflow job for this annotation

GitHub Actions / lint

var-declaration: should omit type int from declaration of var result; it will be inferred from the right-hand side (revive)

endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "subscriptionExpirationTime")
err := n.pulsar.Client.Get(endpoint, &result)
return result, err
}

func (n *namespaces) SetSubscriptionExpirationTime(namespace utils.NameSpaceName, subscriptionExpirationTimeInMinutes int) error {
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "subscriptionExpirationTime")
return n.pulsar.Client.Post(endpoint, &subscriptionExpirationTimeInMinutes)
}

func (n *namespaces) RemoveSubscriptionExpirationTime(namespace utils.NameSpaceName) error {
endpoint := n.pulsar.endpoint(n.basePath, namespace.String(), "subscriptionExpirationTime")
return n.pulsar.Client.Delete(endpoint)
}
76 changes: 76 additions & 0 deletions pulsaradmin/pkg/admin/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,79 @@ func TestGetTopicAutoCreation(t *testing.T) {
}
assert.Equal(t, expected, *topicAutoCreation)
}

func TestNamespaces_SetSubscriptionExpirationTime(t *testing.T) {
config := &config.Config{}
admin, err := New(config)
require.NoError(t, err)
require.NotNil(t, admin)

tests := []struct {
name string
namespace string
subscriptionExpirationTime int
errReason string
}{
{
name: "Set valid subscription expiration time",
namespace: "public/default",
subscriptionExpirationTime: 60,
errReason: "",
},
{
name: "Set invalid subscription expiration time",
namespace: "public/default",
subscriptionExpirationTime: -60,
errReason: "Invalid value for subscription expiration time",
},
{
name: "Set valid subscription expiration time: 0",
namespace: "public/default",
subscriptionExpirationTime: 0,
errReason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
namespace, _ := utils.GetNamespaceName(tt.namespace)
err := admin.Namespaces().SetSubscriptionExpirationTime(*namespace, tt.subscriptionExpirationTime)
if tt.errReason == "" {
assert.Equal(t, nil, err)

err = admin.Namespaces().RemoveSubscriptionExpirationTime(*namespace)
assert.Equal(t, nil, err)
}
if err != nil {
restError := err.(rest.Error)
assert.Equal(t, tt.errReason, restError.Reason)
}
})
}
}

func TestNamespaces_GetSubscriptionExpirationTime(t *testing.T) {
config := &config.Config{}
admin, err := New(config)
require.NoError(t, err)
require.NotNil(t, admin)

namespace, _ := utils.GetNamespaceName("public/default")

// set the subscription expiration time and get it
err = admin.Namespaces().SetSubscriptionExpirationTime(*namespace,
60)
assert.Equal(t, nil, err)
subscriptionExpirationTime, err := admin.Namespaces().GetSubscriptionExpirationTime(*namespace)
assert.Equal(t, nil, err)
expected := 60
assert.Equal(t, expected, subscriptionExpirationTime)

// remove the subscription expiration time and get it
err = admin.Namespaces().RemoveSubscriptionExpirationTime(*namespace)
assert.Equal(t, nil, err)

subscriptionExpirationTime, err = admin.Namespaces().GetSubscriptionExpirationTime(*namespace)
assert.Equal(t, nil, err)
expected = -1
assert.Equal(t, expected, subscriptionExpirationTime)
}
Loading