-
Notifications
You must be signed in to change notification settings - Fork 1
/
servers.go
79 lines (62 loc) · 1.83 KB
/
servers.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mailosaur
import (
"fmt"
"math/rand"
"os"
)
type ServersService struct {
client *MailosaurClient
}
type Server struct {
Id string `json:"id"`
Name string `json:"name"`
Messages int `json:"messages"`
}
type ServerListResult struct {
Items []*Server `json:"items"`
}
type ServerCreateOptions struct {
Name string `json:"name"`
}
func (s *ServersService) List() (*ServerListResult, error) {
result, err := s.client.HttpGet(&ServerListResult{}, "api/servers")
return result.(*ServerListResult), err
}
func (s *ServersService) Create(serverCreateOptions ServerCreateOptions) (*Server, error) {
result, err := s.client.HttpPost(&Server{}, "api/servers", serverCreateOptions)
return result.(*Server), err
}
func (s *ServersService) Get(id string) (*Server, error) {
result, err := s.client.HttpGet(&Server{}, "api/servers/"+id)
return result.(*Server), err
}
func (s *ServersService) GetPassword(id string) (string, error) {
type Result struct {
Value string `json:"value"`
}
result, err := s.client.HttpGet(&Result{}, "api/servers/"+id+"/password")
parsed := result.(*Result)
return parsed.Value, err
}
func (s *ServersService) Update(id string, server *Server) (*Server, error) {
result, err := s.client.HttpPut(&Server{}, "api/servers/"+id, server)
return result.(*Server), err
}
func (s *ServersService) Delete(id string) error {
return s.client.HttpDelete("api/servers/" + id)
}
func (s *ServersService) GenerateEmailAddress(id string) string {
host := os.Getenv("MAILOSAUR_SMTP_HOST")
if len(host) == 0 {
host = "mailosaur.net"
}
return fmt.Sprintf("%s@%s.%s", getRandomString(), id, host)
}
func getRandomString() string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
s := make([]rune, 8)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}