dns.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package schema
  2. import (
  3. "context"
  4. "time"
  5. "github.com/gravitl/netmaker/db"
  6. "gorm.io/datatypes"
  7. )
  8. type Nameserver struct {
  9. ID string `gorm:"primaryKey" json:"id"`
  10. Name string `gorm:"name" json:"name"`
  11. NetworkID string `gorm:"network_id" json:"network_id"`
  12. Description string `gorm:"description" json:"description"`
  13. Servers datatypes.JSONSlice[string] `gorm:"servers" json:"servers"`
  14. MatchDomain string `gorm:"match_domain" json:"match_domain"`
  15. Tags datatypes.JSONMap `gorm:"tags" json:"tags"`
  16. Status bool `gorm:"status" json:"status"`
  17. CreatedBy string `gorm:"created_by" json:"created_by"`
  18. CreatedAt time.Time `gorm:"created_at" json:"created_at"`
  19. UpdatedAt time.Time `gorm:"updated_at" json:"updated_at"`
  20. }
  21. func (ns *Nameserver) Get(ctx context.Context) error {
  22. return db.FromContext(ctx).Model(&Nameserver{}).First(&ns).Where("id = ?", ns.ID).Error
  23. }
  24. func (ns *Nameserver) Update(ctx context.Context) error {
  25. return db.FromContext(ctx).Model(&Nameserver{}).Where("id = ?", ns.ID).Updates(&ns).Error
  26. }
  27. func (ns *Nameserver) Create(ctx context.Context) error {
  28. return db.FromContext(ctx).Model(&Nameserver{}).Create(&ns).Error
  29. }
  30. func (ns *Nameserver) ListByNetwork(ctx context.Context) (dnsli []Nameserver, err error) {
  31. err = db.FromContext(ctx).Model(&Nameserver{}).Where("network_id = ?", ns.NetworkID).Find(&dnsli).Error
  32. return
  33. }
  34. func (ns *Nameserver) Delete(ctx context.Context) error {
  35. return db.FromContext(ctx).Model(&Nameserver{}).Where("id = ?", ns.ID).Delete(&ns).Error
  36. }
  37. func (ns *Nameserver) UpdateStatus(ctx context.Context) error {
  38. return db.FromContext(ctx).Model(&Nameserver{}).Where("id = ?", ns.ID).Updates(map[string]any{
  39. "status": ns.Status,
  40. }).Error
  41. }