From 6359a4e3171ce6d33d07c8315c4361aa6ebce6a8 Mon Sep 17 00:00:00 2001 From: Yawar Jamal Date: Fri, 9 Aug 2024 16:49:44 -0500 Subject: [PATCH] add automatic ssl mode setting support --- zone.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- zone_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/zone.go b/zone.go index de92880a1579..7be43400a515 100644 --- a/zone.go +++ b/zone.go @@ -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"` @@ -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 @@ -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 diff --git a/zone_test.go b/zone_test.go index d8b94cd1c656..459e871fcf84 100644 --- a/zone_test.go +++ b/zone_test.go @@ -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()