Skip to content

Commit

Permalink
add automatic ssl mode setting support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawar Jamal committed Aug 9, 2024
1 parent 80d52bc commit 6359a4e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
50 changes: 49 additions & 1 deletion zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,22 @@ type ZoneSSLSetting struct {
CertificateStatus string `json:"certificate_status"`
}

// ZoneAutomaticSSLModeSetting contains automatic ssl mode
type ZoneAutomaticSSLModeSetting struct {
ID string `json:"id"`
Editable bool `json:"editable"`
ModifiedOn string `json:"modified_on"`
Value string `json:"value"`
NextScheduledScan string `json:"next_scheduled_scan"`
}

// ZoneAutomaticSSLModeSettingResponse represents the response from the Zone automatic SSL mode setting
type ZoneAutomaticSSLModeSettingResponse struct {
Response
Result ZoneAutomaticSSLModeSetting `json:"result"`
}

// ZoneSSLSettingResponse represents the response from the Zone SSL Setting
// endpoint.
type ZoneSSLSettingResponse struct {
Response
Result ZoneSSLSetting `json:"result"`
Expand Down Expand Up @@ -848,6 +862,23 @@ func (api *API) ZoneSSLSettings(ctx context.Context, zoneID string) (ZoneSSLSett
return r.Result, nil
}

// ZoneSSLSettings returns information about SSL setting to the specified zone.
//
// API reference: https://api.cloudflare.com/#zone-settings-get-automatic-ssl-mode-setting
func (api *API) ZoneAutomaticSSLModeSetting(ctx context.Context, zoneID string) (ZoneAutomaticSSLModeSetting, error) {
uri := fmt.Sprintf("/zones/%s/settings/ssl_automatic_mode", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZoneAutomaticSSLModeSetting{}, err
}
var r ZoneAutomaticSSLModeSettingResponse
err = json.Unmarshal(res, &r)
if err != nil {
return ZoneAutomaticSSLModeSetting{}, fmt.Errorf("%s: %s %w", errUnmarshalError, string(res), err)
}
return r.Result, nil
}

// UpdateZoneSSLSettings update information about SSL setting to the specified zone.
//
// API reference: https://api.cloudflare.com/#zone-settings-change-ssl-setting
Expand All @@ -865,6 +896,23 @@ func (api *API) UpdateZoneSSLSettings(ctx context.Context, zoneID string, sslVal
return r.Result, nil
}

// UpdateZoneAutomaticSSLSettingMode update information about Automatic SSL mode setting to the specified zone.
//
// API reference: https://api.cloudflare.com/[#-add-url-slug-here]
func (api *API) UpdateZoneAutomaticSSLSettingMode(ctx context.Context, zoneID, mode string) (ZoneSSLSetting, error) {
uri := fmt.Sprintf("/zones/%s/settings/ssl_automatic_mode", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, ZoneSSLSetting{Value: mode})
if err != nil {
return ZoneSSLSetting{}, err
}
var r ZoneSSLSettingResponse
err = json.Unmarshal(res, &r)
if err != nil {
return ZoneSSLSetting{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}

// FallbackOrigin returns information about the fallback origin for the specified zone.
//
// API reference: https://developers.cloudflare.com/ssl/ssl-for-saas/api-calls/#fallback-origin-configuration
Expand Down
53 changes: 53 additions & 0 deletions zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,59 @@ func TestUpdateZoneSSLSettings(t *testing.T) {
}
}

func TestUpdateZoneAutomaticSSLModeSetting(t *testing.T) {
setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)
w.Header().Set("content-type", "application/json")
// JSON data from: https://api.cloudflare.com/#zone-settings-properties
_, _ = fmt.Fprintf(w, `{
"result": {
"id": "ssl_automatic_mode",
"value": "auto",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z"
}
}`)
}
mux.HandleFunc("/zones/foo/settings/ssl_automatic_mode", handler)
s, err := client.UpdateZoneAutomaticSSLSettingMode(context.Background(), "foo", "auto")
if assert.NoError(t, err) {
assert.Equal(t, s.ID, "ssl_automatic_mode")
assert.Equal(t, s.Value, "auto")
assert.Equal(t, s.Editable, true)
assert.Equal(t, s.ModifiedOn, "2014-01-01T05:20:00.12345Z")
}
}

func TestGetZoneAutomaticSSLModeSetting(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, `{
"result": {
"id": "ssl_automatic_mode",
"value": "custom",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z",
"next_scheduled_scan": "2024-01-01T05:20:00.12345Z"
}
}`)
}
mux.HandleFunc("/zones/foo/settings/ssl_automatic_mode", handler)
s, err := client.ZoneAutomaticSSLModeSetting(context.Background(), "foo")
if assert.NoError(t, err) {
assert.Equal(t, s.ID, "ssl_automatic_mode")
assert.Equal(t, s.Value, "custom")
assert.Equal(t, s.Editable, true)
assert.Equal(t, s.ModifiedOn, "2014-01-01T05:20:00.12345Z")
assert.Equal(t, s.NextScheduledScan, "2024-01-01T05:20:00.12345Z")
}
}

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

0 comments on commit 6359a4e

Please sign in to comment.