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 API support for TotalTLS #1105

Merged
merged 3 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/1105.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
total_tls: adds support for TotalTLS
```
63 changes: 63 additions & 0 deletions total_tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

type TotalTLS struct {
Enabled *bool `json:"enabled,omitempty"`
CertificateAuthority string `json:"certificate_authority,omitempty"`
ValidityDays int `json:"validity_days,omitempty"`
}

type TotalTLSResponse struct {
Response
Result TotalTLS `json:"result"`
}

// GetTotalTLS Get Total TLS Settings for a Zone.
//
// API Reference: https://api.cloudflare.com/#total-tls-total-tls-settings-details
func (api *API) GetTotalTLS(ctx context.Context, rc *ResourceContainer) (TotalTLS, error) {
if rc.Identifier == "" {
return TotalTLS{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/acm/total_tls", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TotalTLS{}, err
}

var r TotalTLSResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TotalTLS{}, err
}

return r.Result, nil
}

// SetTotalTLS Set Total TLS Settings or disable the feature for a Zone.
//
// API Reference: https://api.cloudflare.com/#total-tls-enable-or-disable-total-tls
func (api *API) SetTotalTLS(ctx context.Context, rc *ResourceContainer, params TotalTLS) (TotalTLS, error) {
if rc.Identifier == "" {
return TotalTLS{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/acm/total_tls", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return TotalTLS{}, err
}

var r TotalTLSResponse
err = json.Unmarshal(res, &r)
if err != nil {
return TotalTLS{}, err
}

return r.Result, nil
}
74 changes: 74 additions & 0 deletions total_tls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

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

mux.HandleFunc(fmt.Sprintf("/zones/%s/acm/total_tls", testZoneID), 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": {
"enabled": true,
"certificate_authority": "google",
"validity_days": 90
}
}`)
})

_, err := client.GetTotalTLS(context.Background(), ZoneIdentifier(""))
if assert.Error(t, err) {
assert.Equal(t, ErrMissingZoneID, err)
}

result, err := client.GetTotalTLS(context.Background(), ZoneIdentifier(testZoneID))
if assert.NoError(t, err) {
assert.Equal(t, BoolPtr(true), result.Enabled)
assert.Equal(t, "google", result.CertificateAuthority)
assert.Equal(t, 90, result.ValidityDays)
}
}

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

mux.HandleFunc(fmt.Sprintf("/zones/%s/acm/total_tls", testZoneID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"enabled": true,
"certificate_authority": "google",
"validity_days": 90
}
}`)
})

_, err := client.SetTotalTLS(context.Background(), ZoneIdentifier(""), TotalTLS{})
if assert.Error(t, err) {
assert.Equal(t, ErrMissingZoneID, err)
}

result, err := client.SetTotalTLS(context.Background(), ZoneIdentifier(testZoneID), TotalTLS{CertificateAuthority: "google", Enabled: BoolPtr(true)})
if assert.NoError(t, err) {
assert.Equal(t, BoolPtr(true), result.Enabled)
assert.Equal(t, "google", result.CertificateAuthority)
assert.Equal(t, 90, result.ValidityDays)
}
}