-
Notifications
You must be signed in to change notification settings - Fork 604
/
certificate_authorities.go
82 lines (64 loc) · 2.97 KB
/
certificate_authorities.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
type ListCertificateAuthoritiesHostnameAssociationsParams struct {
MTLSCertificateID string `url:"mtls_certificate_id,omitempty"`
}
type UpdateCertificateAuthoritiesHostnameAssociationsParams struct {
Hostnames []HostnameAssociation `json:"hostnames,omitempty"`
MTLSCertificateID string `json:"mtls_certificate_id,omitempty"`
}
type HostnameAssociationsUpdateRequest struct {
Hostnames []HostnameAssociation `json:"hostnames,omitempty"`
MTLSCertificateID string `json:"mtls_certificate_id,omitempty"`
}
type HostnameAssociationsResponse struct {
Response
Result HostnameAssociations `json:"result"`
}
type HostnameAssociations struct {
Hostnames []HostnameAssociation `json:"hostnames"`
}
type HostnameAssociation = string
// List Hostname Associations
//
// API Reference: https://developers.cloudflare.com/api/resources/certificate_authorities/subresources/hostname_associations/methods/get/
func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params ListCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) {
if rc.Level != ZoneRouteLevel {
return []HostnameAssociation{}, ErrRequiredZoneLevelResourceContainer
}
uri := buildURI(fmt.Sprintf("/zones/%s/certificate_authorities/hostname_associations", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var hostnameAssociationsResponse HostnameAssociationsResponse
err = json.Unmarshal(res, &hostnameAssociationsResponse)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return hostnameAssociationsResponse.Result.Hostnames, nil
}
// Replace Hostname Associations
//
// API Reference: https://developers.cloudflare.com/api/resources/certificate_authorities/subresources/hostname_associations/methods/update/
func (api *API) UpdateCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params UpdateCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) {
if rc.Level != ZoneRouteLevel {
return []HostnameAssociation{}, ErrRequiredZoneLevelResourceContainer
}
uri := fmt.Sprintf("/zones/%s/certificate_authorities/hostname_associations", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}
var hostnameAssociationsResponse HostnameAssociationsResponse
err = json.Unmarshal(res, &hostnameAssociationsResponse)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return hostnameAssociationsResponse.Result.Hostnames, nil
}