api: Fill a structure instead of passing argument to API function

This commit is contained in:
nemunaire 2020-07-04 23:51:46 +02:00
parent 34f5107181
commit 43a75a700c
8 changed files with 167 additions and 159 deletions

View File

@ -38,7 +38,6 @@ import (
"io"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/miekg/dns"
"git.happydns.org/happydns/config"
@ -58,8 +57,8 @@ func init() {
router.DELETE("/api/domains/:domain/rr", apiAuthHandler(domainHandler(delRR)))
}
func getDomains(_ *config.Options, u *happydns.User, p httprouter.Params, body io.Reader) Response {
if domains, err := storage.MainStore.GetDomains(u); err != nil {
func getDomains(_ *config.Options, req *RequestResources, body io.Reader) Response {
if domains, err := storage.MainStore.GetDomains(req.User); err != nil {
return APIErrorResponse{
err: err,
}
@ -74,7 +73,7 @@ func getDomains(_ *config.Options, u *happydns.User, p httprouter.Params, body i
}
}
func addDomain(_ *config.Options, u *happydns.User, p httprouter.Params, body io.Reader) Response {
func addDomain(_ *config.Options, req *RequestResources, body io.Reader) Response {
var uz happydns.Domain
err := json.NewDecoder(body).Decode(&uz)
if err != nil {
@ -97,7 +96,7 @@ func addDomain(_ *config.Options, u *happydns.User, p httprouter.Params, body io
}
}
source, err := storage.MainStore.GetSource(u, uz.IdSource)
source, err := storage.MainStore.GetSource(req.User, uz.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
@ -113,7 +112,7 @@ func addDomain(_ *config.Options, u *happydns.User, p httprouter.Params, body io
return APIErrorResponse{
err: err,
}
} else if err := storage.MainStore.CreateDomain(u, &uz); err != nil {
} else if err := storage.MainStore.CreateDomain(req.User, &uz); err != nil {
return APIErrorResponse{
err: err,
}
@ -124,8 +123,8 @@ func addDomain(_ *config.Options, u *happydns.User, p httprouter.Params, body io
}
}
func delDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
if err := storage.MainStore.DeleteDomain(domain); err != nil {
func delDomain(_ *config.Options, req *RequestResources, body io.Reader) Response {
if err := storage.MainStore.DeleteDomain(req.Domain); err != nil {
return APIErrorResponse{
err: err,
}
@ -136,15 +135,16 @@ func delDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params,
}
}
func domainHandler(f func(*config.Options, *happydns.Domain, httprouter.Params, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
if domain, err := storage.MainStore.GetDomainByDN(u, ps.ByName("domain")); err != nil {
func domainHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(*config.Options, *RequestResources, io.Reader) Response {
return func(opts *config.Options, req *RequestResources, body io.Reader) Response {
var err error
if req.Domain, err = storage.MainStore.GetDomainByDN(req.User, req.Ps.ByName("domain")); err != nil {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Domain not found"),
}
} else {
return f(opts, domain, ps, body)
return f(opts, req, body)
}
}
}
@ -157,16 +157,16 @@ type apiDomain struct {
ZoneHistory []happydns.ZoneMeta `json:"zone_history"`
}
func getDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
func getDomain(_ *config.Options, req *RequestResources, body io.Reader) Response {
ret := &apiDomain{
Id: domain.Id,
IdUser: domain.IdUser,
IdSource: domain.IdSource,
DomainName: domain.DomainName,
Id: req.Domain.Id,
IdUser: req.Domain.IdUser,
IdSource: req.Domain.IdSource,
DomainName: req.Domain.DomainName,
ZoneHistory: []happydns.ZoneMeta{},
}
for _, zm := range domain.ZoneHistory {
for _, zm := range req.Domain.ZoneHistory {
zoneMeta, err := storage.MainStore.GetZoneMeta(zm)
if err != nil {
@ -183,15 +183,15 @@ func getDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params,
}
}
func axfrDomain(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
func axfrDomain(opts *config.Options, req *RequestResources, body io.Reader) Response {
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
rrs, err := source.ImportZone(domain)
rrs, err := source.ImportZone(req.Domain)
if err != nil {
return APIErrorResponse{
err: err,
@ -215,7 +215,7 @@ type uploadedRR struct {
RR string `json:"string"`
}
func addRR(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
func addRR(opts *config.Options, req *RequestResources, body io.Reader) Response {
var urr uploadedRR
err := json.NewDecoder(body).Decode(&urr)
if err != nil {
@ -224,21 +224,21 @@ func addRR(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, b
}
}
rr, err := dns.NewRR(fmt.Sprintf("$ORIGIN %s\n$TTL %d\n%s", domain.DomainName, 3600, urr.RR))
rr, err := dns.NewRR(fmt.Sprintf("$ORIGIN %s\n$TTL %d\n%s", req.Domain.DomainName, 3600, urr.RR))
if err != nil {
return APIErrorResponse{
err: err,
}
}
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
err = source.AddRR(domain, rr)
err = source.AddRR(req.Domain, rr)
if err != nil {
return APIErrorResponse{
status: http.StatusInternalServerError,
@ -253,7 +253,7 @@ func addRR(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, b
}
}
func delRR(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
func delRR(opts *config.Options, req *RequestResources, body io.Reader) Response {
var urr uploadedRR
err := json.NewDecoder(body).Decode(&urr)
if err != nil {
@ -269,14 +269,14 @@ func delRR(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, b
}
}
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
err = source.DeleteRR(domain, rr)
err = source.DeleteRR(req.Domain, rr)
if err != nil {
return APIErrorResponse{
status: http.StatusInternalServerError,

View File

@ -149,7 +149,17 @@ func ApiHandler(f func(*config.Options, httprouter.Params, io.Reader) Response)
}
}
func apiAuthHandler(f func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response) func(http.ResponseWriter, *http.Request, httprouter.Params) {
type RequestResources struct {
Domain *happydns.Domain
Ps httprouter.Params
Session *happydns.Session
Source *happydns.SourceCombined
SourceMeta *happydns.SourceMeta
User *happydns.User
Zone *happydns.Zone
}
func apiAuthHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
@ -191,14 +201,19 @@ func apiAuthHandler(f func(*config.Options, *happydns.User, httprouter.Params, i
err: err,
status: http.StatusUnauthorized,
}.WriteResponse(w)
} else if std, err := storage.MainStore.GetUser(session.IdUser); err != nil {
} else if user, err := storage.MainStore.GetUser(session.IdUser); err != nil {
APIErrorResponse{
err: err,
status: http.StatusUnauthorized,
}.WriteResponse(w)
} else {
opts := r.Context().Value("opts").(*config.Options)
f(opts, std, ps, r.Body).WriteResponse(w)
req := &RequestResources{
Ps: ps,
Session: session,
User: user,
}
f(opts, req, r.Body).WriteResponse(w)
}
}
}

View File

@ -39,11 +39,9 @@ import (
"strings"
"time"
"github.com/julienschmidt/httprouter"
"github.com/miekg/dns"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
)
func init() {
@ -56,7 +54,7 @@ type resolverRequest struct {
Type string `json:"type"`
}
func runResolver(_ *config.Options, u *happydns.User, _ httprouter.Params, body io.Reader) Response {
func runResolver(_ *config.Options, req *RequestResources, body io.Reader) Response {
var urr resolverRequest
err := json.NewDecoder(body).Decode(&urr)
if err != nil {

View File

@ -37,7 +37,6 @@ import (
"github.com/julienschmidt/httprouter"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/services"
"git.happydns.org/happydns/storage"
)
@ -61,22 +60,22 @@ func listServices(_ *config.Options, _ httprouter.Params, _ io.Reader) Response
}
}
func analyzeDomain(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
func analyzeDomain(opts *config.Options, req *RequestResources, body io.Reader) Response {
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
zone, err := source.ImportZone(domain)
zone, err := source.ImportZone(req.Domain)
if err != nil {
return APIErrorResponse{
err: err,
}
}
services, defaultTTL, err := svcs.AnalyzeZone(domain.DomainName, zone)
services, defaultTTL, err := svcs.AnalyzeZone(req.Domain.DomainName, zone)
if err != nil {
return APIErrorResponse{
err: err,

View File

@ -38,8 +38,6 @@ import (
"io/ioutil"
"strconv"
"github.com/julienschmidt/httprouter"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/sources"
@ -57,8 +55,8 @@ func init() {
router.GET("/api/sources/:sid/domains", apiAuthHandler(sourceHandler(getDomainsHostedBySource)))
}
func getSources(_ *config.Options, u *happydns.User, p httprouter.Params, body io.Reader) Response {
if sources, err := storage.MainStore.GetSourceMetas(u); err != nil {
func getSources(_ *config.Options, req *RequestResources, body io.Reader) Response {
if sources, err := storage.MainStore.GetSourceMetas(req.User); err != nil {
return APIErrorResponse{
err: err,
}
@ -73,33 +71,34 @@ func getSources(_ *config.Options, u *happydns.User, p httprouter.Params, body i
}
}
func sourceMetaHandler(f func(*config.Options, *happydns.SourceMeta, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil {
func sourceMetaHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(*config.Options, *RequestResources, io.Reader) Response {
return func(opts *config.Options, req *RequestResources, body io.Reader) Response {
if sid, err := strconv.ParseInt(string(req.Ps.ByName("sid")), 10, 64); err != nil {
return APIErrorResponse{err: err}
} else if srcMeta, err := storage.MainStore.GetSourceMeta(u, sid); err != nil {
} else if req.SourceMeta, err = storage.MainStore.GetSourceMeta(req.User, sid); err != nil {
return APIErrorResponse{err: err}
} else {
return f(opts, srcMeta, u, body)
return f(opts, req, body)
}
}
}
func sourceHandler(f func(*config.Options, *happydns.SourceCombined, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
if sid, err := strconv.ParseInt(string(ps.ByName("sid")), 10, 64); err != nil {
func sourceHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(*config.Options, *RequestResources, io.Reader) Response {
return func(opts *config.Options, req *RequestResources, body io.Reader) Response {
if sid, err := strconv.ParseInt(string(req.Ps.ByName("sid")), 10, 64); err != nil {
return APIErrorResponse{err: err}
} else if source, err := storage.MainStore.GetSource(u, sid); err != nil {
} else if req.Source, err = storage.MainStore.GetSource(req.User, sid); err != nil {
return APIErrorResponse{err: err}
} else {
return f(opts, source, u, body)
req.SourceMeta = &req.Source.SourceMeta
return f(opts, req, body)
}
}
}
func getSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response {
func getSource(_ *config.Options, req *RequestResources, body io.Reader) Response {
return APIResponse{
response: s,
response: req.Source,
}
}
@ -138,7 +137,7 @@ func DecodeSource(body io.Reader) (*happydns.SourceCombined, error) {
return src, nil
}
func addSource(_ *config.Options, u *happydns.User, p httprouter.Params, body io.Reader) Response {
func addSource(_ *config.Options, req *RequestResources, body io.Reader) Response {
src, err := DecodeSource(body)
if err != nil {
return APIErrorResponse{
@ -146,7 +145,7 @@ func addSource(_ *config.Options, u *happydns.User, p httprouter.Params, body io
}
}
if s, err := storage.MainStore.CreateSource(u, src.Source, src.Comment); err != nil {
if s, err := storage.MainStore.CreateSource(req.User, src.Source, src.Comment); err != nil {
return APIErrorResponse{
err: err,
}
@ -157,7 +156,7 @@ func addSource(_ *config.Options, u *happydns.User, p httprouter.Params, body io
}
}
func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response {
func updateSource(_ *config.Options, req *RequestResources, body io.Reader) Response {
src, err := DecodeSource(body)
if err != nil {
return APIErrorResponse{
@ -165,8 +164,8 @@ func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.Use
}
}
src.Id = s.Id
src.OwnerId = s.OwnerId
src.Id = req.Source.Id
src.OwnerId = req.Source.OwnerId
if err := storage.MainStore.UpdateSource(src); err != nil {
return APIErrorResponse{
@ -179,8 +178,8 @@ func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.Use
}
}
func deleteSource(_ *config.Options, st *happydns.SourceMeta, u *happydns.User, body io.Reader) Response {
if err := storage.MainStore.DeleteSource(st); err != nil {
func deleteSource(_ *config.Options, req *RequestResources, body io.Reader) Response {
if err := storage.MainStore.DeleteSource(req.SourceMeta); err != nil {
return APIErrorResponse{
err: err,
}
@ -191,8 +190,8 @@ func deleteSource(_ *config.Options, st *happydns.SourceMeta, u *happydns.User,
}
}
func getDomainsHostedBySource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response {
sr, ok := s.Source.(sources.ListDomainsSource)
func getDomainsHostedBySource(_ *config.Options, req *RequestResources, body io.Reader) Response {
sr, ok := req.Source.Source.(sources.ListDomainsSource)
if !ok {
return APIErrorResponse{
err: fmt.Errorf("Source doesn't support domain listing."),

View File

@ -71,9 +71,9 @@ func currentUser(u *happydns.User) *DisplayUser {
}
}
func displayAuthToken(_ *config.Options, u *happydns.User, _ httprouter.Params, _ io.Reader) Response {
func displayAuthToken(_ *config.Options, req *RequestResources, _ io.Reader) Response {
return APIResponse{
response: currentUser(u),
response: currentUser(req.User),
}
}

View File

@ -222,9 +222,9 @@ func specialUserOperations(opts *config.Options, p httprouter.Params, body io.Re
return res
}
func sameUserHandler(f func(*config.Options, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
if uid, err := strconv.ParseInt(ps.ByName("uid"), 16, 64); err != nil {
func sameUserHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(*config.Options, *RequestResources, io.Reader) Response {
return func(opts *config.Options, req *RequestResources, body io.Reader) Response {
if uid, err := strconv.ParseInt(req.Ps.ByName("uid"), 16, 64); err != nil {
return APIErrorResponse{
status: http.StatusNotFound,
err: fmt.Errorf("Invalid user identifier given: %w", err),
@ -234,20 +234,20 @@ func sameUserHandler(f func(*config.Options, *happydns.User, io.Reader) Response
status: http.StatusNotFound,
err: errors.New("User not found"),
}
} else if user.Id != u.Id {
} else if user.Id != req.User.Id {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("User not found"),
}
} else {
return f(opts, user, body)
return f(opts, req, body)
}
}
}
func getUser(opts *config.Options, user *happydns.User, _ io.Reader) Response {
func getUser(opts *config.Options, req *RequestResources, _ io.Reader) Response {
return APIResponse{
response: user,
response: req.User,
}
}

View File

@ -43,7 +43,6 @@ import (
"strings"
"time"
"github.com/julienschmidt/httprouter"
"github.com/miekg/dns"
"git.happydns.org/happydns/config"
@ -54,66 +53,64 @@ import (
)
func init() {
router.GET("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(getZone)))
router.PATCH("/api/domains/:domain/zone/:zoneid", apiAuthHandler(zoneHandler(updateZoneService)))
router.GET("/api/domains/:domain/zone/:zoneid", apiAuthHandler(domainHandler(zoneHandler(getZone))))
router.PATCH("/api/domains/:domain/zone/:zoneid", apiAuthHandler(domainHandler(zoneHandler(updateZoneService))))
router.GET("/api/domains/:domain/zone/:zoneid/:subdomain", apiAuthHandler(zoneHandler(getZoneSubdomain)))
router.POST("/api/domains/:domain/zone/:zoneid/:subdomain", apiAuthHandler(zoneHandler(addZoneService)))
router.GET("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(zoneHandler(getZoneService)))
router.DELETE("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(zoneHandler(deleteZoneService)))
router.GET("/api/domains/:domain/zone/:zoneid/:subdomain", apiAuthHandler(domainHandler(zoneHandler(getZoneSubdomain))))
router.POST("/api/domains/:domain/zone/:zoneid/:subdomain", apiAuthHandler(domainHandler(zoneHandler(addZoneService))))
router.GET("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(domainHandler(zoneHandler(getZoneService))))
router.DELETE("/api/domains/:domain/zone/:zoneid/:subdomain/*serviceid", apiAuthHandler(domainHandler(zoneHandler(deleteZoneService))))
router.POST("/api/domains/:domain/import_zone", apiAuthHandler(domainHandler(importZone)))
router.POST("/api/domains/:domain/view_zone/:zoneid", apiAuthHandler(zoneHandler(viewZone)))
router.POST("/api/domains/:domain/apply_zone/:zoneid", apiAuthHandler(zoneHandler(applyZone)))
router.POST("/api/domains/:domain/view_zone/:zoneid", apiAuthHandler(domainHandler(zoneHandler(viewZone))))
router.POST("/api/domains/:domain/apply_zone/:zoneid", apiAuthHandler(domainHandler(zoneHandler(applyZone))))
router.POST("/api/domains/:domain/diff_zones/:zoneid1/:zoneid2", apiAuthHandler(domainHandler(diffZones)))
}
func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, httprouter.Params, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
return func(opts *config.Options, u *happydns.User, ps httprouter.Params, body io.Reader) Response {
zoneid, err := strconv.ParseInt(ps.ByName("zoneid"), 10, 64)
func zoneHandler(f func(*config.Options, *RequestResources, io.Reader) Response) func(*config.Options, *RequestResources, io.Reader) Response {
return func(opts *config.Options, req *RequestResources, body io.Reader) Response {
zoneid, err := strconv.ParseInt(req.Ps.ByName("zoneid"), 10, 64)
if err != nil {
return APIErrorResponse{
err: err,
}
}
return domainHandler(func(opts *config.Options, domain *happydns.Domain, ps httprouter.Params, body io.Reader) Response {
// Check that the zoneid exists in the domain history
if !domain.HasZone(zoneid) {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone not found"),
}
// Check that the zoneid exists in the domain history
if !req.Domain.HasZone(zoneid) {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone not found"),
}
}
if zone, err := storage.MainStore.GetZone(zoneid); err != nil {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone not found"),
}
} else {
return f(opts, domain, zone, ps, body)
if req.Zone, err = storage.MainStore.GetZone(zoneid); err != nil {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone not found"),
}
})(opts, u, ps, body)
} else {
return f(opts, req, body)
}
}
}
func getZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response {
func getZone(opts *config.Options, req *RequestResources, body io.Reader) Response {
return APIResponse{
response: zone,
response: req.Zone,
}
}
func getZoneSubdomain(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response {
subdomain := strings.TrimSuffix(ps.ByName("subdomain"), "@")
func getZoneSubdomain(opts *config.Options, req *RequestResources, body io.Reader) Response {
subdomain := strings.TrimSuffix(req.Ps.ByName("subdomain"), "@")
return APIResponse{
response: map[string]interface{}{
"services": zone.Services[subdomain],
"services": req.Zone.Services[subdomain],
},
}
}
func addZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response {
func addZoneService(opts *config.Options, req *RequestResources, body io.Reader) Response {
usc := &happydns.ServiceCombined{}
err := json.NewDecoder(body).Decode(&usc)
if err != nil {
@ -128,9 +125,9 @@ func addZoneService(opts *config.Options, domain *happydns.Domain, zone *happydn
}
}
subdomain := strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(ps.ByName("subdomain"), "."+domain.DomainName), "@"), domain.DomainName)
subdomain := strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(req.Ps.ByName("subdomain"), "."+req.Domain.DomainName), "@"), req.Domain.DomainName)
records := usc.Service.GenRRs(subdomain, usc.Ttl, domain.DomainName)
records := usc.Service.GenRRs(subdomain, usc.Ttl, req.Domain.DomainName)
if len(records) == 0 {
return APIErrorResponse{
err: fmt.Errorf("No record can be generated from your service."),
@ -143,11 +140,11 @@ func addZoneService(opts *config.Options, domain *happydns.Domain, zone *happydn
usc.Id = hash.Sum(nil)
usc.Domain = subdomain
usc.NbResources = usc.Service.GetNbResources()
usc.Comment = usc.Service.GenComment(domain.DomainName)
usc.Comment = usc.Service.GenComment(req.Domain.DomainName)
zone.Services[subdomain] = append(zone.Services[subdomain], usc)
req.Zone.Services[subdomain] = append(req.Zone.Services[subdomain], usc)
err = storage.MainStore.UpdateZone(zone)
err = storage.MainStore.UpdateZone(req.Zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -155,12 +152,12 @@ func addZoneService(opts *config.Options, domain *happydns.Domain, zone *happydn
}
return APIResponse{
response: zone,
response: req.Zone,
}
}
func getZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response {
serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:])
func getZoneService(opts *config.Options, req *RequestResources, body io.Reader) Response {
serviceid, err := base64.StdEncoding.DecodeString(req.Ps.ByName("serviceid")[1:])
if err != nil {
return APIErrorResponse{
err: err,
@ -168,26 +165,26 @@ func getZoneService(opts *config.Options, domain *happydns.Domain, zone *happydn
}
return APIResponse{
response: zone.FindSubdomainService(ps.ByName("subdomain"), serviceid),
response: req.Zone.FindSubdomainService(req.Ps.ByName("subdomain"), serviceid),
}
}
func importZone(opts *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) Response {
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
func importZone(opts *config.Options, req *RequestResources, body io.Reader) Response {
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
zone, err := source.ImportZone(domain)
zone, err := source.ImportZone(req.Domain)
if err != nil {
return APIErrorResponse{
err: err,
}
}
services, defaultTTL, err := svcs.AnalyzeZone(domain.DomainName, zone)
services, defaultTTL, err := svcs.AnalyzeZone(req.Domain.DomainName, zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -196,7 +193,7 @@ func importZone(opts *config.Options, domain *happydns.Domain, _ httprouter.Para
myZone := &happydns.Zone{
ZoneMeta: happydns.ZoneMeta{
IdAuthor: domain.IdUser,
IdAuthor: req.Domain.IdUser,
DefaultTTL: defaultTTL,
LastModified: time.Now(),
},
@ -210,10 +207,10 @@ func importZone(opts *config.Options, domain *happydns.Domain, _ httprouter.Para
}
}
domain.ZoneHistory = append(
[]int64{myZone.Id}, domain.ZoneHistory...)
req.Domain.ZoneHistory = append(
[]int64{myZone.Id}, req.Domain.ZoneHistory...)
err = storage.MainStore.UpdateDomain(domain)
err = storage.MainStore.UpdateDomain(req.Domain)
if err != nil {
return APIErrorResponse{
err: err,
@ -225,16 +222,16 @@ func importZone(opts *config.Options, domain *happydns.Domain, _ httprouter.Para
}
}
func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Params, body io.Reader) Response {
zoneid1, err := strconv.ParseInt(ps.ByName("zoneid1"), 10, 64)
if err != nil && ps.ByName("zoneid1") != "@" {
func diffZones(opts *config.Options, req *RequestResources, body io.Reader) Response {
zoneid1, err := strconv.ParseInt(req.Ps.ByName("zoneid1"), 10, 64)
if err != nil && req.Ps.ByName("zoneid1") != "@" {
return APIErrorResponse{
err: err,
}
}
zoneid2, err := strconv.ParseInt(ps.ByName("zoneid2"), 10, 64)
if err != nil && ps.ByName("zoneid2") != "@" {
zoneid2, err := strconv.ParseInt(req.Ps.ByName("zoneid2"), 10, 64)
if err != nil && req.Ps.ByName("zoneid2") != "@" {
return APIErrorResponse{
err: err,
}
@ -250,7 +247,7 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
var zone2 []dns.RR
if zoneid1 == 0 || zoneid2 == 0 {
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
@ -258,10 +255,10 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
}
if zoneid1 == 0 {
zone1, err = source.ImportZone(domain)
zone1, err = source.ImportZone(req.Domain)
}
if zoneid2 == 0 {
zone2, err = source.ImportZone(domain)
zone2, err = source.ImportZone(req.Domain)
}
if err != nil {
@ -272,7 +269,7 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
}
if zoneid1 != 0 {
if !domain.HasZone(zoneid1) {
if !req.Domain.HasZone(zoneid1) {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone A not found"),
@ -283,12 +280,12 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
err: errors.New("Zone A not found"),
}
} else {
zone1 = z1.GenerateRRs(domain.DomainName)
zone1 = z1.GenerateRRs(req.Domain.DomainName)
}
}
if zoneid2 != 0 {
if !domain.HasZone(zoneid2) {
if !req.Domain.HasZone(zoneid2) {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Zone B not found"),
@ -299,7 +296,7 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
err: errors.New("Zone B not found"),
}
} else {
zone2 = z2.GenerateRRs(domain.DomainName)
zone2 = z2.GenerateRRs(req.Domain.DomainName)
}
}
@ -323,15 +320,15 @@ func diffZones(opts *config.Options, domain *happydns.Domain, ps httprouter.Para
}
}
func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response {
source, err := storage.MainStore.GetSource(&happydns.User{Id: domain.IdUser}, domain.IdSource)
func applyZone(opts *config.Options, req *RequestResources, body io.Reader) Response {
source, err := storage.MainStore.GetSource(req.User, req.Domain.IdSource)
if err != nil {
return APIErrorResponse{
err: err,
}
}
newSOA, err := sources.ApplyZone(source, domain, zone.GenerateRRs(domain.DomainName), true)
newSOA, err := sources.ApplyZone(source, req.Domain, req.Zone.GenerateRRs(req.Domain.DomainName), true)
if err != nil {
return APIErrorResponse{
err: err,
@ -340,7 +337,7 @@ func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zon
// Update serial
if newSOA != nil {
for _, svc := range zone.Services[""] {
for _, svc := range req.Zone.Services[""] {
if origin, ok := svc.Service.(*svcs.Origin); ok {
origin.Serial = newSOA.Serial
break
@ -349,7 +346,7 @@ func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zon
}
// Create a new zone in history for futher updates
newZone := zone.DerivateNew()
newZone := req.Zone.DerivateNew()
//newZone.IdAuthor = //TODO get current user id
err = storage.MainStore.CreateZone(newZone)
if err != nil {
@ -358,10 +355,10 @@ func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zon
}
}
domain.ZoneHistory = append(
[]int64{newZone.Id}, domain.ZoneHistory...)
req.Domain.ZoneHistory = append(
[]int64{newZone.Id}, req.Domain.ZoneHistory...)
err = storage.MainStore.UpdateDomain(domain)
err = storage.MainStore.UpdateDomain(req.Domain)
if err != nil {
return APIErrorResponse{
err: err,
@ -371,11 +368,11 @@ func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zon
// Commit changes in previous zone
now := time.Now()
// zone.ZoneMeta.IdAuthor = // TODO get current user id
zone.ZoneMeta.Published = &now
req.Zone.ZoneMeta.Published = &now
zone.LastModified = time.Now()
req.Zone.LastModified = time.Now()
err = storage.MainStore.UpdateZone(zone)
err = storage.MainStore.UpdateZone(req.Zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -387,10 +384,10 @@ func applyZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zon
}
}
func viewZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response {
func viewZone(opts *config.Options, req *RequestResources, body io.Reader) Response {
var ret string
for _, rr := range zone.GenerateRRs(domain.DomainName) {
for _, rr := range req.Zone.GenerateRRs(req.Domain.DomainName) {
ret += rr.String() + "\n"
}
@ -399,7 +396,7 @@ func viewZone(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone
}
}
func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, _ httprouter.Params, body io.Reader) Response {
func updateZoneService(opts *config.Options, req *RequestResources, body io.Reader) Response {
usc := &happydns.ServiceCombined{}
err := json.NewDecoder(body).Decode(&usc)
if err != nil {
@ -408,16 +405,16 @@ func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happ
}
}
err = zone.EraseService(usc.Domain, domain.DomainName, usc.Id, usc)
err = req.Zone.EraseService(usc.Domain, req.Domain.DomainName, usc.Id, usc)
if err != nil {
return APIErrorResponse{
err: err,
}
}
zone.LastModified = time.Now()
req.Zone.LastModified = time.Now()
err = storage.MainStore.UpdateZone(zone)
err = storage.MainStore.UpdateZone(req.Zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -425,28 +422,28 @@ func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happ
}
return APIResponse{
response: zone,
response: req.Zone,
}
}
func deleteZoneService(opts *config.Options, domain *happydns.Domain, zone *happydns.Zone, ps httprouter.Params, body io.Reader) Response {
serviceid, err := base64.StdEncoding.DecodeString(ps.ByName("serviceid")[1:])
func deleteZoneService(opts *config.Options, req *RequestResources, body io.Reader) Response {
serviceid, err := base64.StdEncoding.DecodeString(req.Ps.ByName("serviceid")[1:])
if err != nil {
return APIErrorResponse{
err: err,
}
}
err = zone.EraseService(ps.ByName("subdomain"), domain.DomainName, serviceid, nil)
err = req.Zone.EraseService(req.Ps.ByName("subdomain"), req.Domain.DomainName, serviceid, nil)
if err != nil {
return APIErrorResponse{
err: err,
}
}
zone.LastModified = time.Now()
req.Zone.LastModified = time.Now()
err = storage.MainStore.UpdateZone(zone)
err = storage.MainStore.UpdateZone(req.Zone)
if err != nil {
return APIErrorResponse{
err: err,
@ -454,6 +451,6 @@ func deleteZoneService(opts *config.Options, domain *happydns.Domain, zone *happ
}
return APIResponse{
response: zone,
response: req.Zone,
}
}