-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathteams_certificates.go
More file actions
158 lines (127 loc) · 5.44 KB
/
Copy pathteams_certificates.go
File metadata and controls
158 lines (127 loc) · 5.44 KB
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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
type TeamsCertificate struct {
InUse *bool `json:"in_use"`
ID string `json:"id"`
BindingStatus string `json:"binding_status"`
QsPackId string `json:"qs_pack_id"`
Type string `json:"type"`
UpdatedAt *time.Time `json:"updated_at"`
UploadedOn *time.Time `json:"uploaded_on"`
CreatedAt *time.Time `json:"created_at"`
ExpiresOn *time.Time `json:"expires_on"`
}
type TeamsCertificateCreateRequest struct {
ValidityPeriodDays int `json:"validity_period_days,omitempty"`
}
const DEFAULT_VALIDITY_PERIOD_DAYS = 1826
// TeamsCertificateResponse is the API response, containing a single certificate.
type TeamsCertificateResponse struct {
Response
Result TeamsCertificate `json:"result"`
}
// TeamsCertificatesResponse is the API response, containing an array of certificates.
type TeamsCertificatesResponse struct {
Response
Result []TeamsCertificate `json:"result"`
}
// TeamsCertificates returns all certificates in an account
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/list/
func (api *API) TeamsCertificates(ctx context.Context, accountID string) ([]TeamsCertificate, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []TeamsCertificate{}, err
}
var teamsCertificatesResponse TeamsCertificatesResponse
err = json.Unmarshal(res, &teamsCertificatesResponse)
if err != nil {
return []TeamsCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsCertificatesResponse.Result, nil
}
// TeamsCertificate returns teams account certificate.
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/get/
func (api *API) TeamsCertificate(ctx context.Context, accountID string, certificateId string) (TeamsCertificate, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates/%s", accountID, certificateId)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TeamsCertificate{}, err
}
var teamsCertificateResponse TeamsCertificateResponse
err = json.Unmarshal(res, &teamsCertificateResponse)
if err != nil {
return TeamsCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsCertificateResponse.Result, nil
}
// TeamsGenerateCertificate generates a new gateway managed certificate
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/create/
func (api *API) TeamsGenerateCertificate(ctx context.Context, accountID string, certificateRequest TeamsCertificateCreateRequest) (TeamsCertificate, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates", accountID)
if certificateRequest.ValidityPeriodDays == 0 {
certificateRequest.ValidityPeriodDays = DEFAULT_VALIDITY_PERIOD_DAYS
}
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, certificateRequest)
if err != nil {
return TeamsCertificate{}, err
}
var teamsCertResponse TeamsCertificateResponse
err = json.Unmarshal(res, &teamsCertResponse)
if err != nil {
return TeamsCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsCertResponse.Result, nil
}
// TeamsActivateCertificate activates a certificate
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/activate/
func (api *API) TeamsActivateCertificate(ctx context.Context, accountID string, certificateId string) (TeamsCertificate, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates/%s/activate", accountID, certificateId)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return TeamsCertificate{}, err
}
var teamsCertResponse TeamsCertificateResponse
err = json.Unmarshal(res, &teamsCertResponse)
if err != nil {
return TeamsCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsCertResponse.Result, nil
}
// TeamsDectivateCertificate deactivates a certificate
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/deactivate/
func (api *API) TeamsDeactivateCertificate(ctx context.Context, accountID string, certificateId string) (TeamsCertificate, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates/%s/deactivate", accountID, certificateId)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return TeamsCertificate{}, err
}
var teamsCertResponse TeamsCertificateResponse
err = json.Unmarshal(res, &teamsCertResponse)
if err != nil {
return TeamsCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsCertResponse.Result, nil
}
// TeamsDeleteCertificate deletes a certificate.
//
// API reference: https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/delete/
func (api *API) TeamsDeleteCertificate(ctx context.Context, accountID string, certificateId string) error {
uri := fmt.Sprintf("/accounts/%s/gateway/certificates/%s", accountID, certificateId)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}