Skip to content

Commit

Permalink
bugfix: certificate_authorities: add mtls_certificate_id to query params
Browse files Browse the repository at this point in the history
  • Loading branch information
ebisso committed Dec 17, 2024
1 parent 8d86952 commit bd029a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
24 changes: 14 additions & 10 deletions certificate_authorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type UpdateCertificateAuthoritiesHostnameAssociationsParams struct {
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 []HostnameAssociation `json:"result"`
Expand All @@ -28,12 +33,11 @@ type HostnameAssociation = string
//
// 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 := fmt.Sprintf(
"/%s/%s/certificate_authorities/hostname_associations",
rc.Level,
rc.Identifier,
)
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 {
Expand All @@ -53,11 +57,11 @@ func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Conte
//
// 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) {
uri := fmt.Sprintf(
"/%s/%s/certificate_authorities/hostname_associations",
rc.Level,
rc.Identifier,
)
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 {
Expand Down
13 changes: 13 additions & 0 deletions certificate_authorities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"testing"

"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)

Expand All @@ -15,6 +16,7 @@ func TestListCertificateAuthoritiesHostnameAssociations(t *testing.T) {

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, "72ef4d06-4752-4493-a60a-7421470fd585", r.URL.Query().Get("mtls_certificate_id"))
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
Expand Down Expand Up @@ -51,6 +53,16 @@ func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) {

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)

wantReqHostnames := []HostnameAssociation{
"admin.example.com",
"foobar.example.com",
}
var req HostnameAssociationsUpdateRequest
assert.NoError(t, json.NewDecoder(r.Body).Decode(&req))
assert.Equal(t, "72ef4d06-4752-4493-a60a-7421470fd585", req.MTLSCertificateID)
assert.Equal(t, wantReqHostnames, req.Hostnames)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
Expand All @@ -64,6 +76,7 @@ func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) {
}

hostnameAssociations := UpdateCertificateAuthoritiesHostnameAssociationsParams{
MTLSCertificateID: "72ef4d06-4752-4493-a60a-7421470fd585",
Hostnames: []HostnameAssociation{
"admin.example.com",
"foobar.example.com",
Expand Down

0 comments on commit bd029a5

Please sign in to comment.