From ab6fe350cac2f4c1e95c6fbc8eb0355c35fa1da1 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 24 Jun 2020 17:33:34 +0200 Subject: [PATCH] Import ZoneMeta in domain history --- admin/db-zone.go | 4 ++-- api/domains.go | 30 +++++++++++++++++++++++++++++- api/zones.go | 22 ++++++++++++++-------- model/domain.go | 10 +++++----- model/zone.go | 18 +++++++++--------- storage/interface.go | 1 + 6 files changed, 60 insertions(+), 25 deletions(-) diff --git a/admin/db-zone.go b/admin/db-zone.go index b908241..f546f84 100644 --- a/admin/db-zone.go +++ b/admin/db-zone.go @@ -100,7 +100,7 @@ func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, httpr // Check that the zoneid exists in the domain history found := false for _, v := range domain.ZoneHistory { - if v.Id == zoneid { + if v == zoneid { found = true break } @@ -185,6 +185,6 @@ func deleteZone(opts *config.Options, ps httprouter.Params, body io.Reader) api. if err != nil { return api.NewAPIErrorResponse(http.StatusNotFound, err) } else { - return api.NewAPIResponse(true, storage.MainStore.DeleteZone(&happydns.Zone{Id: zoneid})) + return api.NewAPIResponse(true, storage.MainStore.DeleteZone(&happydns.Zone{ZoneMeta: happydns.ZoneMeta{Id: zoneid}})) } } diff --git a/api/domains.go b/api/domains.go index 674792a..a52b861 100644 --- a/api/domains.go +++ b/api/domains.go @@ -149,9 +149,37 @@ func domainHandler(f func(*config.Options, *happydns.Domain, io.Reader) Response } } +type apiDomain struct { + Id int64 `json:"id"` + IdUser int64 `json:"id_owner"` + IdSource int64 `json:"id_source"` + DomainName string `json:"domain"` + ZoneHistory []happydns.ZoneMeta `json:"zone_history"` +} + func getDomain(_ *config.Options, domain *happydns.Domain, body io.Reader) Response { + ret := &apiDomain{ + Id: domain.Id, + IdUser: domain.IdUser, + IdSource: domain.IdSource, + DomainName: domain.DomainName, + ZoneHistory: []happydns.ZoneMeta{}, + } + + for _, zm := range domain.ZoneHistory { + zoneMeta, err := storage.MainStore.GetZoneMeta(zm) + + if err != nil { + return APIErrorResponse{ + err: err, + } + } + + ret.ZoneHistory = append(ret.ZoneHistory, *zoneMeta) + } + return APIResponse{ - response: domain, + response: ret, } } diff --git a/api/zones.go b/api/zones.go index a0d9f8b..3afd816 100644 --- a/api/zones.go +++ b/api/zones.go @@ -41,6 +41,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/julienschmidt/httprouter" @@ -74,7 +75,7 @@ func zoneHandler(f func(*config.Options, *happydns.Domain, *happydns.Zone, httpr // Check that the zoneid exists in the domain history found := false for _, v := range domain.ZoneHistory { - if v.Id == zoneid { + if v == zoneid { found = true break } @@ -195,9 +196,12 @@ func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) R } myZone := &happydns.Zone{ - IdAuthor: domain.IdUser, - DefaultTTL: defaultTTL, - Services: services, + ZoneMeta: happydns.ZoneMeta{ + IdAuthor: domain.IdUser, + DefaultTTL: defaultTTL, + LastModified: time.Now(), + }, + Services: services, } err = storage.MainStore.CreateZone(myZone) @@ -208,9 +212,7 @@ func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) R } domain.ZoneHistory = append( - []happydns.ZoneMeta{ - happydns.ZoneMeta{myZone.Id}, - }, domain.ZoneHistory...) + []int64{myZone.Id}, domain.ZoneHistory...) err = storage.MainStore.UpdateDomain(domain) if err != nil { @@ -220,7 +222,7 @@ func importZone(opts *config.Options, domain *happydns.Domain, body io.Reader) R } return APIResponse{ - response: happydns.ZoneMeta{myZone.Id}, + response: &myZone.ZoneMeta, } } @@ -240,6 +242,8 @@ func updateZoneService(opts *config.Options, domain *happydns.Domain, zone *happ } } + zone.LastModified = time.Now() + err = storage.MainStore.UpdateZone(zone) if err != nil { return APIErrorResponse{ @@ -267,6 +271,8 @@ func deleteZoneService(opts *config.Options, domain *happydns.Domain, zone *happ } } + zone.LastModified = time.Now() + err = storage.MainStore.UpdateZone(zone) if err != nil { return APIErrorResponse{ diff --git a/model/domain.go b/model/domain.go index 3345b04..fdba0ee 100644 --- a/model/domain.go +++ b/model/domain.go @@ -36,11 +36,11 @@ import ( ) type Domain struct { - Id int64 `json:"id"` - IdUser int64 `json:"id_owner"` - IdSource int64 `json:"id_source"` - DomainName string `json:"domain"` - ZoneHistory []ZoneMeta `json:"zone_history"` + Id int64 `json:"id"` + IdUser int64 `json:"id_owner"` + IdSource int64 `json:"id_source"` + DomainName string `json:"domain"` + ZoneHistory []int64 `json:"zone_history"` } type Domains []*Domain diff --git a/model/zone.go b/model/zone.go index daab433..2c3c250 100644 --- a/model/zone.go +++ b/model/zone.go @@ -38,18 +38,18 @@ import ( ) type ZoneMeta struct { - Id int64 `json:"id"` + Id int64 `json:"id"` + IdAuthor int64 `json:"id_author"` + DefaultTTL uint32 `json:"default_ttl"` + LastModified time.Time `json:"last_modified,omitempty"` + CommitMsg *string `json:"commit_message,omitempty"` + CommitDate *time.Time `json:"commit_date,omitempty"` + Published *time.Time `json:"published,omitempty"` } type Zone struct { - Id int64 `json:"id"` - IdAuthor int64 `json:"id_author"` - DefaultTTL uint32 `json:"default_ttl"` - LastModified *time.Time `json:"last_modified,omitempty"` - CommitMsg *string `json:"commit_message,omitempty"` - CommitDate *time.Time `json:"commit_date,omitempty"` - Published *time.Time `json:"published,omitempty"` - Services map[string][]*ServiceCombined `json:"services"` + ZoneMeta + Services map[string][]*ServiceCombined `json:"services"` } func (z *Zone) FindService(id []byte) (string, *ServiceCombined) { diff --git a/storage/interface.go b/storage/interface.go index 09eb685..5daeb52 100644 --- a/storage/interface.go +++ b/storage/interface.go @@ -75,6 +75,7 @@ type Storage interface { ClearUsers() error GetZone(id int64) (*happydns.Zone, error) + GetZoneMeta(id int64) (*happydns.ZoneMeta, error) CreateZone(zone *happydns.Zone) error UpdateZone(zone *happydns.Zone) error DeleteZone(zone *happydns.Zone) error