pending_hosts.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package schema
  2. import (
  3. "context"
  4. "time"
  5. "github.com/gravitl/netmaker/db"
  6. "gorm.io/datatypes"
  7. )
  8. type PendingHost struct {
  9. ID string `gorm:"id" json:"id"`
  10. HostID string `gorm:"host_id" json:"host_id"`
  11. Hostname string `gorm:"host_name" json:"host_name"`
  12. Network string `gorm:"network" json:"network"`
  13. PublicKey string `gorm:"public_key" json:"public_key"`
  14. EnrollmentKey datatypes.JSON `gorm:"enrollment_key_id" json:"enrollment_key_id"`
  15. OS string `gorm:"os" json:"os"`
  16. Version string `gorm:"version" json:"version"`
  17. Location string `gorm:"location" json:"location"` // Format: "lat,lon"
  18. RequestedAt time.Time `gorm:"requested_at" json:"requested_at"`
  19. }
  20. func (p *PendingHost) Get(ctx context.Context) error {
  21. return db.FromContext(ctx).Model(&PendingHost{}).First(&p).Where("id = ?", p.ID).Error
  22. }
  23. func (p *PendingHost) Create(ctx context.Context) error {
  24. return db.FromContext(ctx).Model(&PendingHost{}).Create(&p).Error
  25. }
  26. func (p *PendingHost) List(ctx context.Context) (pendingHosts []PendingHost, err error) {
  27. err = db.FromContext(ctx).Model(&PendingHost{}).Find(&pendingHosts).Error
  28. return
  29. }
  30. func (p *PendingHost) Delete(ctx context.Context) error {
  31. return db.FromContext(ctx).Model(&PendingHost{}).Where("id = ?", p.ID).Delete(&p).Error
  32. }
  33. func (p *PendingHost) CheckIfPendingHostExists(ctx context.Context) error {
  34. return db.FromContext(ctx).Model(&PendingHost{}).Where("host_id = ? AND network = ?", p.HostID, p.Network).First(&p).Error
  35. }
  36. func (p *PendingHost) DeleteAllPendingHosts(ctx context.Context) error {
  37. return db.FromContext(ctx).Model(&PendingHost{}).Where("host_id = ?", p.HostID).Delete(&p).Error
  38. }