diff --git a/management/server/http/accounts.go b/management/server/http/accounts_handler.go similarity index 77% rename from management/server/http/accounts.go rename to management/server/http/accounts_handler.go index 6d97893a7..45ded32d0 100644 --- a/management/server/http/accounts.go +++ b/management/server/http/accounts_handler.go @@ -2,25 +2,27 @@ package http import ( "encoding/json" + "net/http" + "time" + "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/status" - "net/http" - "time" ) -// Accounts is a handler that handles the server.Account HTTP endpoints -type Accounts struct { +// AccountsHandler is a handler that handles the server.Account HTTP endpoints +type AccountsHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -// NewAccounts creates a new Accounts HTTP handler -func NewAccounts(accountManager server.AccountManager, authCfg AuthCfg) *Accounts { - return &Accounts{ +// NewAccountsHandler creates a new AccountsHandler HTTP handler +func NewAccountsHandler(accountManager server.AccountManager, authCfg AuthCfg) *AccountsHandler { + return &AccountsHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,8 +31,8 @@ func NewAccounts(accountManager server.AccountManager, authCfg AuthCfg) *Account } } -// GetAccountsHandler is HTTP GET handler that returns a list of accounts. Effectively returns just a single account. -func (h *Accounts) GetAccountsHandler(w http.ResponseWriter, r *http.Request) { +// GetAllAccounts is HTTP GET handler that returns a list of accounts. Effectively returns just a single account. +func (h *AccountsHandler) GetAllAccounts(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -47,8 +49,8 @@ func (h *Accounts) GetAccountsHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, []*api.Account{resp}) } -// UpdateAccountHandler is HTTP PUT handler that updates the provided account. Updates only account settings (server.Settings) -func (h *Accounts) UpdateAccountHandler(w http.ResponseWriter, r *http.Request) { +// UpdateAccount is HTTP PUT handler that updates the provided account. Updates only account settings (server.Settings) +func (h *AccountsHandler) UpdateAccount(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) _, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/accounts_test.go b/management/server/http/accounts_handler_test.go similarity index 96% rename from management/server/http/accounts_test.go rename to management/server/http/accounts_handler_test.go index b01af860f..434c0eebe 100644 --- a/management/server/http/accounts_test.go +++ b/management/server/http/accounts_handler_test.go @@ -3,22 +3,24 @@ package http import ( "bytes" "encoding/json" - "github.com/gorilla/mux" - "github.com/netbirdio/netbird/management/server" - "github.com/netbirdio/netbird/management/server/http/api" - "github.com/netbirdio/netbird/management/server/jwtclaims" - "github.com/netbirdio/netbird/management/server/mock_server" - "github.com/netbirdio/netbird/management/server/status" - "github.com/stretchr/testify/assert" "io" "net/http" "net/http/httptest" "testing" "time" + + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" + + "github.com/netbirdio/netbird/management/server" + "github.com/netbirdio/netbird/management/server/http/api" + "github.com/netbirdio/netbird/management/server/jwtclaims" + "github.com/netbirdio/netbird/management/server/mock_server" + "github.com/netbirdio/netbird/management/server/status" ) -func initAccountsTestData(account *server.Account, admin *server.User) *Accounts { - return &Accounts{ +func initAccountsTestData(account *server.Account, admin *server.User) *AccountsHandler { + return &AccountsHandler{ accountManager: &mock_server.MockAccountManager{ GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) { return account, admin, nil @@ -81,7 +83,7 @@ func TestAccounts_AccountsHandler(t *testing.T) { requestBody io.Reader }{ { - name: "GetAccounts OK", + name: "GetAllAccounts OK", expectedBody: true, requestType: http.MethodGet, requestPath: "/api/accounts", @@ -133,8 +135,8 @@ func TestAccounts_AccountsHandler(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/accounts", handler.GetAccountsHandler).Methods("GET") - router.HandleFunc("/api/accounts/{id}", handler.UpdateAccountHandler).Methods("PUT") + router.HandleFunc("/api/accounts", handler.GetAllAccounts).Methods("GET") + router.HandleFunc("/api/accounts/{id}", handler.UpdateAccount).Methods("PUT") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/dns_settings.go b/management/server/http/dns_settings_handler.go similarity index 79% rename from management/server/http/dns_settings.go rename to management/server/http/dns_settings_handler.go index 984faa988..baaf7ba69 100644 --- a/management/server/http/dns_settings.go +++ b/management/server/http/dns_settings_handler.go @@ -4,22 +4,23 @@ import ( "encoding/json" "net/http" + log "github.com/sirupsen/logrus" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" - log "github.com/sirupsen/logrus" ) -// DNSSettings is a handler that returns the DNS settings of the account -type DNSSettings struct { +// DNSSettingsHandler is a handler that returns the DNS settings of the account +type DNSSettingsHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -// NewDNSSettings returns a new instance of DNSSettings handler -func NewDNSSettings(accountManager server.AccountManager, authCfg AuthCfg) *DNSSettings { - return &DNSSettings{ +// NewDNSSettingsHandler returns a new instance of DNSSettingsHandler handler +func NewDNSSettingsHandler(accountManager server.AccountManager, authCfg AuthCfg) *DNSSettingsHandler { + return &DNSSettingsHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,7 +30,7 @@ func NewDNSSettings(accountManager server.AccountManager, authCfg AuthCfg) *DNSS } // GetDNSSettings returns the DNS settings for the account -func (h *DNSSettings) GetDNSSettings(w http.ResponseWriter, r *http.Request) { +func (h *DNSSettingsHandler) GetDNSSettings(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -52,7 +53,7 @@ func (h *DNSSettings) GetDNSSettings(w http.ResponseWriter, r *http.Request) { } // UpdateDNSSettings handles update to DNS settings of an account -func (h *DNSSettings) UpdateDNSSettings(w http.ResponseWriter, r *http.Request) { +func (h *DNSSettingsHandler) UpdateDNSSettings(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/dns_settings_test.go b/management/server/http/dns_settings_handler_test.go similarity index 98% rename from management/server/http/dns_settings_test.go rename to management/server/http/dns_settings_handler_test.go index a82a9aae8..c7d135fd1 100644 --- a/management/server/http/dns_settings_test.go +++ b/management/server/http/dns_settings_handler_test.go @@ -8,11 +8,13 @@ import ( "net/http/httptest" "testing" - "github.com/netbirdio/netbird/management/server/http/api" - "github.com/netbirdio/netbird/management/server/status" "github.com/stretchr/testify/assert" + "github.com/netbirdio/netbird/management/server/http/api" + "github.com/netbirdio/netbird/management/server/status" + "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/mock_server" @@ -37,8 +39,8 @@ var testingDNSSettingsAccount = &server.Account{ DNSSettings: baseExistingDNSSettings, } -func initDNSSettingsTestData() *DNSSettings { - return &DNSSettings{ +func initDNSSettingsTestData() *DNSSettingsHandler { + return &DNSSettingsHandler{ accountManager: &mock_server.MockAccountManager{ GetDNSSettingsFunc: func(accountID string, userID string) (*server.DNSSettings, error) { return testingDNSSettingsAccount.DNSSettings, nil diff --git a/management/server/http/events.go b/management/server/http/events_handler.go similarity index 81% rename from management/server/http/events.go rename to management/server/http/events_handler.go index b39da173c..1d1c176e5 100644 --- a/management/server/http/events.go +++ b/management/server/http/events_handler.go @@ -4,23 +4,24 @@ import ( "fmt" "net/http" + log "github.com/sirupsen/logrus" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/activity" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" - log "github.com/sirupsen/logrus" ) -// Events HTTP handler -type Events struct { +// EventsHandler HTTP handler +type EventsHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -// NewEvents creates a new Events HTTP handler -func NewEvents(accountManager server.AccountManager, authCfg AuthCfg) *Events { - return &Events{ +// NewEventsHandler creates a new EventsHandler HTTP handler +func NewEventsHandler(accountManager server.AccountManager, authCfg AuthCfg) *EventsHandler { + return &EventsHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,8 +30,8 @@ func NewEvents(accountManager server.AccountManager, authCfg AuthCfg) *Events { } } -// GetEvents list of the given account -func (h *Events) GetEvents(w http.ResponseWriter, r *http.Request) { +// GetAllEvents list of the given account +func (h *EventsHandler) GetAllEvents(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/events_test.go b/management/server/http/events_handler_test.go similarity index 97% rename from management/server/http/events_test.go rename to management/server/http/events_handler_test.go index 707b7af45..015c25458 100644 --- a/management/server/http/events_test.go +++ b/management/server/http/events_handler_test.go @@ -10,16 +10,17 @@ import ( "time" "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/activity" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/mock_server" - "github.com/stretchr/testify/assert" ) -func initEventsTestData(account string, user *server.User, events ...*activity.Event) *Events { - return &Events{ +func initEventsTestData(account string, user *server.User, events ...*activity.Event) *EventsHandler { + return &EventsHandler{ accountManager: &mock_server.MockAccountManager{ GetEventsFunc: func(accountID, userID string) ([]*activity.Event, error) { if accountID == account { @@ -184,7 +185,7 @@ func TestEvents_GetEvents(t *testing.T) { requestBody io.Reader }{ { - name: "GetEvents OK", + name: "GetAllEvents OK", expectedBody: true, requestType: http.MethodGet, requestPath: "/api/events/", @@ -202,7 +203,7 @@ func TestEvents_GetEvents(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/events/", handler.GetEvents).Methods("GET") + router.HandleFunc("/api/events/", handler.GetAllEvents).Methods("GET") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/groups.go b/management/server/http/groups_handler.go similarity index 90% rename from management/server/http/groups.go rename to management/server/http/groups_handler.go index 098776160..17f3168a2 100644 --- a/management/server/http/groups.go +++ b/management/server/http/groups_handler.go @@ -8,22 +8,23 @@ import ( "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/status" + "github.com/rs/xid" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/jwtclaims" - "github.com/rs/xid" "github.com/gorilla/mux" log "github.com/sirupsen/logrus" ) -// Groups is a handler that returns groups of the account -type Groups struct { +// GroupsHandler is a handler that returns groups of the account +type GroupsHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -func NewGroups(accountManager server.AccountManager, authCfg AuthCfg) *Groups { - return &Groups{ +func NewGroupsHandler(accountManager server.AccountManager, authCfg AuthCfg) *GroupsHandler { + return &GroupsHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -32,8 +33,8 @@ func NewGroups(accountManager server.AccountManager, authCfg AuthCfg) *Groups { } } -// GetAllGroupsHandler list for the account -func (h *Groups) GetAllGroupsHandler(w http.ResponseWriter, r *http.Request) { +// GetAllGroups list for the account +func (h *GroupsHandler) GetAllGroups(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -50,8 +51,8 @@ func (h *Groups) GetAllGroupsHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, groups) } -// UpdateGroupHandler handles update to a group identified by a given ID -func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) { +// UpdateGroup handles update to a group identified by a given ID +func (h *GroupsHandler) UpdateGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -119,8 +120,8 @@ func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, toGroupResponse(account, &group)) } -// PatchGroupHandler handles patch updates to a group identified by a given ID -func (h *Groups) PatchGroupHandler(w http.ResponseWriter, r *http.Request) { +// PatchGroup handles patch updates to a group identified by a given ID +func (h *GroupsHandler) PatchGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -205,7 +206,7 @@ func (h *Groups) PatchGroupHandler(w http.ResponseWriter, r *http.Request) { }) default: util.WriteError(status.Errorf(status.InvalidArgument, - "invalid operation, \"%v\", for Peers field", patch.Op), w) + "invalid operation, \"%v\", for PeersHandler field", patch.Op), w) return } default: @@ -223,8 +224,8 @@ func (h *Groups) PatchGroupHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, toGroupResponse(account, group)) } -// CreateGroupHandler handles group creation request -func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) { +// CreateGroup handles group creation request +func (h *GroupsHandler) CreateGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -265,8 +266,8 @@ func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, toGroupResponse(account, &group)) } -// DeleteGroupHandler handles group deletion request -func (h *Groups) DeleteGroupHandler(w http.ResponseWriter, r *http.Request) { +// DeleteGroup handles group deletion request +func (h *GroupsHandler) DeleteGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -301,8 +302,8 @@ func (h *Groups) DeleteGroupHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, "") } -// GetGroupHandler returns a group -func (h *Groups) GetGroupHandler(w http.ResponseWriter, r *http.Request) { +// GetGroup returns a group +func (h *GroupsHandler) GetGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/groups_test.go b/management/server/http/groups_handler_test.go similarity index 95% rename from management/server/http/groups_test.go rename to management/server/http/groups_handler_test.go index 71e4be9b6..cefc2ac17 100644 --- a/management/server/http/groups_test.go +++ b/management/server/http/groups_handler_test.go @@ -15,9 +15,11 @@ import ( "github.com/netbirdio/netbird/management/server/status" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/magiconair/properties/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/mock_server" ) @@ -27,8 +29,8 @@ var TestPeers = map[string]*server.Peer{ "B": {Key: "B", ID: "peer-B-ID", IP: net.ParseIP("200.200.200.200")}, } -func initGroupTestData(user *server.User, groups ...*server.Group) *Groups { - return &Groups{ +func initGroupTestData(user *server.User, groups ...*server.Group) *GroupsHandler { + return &GroupsHandler{ accountManager: &mock_server.MockAccountManager{ SaveGroupFunc: func(accountID, userID string, group *server.Group) error { if !strings.HasPrefix(group.ID, "id-") { @@ -134,7 +136,7 @@ func TestGetGroup(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/groups/{id}", p.GetGroupHandler).Methods("GET") + router.HandleFunc("/api/groups/{id}", p.GetGroup).Methods("GET") router.ServeHTTP(recorder, req) res := recorder.Result() @@ -259,7 +261,7 @@ func TestWriteGroup(t *testing.T) { expectedBody: false, }, { - name: "Write Group PATCH Peers OK", + name: "Write Group PATCH PeersHandler OK", requestType: http.MethodPatch, requestPath: "/api/groups/id-existed", requestBody: bytes.NewBuffer( @@ -286,9 +288,9 @@ func TestWriteGroup(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/groups", p.CreateGroupHandler).Methods("POST") - router.HandleFunc("/api/groups/{id}", p.UpdateGroupHandler).Methods("PUT") - router.HandleFunc("/api/groups/{id}", p.PatchGroupHandler).Methods("PATCH") + router.HandleFunc("/api/groups", p.CreateGroup).Methods("POST") + router.HandleFunc("/api/groups/{id}", p.UpdateGroup).Methods("PUT") + router.HandleFunc("/api/groups/{id}", p.PatchGroup).Methods("PATCH") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/handler.go b/management/server/http/handler.go index 069068106..40420655b 100644 --- a/management/server/http/handler.go +++ b/management/server/http/handler.go @@ -4,10 +4,11 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/rs/cors" + s "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/middleware" "github.com/netbirdio/netbird/management/server/telemetry" - "github.com/rs/cors" ) // AuthCfg contains parameters for authentication middleware @@ -41,61 +42,61 @@ func APIHandler(accountManager s.AccountManager, appMetrics telemetry.AppMetrics apiHandler := rootRouter.PathPrefix("/api").Subrouter() apiHandler.Use(metricsMiddleware.Handler, corsMiddleware.Handler, jwtMiddleware.Handler, acMiddleware.Handler) - groupsHandler := NewGroups(accountManager, authCfg) - rulesHandler := NewRules(accountManager, authCfg) - peersHandler := NewPeers(accountManager, authCfg) + groupsHandler := NewGroupsHandler(accountManager, authCfg) + rulesHandler := NewRulesHandler(accountManager, authCfg) + peersHandler := NewPeersHandler(accountManager, authCfg) keysHandler := NewSetupKeysHandler(accountManager, authCfg) userHandler := NewUserHandler(accountManager, authCfg) - routesHandler := NewRoutes(accountManager, authCfg) - nameserversHandler := NewNameservers(accountManager, authCfg) - eventsHandler := NewEvents(accountManager, authCfg) - dnsSettingsHandler := NewDNSSettings(accountManager, authCfg) - accountsHandler := NewAccounts(accountManager, authCfg) + routesHandler := NewRoutesHandler(accountManager, authCfg) + nameserversHandler := NewNameserversHandler(accountManager, authCfg) + eventsHandler := NewEventsHandler(accountManager, authCfg) + dnsSettingsHandler := NewDNSSettingsHandler(accountManager, authCfg) + accountsHandler := NewAccountsHandler(accountManager, authCfg) - apiHandler.HandleFunc("/accounts/{id}", accountsHandler.UpdateAccountHandler).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/accounts", accountsHandler.GetAccountsHandler).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/accounts/{id}", accountsHandler.UpdateAccount).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/accounts", accountsHandler.GetAllAccounts).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/peers", peersHandler.GetPeers).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/peers", peersHandler.GetAllPeers).Methods("GET", "OPTIONS") apiHandler.HandleFunc("/peers/{id}", peersHandler.HandlePeer). Methods("GET", "PUT", "DELETE", "OPTIONS") - apiHandler.HandleFunc("/users", userHandler.GetUsers).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/users", userHandler.GetAllUsers).Methods("GET", "OPTIONS") apiHandler.HandleFunc("/users/{id}", userHandler.UpdateUser).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/users", userHandler.CreateUserHandler).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/users", userHandler.CreateUser).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/setup-keys", keysHandler.GetAllSetupKeysHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/setup-keys", keysHandler.CreateSetupKeyHandler).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/setup-keys/{id}", keysHandler.GetSetupKeyHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/setup-keys/{id}", keysHandler.UpdateSetupKeyHandler).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/setup-keys", keysHandler.GetAllSetupKeys).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/setup-keys", keysHandler.CreateSetupKey).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/setup-keys/{id}", keysHandler.GetSetupKey).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/setup-keys/{id}", keysHandler.UpdateSetupKey).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/rules", rulesHandler.GetAllRulesHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/rules", rulesHandler.CreateRuleHandler).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/rules/{id}", rulesHandler.UpdateRuleHandler).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/rules/{id}", rulesHandler.PatchRuleHandler).Methods("PATCH", "OPTIONS") - apiHandler.HandleFunc("/rules/{id}", rulesHandler.GetRuleHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/rules/{id}", rulesHandler.DeleteRuleHandler).Methods("DELETE", "OPTIONS") + apiHandler.HandleFunc("/rules", rulesHandler.GetAllRules).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/rules", rulesHandler.CreateRule).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/rules/{id}", rulesHandler.UpdateRule).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/rules/{id}", rulesHandler.PatchRule).Methods("PATCH", "OPTIONS") + apiHandler.HandleFunc("/rules/{id}", rulesHandler.GetRule).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/rules/{id}", rulesHandler.DeleteRule).Methods("DELETE", "OPTIONS") - apiHandler.HandleFunc("/groups", groupsHandler.GetAllGroupsHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/groups", groupsHandler.CreateGroupHandler).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/groups/{id}", groupsHandler.UpdateGroupHandler).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/groups/{id}", groupsHandler.PatchGroupHandler).Methods("PATCH", "OPTIONS") - apiHandler.HandleFunc("/groups/{id}", groupsHandler.GetGroupHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/groups/{id}", groupsHandler.DeleteGroupHandler).Methods("DELETE", "OPTIONS") + apiHandler.HandleFunc("/groups", groupsHandler.GetAllGroups).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/groups", groupsHandler.CreateGroup).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/groups/{id}", groupsHandler.UpdateGroup).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/groups/{id}", groupsHandler.PatchGroup).Methods("PATCH", "OPTIONS") + apiHandler.HandleFunc("/groups/{id}", groupsHandler.GetGroup).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/groups/{id}", groupsHandler.DeleteGroup).Methods("DELETE", "OPTIONS") - apiHandler.HandleFunc("/routes", routesHandler.GetAllRoutesHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/routes", routesHandler.CreateRouteHandler).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/routes/{id}", routesHandler.UpdateRouteHandler).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/routes/{id}", routesHandler.PatchRouteHandler).Methods("PATCH", "OPTIONS") - apiHandler.HandleFunc("/routes/{id}", routesHandler.GetRouteHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/routes/{id}", routesHandler.DeleteRouteHandler).Methods("DELETE", "OPTIONS") + apiHandler.HandleFunc("/routes", routesHandler.GetAllRoutes).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/routes", routesHandler.CreateRoute).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/routes/{id}", routesHandler.UpdateRoute).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/routes/{id}", routesHandler.PatchRoute).Methods("PATCH", "OPTIONS") + apiHandler.HandleFunc("/routes/{id}", routesHandler.GetRoute).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/routes/{id}", routesHandler.DeleteRoute).Methods("DELETE", "OPTIONS") - apiHandler.HandleFunc("/dns/nameservers", nameserversHandler.GetAllNameserversHandler).Methods("GET", "OPTIONS") - apiHandler.HandleFunc("/dns/nameservers", nameserversHandler.CreateNameserverGroupHandler).Methods("POST", "OPTIONS") - apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.UpdateNameserverGroupHandler).Methods("PUT", "OPTIONS") - apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.PatchNameserverGroupHandler).Methods("PATCH", "OPTIONS") + apiHandler.HandleFunc("/dns/nameservers", nameserversHandler.GetAllNameservers).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/dns/nameservers", nameserversHandler.CreateNameserverGroup).Methods("POST", "OPTIONS") + apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.UpdateNameserverGroup).Methods("PUT", "OPTIONS") + apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.PatchNameserverGroup).Methods("PATCH", "OPTIONS") apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.GetNameserverGroupHandler).Methods("GET", "OPTIONS") apiHandler.HandleFunc("/dns/nameservers/{id}", nameserversHandler.DeleteNameserverGroupHandler).Methods("DELETE", "OPTIONS") - apiHandler.HandleFunc("/events", eventsHandler.GetEvents).Methods("GET", "OPTIONS") + apiHandler.HandleFunc("/events", eventsHandler.GetAllEvents).Methods("GET", "OPTIONS") apiHandler.HandleFunc("/dns/settings", dnsSettingsHandler.GetDNSSettings).Methods("GET", "OPTIONS") apiHandler.HandleFunc("/dns/settings", dnsSettingsHandler.UpdateDNSSettings).Methods("PUT", "OPTIONS") diff --git a/management/server/http/nameservers.go b/management/server/http/nameservers_handler.go similarity index 88% rename from management/server/http/nameservers.go rename to management/server/http/nameservers_handler.go index 3e0df5201..81c2dca7b 100644 --- a/management/server/http/nameservers.go +++ b/management/server/http/nameservers_handler.go @@ -6,24 +6,25 @@ import ( "net/http" "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" + nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/status" - log "github.com/sirupsen/logrus" ) -// Nameservers is the nameserver group handler of the account -type Nameservers struct { +// NameserversHandler is the nameserver group handler of the account +type NameserversHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -// NewNameservers returns a new instance of Nameservers handler -func NewNameservers(accountManager server.AccountManager, authCfg AuthCfg) *Nameservers { - return &Nameservers{ +// NewNameserversHandler returns a new instance of NameserversHandler handler +func NewNameserversHandler(accountManager server.AccountManager, authCfg AuthCfg) *NameserversHandler { + return &NameserversHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -32,8 +33,8 @@ func NewNameservers(accountManager server.AccountManager, authCfg AuthCfg) *Name } } -// GetAllNameserversHandler returns the list of nameserver groups for the account -func (h *Nameservers) GetAllNameserversHandler(w http.ResponseWriter, r *http.Request) { +// GetAllNameservers returns the list of nameserver groups for the account +func (h *NameserversHandler) GetAllNameservers(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -56,8 +57,8 @@ func (h *Nameservers) GetAllNameserversHandler(w http.ResponseWriter, r *http.Re util.WriteJSONObject(w, apiNameservers) } -// CreateNameserverGroupHandler handles nameserver group creation request -func (h *Nameservers) CreateNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { +// CreateNameserverGroup handles nameserver group creation request +func (h *NameserversHandler) CreateNameserverGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -89,8 +90,8 @@ func (h *Nameservers) CreateNameserverGroupHandler(w http.ResponseWriter, r *htt util.WriteJSONObject(w, &resp) } -// UpdateNameserverGroupHandler handles update to a nameserver group identified by a given ID -func (h *Nameservers) UpdateNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { +// UpdateNameserverGroup handles update to a nameserver group identified by a given ID +func (h *NameserversHandler) UpdateNameserverGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -139,8 +140,8 @@ func (h *Nameservers) UpdateNameserverGroupHandler(w http.ResponseWriter, r *htt util.WriteJSONObject(w, &resp) } -// PatchNameserverGroupHandler handles patch updates to a nameserver group identified by a given ID -func (h *Nameservers) PatchNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { +// PatchNameserverGroup handles patch updates to a nameserver group identified by a given ID +func (h *NameserversHandler) PatchNameserverGroup(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -222,7 +223,7 @@ func (h *Nameservers) PatchNameserverGroupHandler(w http.ResponseWriter, r *http } // DeleteNameserverGroupHandler handles nameserver group deletion request -func (h *Nameservers) DeleteNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { +func (h *NameserversHandler) DeleteNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -246,7 +247,7 @@ func (h *Nameservers) DeleteNameserverGroupHandler(w http.ResponseWriter, r *htt } // GetNameserverGroupHandler handles a nameserver group Get request identified by ID -func (h *Nameservers) GetNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { +func (h *NameserversHandler) GetNameserverGroupHandler(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/nameservers_test.go b/management/server/http/nameservers_handler_test.go similarity index 98% rename from management/server/http/nameservers_test.go rename to management/server/http/nameservers_handler_test.go index 78d8430ac..3d1351d81 100644 --- a/management/server/http/nameservers_test.go +++ b/management/server/http/nameservers_handler_test.go @@ -9,12 +9,14 @@ import ( "net/netip" "testing" + "github.com/stretchr/testify/assert" + nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/status" - "github.com/stretchr/testify/assert" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/mock_server" @@ -55,8 +57,8 @@ var baseExistingNSGroup = &nbdns.NameServerGroup{ Enabled: true, } -func initNameserversTestData() *Nameservers { - return &Nameservers{ +func initNameserversTestData() *NameserversHandler { + return &NameserversHandler{ accountManager: &mock_server.MockAccountManager{ GetNameServerGroupFunc: func(accountID, nsGroupID string) (*nbdns.NameServerGroup, error) { if nsGroupID == existingNSGroupID { @@ -261,10 +263,10 @@ func TestNameserversHandlers(t *testing.T) { router := mux.NewRouter() router.HandleFunc("/api/dns/nameservers/{id}", p.GetNameserverGroupHandler).Methods("GET") - router.HandleFunc("/api/dns/nameservers", p.CreateNameserverGroupHandler).Methods("POST") + router.HandleFunc("/api/dns/nameservers", p.CreateNameserverGroup).Methods("POST") router.HandleFunc("/api/dns/nameservers/{id}", p.DeleteNameserverGroupHandler).Methods("DELETE") - router.HandleFunc("/api/dns/nameservers/{id}", p.UpdateNameserverGroupHandler).Methods("PUT") - router.HandleFunc("/api/dns/nameservers/{id}", p.PatchNameserverGroupHandler).Methods("PATCH") + router.HandleFunc("/api/dns/nameservers/{id}", p.UpdateNameserverGroup).Methods("PUT") + router.HandleFunc("/api/dns/nameservers/{id}", p.PatchNameserverGroup).Methods("PATCH") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/peers.go b/management/server/http/peers_handler.go similarity index 86% rename from management/server/http/peers.go rename to management/server/http/peers_handler.go index 64752a680..34c52c36e 100644 --- a/management/server/http/peers.go +++ b/management/server/http/peers_handler.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" @@ -13,14 +14,14 @@ import ( "github.com/netbirdio/netbird/management/server/status" ) -// Peers is a handler that returns peers of the account -type Peers struct { +// PeersHandler is a handler that returns peers of the account +type PeersHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -func NewPeers(accountManager server.AccountManager, authCfg AuthCfg) *Peers { - return &Peers{ +func NewPeersHandler(accountManager server.AccountManager, authCfg AuthCfg) *PeersHandler { + return &PeersHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,7 +30,7 @@ func NewPeers(accountManager server.AccountManager, authCfg AuthCfg) *Peers { } } -func (h *Peers) getPeer(account *server.Account, peerID, userID string, w http.ResponseWriter) { +func (h *PeersHandler) getPeer(account *server.Account, peerID, userID string, w http.ResponseWriter) { peer, err := h.accountManager.GetPeer(account.Id, peerID, userID) if err != nil { util.WriteError(err, w) @@ -39,7 +40,7 @@ func (h *Peers) getPeer(account *server.Account, peerID, userID string, w http.R util.WriteJSONObject(w, toPeerResponse(peer, account, h.accountManager.GetDNSDomain())) } -func (h *Peers) updatePeer(account *server.Account, user *server.User, peerID string, w http.ResponseWriter, r *http.Request) { +func (h *PeersHandler) updatePeer(account *server.Account, user *server.User, peerID string, w http.ResponseWriter, r *http.Request) { req := &api.PutApiPeersIdJSONBody{} err := json.NewDecoder(r.Body).Decode(&req) if err != nil { @@ -58,7 +59,7 @@ func (h *Peers) updatePeer(account *server.Account, user *server.User, peerID st util.WriteJSONObject(w, toPeerResponse(peer, account, dnsDomain)) } -func (h *Peers) deletePeer(accountID, userID string, peerID string, w http.ResponseWriter) { +func (h *PeersHandler) deletePeer(accountID, userID string, peerID string, w http.ResponseWriter) { _, err := h.accountManager.DeletePeer(accountID, peerID, userID) if err != nil { util.WriteError(err, w) @@ -67,7 +68,7 @@ func (h *Peers) deletePeer(accountID, userID string, peerID string, w http.Respo util.WriteJSONObject(w, "") } -func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) { +func (h *PeersHandler) HandlePeer(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -96,7 +97,7 @@ func (h *Peers) HandlePeer(w http.ResponseWriter, r *http.Request) { } } -func (h *Peers) GetPeers(w http.ResponseWriter, r *http.Request) { +func (h *PeersHandler) GetAllPeers(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: claims := h.claimsExtractor.FromRequestContext(r) diff --git a/management/server/http/peers_test.go b/management/server/http/peers_handler_test.go similarity index 95% rename from management/server/http/peers_test.go rename to management/server/http/peers_handler_test.go index d6810ffe2..c7feb3ab9 100644 --- a/management/server/http/peers_test.go +++ b/management/server/http/peers_handler_test.go @@ -3,7 +3,6 @@ package http import ( "bytes" "encoding/json" - "github.com/gorilla/mux" "io" "net" "net/http" @@ -11,19 +10,22 @@ import ( "testing" "time" + "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/magiconair/properties/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/mock_server" ) const testPeerID = "test_peer" -func initTestMetaData(peers ...*server.Peer) *Peers { - return &Peers{ +func initTestMetaData(peers ...*server.Peer) *PeersHandler { + return &PeersHandler{ accountManager: &mock_server.MockAccountManager{ UpdatePeerFunc: func(accountID, userID string, update *server.Peer) (*server.Peer, error) { p := peers[0].Copy() @@ -68,7 +70,7 @@ func initTestMetaData(peers ...*server.Peer) *Peers { } } -// Tests the GetPeers endpoint reachable in the route /api/peers +// Tests the GetAllPeers endpoint reachable in the route /api/peers // Use the metadata generated by initTestMetaData() to check for values func TestGetPeers(t *testing.T) { @@ -143,7 +145,7 @@ func TestGetPeers(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/peers/", p.GetPeers).Methods("GET") + router.HandleFunc("/api/peers/", p.GetAllPeers).Methods("GET") router.HandleFunc("/api/peers/{id}", p.HandlePeer).Methods("GET") router.HandleFunc("/api/peers/{id}", p.HandlePeer).Methods("PUT") router.ServeHTTP(recorder, req) diff --git a/management/server/http/routes.go b/management/server/http/routes_handler.go similarity index 90% rename from management/server/http/routes.go rename to management/server/http/routes_handler.go index 36104415b..09b842eba 100644 --- a/management/server/http/routes.go +++ b/management/server/http/routes_handler.go @@ -6,6 +6,7 @@ import ( "unicode/utf8" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" @@ -14,15 +15,15 @@ import ( "github.com/netbirdio/netbird/route" ) -// Routes is the routes handler of the account -type Routes struct { +// RoutesHanlder is the routes handler of the account +type RoutesHanlder struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -// NewRoutes returns a new instance of Routes handler -func NewRoutes(accountManager server.AccountManager, authCfg AuthCfg) *Routes { - return &Routes{ +// NewRoutesHandler returns a new instance of RoutesHanlder handler +func NewRoutesHandler(accountManager server.AccountManager, authCfg AuthCfg) *RoutesHanlder { + return &RoutesHanlder{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -31,8 +32,8 @@ func NewRoutes(accountManager server.AccountManager, authCfg AuthCfg) *Routes { } } -// GetAllRoutesHandler returns the list of routes for the account -func (h *Routes) GetAllRoutesHandler(w http.ResponseWriter, r *http.Request) { +// GetAllRoutes returns the list of routes for the account +func (h *RoutesHanlder) GetAllRoutes(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -53,8 +54,8 @@ func (h *Routes) GetAllRoutesHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, apiRoutes) } -// CreateRouteHandler handles route creation request -func (h *Routes) CreateRouteHandler(w http.ResponseWriter, r *http.Request) { +// CreateRoute handles route creation request +func (h *RoutesHanlder) CreateRoute(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -92,8 +93,8 @@ func (h *Routes) CreateRouteHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// UpdateRouteHandler handles update to a route identified by a given ID -func (h *Routes) UpdateRouteHandler(w http.ResponseWriter, r *http.Request) { +// UpdateRoute handles update to a route identified by a given ID +func (h *RoutesHanlder) UpdateRoute(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -158,8 +159,8 @@ func (h *Routes) UpdateRouteHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// PatchRouteHandler handles patch updates to a route identified by a given ID -func (h *Routes) PatchRouteHandler(w http.ResponseWriter, r *http.Request) { +// PatchRoute handles patch updates to a route identified by a given ID +func (h *RoutesHanlder) PatchRoute(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -299,8 +300,8 @@ func (h *Routes) PatchRouteHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// DeleteRouteHandler handles route deletion request -func (h *Routes) DeleteRouteHandler(w http.ResponseWriter, r *http.Request) { +// DeleteRoute handles route deletion request +func (h *RoutesHanlder) DeleteRoute(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -323,8 +324,8 @@ func (h *Routes) DeleteRouteHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, "") } -// GetRouteHandler handles a route Get request identified by ID -func (h *Routes) GetRouteHandler(w http.ResponseWriter, r *http.Request) { +// GetRoute handles a route Get request identified by ID +func (h *RoutesHanlder) GetRoute(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/routes_test.go b/management/server/http/routes_handler_test.go similarity index 96% rename from management/server/http/routes_test.go rename to management/server/http/routes_handler_test.go index f7994c3f6..58c2ff9c7 100644 --- a/management/server/http/routes_test.go +++ b/management/server/http/routes_handler_test.go @@ -17,6 +17,7 @@ import ( "github.com/gorilla/mux" "github.com/magiconair/properties/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/mock_server" @@ -60,8 +61,8 @@ var testingAccount = &server.Account{ }, } -func initRoutesTestData() *Routes { - return &Routes{ +func initRoutesTestData() *RoutesHanlder { + return &RoutesHanlder{ accountManager: &mock_server.MockAccountManager{ GetRouteFunc: func(_, routeID, _ string) (*route.Route, error) { if routeID == existingRouteID { @@ -352,11 +353,11 @@ func TestRoutesHandlers(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/routes/{id}", p.GetRouteHandler).Methods("GET") - router.HandleFunc("/api/routes/{id}", p.DeleteRouteHandler).Methods("DELETE") - router.HandleFunc("/api/routes", p.CreateRouteHandler).Methods("POST") - router.HandleFunc("/api/routes/{id}", p.UpdateRouteHandler).Methods("PUT") - router.HandleFunc("/api/routes/{id}", p.PatchRouteHandler).Methods("PATCH") + router.HandleFunc("/api/routes/{id}", p.GetRoute).Methods("GET") + router.HandleFunc("/api/routes/{id}", p.DeleteRoute).Methods("DELETE") + router.HandleFunc("/api/routes", p.CreateRoute).Methods("POST") + router.HandleFunc("/api/routes/{id}", p.UpdateRoute).Methods("PUT") + router.HandleFunc("/api/routes/{id}", p.PatchRoute).Methods("PATCH") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/rules.go b/management/server/http/rules_handler.go similarity index 92% rename from management/server/http/rules.go rename to management/server/http/rules_handler.go index dd281970b..a2a35d5cc 100644 --- a/management/server/http/rules.go +++ b/management/server/http/rules_handler.go @@ -5,22 +5,23 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/rs/xid" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/status" - "github.com/rs/xid" ) -// Rules is a handler that returns rules of the account -type Rules struct { +// RulesHandler is a handler that returns rules of the account +type RulesHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -func NewRules(accountManager server.AccountManager, authCfg AuthCfg) *Rules { - return &Rules{ +func NewRulesHandler(accountManager server.AccountManager, authCfg AuthCfg) *RulesHandler { + return &RulesHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,8 +30,8 @@ func NewRules(accountManager server.AccountManager, authCfg AuthCfg) *Rules { } } -// GetAllRulesHandler list for the account -func (h *Rules) GetAllRulesHandler(w http.ResponseWriter, r *http.Request) { +// GetAllRules list for the account +func (h *RulesHandler) GetAllRules(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -51,8 +52,8 @@ func (h *Rules) GetAllRulesHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, rules) } -// UpdateRuleHandler handles update to a rule identified by a given ID -func (h *Rules) UpdateRuleHandler(w http.ResponseWriter, r *http.Request) { +// UpdateRule handles update to a rule identified by a given ID +func (h *RulesHandler) UpdateRule(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -122,8 +123,8 @@ func (h *Rules) UpdateRuleHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// PatchRuleHandler handles patch updates to a rule identified by a given ID -func (h *Rules) PatchRuleHandler(w http.ResponseWriter, r *http.Request) { +// PatchRule handles patch updates to a rule identified by a given ID +func (h *RulesHandler) PatchRule(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, _, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -265,8 +266,8 @@ func (h *Rules) PatchRuleHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// CreateRuleHandler handles rule creation request -func (h *Rules) CreateRuleHandler(w http.ResponseWriter, r *http.Request) { +// CreateRule handles rule creation request +func (h *RulesHandler) CreateRule(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -324,8 +325,8 @@ func (h *Rules) CreateRuleHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, &resp) } -// DeleteRuleHandler handles rule deletion request -func (h *Rules) DeleteRuleHandler(w http.ResponseWriter, r *http.Request) { +// DeleteRule handles rule deletion request +func (h *RulesHandler) DeleteRule(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -349,8 +350,8 @@ func (h *Rules) DeleteRuleHandler(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, "") } -// GetRuleHandler handles a group Get request identified by ID -func (h *Rules) GetRuleHandler(w http.ResponseWriter, r *http.Request) { +// GetRule handles a group Get request identified by ID +func (h *RulesHandler) GetRule(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/rules_test.go b/management/server/http/rules_handler_test.go similarity index 95% rename from management/server/http/rules_test.go rename to management/server/http/rules_handler_test.go index b30e7576b..ed74c9700 100644 --- a/management/server/http/rules_test.go +++ b/management/server/http/rules_handler_test.go @@ -13,15 +13,17 @@ import ( "github.com/netbirdio/netbird/management/server/http/api" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/magiconair/properties/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/mock_server" ) -func initRulesTestData(rules ...*server.Rule) *Rules { - return &Rules{ +func initRulesTestData(rules ...*server.Rule) *RulesHandler { + return &RulesHandler{ accountManager: &mock_server.MockAccountManager{ SaveRuleFunc: func(_, _ string, rule *server.Rule) error { if !strings.HasPrefix(rule.ID, "id-") { @@ -132,7 +134,7 @@ func TestRulesGetRule(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/rules/{id}", p.GetRuleHandler).Methods("GET") + router.HandleFunc("/api/rules/{id}", p.GetRule).Methods("GET") router.ServeHTTP(recorder, req) res := recorder.Result() @@ -278,9 +280,9 @@ func TestRulesWriteRule(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/rules", p.CreateRuleHandler).Methods("POST") - router.HandleFunc("/api/rules/{id}", p.UpdateRuleHandler).Methods("PUT") - router.HandleFunc("/api/rules/{id}", p.PatchRuleHandler).Methods("PATCH") + router.HandleFunc("/api/rules", p.CreateRule).Methods("POST") + router.HandleFunc("/api/rules/{id}", p.UpdateRule).Methods("PUT") + router.HandleFunc("/api/rules/{id}", p.PatchRule).Methods("PATCH") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/setupkeys.go b/management/server/http/setupkeys_handler.go similarity index 87% rename from management/server/http/setupkeys.go rename to management/server/http/setupkeys_handler.go index aec7160b0..3366f7a94 100644 --- a/management/server/http/setupkeys.go +++ b/management/server/http/setupkeys_handler.go @@ -6,6 +6,7 @@ import ( "time" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" @@ -13,14 +14,14 @@ import ( "github.com/netbirdio/netbird/management/server/status" ) -// SetupKeys is a handler that returns a list of setup keys of the account -type SetupKeys struct { +// SetupKeysHandler is a handler that returns a list of setup keys of the account +type SetupKeysHandler struct { accountManager server.AccountManager claimsExtractor *jwtclaims.ClaimsExtractor } -func NewSetupKeysHandler(accountManager server.AccountManager, authCfg AuthCfg) *SetupKeys { - return &SetupKeys{ +func NewSetupKeysHandler(accountManager server.AccountManager, authCfg AuthCfg) *SetupKeysHandler { + return &SetupKeysHandler{ accountManager: accountManager, claimsExtractor: jwtclaims.NewClaimsExtractor( jwtclaims.WithAudience(authCfg.Audience), @@ -29,8 +30,8 @@ func NewSetupKeysHandler(accountManager server.AccountManager, authCfg AuthCfg) } } -// CreateSetupKeyHandler is a POST requests that creates a new SetupKey -func (h *SetupKeys) CreateSetupKeyHandler(w http.ResponseWriter, r *http.Request) { +// CreateSetupKey is a POST requests that creates a new SetupKey +func (h *SetupKeysHandler) CreateSetupKey(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -72,8 +73,8 @@ func (h *SetupKeys) CreateSetupKeyHandler(w http.ResponseWriter, r *http.Request writeSuccess(w, setupKey) } -// GetSetupKeyHandler is a GET request to get a SetupKey by ID -func (h *SetupKeys) GetSetupKeyHandler(w http.ResponseWriter, r *http.Request) { +// GetSetupKey is a GET request to get a SetupKey by ID +func (h *SetupKeysHandler) GetSetupKey(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -97,8 +98,8 @@ func (h *SetupKeys) GetSetupKeyHandler(w http.ResponseWriter, r *http.Request) { writeSuccess(w, key) } -// UpdateSetupKeyHandler is a PUT request to update server.SetupKey -func (h *SetupKeys) UpdateSetupKeyHandler(w http.ResponseWriter, r *http.Request) { +// UpdateSetupKey is a PUT request to update server.SetupKey +func (h *SetupKeysHandler) UpdateSetupKey(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { @@ -144,8 +145,8 @@ func (h *SetupKeys) UpdateSetupKeyHandler(w http.ResponseWriter, r *http.Request writeSuccess(w, newKey) } -// GetAllSetupKeysHandler is a GET request that returns a list of SetupKey -func (h *SetupKeys) GetAllSetupKeysHandler(w http.ResponseWriter, r *http.Request) { +// GetAllSetupKeys is a GET request that returns a list of SetupKey +func (h *SetupKeysHandler) GetAllSetupKeys(w http.ResponseWriter, r *http.Request) { claims := h.claimsExtractor.FromRequestContext(r) account, user, err := h.accountManager.GetAccountFromToken(claims) if err != nil { diff --git a/management/server/http/setupkeys_test.go b/management/server/http/setupkeys_handler_test.go similarity index 95% rename from management/server/http/setupkeys_test.go rename to management/server/http/setupkeys_handler_test.go index 861e102ed..166d6e81a 100644 --- a/management/server/http/setupkeys_test.go +++ b/management/server/http/setupkeys_handler_test.go @@ -11,9 +11,10 @@ import ( "time" "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" + "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/status" - "github.com/stretchr/testify/assert" "github.com/netbirdio/netbird/management/server/jwtclaims" @@ -30,8 +31,8 @@ const ( func initSetupKeysTestMetaData(defaultKey *server.SetupKey, newKey *server.SetupKey, updatedSetupKey *server.SetupKey, user *server.User, -) *SetupKeys { - return &SetupKeys{ +) *SetupKeysHandler { + return &SetupKeysHandler{ accountManager: &mock_server.MockAccountManager{ GetAccountFromTokenFunc: func(claims jwtclaims.AuthorizationClaims) (*server.Account, *server.User, error) { return &server.Account{ @@ -171,10 +172,10 @@ func TestSetupKeysHandlers(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, tc.requestBody) router := mux.NewRouter() - router.HandleFunc("/api/setup-keys", handler.GetAllSetupKeysHandler).Methods("GET", "OPTIONS") - router.HandleFunc("/api/setup-keys", handler.CreateSetupKeyHandler).Methods("POST", "OPTIONS") - router.HandleFunc("/api/setup-keys/{id}", handler.GetSetupKeyHandler).Methods("GET", "OPTIONS") - router.HandleFunc("/api/setup-keys/{id}", handler.UpdateSetupKeyHandler).Methods("PUT", "OPTIONS") + router.HandleFunc("/api/setup-keys", handler.GetAllSetupKeys).Methods("GET", "OPTIONS") + router.HandleFunc("/api/setup-keys", handler.CreateSetupKey).Methods("POST", "OPTIONS") + router.HandleFunc("/api/setup-keys/{id}", handler.GetSetupKey).Methods("GET", "OPTIONS") + router.HandleFunc("/api/setup-keys/{id}", handler.UpdateSetupKey).Methods("PUT", "OPTIONS") router.ServeHTTP(recorder, req) res := recorder.Result() diff --git a/management/server/http/users.go b/management/server/http/users_handler.go similarity index 92% rename from management/server/http/users.go rename to management/server/http/users_handler.go index 7cc57e3fd..bfb3c4220 100644 --- a/management/server/http/users.go +++ b/management/server/http/users_handler.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/util" "github.com/netbirdio/netbird/management/server/status" @@ -74,8 +75,8 @@ func (h *UserHandler) UpdateUser(w http.ResponseWriter, r *http.Request) { util.WriteJSONObject(w, toUserResponse(newUser, claims.UserId)) } -// CreateUserHandler creates a User in the system with a status "invited" (effectively this is a user invite). -func (h *UserHandler) CreateUserHandler(w http.ResponseWriter, r *http.Request) { +// CreateUser creates a User in the system with a status "invited" (effectively this is a user invite). +func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) return @@ -113,9 +114,9 @@ func (h *UserHandler) CreateUserHandler(w http.ResponseWriter, r *http.Request) util.WriteJSONObject(w, toUserResponse(newUser, claims.UserId)) } -// GetUsers returns a list of users of the account this user belongs to. +// GetAllUsers returns a list of users of the account this user belongs to. // It also gathers additional user data (like email and name) from the IDP manager. -func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) { +func (h *UserHandler) GetAllUsers(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w) return diff --git a/management/server/http/users_test.go b/management/server/http/users_handler_test.go similarity index 98% rename from management/server/http/users_test.go rename to management/server/http/users_handler_test.go index 44494b699..1675f39f1 100644 --- a/management/server/http/users_test.go +++ b/management/server/http/users_handler_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/magiconair/properties/assert" + "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/management/server/mock_server" @@ -72,7 +73,7 @@ func TestGetUsers(t *testing.T) { req := httptest.NewRequest(tc.requestType, tc.requestPath, nil) rr := httptest.NewRecorder() - userHandler.GetUsers(rr, req) + userHandler.GetAllUsers(rr, req) res := rr.Result() defer res.Body.Close() diff --git a/management/server/mock_server/account_mock.go b/management/server/mock_server/account_mock.go index 90b6f1d07..1776c62aa 100644 --- a/management/server/mock_server/account_mock.go +++ b/management/server/mock_server/account_mock.go @@ -1,14 +1,16 @@ package mock_server import ( + "time" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + nbdns "github.com/netbirdio/netbird/dns" "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/activity" "github.com/netbirdio/netbird/management/server/jwtclaims" "github.com/netbirdio/netbird/route" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - "time" ) type MockAccountManager struct { @@ -496,7 +498,7 @@ func (am *MockAccountManager) GetPeers(accountID, userID string) ([]*server.Peer if am.GetAccountFromTokenFunc != nil { return am.GetPeersFunc(accountID, userID) } - return nil, status.Errorf(codes.Unimplemented, "method GetPeers is not implemented") + return nil, status.Errorf(codes.Unimplemented, "method GetAllPeers is not implemented") } // GetDNSDomain mocks GetDNSDomain of the AccountManager interface @@ -512,7 +514,7 @@ func (am *MockAccountManager) GetEvents(accountID, userID string) ([]*activity.E if am.GetEventsFunc != nil { return am.GetEventsFunc(accountID, userID) } - return nil, status.Errorf(codes.Unimplemented, "method GetEvents is not implemented") + return nil, status.Errorf(codes.Unimplemented, "method GetAllEvents is not implemented") } // GetDNSSettings mocks GetDNSSettings of the AccountManager interface