Rename SourceType as SourceMeta to follow ZoneMeta

This commit is contained in:
nemunaire 2020-06-24 17:48:05 +02:00
parent dca3779115
commit beb87a8572
8 changed files with 45 additions and 45 deletions

View File

@ -57,7 +57,7 @@ func init() {
}
func getUserSources(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(storage.MainStore.GetSourceTypes(user))
return api.NewAPIResponse(storage.MainStore.GetSourceMetas(user))
}
func newUserSource(_ *config.Options, user *happydns.User, _ httprouter.Params, body io.Reader) api.Response {
@ -101,7 +101,7 @@ func updateUserSource(_ *config.Options, source *happydns.SourceCombined, _ http
}
func deleteUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(true, storage.MainStore.DeleteSource(&source.SourceType))
return api.NewAPIResponse(true, storage.MainStore.DeleteSource(&source.SourceMeta))
}
func clearSources(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response {

View File

@ -110,11 +110,11 @@ func getSourceSpec(_ *config.Options, p httprouter.Params, body io.Reader) Respo
}
}
srcType := reflect.Indirect(reflect.ValueOf(src)).Type()
srcMeta := reflect.Indirect(reflect.ValueOf(src)).Type()
fields := []source_field{}
for i := 0; i < srcType.NumField(); i += 1 {
jsonTag := srcType.Field(i).Tag.Get("json")
for i := 0; i < srcMeta.NumField(); i += 1 {
jsonTag := srcMeta.Field(i).Tag.Get("json")
jsonTuples := strings.Split(jsonTag, ",")
f := source_field{}
@ -122,10 +122,10 @@ func getSourceSpec(_ *config.Options, p httprouter.Params, body io.Reader) Respo
if len(jsonTuples) > 0 && len(jsonTuples[0]) > 0 {
f.Id = jsonTuples[0]
} else {
f.Id = srcType.Field(i).Name
f.Id = srcMeta.Field(i).Name
}
tag := srcType.Field(i).Tag.Get("happydns")
tag := srcMeta.Field(i).Tag.Get("happydns")
tuples := strings.Split(tag, ",")
for _, t := range tuples {

View File

@ -52,13 +52,13 @@ func init() {
router.GET("/api/sources/:sid", apiAuthHandler(sourceHandler(getSource)))
router.PUT("/api/sources/:sid", apiAuthHandler(sourceHandler(updateSource)))
router.DELETE("/api/sources/:sid", apiAuthHandler(sourceTypeHandler(deleteSource)))
router.DELETE("/api/sources/:sid", apiAuthHandler(sourceMetaHandler(deleteSource)))
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.GetSourceTypes(u); err != nil {
if sources, err := storage.MainStore.GetSourceMetas(u); err != nil {
return APIErrorResponse{
err: err,
}
@ -73,14 +73,14 @@ func getSources(_ *config.Options, u *happydns.User, p httprouter.Params, body i
}
}
func sourceTypeHandler(f func(*config.Options, *happydns.SourceType, *happydns.User, io.Reader) Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) Response {
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 {
return APIErrorResponse{err: err}
} else if srcType, err := storage.MainStore.GetSourceType(u, sid); err != nil {
} else if srcMeta, err := storage.MainStore.GetSourceMeta(u, sid); err != nil {
return APIErrorResponse{err: err}
} else {
return f(opts, srcType, u, body)
return f(opts, srcMeta, u, body)
}
}
}
@ -109,7 +109,7 @@ func DecodeSource(body io.Reader) (*happydns.SourceCombined, error) {
return nil, err
}
var ust happydns.SourceType
var ust happydns.SourceMeta
err = json.Unmarshal(cnt, &ust)
if err != nil {
return nil, err
@ -179,7 +179,7 @@ func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.Use
}
}
func deleteSource(_ *config.Options, st *happydns.SourceType, u *happydns.User, body io.Reader) Response {
func deleteSource(_ *config.Options, st *happydns.SourceMeta, u *happydns.User, body io.Reader) Response {
if err := storage.MainStore.DeleteSource(st); err != nil {
return APIErrorResponse{
err: err,

View File

@ -53,7 +53,7 @@ func (d *Domain) NormalizedNSServer() string {
}
}
func NewDomain(u *User, st *SourceType, dn string) (d *Domain) {
func NewDomain(u *User, st *SourceMeta, dn string) (d *Domain) {
d = &Domain{
IdUser: u.Id,
IdSource: st.Id,

View File

@ -43,7 +43,7 @@ type Source interface {
DeleteRR(*Domain, dns.RR) error
}
type SourceType struct {
type SourceMeta struct {
Type string `json:"_srctype"`
Id int64 `json:"_id"`
OwnerId int64 `json:"_ownerid"`
@ -52,5 +52,5 @@ type SourceType struct {
type SourceCombined struct {
Source
SourceType
SourceMeta
}

View File

@ -56,13 +56,13 @@ type Storage interface {
DeleteSession(session *happydns.Session) error
ClearSessions() error
GetSourceTypes(u *happydns.User) ([]happydns.SourceType, error)
GetSourceMetas(u *happydns.User) ([]happydns.SourceMeta, error)
GetSource(u *happydns.User, id int64) (*happydns.SourceCombined, error)
GetSourceType(u *happydns.User, id int64) (*happydns.SourceType, error)
GetSourceMeta(u *happydns.User, id int64) (*happydns.SourceMeta, error)
CreateSource(u *happydns.User, s happydns.Source, comment string) (*happydns.SourceCombined, error)
UpdateSource(s *happydns.SourceCombined) error
UpdateSourceOwner(s *happydns.SourceCombined, newOwner *happydns.User) error
DeleteSource(s *happydns.SourceType) error
DeleteSource(s *happydns.SourceMeta) error
ClearSources() error
GetUsers() (happydns.Users, error)

View File

@ -43,48 +43,48 @@ import (
"git.happydns.org/happydns/sources"
)
func (s *LevelDBStorage) getSourceType(id []byte) (srcType *happydns.SourceType, err error) {
srcType = &happydns.SourceType{}
err = decodeData(id, srcType)
func (s *LevelDBStorage) getSourceMeta(id []byte) (srcMeta *happydns.SourceMeta, err error) {
srcMeta = &happydns.SourceMeta{}
err = decodeData(id, srcMeta)
return
}
func (s *LevelDBStorage) GetSourceTypes(u *happydns.User) (srcs []happydns.SourceType, err error) {
func (s *LevelDBStorage) GetSourceMetas(u *happydns.User) (srcs []happydns.SourceMeta, err error) {
iter := s.search("source-")
defer iter.Release()
for iter.Next() {
var srcType happydns.SourceType
err = decodeData(iter.Value(), &srcType)
var srcMeta happydns.SourceMeta
err = decodeData(iter.Value(), &srcMeta)
if err != nil {
return
}
if srcType.OwnerId != u.Id {
if srcMeta.OwnerId != u.Id {
continue
}
srcs = append(srcs, srcType)
srcs = append(srcs, srcMeta)
}
return
}
func (s *LevelDBStorage) GetSourceType(u *happydns.User, id int64) (srcType *happydns.SourceType, err error) {
func (s *LevelDBStorage) GetSourceMeta(u *happydns.User, id int64) (srcMeta *happydns.SourceMeta, err error) {
var v []byte
v, err = s.db.Get([]byte(fmt.Sprintf("source-%d", id)), nil)
if err != nil {
return
}
srcType = new(happydns.SourceType)
err = decodeData(v, &srcType)
srcMeta = new(happydns.SourceMeta)
err = decodeData(v, &srcMeta)
if err != nil {
return
}
if srcType.OwnerId != u.Id {
srcType = nil
if srcMeta.OwnerId != u.Id {
srcMeta = nil
err = leveldb.ErrNotFound
}
@ -98,23 +98,23 @@ func (s *LevelDBStorage) GetSource(u *happydns.User, id int64) (src *happydns.So
return
}
var srcType happydns.SourceType
err = decodeData(v, &srcType)
var srcMeta happydns.SourceMeta
err = decodeData(v, &srcMeta)
if err != nil {
return
}
if srcType.OwnerId != u.Id {
if srcMeta.OwnerId != u.Id {
src = nil
err = leveldb.ErrNotFound
}
var tsrc happydns.Source
tsrc, err = sources.FindSource(srcType.Type)
tsrc, err = sources.FindSource(srcMeta.Type)
src = &happydns.SourceCombined{
tsrc,
srcType,
srcMeta,
}
err = decodeData(v, src)
@ -132,7 +132,7 @@ func (s *LevelDBStorage) CreateSource(u *happydns.User, src happydns.Source, com
st := &happydns.SourceCombined{
src,
happydns.SourceType{
happydns.SourceMeta{
Type: sType.PkgPath() + "/" + sType.Name(),
Id: id,
OwnerId: u.Id,
@ -151,7 +151,7 @@ func (s *LevelDBStorage) UpdateSourceOwner(src *happydns.SourceCombined, newOwne
return s.UpdateSource(src)
}
func (s *LevelDBStorage) DeleteSource(src *happydns.SourceType) error {
func (s *LevelDBStorage) DeleteSource(src *happydns.SourceMeta) error {
return s.delete(fmt.Sprintf("source-%d", src.Id))
}
@ -191,17 +191,17 @@ func (s *LevelDBStorage) TidySources() error {
defer iter.Release()
for iter.Next() {
srcType, err := s.getSourceType(iter.Key())
srcMeta, err := s.getSourceMeta(iter.Key())
if err != nil {
// Drop unreadable sources
log.Printf("Deleting unreadable source (%w): %v\n", err, srcType)
log.Printf("Deleting unreadable source (%w): %v\n", err, srcMeta)
err = tx.Delete(iter.Key(), nil)
} else {
_, err = s.GetUser(srcType.OwnerId)
_, err = s.GetUser(srcMeta.OwnerId)
if err == leveldb.ErrNotFound {
// Drop sources of unexistant users
log.Printf("Deleting orphan source (user %d not found): %v\n", srcType.OwnerId, srcType)
log.Printf("Deleting orphan source (user %d not found): %v\n", srcMeta.OwnerId, srcMeta)
err = tx.Delete(iter.Key(), nil)
}
}

View File

@ -74,7 +74,7 @@ func (s *MySQLStorage) DomainExists(dn string) bool {
return err == nil && z == 1
}
func (s *MySQLStorage) CreateDomain(u *happydns.User, src happydns.SourceType, z *happydns.Domain) error {
func (s *MySQLStorage) CreateDomain(u *happydns.User, src happydns.SourceMeta, z *happydns.Domain) error {
if res, err := s.db.Exec("INSERT INTO domains (id_user, id_source, domain) VALUES (?, ?, ?)", u.Id, src.Id, z.DomainName); err != nil {
return err
} else if z.Id, err = res.LastInsertId(); err != nil {