further cleanup

This commit is contained in:
Asim Aslam 2019-10-23 22:54:55 +01:00
parent 3ce71e12ff
commit 70aaca9876
3 changed files with 49 additions and 57 deletions

View File

@ -55,14 +55,11 @@ func TestStorageImplementation(t *testing.T) {
}
var s certmagic.Storage
st, err := cfstore.NewStore(
st := cfstore.NewStore(
cfstore.ApiToken(apiToken),
cfstore.AccountID(accountID),
cfstore.Namespace(kvID),
)
if err != nil {
t.Fatalf("Couldn't initialise cloudflare storage: %s\n", err.Error())
}
s = &storage{
lock: memory.NewLock(),
store: st,
@ -195,14 +192,11 @@ func TestE2e(t *testing.T) {
}
testLock := memory.NewLock()
testStore, err := cfstore.NewStore(
testStore := cfstore.NewStore(
cfstore.ApiToken(apiToken),
cfstore.AccountID(accountID),
cfstore.Namespace(kvID),
)
if err != nil {
t.Fatal(err.Error())
}
testStorage := NewStorage(testLock, testStore)
conf := cloudflare.NewDefaultConfig()

View File

@ -21,7 +21,9 @@ import (
"github.com/pkg/errors"
)
const apiBaseURL = "https://api.cloudflare.com/client/v4/"
const (
apiBaseURL = "https://api.cloudflare.com/client/v4/"
)
type workersKV struct {
options.Options
@ -35,49 +37,6 @@ type workersKV struct {
httpClient *http.Client
}
// New returns a cloudflare Store implementation.
// Options expects CF_API_TOKEN to a cloudflare API token scoped to Workers KV,
// CF_ACCOUNT_ID to contain a string with your cloudflare account ID and
// KV_NAMESPACE_ID to contain the namespace UUID for your KV storage.
func NewStore(opts ...options.Option) (store.Store, error) {
// Validate Options
options := options.NewOptions(opts...)
var account, token, namespace string
apiToken, ok := options.Values().Get("CF_API_TOKEN")
if !ok {
log.Fatal("Store: No CF_API_TOKEN passed as an option")
}
if token, ok = apiToken.(string); !ok {
log.Fatal("Store: Option CF_API_TOKEN contains a non-string")
}
accountID, ok := options.Values().Get("CF_ACCOUNT_ID")
if !ok {
log.Fatal("Store: No CF_ACCOUNT_ID passed as an option")
}
if account, ok = accountID.(string); !ok {
log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string")
}
uuid, ok := options.Values().Get("KV_NAMESPACE_ID")
if !ok {
log.Fatal("Store: No KV_NAMESPACE_ID passed as an option")
}
if namespace, ok = uuid.(string); !ok {
log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string")
}
return &workersKV{
account: account,
namespace: namespace,
token: token,
Options: options,
httpClient: &http.Client{},
}, nil
}
// In the cloudflare workers KV implemention, List() doesn't guarantee
// anything as the workers API is eventually consistent.
func (w *workersKV) List() ([]*store.Record, error) {
@ -296,3 +255,46 @@ type apiMessage struct {
Code int `json:"code"`
Message string `json:"message"`
}
// New returns a cloudflare Store implementation.
// Options expects CF_API_TOKEN to a cloudflare API token scoped to Workers KV,
// CF_ACCOUNT_ID to contain a string with your cloudflare account ID and
// KV_NAMESPACE_ID to contain the namespace UUID for your KV storage.
func NewStore(opts ...options.Option) store.Store {
// Validate Options
options := options.NewOptions(opts...)
var account, token, namespace string
apiToken, ok := options.Values().Get("CF_API_TOKEN")
if !ok {
log.Fatal("Store: No CF_API_TOKEN passed as an option")
}
if token, ok = apiToken.(string); !ok {
log.Fatal("Store: Option CF_API_TOKEN contains a non-string")
}
accountID, ok := options.Values().Get("CF_ACCOUNT_ID")
if !ok {
log.Fatal("Store: No CF_ACCOUNT_ID passed as an option")
}
if account, ok = accountID.(string); !ok {
log.Fatal("Store: Option CF_ACCOUNT_ID contains a non-string")
}
uuid, ok := options.Values().Get("KV_NAMESPACE_ID")
if !ok {
log.Fatal("Store: No KV_NAMESPACE_ID passed as an option")
}
if namespace, ok = uuid.(string); !ok {
log.Fatal("Store: Option KV_NAMESPACE_ID contains a non-string")
}
return &workersKV{
account: account,
namespace: namespace,
token: token,
Options: options,
httpClient: &http.Client{},
}
}

View File

@ -20,16 +20,12 @@ func TestCloudflare(t *testing.T) {
randomK := strconv.Itoa(rand.Int())
randomV := strconv.Itoa(rand.Int())
wkv, err := NewStore(
wkv := NewStore(
ApiToken(apiToken),
AccountID(accountID),
Namespace(kvID),
)
if err != nil {
t.Fatal(err.Error())
}
records, err := wkv.List()
if err != nil {
t.Fatalf("List: %s\n", err.Error())