From 2fef8f0c3b254d48b5d73f9e898f4bd3e8ce999c Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Mon, 8 Jul 2024 15:42:22 +0100 Subject: [PATCH 1/2] fix: set default namespace for apis from config Signed-off-by: Enrique Lacal --- internal/apiserver/server.go | 4 ++++ internal/apiserver/server_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/apiserver/server.go b/internal/apiserver/server.go index fb61b64b81..3fb3378688 100644 --- a/internal/apiserver/server.go +++ b/internal/apiserver/server.go @@ -188,7 +188,11 @@ func (as *apiServer) getBaseURL(req *http.Request) string { vars := mux.Vars(req) if ns, ok := vars["ns"]; ok && ns != "" { baseURL += `/namespaces/` + ns + } else if ns := config.GetString(coreconfig.NamespacesDefault); ns != "" { + // Use the default namespace + baseURL += `/namespaces/` + ns } + return baseURL } diff --git a/internal/apiserver/server_test.go b/internal/apiserver/server_test.go index 8d61e03c07..7855a033ef 100644 --- a/internal/apiserver/server_test.go +++ b/internal/apiserver/server_test.go @@ -549,3 +549,29 @@ func TestGetNamespacedWebSocketHandlerUnknownNamespace(t *testing.T) { assert.Error(t, err) assert.Equal(t, 404, status) } + +func TestContractAPIDefaultNS(t *testing.T) { + mgr, o, as := newTestServer() + r := as.createMuxRouter(context.Background(), mgr) + mcm := &contractmocks.Manager{} + o.On("Contracts").Return(mcm) + mffi := apiservermocks.NewFFISwaggerGen(t) + as.ffiSwaggerGen = mffi + s := httptest.NewServer(r) + defer s.Close() + + o.On("Authorize", mock.Anything, mock.Anything).Return(nil) + + api := &core.ContractAPI{ + Interface: &fftypes.FFIReference{ + ID: fftypes.NewUUID(), + }, + } + + mcm.On("GetContractAPIs", mock.Anything, "http://127.0.0.1:5000/api/v1/namespaces/default", mock.Anything).Return([]*core.ContractAPI{api}, nil, nil) + + res, err := resty.New().R(). + Get(fmt.Sprintf("http://%s/api/v1/apis", s.Listener.Addr())) + assert.NoError(t, err) + assert.Equal(t, 200, res.StatusCode()) +} From 46cf5cde6836af93d10ff4122df65646ce581c53 Mon Sep 17 00:00:00 2001 From: Enrique Lacal Date: Tue, 9 Jul 2024 11:08:04 +0100 Subject: [PATCH 2/2] load default namespace at initialisation Signed-off-by: Enrique Lacal --- internal/apiserver/server.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/apiserver/server.go b/internal/apiserver/server.go index 3fb3378688..ca1671a051 100644 --- a/internal/apiserver/server.go +++ b/internal/apiserver/server.go @@ -61,6 +61,7 @@ type apiServer struct { ffiSwaggerGen FFISwaggerGen apiPublicURL string dynamicPublicURLHeader string + defaultNamespace string } func InitConfig() { @@ -76,6 +77,7 @@ func NewAPIServer() Server { apiTimeout: config.GetDuration(coreconfig.APIRequestTimeout), apiMaxTimeout: config.GetDuration(coreconfig.APIRequestMaxTimeout), dynamicPublicURLHeader: config.GetString(coreconfig.APIDynamicPublicURLHeader), + defaultNamespace: config.GetString(coreconfig.NamespacesDefault), metricsEnabled: config.GetBool(coreconfig.MetricsEnabled), ffiSwaggerGen: &ffiSwaggerGen{}, } @@ -188,9 +190,9 @@ func (as *apiServer) getBaseURL(req *http.Request) string { vars := mux.Vars(req) if ns, ok := vars["ns"]; ok && ns != "" { baseURL += `/namespaces/` + ns - } else if ns := config.GetString(coreconfig.NamespacesDefault); ns != "" { + } else { // Use the default namespace - baseURL += `/namespaces/` + ns + baseURL += `/namespaces/` + as.defaultNamespace } return baseURL