Repository: cloudflare/cloudflare-go
Pull request #1105 (2022-10-15 15:15:25)

Add API support for TotalTLS

## Description
Adds API support for [TotalTLS ](https://api.cloudflare.com/#total-tls-properties). Needed for https://github.com/cloudflare/terraform-provider-cloudflare/issues/1967

## Has your change been tested?

Created new unit tests and they passed

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.





Patch:
diff --git a/.changelog/1105.txt b/.changelog/1105.txt
new file mode 100644
index 000000000..a8631dec8
--- /dev/null
+++ b/.changelog/1105.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+total_tls: adds support for TotalTLS
+```
diff --git a/total_tls.go b/total_tls.go
new file mode 100644
index 000000000..98772e8b3
--- /dev/null
+++ b/total_tls.go
@@ -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
+}


Test patch:
diff --git a/total_tls_test.go b/total_tls_test.go
new file mode 100644
index 000000000..3b5e23963
--- /dev/null
+++ b/total_tls_test.go
@@ -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)
+	}
+}