-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3740 from ebisso/feat/certificate-authorities
Feat/certificate authorities
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
certificate_authorities: add new methods to interact with Certificate Authorities Hostname Associations API | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 HostnameAssociationsResponse struct { | ||
Response | ||
Result []HostnameAssociation `json:"result"` | ||
} | ||
|
||
type HostnameAssociation = string | ||
|
||
// List Hostname Associations | ||
// | ||
// API Reference: https://developers.cloudflare.com/api/operations/client-certificate-for-a-zone-list-hostname-associations | ||
func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params ListCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) { | ||
|
||
uri := fmt.Sprintf( | ||
"/%s/%s/certificate_authorities/hostname_associations", | ||
rc.Level, | ||
rc.Identifier, | ||
) | ||
|
||
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, nil | ||
} | ||
|
||
// Replace Hostname Associations | ||
// | ||
// API Reference: https://developers.cloudflare.com/api/operations/client-certificate-for-a-zone-put-hostname-associations | ||
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, | ||
) | ||
|
||
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, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestListCertificateAuthoritiesHostnameAssociations(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [ | ||
"admin.example.com", | ||
"foobar.example.com" | ||
] | ||
}`) | ||
} | ||
|
||
hostnameAssociations := ListCertificateAuthoritiesHostnameAssociationsParams{ | ||
MTLSCertificateID: "72ef4d06-4752-4493-a60a-7421470fd585", | ||
} | ||
|
||
want := []HostnameAssociation{ | ||
"admin.example.com", | ||
"foobar.example.com", | ||
} | ||
|
||
mux.HandleFunc("/zones/"+testZoneID+"/certificate_authorities/hostname_associations", handler) | ||
|
||
actual, err := client.ListCertificateAuthoritiesHostnameAssociations(context.Background(), testZoneRC, hostnameAssociations) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method) | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [ | ||
"admin.example.com", | ||
"foobar.example.com" | ||
] | ||
}`) | ||
} | ||
|
||
hostnameAssociations := UpdateCertificateAuthoritiesHostnameAssociationsParams{ | ||
Hostnames: []HostnameAssociation{ | ||
"admin.example.com", | ||
"foobar.example.com", | ||
}, | ||
} | ||
|
||
want := []HostnameAssociation{ | ||
"admin.example.com", | ||
"foobar.example.com", | ||
} | ||
|
||
mux.HandleFunc("/zones/"+testZoneID+"/certificate_authorities/hostname_associations", handler) | ||
|
||
actual, err := client.UpdateCertificateAuthoritiesHostnameAssociations(context.Background(), testZoneRC, hostnameAssociations) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} |