admin: add routes listing all sources and domains

This commit is contained in:
nemunaire 2020-07-25 13:20:34 +02:00
parent 8ea5b0d235
commit fe57f5e4f9
2 changed files with 40 additions and 0 deletions

View File

@ -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)

View File

@ -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 {