-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdomains.go
185 lines (158 loc) · 6.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package scalingo
import (
"context"
"errors"
"time"
"gopkg.in/errgo.v1"
)
type DomainsService interface {
DomainsList(ctx context.Context, app string) ([]Domain, error)
DomainsAdd(ctx context.Context, app string, d DomainsAddParams) (Domain, error)
DomainsUpdate(ctx context.Context, app string, id string, d DomainsUpdateParams) (Domain, error)
DomainsRemove(ctx context.Context, app string, id string) error
// DomainsUpdate helpers
DomainSetCanonical(ctx context.Context, app, id string) (Domain, error)
DomainUnsetCanonical(ctx context.Context, app string) (Domain, error)
DomainSetCertificate(ctx context.Context, app, id, tlsCert, tlsKey string) (Domain, error)
DomainUnsetCertificate(ctx context.Context, app, id string) (Domain, error)
}
var _ DomainsService = (*Client)(nil)
type LetsEncryptStatus string
const (
LetsEncryptStatusPendingDNS LetsEncryptStatus = "pending_dns"
LetsEncryptStatusNew LetsEncryptStatus = "new"
LetsEncryptStatusCreated LetsEncryptStatus = "created"
LetsEncryptStatusDNSRequired LetsEncryptStatus = "dns_required"
LetsEncryptStatusError LetsEncryptStatus = "error"
)
type SslStatus string
const (
SslStatusPendingDNS SslStatus = "pending"
SslStatusNew SslStatus = "error"
SslStatusCreated SslStatus = "success"
)
type ACMEErrorVariables struct {
DNSProvider string `json:"dns_provider"`
Variables []string `json:"variables"`
}
type Domain struct {
ID string `json:"id"`
AppID string `json:"app_id"`
Name string `json:"name"`
TLSCert string `json:"tlscert,omitempty"`
TLSKey string `json:"tlskey,omitempty"`
// LetsEncryptEnabled is true (default) if automatic certificate generation
// is enabled
LetsEncryptEnabled bool `json:"letsencrypt_enabled"`
SSL bool `json:"ssl"`
Validity time.Time `json:"validity"`
Canonical bool `json:"canonical"`
// LetsEncrypt is true if the certificate is generated by LetsEncrypt
LetsEncrypt bool `json:"letsencrypt"`
// LetsEncryptStatus is the status of the LetsEncrypt certificate generation
LetsEncryptStatus LetsEncryptStatus `json:"letsencrypt_status"`
SslStatus SslStatus `json:"ssl_status"`
AcmeDNSFqdn string `json:"acme_dns_fqdn"`
AcmeDNSValue string `json:"acme_dns_value"`
AcmeDNSError ACMEErrorVariables `json:"acme_dns_error"`
}
// domainUnsetCertificateParams is the params of the request to unset the domain certificate.
// We need a dedicated structure rather than using `Domain` so that we remove omitempty from the JSON tags.
type domainUnsetCertificateParams struct {
TLSCert string `json:"tlscert"`
TLSKey string `json:"tlskey"`
}
type DomainsRes struct {
Domains []Domain `json:"domains"`
}
type DomainRes struct {
Domain Domain `json:"domain"`
}
func (c *Client) DomainsList(ctx context.Context, app string) ([]Domain, error) {
var domainRes DomainsRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "domains", nil, &domainRes)
if err != nil {
return nil, errgo.Notef(err, "fail to list the domains")
}
return domainRes.Domains, nil
}
type DomainsAddParams struct {
Name string `json:"name"`
Canonical *bool `json:"canonical,omitempty"`
TLSCert *string `json:"tlscert,omitempty"`
TLSKey *string `json:"tlskey,omitempty"`
LetsEncryptEnabled *bool `json:"letsencrypt_enabled,omitempty"`
}
func (c *Client) DomainsAdd(ctx context.Context, app string, params DomainsAddParams) (Domain, error) {
var domainRes DomainRes
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "domains", map[string]DomainsAddParams{"domain": params}, &domainRes)
if err != nil {
return Domain{}, errgo.Notef(err, "fail to add a domain")
}
return domainRes.Domain, nil
}
func (c *Client) DomainsRemove(ctx context.Context, app, id string) error {
return c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "domains", id)
}
func (c *Client) DomainsShow(ctx context.Context, app, id string) (Domain, error) {
var domainRes DomainRes
err := c.ScalingoAPI().SubresourceGet(ctx, "apps", app, "domains", id, nil, &domainRes)
if err != nil {
return Domain{}, errgo.Notef(err, "fail to show the domain")
}
return domainRes.Domain, nil
}
type DomainsUpdateParams struct {
Canonical *bool `json:"canonical,omitempty"`
TLSCert *string `json:"tlscert,omitempty"`
TLSKey *string `json:"tlskey,omitempty"`
LetsEncryptEnabled *bool `json:"letsencrypt_enabled,omitempty"`
}
func (c *Client) DomainsUpdate(ctx context.Context, app, id string, params DomainsUpdateParams) (Domain, error) {
var domainRes DomainRes
err := c.ScalingoAPI().SubresourceUpdate(ctx, "apps", app, "domains", id, map[string]DomainsUpdateParams{"domain": params}, &domainRes)
if err != nil {
return Domain{}, errgo.Notef(err, "fail to update the domain")
}
return domainRes.Domain, nil
}
func (c *Client) DomainSetCertificate(ctx context.Context, app, id, tlsCert, tlsKey string) (Domain, error) {
domain, err := c.DomainsUpdate(ctx, app, id, DomainsUpdateParams{TLSCert: &tlsCert, TLSKey: &tlsKey})
if err != nil {
return Domain{}, errgo.Notef(err, "fail to set the domain certificate")
}
return domain, nil
}
func (c *Client) DomainUnsetCertificate(ctx context.Context, app, id string) (Domain, error) {
empty := ""
domain, err := c.DomainsUpdate(ctx, app, id, DomainsUpdateParams{TLSCert: &empty, TLSKey: &empty})
if err != nil {
return Domain{}, errgo.Notef(err, "fail to unset the domain certificate")
}
return domain, nil
}
func (c *Client) DomainSetCanonical(ctx context.Context, app, id string) (Domain, error) {
value := true
domain, err := c.DomainsUpdate(ctx, app, id, DomainsUpdateParams{Canonical: &value})
if err != nil {
return Domain{}, errgo.Notef(err, "fail to set the domain as canonical")
}
return domain, nil
}
func (c *Client) DomainUnsetCanonical(ctx context.Context, app string) (Domain, error) {
domains, err := c.DomainsList(ctx, app)
if err != nil {
return Domain{}, errgo.Notef(err, "fail to list the domains to unset the canonical one")
}
for _, domain := range domains {
if domain.Canonical {
value := false
domain, err := c.DomainsUpdate(ctx, app, domain.ID, DomainsUpdateParams{Canonical: &value})
if err != nil {
return Domain{}, errgo.Notef(err, "fail to unset the domain as canonical")
}
return domain, nil
}
}
return Domain{}, errors.New("no canonical domain configured")
}