Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic ssl mode setting support #2855

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2855.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-notes:enhancement
ssl: add support for ssl/tls automatic mode setting
```
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a method for fetching/updating zone settings (https://github.com/cloudflare/cloudflare-go/blob/master/zone.go#L958). is there a reason we cannot reuse that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, however, there is a slight difference in the response we return for this specific setting. As it returns a field called next_scheduled_scan: https://github.com/cloudflare/cloudflare-go/pull/2855/files#diff-ceb3700215079abfa8dfc51b35af780ed6292f4747db0d4710c6c72de18a0a66R178

Also I think the return type here should reference the ZoneAutomaticSSLModeSetting struct vs ZoneSSLSetting. Will fix that typo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could always put it in a pointer with omitempty which will conditionally set it when it is provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the generated version of the libraries, these endpoints are flattened into a single struct so that would make sense to reflect here too if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use the existing Get/UpdateZoneSettings construct and added the field as part of the ZoneSetting struct with omitempty markup

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
Loading