From fe57f5e4f9950fabc1315e8dd92ce0a2b7feb602 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sat, 25 Jul 2020 13:20:34 +0200 Subject: [PATCH] admin: add routes listing all sources and domains --- admin/db-domain.go | 20 ++++++++++++++++++++ admin/db-source.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/admin/db-domain.go b/admin/db-domain.go index d977bb0..b3a221d 100644 --- a/admin/db-domain.go +++ b/admin/db-domain.go @@ -54,6 +54,7 @@ func init() { router.PUT("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(updateUserDomain)))) router.DELETE("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(deleteUserDomain))) + router.GET("/api/domains", api.ApiHandler(getAllDomains)) router.DELETE("/api/domains", api.ApiHandler(clearDomains)) } @@ -98,6 +99,25 @@ func getUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Para return api.NewAPIResponse(domain, nil) } +func getAllDomains(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response { + var domains happydns.Domains + + users, err := storage.MainStore.GetUsers() + if err != nil { + return api.NewAPIResponse(nil, fmt.Errorf("Unable to retrieve users list: %w", err)) + } + for _, user := range users { + usersDomains, err := storage.MainStore.GetDomains(user) + if err != nil { + return api.NewAPIResponse(nil, fmt.Errorf("Unable to retrieve %s's domains: %w", user.Email, err)) + } + + domains = append(domains, usersDomains...) + } + + return api.NewAPIResponse(domains, nil) +} + func updateUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) api.Response { ud := &happydns.Domain{} err := json.NewDecoder(body).Decode(&ud) diff --git a/admin/db-source.go b/admin/db-source.go index f2660d0..99b0d9e 100644 --- a/admin/db-source.go +++ b/admin/db-source.go @@ -53,6 +53,7 @@ func init() { router.PUT("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(updateUserSource)))) router.DELETE("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceMetaHandler(deleteUserSource)))) + router.GET("/api/sources", api.ApiHandler(getAllSources)) router.DELETE("/api/sources", api.ApiHandler(clearSources)) } @@ -106,6 +107,25 @@ func getUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprou return api.NewAPIResponse(source, nil) } +func getAllSources(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response { + var sources []happydns.SourceMeta + + users, err := storage.MainStore.GetUsers() + if err != nil { + return api.NewAPIResponse(nil, fmt.Errorf("Unable to retrieve users list: %w", err)) + } + for _, user := range users { + usersSources, err := storage.MainStore.GetSourceMetas(user) + if err != nil { + return api.NewAPIResponse(nil, fmt.Errorf("Unable to retrieve %s's sources: %w", user.Email, err)) + } + + sources = append(sources, usersSources...) + } + + return api.NewAPIResponse(sources, nil) +} + func updateUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, body io.Reader) api.Response { us, err := api.DecodeSource(body) if err != nil {