forked from flynn/flynn
/
domain.go
73 lines (63 loc) · 1.54 KB
/
domain.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
package installer
import (
"errors"
"net/http"
"time"
"github.com/flynn/flynn/pkg/httpclient"
)
func AllocateDomain() (*Domain, error) {
domain := &Domain{}
return domain, domain.client().Post("/domains", nil, domain)
}
type Domain struct {
ClusterID string `json:"-"`
Name string `json:"domain"`
Token string `json:"token"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
c *httpclient.Client
}
func (d *Domain) client() *httpclient.Client {
if d.c == nil {
d.c = &httpclient.Client{
ErrNotFound: errors.New("domain not found"),
URL: "https://domains.flynn.io",
HTTP: http.DefaultClient,
}
}
return d.c
}
func (d *Domain) authHeader() http.Header {
return http.Header{
"Authorization": {"Token " + d.Token},
}
}
func (d *Domain) path() string {
return "/domains/" + d.Name
}
func (d *Domain) Configure(nameservers []string) error {
data := struct {
Nameservers []string `json:"nameservers"`
}{nameservers}
res, err := d.client().RawReq("PUT", d.path(), d.authHeader(), data, nil)
if err == nil {
res.Body.Close()
}
return err
}
func (d *Domain) ConfigureIPAddresses(ips []string) error {
data := struct {
IPAddresses []string `json:"ip_addresses"`
}{ips}
res, err := d.client().RawReq("PUT", d.path(), d.authHeader(), data, nil)
if err == nil {
res.Body.Close()
}
return err
}
func (d *Domain) Status() (string, error) {
var res struct {
Status string
}
_, err := d.client().RawReq("GET", d.path()+"/status", d.authHeader(), nil, &res)
return res.Status, err
}