Skip to content

Commit

Permalink
Add support for custom_hostnames status filters
Browse files Browse the repository at this point in the history
- Filter by `hostname_status`
- Filter by `ssl_status`
  • Loading branch information
adampurser-zendesk committed Nov 8, 2023
1 parent c674c62 commit d8fe799
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/1435.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
custom_hostname: Add support for `hostname_status` and `ssl_status` filters in CustomHostnames().
```
11 changes: 10 additions & 1 deletion custom_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (api *API) CreateCustomHostname(ctx context.Context, zoneID string, ch Cust
}

// CustomHostnames fetches custom hostnames for the given zone,
// by applying filter.Hostname if not empty and scoping the result to page'th 50 items.
// by applying filter and scoping the result to page'th 50 items.
//
// The returned ResultInfo can be used to implement pagination.
//
Expand All @@ -225,6 +225,15 @@ func (api *API) CustomHostnames(ctx context.Context, zoneID string, page int, fi
v.Set("page", strconv.Itoa(page))
if filter.Hostname != "" {
v.Set("hostname", filter.Hostname)
} else {
if filter.Status != "" {
v.Set("hostname_status", string(filter.Status))
}
if ssl := filter.SSL; ssl != nil {
if ssl.Status != "" {
v.Set("ssl_status", ssl.Status)
}
}
}

uri := fmt.Sprintf("/zones/%s/custom_hostnames?%s", zoneID, v.Encode())
Expand Down
156 changes: 156 additions & 0 deletions custom_hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,162 @@ func TestCustomHostname_CustomHostnames(t *testing.T) {
}
}

func TestCustomHostname_CustomHostnames_WithHostnameStatusQueryParam(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/zones/foo/custom_hostnames", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)

hostnameStatus := r.URL.Query().Get("hostname_status")
assert.Equal(t, "blocked", hostnameStatus, "Expected query param hostname_status 'blocked', got %s", hostnameStatus)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"result": [
{
"id": "custom_host_1",
"hostname": "custom.host.one",
"custom_metadata": {
"a_random_field": "random field value"
},
"status": "blocked",
"verification_errors": [
"None of the A or AAAA records are owned by this account and the pre-generated ownership verification token was not found."
],
"ownership_verification": {
"type": "txt",
"name": "_cf-custom-hostname.app.example.com",
"value": "5cc07c04-ea62-4a5a-95f0-419334a875a4"
}
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 1
}
}`)
})

customHostnames, _, err := client.CustomHostnames(context.Background(), "foo", 1, CustomHostname{
Status: BLOCKED,
})

want := []CustomHostname{
{
ID: "custom_host_1",
Hostname: "custom.host.one",
CustomMetadata: &CustomMetadata{"a_random_field": "random field value"},
Status: BLOCKED,
VerificationErrors: []string{"None of the A or AAAA records are owned " +
"by this account and the pre-generated ownership verification token was not found."},
OwnershipVerification: CustomHostnameOwnershipVerification{
Type: "txt",
Name: "_cf-custom-hostname.app.example.com",
Value: "5cc07c04-ea62-4a5a-95f0-419334a875a4",
},
},
}

if assert.NoError(t, err) {
assert.Equal(t, want, customHostnames)
}
}

func TestCustomHostname_CustomHostnames_WithSSLStatusQueryParam(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/zones/foo/custom_hostnames", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)

sslStatus := r.URL.Query().Get("ssl_status")
assert.Equal(t, "pending_validation", sslStatus, "Expected query param ssl_status 'pending_validation', got %s", sslStatus)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"result": [
{
"id": "custom_host_1",
"hostname": "custom.host.one",
"ssl": {
"id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
"type": "dv",
"method": "cname",
"status": "pending_validation",
"cname_target": "dcv.digicert.com",
"cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com",
"issuer": "DigiCertInc",
"serial_number": "6743787633689793699141714808227354901",
"http_url": "http://app.example.com/.well-known/pki-validation/ca3-da12a1c25e7b48cf80408c6c1763b8a2.txt",
"http_body": "ca3-574923932a82475cb8592200f1a2a23d"
},
"custom_metadata": {
"a_random_field": "random field value"
},
"status": "pending",
"verification_errors": [
"None of the A or AAAA records are owned by this account and the pre-generated ownership verification token was not found."
],
"ownership_verification": {
"type": "txt",
"name": "_cf-custom-hostname.app.example.com",
"value": "5cc07c04-ea62-4a5a-95f0-419334a875a4"
}
}
],
"result_info": {
"page": 1,
"per_page": 20,
"count": 1,
"total_count": 1
}
}`)
})

customHostnames, _, err := client.CustomHostnames(context.Background(), "foo", 1, CustomHostname{
SSL: &CustomHostnameSSL{Status: "pending_validation"},
})

want := []CustomHostname{
{
ID: "custom_host_1",
Hostname: "custom.host.one",
SSL: &CustomHostnameSSL{
ID: "0d89c70d-ad9f-4843-b99f-6cc0252067e9",
Type: "dv",
Method: "cname",
Status: "pending_validation",
SSLValidationRecord: SSLValidationRecord{
CnameTarget: "dcv.digicert.com",
CnameName: "810b7d5f01154524b961ba0cd578acc2.app.example.com",
HTTPUrl: "http://app.example.com/.well-known/pki-validation/ca3-da12a1c25e7b48cf80408c6c1763b8a2.txt",
HTTPBody: "ca3-574923932a82475cb8592200f1a2a23d",
},
Issuer: "DigiCertInc",
SerialNumber: "6743787633689793699141714808227354901",
},
CustomMetadata: &CustomMetadata{"a_random_field": "random field value"},
Status: PENDING,
VerificationErrors: []string{"None of the A or AAAA records are owned " +
"by this account and the pre-generated ownership verification token was not found."},
OwnershipVerification: CustomHostnameOwnershipVerification{
Type: "txt",
Name: "_cf-custom-hostname.app.example.com",
Value: "5cc07c04-ea62-4a5a-95f0-419334a875a4",
},
},
}

if assert.NoError(t, err) {
assert.Equal(t, want, customHostnames)
}
}

func TestCustomHostname_CustomHostname(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit d8fe799

Please sign in to comment.