From bfe57d5d4839b7b2e411ea1dfde49a1a7d905d06 Mon Sep 17 00:00:00 2001 From: labuladong Date: Wed, 26 Jun 2024 13:31:45 +0800 Subject: [PATCH 1/4] simplify --- pulsaradmin/pkg/admin/brokers_test.go | 57 +++++++++++++++++++++++++++ pulsaradmin/pkg/rest/client.go | 23 +++++++++++ 2 files changed, 80 insertions(+) diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 5be90f6c34..54a0feab85 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -18,10 +18,15 @@ package admin import ( + "encoding/json" + "net/http" + "net/url" "os" "testing" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/auth" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin/config" + "github.com/apache/pulsar-client-go/pulsaradmin/pkg/rest" "github.com/apache/pulsar-client-go/pulsaradmin/pkg/utils" "github.com/stretchr/testify/assert" ) @@ -91,3 +96,55 @@ func TestUpdateDynamicConfiguration(t *testing.T) { assert.NoError(t, err) assert.NotEmpty(t, configurations) } + +func TestUpdateDynamicConfigurationWithCustomURL(t *testing.T) { + readFile, err := os.ReadFile("../../../integration-tests/tokens/admin-token") + assert.NoError(t, err) + cfg := &config.Config{ + WebServiceURL: DefaultWebServiceURL, + Token: string(readFile), + } + + authProvider, err := auth.GetAuthProvider(cfg) + assert.NoError(t, err) + + client := rest.Client{ + ServiceURL: cfg.WebServiceURL, + VersionInfo: ReleaseVersion, + HTTPClient: &http.Client{ + Timeout: DefaultHTTPTimeOutDuration, + Transport: authProvider, + }, + } + u, err := url.Parse(cfg.WebServiceURL) + assert.NoError(t, err) + + // example config value with '/' + value := `{"key/123":"https://example.com/"}` + encoded := url.QueryEscape(value) + + resp, err := client.MakeRequestWithURL(http.MethodPost, &url.URL{ + Scheme: u.Scheme, + User: u.User, + Host: u.Host, + Path: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + value, + RawPath: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + encoded, + }) + assert.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, http.StatusOK, resp.StatusCode) + + // get the config, check if it's updated + admin, err := New(cfg) + assert.NoError(t, err) + assert.NotNil(t, admin) + + configurations, err := admin.Brokers().GetAllDynamicConfigurations() + assert.NoError(t, err) + assert.NotEmpty(t, configurations) + + var m map[string]interface{} + err = json.Unmarshal([]byte(configurations["allowAutoSubscriptionCreation"]), &m) + assert.NoError(t, err) + assert.Equal(t, "https://example.com/", m["key/123"]) +} diff --git a/pulsaradmin/pkg/rest/client.go b/pulsaradmin/pkg/rest/client.go index e6b603f5f8..61382a2a5d 100644 --- a/pulsaradmin/pkg/rest/client.go +++ b/pulsaradmin/pkg/rest/client.go @@ -67,6 +67,15 @@ func (c *Client) newRequest(method, path string) (*request, error) { return req, nil } +func (c *Client) newRequestWithURL(method string, urlOpt *url.URL) (*request, error) { + req := &request{ + method: method, + url: urlOpt, + params: make(url.Values), + } + return req, nil +} + func (c *Client) doRequest(r *request) (*http.Response, error) { req, err := r.toHTTP() if err != nil { @@ -104,6 +113,20 @@ func (c *Client) MakeRequest(method, endpoint string) (*http.Response, error) { return resp, nil } +func (c *Client) MakeRequestWithURL(method string, urlOpt *url.URL) (*http.Response, error) { + req, err := c.newRequestWithURL(method, urlOpt) + if err != nil { + return nil, err + } + + resp, err := checkSuccessful(c.doRequest(req)) + if err != nil { + return nil, err + } + + return resp, nil +} + func (c *Client) Get(endpoint string, obj interface{}) error { _, err := c.GetWithQueryParams(endpoint, obj, nil, true) return err From 597be31aa752ef2187ba49c3397272b2998b32ca Mon Sep 17 00:00:00 2001 From: labuladong Date: Wed, 26 Jun 2024 13:35:30 +0800 Subject: [PATCH 2/4] fix lint --- pulsaradmin/pkg/rest/client.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pulsaradmin/pkg/rest/client.go b/pulsaradmin/pkg/rest/client.go index 61382a2a5d..6541cf50c6 100644 --- a/pulsaradmin/pkg/rest/client.go +++ b/pulsaradmin/pkg/rest/client.go @@ -67,13 +67,13 @@ func (c *Client) newRequest(method, path string) (*request, error) { return req, nil } -func (c *Client) newRequestWithURL(method string, urlOpt *url.URL) (*request, error) { +func (c *Client) newRequestWithURL(method string, urlOpt *url.URL) *request { req := &request{ method: method, url: urlOpt, params: make(url.Values), } - return req, nil + return req } func (c *Client) doRequest(r *request) (*http.Response, error) { @@ -114,10 +114,7 @@ func (c *Client) MakeRequest(method, endpoint string) (*http.Response, error) { } func (c *Client) MakeRequestWithURL(method string, urlOpt *url.URL) (*http.Response, error) { - req, err := c.newRequestWithURL(method, urlOpt) - if err != nil { - return nil, err - } + req := c.newRequestWithURL(method, urlOpt) resp, err := checkSuccessful(c.doRequest(req)) if err != nil { From 9afee8563a7f8ac216759de001b9150c521037e4 Mon Sep 17 00:00:00 2001 From: labuladong Date: Wed, 26 Jun 2024 13:44:47 +0800 Subject: [PATCH 3/4] ci --- pulsaradmin/pkg/rest/client.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/pulsaradmin/pkg/rest/client.go b/pulsaradmin/pkg/rest/client.go index 6541cf50c6..9878ec41f4 100644 --- a/pulsaradmin/pkg/rest/client.go +++ b/pulsaradmin/pkg/rest/client.go @@ -67,15 +67,6 @@ func (c *Client) newRequest(method, path string) (*request, error) { return req, nil } -func (c *Client) newRequestWithURL(method string, urlOpt *url.URL) *request { - req := &request{ - method: method, - url: urlOpt, - params: make(url.Values), - } - return req -} - func (c *Client) doRequest(r *request) (*http.Response, error) { req, err := r.toHTTP() if err != nil { @@ -114,8 +105,11 @@ func (c *Client) MakeRequest(method, endpoint string) (*http.Response, error) { } func (c *Client) MakeRequestWithURL(method string, urlOpt *url.URL) (*http.Response, error) { - req := c.newRequestWithURL(method, urlOpt) - + req := &request{ + method: method, + url: urlOpt, + params: make(url.Values), + } resp, err := checkSuccessful(c.doRequest(req)) if err != nil { return nil, err From 64525d57c988aa0dc3c57783ee427473a12c4c55 Mon Sep 17 00:00:00 2001 From: labuladong Date: Wed, 26 Jun 2024 13:56:46 +0800 Subject: [PATCH 4/4] restore test config --- pulsaradmin/pkg/admin/brokers_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pulsaradmin/pkg/admin/brokers_test.go b/pulsaradmin/pkg/admin/brokers_test.go index 54a0feab85..45c1dce231 100644 --- a/pulsaradmin/pkg/admin/brokers_test.go +++ b/pulsaradmin/pkg/admin/brokers_test.go @@ -124,9 +124,10 @@ func TestUpdateDynamicConfigurationWithCustomURL(t *testing.T) { encoded := url.QueryEscape(value) resp, err := client.MakeRequestWithURL(http.MethodPost, &url.URL{ - Scheme: u.Scheme, - User: u.User, - Host: u.Host, + Scheme: u.Scheme, + User: u.User, + Host: u.Host, + // use this config to test, will restore it later Path: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + value, RawPath: "/admin/v2/brokers/configuration/allowAutoSubscriptionCreation/" + encoded, }) @@ -147,4 +148,8 @@ func TestUpdateDynamicConfigurationWithCustomURL(t *testing.T) { err = json.Unmarshal([]byte(configurations["allowAutoSubscriptionCreation"]), &m) assert.NoError(t, err) assert.Equal(t, "https://example.com/", m["key/123"]) + + // restore the config + err = admin.Brokers().UpdateDynamicConfiguration("allowAutoSubscriptionCreation", "true") + assert.NoError(t, err) }