-
Notifications
You must be signed in to change notification settings - Fork 77
/
site.go
119 lines (99 loc) · 3.73 KB
/
site.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
package porcelain
import (
"fmt"
"time"
"github.com/cenkalti/backoff"
"github.com/netlify/open-api/go/models"
"github.com/netlify/open-api/go/plumbing/operations"
"github.com/netlify/open-api/go/porcelain/context"
)
// CustomTLSCertificate holds information
// about custom TLS certificates.
type CustomTLSCertificate struct {
Certificate string
Key string
CACertificates string
}
// ListSites lists the sites a user has access to.
func (n *Netlify) ListSites(ctx context.Context, params *operations.ListSitesParams) ([]*models.Site, error) {
resp, err := n.Netlify.Operations.ListSites(params, context.GetAuthInfo(ctx))
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// GetSite returns a site.
func (n *Netlify) GetSite(ctx context.Context, siteID string) (*models.Site, error) {
authInfo := context.GetAuthInfo(ctx)
resp, err := n.Netlify.Operations.GetSite(operations.NewGetSiteParams().WithSiteID(siteID), authInfo)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// CreateSite creates a new site.
func (n *Netlify) CreateSite(ctx context.Context, site *models.SiteSetup, configureDNS bool) (*models.Site, error) {
authInfo := context.GetAuthInfo(ctx)
params := operations.NewCreateSiteParams().WithSite(site).WithConfigureDNS(&configureDNS)
resp, err := n.Netlify.Operations.CreateSite(params, authInfo)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// UpdateSite modifies an existent site.
func (n *Netlify) UpdateSite(ctx context.Context, site *models.SiteSetup) (*models.Site, error) {
authInfo := context.GetAuthInfo(ctx)
params := operations.NewUpdateSiteParams().WithSite(site).WithSiteID(site.ID)
resp, err := n.Netlify.Operations.UpdateSite(params, authInfo)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// ConfigureSiteTLSCertificate provisions a TLS certificate for a site with a custom domain.
// It uses Let's Encrypt if the certificate is empty.
func (n *Netlify) ConfigureSiteTLSCertificate(ctx context.Context, siteID string, cert *CustomTLSCertificate) (*models.SniCertificate, error) {
authInfo := context.GetAuthInfo(ctx)
params := operations.NewProvisionSiteTLSCertificateParams().WithSiteID(siteID)
if cert != nil {
params = params.WithCertificate(&cert.Certificate).WithKey(&cert.Key).WithCaCertificates(&cert.CACertificates)
}
resp, err := n.Netlify.Operations.ProvisionSiteTLSCertificate(params, authInfo)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// GetSiteTLSCertificate shows the TLS certificate configured for a site.
func (n *Netlify) GetSiteTLSCertificate(ctx context.Context, siteID string) (*models.SniCertificate, error) {
authInfo := context.GetAuthInfo(ctx)
params := operations.NewShowSiteTLSCertificateParams().WithSiteID(siteID)
resp, err := n.Netlify.Operations.ShowSiteTLSCertificate(params, authInfo)
if err != nil {
return nil, err
}
return resp.Payload, nil
}
// WaitUntilTLSCertificateReady checks the state of a site's certificate.
// It waits until the state is "issued", for Let's Encrypt certificates
// or "custom", which means that the certificate was provided by the user.
func (n *Netlify) WaitUntilTLSCertificateReady(ctx context.Context, siteID string, cert *models.SniCertificate) (*models.SniCertificate, error) {
if cert != nil && (cert.State == "issued" || cert.State == "custom") {
return cert, nil
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 2 * time.Minute
err := backoff.Retry(func() error {
var err error
cert, err = n.GetSiteTLSCertificate(ctx, siteID)
if err != nil {
return err
}
if cert.State != "issued" && cert.State != "custom" {
return fmt.Errorf("certificate for site %s is not ready yet", siteID)
}
return nil
}, b)
return cert, err
}