-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1105 from Cyb3r-Jak3/master
Add API support for TotalTLS
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
total_tls: adds support for TotalTLS | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |