-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
51 lines (42 loc) · 1.86 KB
/
database.go
File metadata and controls
51 lines (42 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package wireguardhttps
import (
"net"
"github.com/joncooperworks/wgrpcd"
)
// Database represents all operations needed to persist devices, IP address and user info.
// Implementations of Database should ensure all errors are wrapped in the appropriate wireguardhttps error type.
type Database interface {
Initialize() error
Addresses() ([]IPAddress, error)
AllocateSubnet(addresses []net.IP) error
CreateDevice(owner UserProfile, name, os string, deviceFunc DeviceFunc) (Device, *wgrpcd.PeerConfigInfo, error)
RekeyDevice(owner UserProfile, device Device, deviceFunc DeviceFunc) (Device, *wgrpcd.PeerConfigInfo, error)
Devices(owner UserProfile) ([]Device, error)
Device(owner UserProfile, deviceID int) (Device, error)
RemoveDevice(owner UserProfile, device Device, deleteFunc DeleteFunc) error
RegisterUser(authPlatformUserID, authPlatform string) (UserProfile, error)
GetUser(userID int) (UserProfile, error)
DeleteUser(userID int) error
Close() error
}
// DeviceFunc creates a device on the Wireguard interface and returns an error on failure.
// This allows us to take advantage of SQL transactions.
type DeviceFunc func(IPAddress) (*wgrpcd.PeerConfigInfo, error)
// DeleteFunc deletes a device on the Wireguard interface.
type DeleteFunc func() error
// RecordNotFoundError is our package specific not found error.
// Database implementations should return this when they can't find a record, so the caller can handle this case without knowing about the underlying database.
type RecordNotFoundError struct {
err error
}
func (r *RecordNotFoundError) Error() string {
return r.err.Error()
}
// DatabaseError is our package specific error for all other errors.
// If a Database implementation cannot fit an error into any other error type, it should return DatabaseError.
type DatabaseError struct {
err error
}
func (d *DatabaseError) Error() string {
return d.err.Error()
}