From 70aaca987616b6b975ebe77faea8813b1f57fe32 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 23 Oct 2019 22:54:55 +0100 Subject: [PATCH] further cleanup --- api/server/acme/certmagic/certmagic_test.go | 10 +-- store/cloudflare/cloudflare.go | 90 +++++++++++---------- store/cloudflare/cloudflare_test.go | 6 +- 3 files changed, 49 insertions(+), 57 deletions(-) diff --git a/api/server/acme/certmagic/certmagic_test.go b/api/server/acme/certmagic/certmagic_test.go index 59afe557..2ff44b83 100644 --- a/api/server/acme/certmagic/certmagic_test.go +++ b/api/server/acme/certmagic/certmagic_test.go @@ -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() diff --git a/store/cloudflare/cloudflare.go b/store/cloudflare/cloudflare.go index 21e0f686..2a8e48ec 100644 --- a/store/cloudflare/cloudflare.go +++ b/store/cloudflare/cloudflare.go @@ -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{}, + } +} diff --git a/store/cloudflare/cloudflare_test.go b/store/cloudflare/cloudflare_test.go index 6b5a6815..dd2b1909 100644 --- a/store/cloudflare/cloudflare_test.go +++ b/store/cloudflare/cloudflare_test.go @@ -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())