-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains.go
124 lines (106 loc) · 2.83 KB
/
domains.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package webhookrelay
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
// Domain is a domain reservation
type Domain struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Domain can be any 3rd party domain such as
// user.example.com
Domain string `json:"domain"`
}
// MarshalJSON helper to marshal unix time
func (d *Domain) MarshalJSON() ([]byte, error) {
type Alias Domain
return json.Marshal(&struct {
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
*Alias
}{
CreatedAt: d.CreatedAt.Unix(),
UpdatedAt: d.UpdatedAt.Unix(),
Alias: (*Alias)(d),
})
}
// UnmarshalJSON helper to unmarshal unix time
func (d *Domain) UnmarshalJSON(data []byte) error {
type Alias Domain
aux := &struct {
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
*Alias
}{
Alias: (*Alias)(d),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
d.CreatedAt = time.Unix(aux.CreatedAt, 0)
d.UpdatedAt = time.Unix(aux.UpdatedAt, 0)
return nil
}
// DomainDeleteOptions are used to delete domain reservation
type DomainDeleteOptions struct {
Ref string `json:"ref"`
}
// DomainListOptions - TODO
type DomainListOptions struct{}
// ListDomainReservations lists domain reservations for an account
func (api *API) ListDomainReservations(options *DomainListOptions) ([]*Domain, error) {
resp, err := api.makeRequest(http.MethodGet, "/domains", nil)
if err != nil {
return nil, errors.Wrap(err, errMakeRequestError)
}
var domains []*Domain
err = json.Unmarshal(resp, &domains)
if err != nil {
return nil, errors.Wrap(err, errUnmarshalError)
}
return domains, nil
}
// ReserveDomain - reserve domain
func (api *API) ReserveDomain(options *Domain) (*Domain, error) {
resp, err := api.makeRequest(http.MethodPost, "/domains", options)
if err != nil {
return nil, err
}
var domainReservation Domain
if err := json.Unmarshal(resp, &domainReservation); err != nil {
return nil, err
}
return &domainReservation, nil
}
// DeleteDomainReservation deletes domain reservation. It can only be removed
// once no Input or Tunnel is using it.
func (api *API) DeleteDomainReservation(options *DomainDeleteOptions) error {
if !IsUUID(options.Ref) {
var err error
options.Ref, err = api.domainIDFromName(options.Ref)
if err != nil {
return err
}
}
_, err := api.makeRequest(http.MethodDelete, "/domains/"+options.Ref, nil)
if err != nil {
return err
}
return nil
}
func (api *API) domainIDFromName(domainName string) (id string, err error) {
domains, err := api.ListDomainReservations(&DomainListOptions{})
if err != nil {
return
}
for _, b := range domains {
if b.Domain == domainName {
return b.ID, nil
}
}
return "", fmt.Errorf("no such domain '%s'", domainName)
}