Update options usage in store/api

This commit is contained in:
Asim Aslam 2019-10-23 22:31:36 +01:00
parent 3fc04f4dff
commit d65658c890
5 changed files with 57 additions and 36 deletions

View File

@ -11,8 +11,7 @@ import (
"github.com/go-acme/lego/v3/providers/dns/cloudflare" "github.com/go-acme/lego/v3/providers/dns/cloudflare"
"github.com/mholt/certmagic" "github.com/mholt/certmagic"
"github.com/micro/go-micro/api/server/acme" "github.com/micro/go-micro/api/server/acme"
"github.com/micro/go-micro/config/options" cfstore "github.com/micro/go-micro/store/cloudflare"
cloudflarestorage "github.com/micro/go-micro/store/cloudflare"
"github.com/micro/go-micro/sync/lock/memory" "github.com/micro/go-micro/sync/lock/memory"
) )
@ -22,7 +21,7 @@ func TestCertMagic(t *testing.T) {
} }
l, err := New().NewListener() l, err := New().NewListener()
if err != nil { if err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} }
l.Close() l.Close()
@ -34,7 +33,7 @@ func TestCertMagic(t *testing.T) {
p, err := cloudflare.NewDNSProviderConfig(c) p, err := cloudflare.NewDNSProviderConfig(c)
if err != nil { if err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} }
l, err = New(acme.AcceptToS(true), l, err = New(acme.AcceptToS(true),
@ -43,7 +42,7 @@ func TestCertMagic(t *testing.T) {
).NewListener() ).NewListener()
if err != nil { if err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} }
l.Close() l.Close()
} }
@ -56,10 +55,10 @@ func TestStorageImplementation(t *testing.T) {
} }
var s certmagic.Storage var s certmagic.Storage
st, err := cloudflarestorage.New( st, err := cfstore.NewStore(
options.WithValue("CF_API_TOKEN", apiToken), cfstore.ApiToken(apiToken),
options.WithValue("CF_ACCOUNT_ID", accountID), cfstore.AccountID(accountID),
options.WithValue("KV_NAMESPACE_ID", kvID), cfstore.Namespace(kvID),
) )
if err != nil { if err != nil {
t.Fatalf("Couldn't initialise cloudflare storage: %s\n", err.Error()) t.Fatalf("Couldn't initialise cloudflare storage: %s\n", err.Error())
@ -71,12 +70,12 @@ func TestStorageImplementation(t *testing.T) {
// Test Lock // Test Lock
if err := s.Lock("test"); err != nil { if err := s.Lock("test"); err != nil {
t.Error(err) t.Fatal(err)
} }
// Test Unlock // Test Unlock
if err := s.Unlock("test"); err != nil { if err := s.Unlock("test"); err != nil {
t.Error(err) t.Fatal(err)
} }
// Test data // Test data
@ -107,17 +106,17 @@ func TestStorageImplementation(t *testing.T) {
// Test Store // Test Store
for _, d := range testdata { for _, d := range testdata {
if err := s.Store(d.key, d.value); err != nil { if err := s.Store(d.key, d.value); err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} }
} }
// Test Load // Test Load
for _, d := range testdata { for _, d := range testdata {
if value, err := s.Load(d.key); err != nil { if value, err := s.Load(d.key); err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} else { } else {
if !reflect.DeepEqual(value, d.value) { if !reflect.DeepEqual(value, d.value) {
t.Errorf("Load %s: expected %v, got %v", d.key, d.value, value) t.Fatalf("Load %s: expected %v, got %v", d.key, d.value, value)
} }
} }
} }
@ -125,13 +124,13 @@ func TestStorageImplementation(t *testing.T) {
// Test Exists // Test Exists
for _, d := range testdata { for _, d := range testdata {
if !s.Exists(d.key) { if !s.Exists(d.key) {
t.Errorf("%s should exist, but doesn't\n", d.key) t.Fatalf("%s should exist, but doesn't\n", d.key)
} }
} }
// Test List // Test List
if list, err := s.List("/", true); err != nil { if list, err := s.List("/", true); err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} else { } else {
var expected []string var expected []string
for i, d := range testdata { for i, d := range testdata {
@ -143,16 +142,16 @@ func TestStorageImplementation(t *testing.T) {
sort.Strings(expected) sort.Strings(expected)
sort.Strings(list) sort.Strings(list)
if !reflect.DeepEqual(expected, list) { if !reflect.DeepEqual(expected, list) {
t.Errorf("List: Expected %v, got %v\n", expected, list) t.Fatalf("List: Expected %v, got %v\n", expected, list)
} }
} }
if list, err := s.List("/foo", false); err != nil { if list, err := s.List("/foo", false); err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} else { } else {
sort.Strings(list) sort.Strings(list)
expected := []string{"/foo/a", "/foo/b", "/foo/bar", "/foo/c", "/foo/d"} expected := []string{"/foo/a", "/foo/b", "/foo/bar", "/foo/c", "/foo/d"}
if !reflect.DeepEqual(expected, list) { if !reflect.DeepEqual(expected, list) {
t.Errorf("List: expected %s, got %s\n", expected, list) t.Fatalf("List: expected %s, got %s\n", expected, list)
} }
} }
@ -160,16 +159,16 @@ func TestStorageImplementation(t *testing.T) {
for _, d := range testdata { for _, d := range testdata {
info, err := s.Stat(d.key) info, err := s.Stat(d.key)
if err != nil { if err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} else { } else {
if info.Key != d.key { if info.Key != d.key {
t.Errorf("Stat().Key: expected %s, got %s\n", d.key, info.Key) t.Fatalf("Stat().Key: expected %s, got %s\n", d.key, info.Key)
} }
if info.Size != int64(len(d.value)) { if info.Size != int64(len(d.value)) {
t.Errorf("Stat().Size: expected %d, got %d\n", len(d.value), info.Size) t.Fatalf("Stat().Size: expected %d, got %d\n", len(d.value), info.Size)
} }
if time.Since(info.Modified) > time.Minute { if time.Since(info.Modified) > time.Minute {
t.Errorf("Stat().Modified: expected time since last modified to be < 1 minute, got %v\n", time.Since(info.Modified)) t.Fatalf("Stat().Modified: expected time since last modified to be < 1 minute, got %v\n", time.Since(info.Modified))
} }
} }
@ -178,7 +177,7 @@ func TestStorageImplementation(t *testing.T) {
// Test Delete // Test Delete
for _, d := range testdata { for _, d := range testdata {
if err := s.Delete(d.key); err != nil { if err := s.Delete(d.key); err != nil {
t.Error(err.Error()) t.Fatal(err.Error())
} }
} }
@ -196,10 +195,10 @@ func TestE2e(t *testing.T) {
} }
testLock := memory.NewLock() testLock := memory.NewLock()
testStore, err := cloudflarestorage.New( testStore, err := cfstore.NewStore(
options.WithValue("CF_API_TOKEN", apiToken), cfstore.ApiToken(apiToken),
options.WithValue("CF_ACCOUNT_ID", accountID), cfstore.AccountID(accountID),
options.WithValue("KV_NAMESPACE_ID", kvID), cfstore.Namespace(kvID),
) )
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())

View File

@ -32,7 +32,7 @@ type workersKV struct {
// Options expects CF_API_TOKEN to a cloudflare API token scoped to Workers KV, // 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 // 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. // KV_NAMESPACE_ID to contain the namespace UUID for your KV storage.
func New(opts ...options.Option) (store.Store, error) { func NewStore(opts ...options.Option) (store.Store, error) {
// Validate Options // Validate Options
options := options.NewOptions(opts...) options := options.NewOptions(opts...)
apiToken, ok := options.Values().Get("CF_API_TOKEN") apiToken, ok := options.Values().Get("CF_API_TOKEN")

View File

@ -7,7 +7,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/store" "github.com/micro/go-micro/store"
) )
@ -21,10 +20,10 @@ func TestCloudflare(t *testing.T) {
randomK := strconv.Itoa(rand.Int()) randomK := strconv.Itoa(rand.Int())
randomV := strconv.Itoa(rand.Int()) randomV := strconv.Itoa(rand.Int())
wkv, err := New( wkv, err := NewStore(
options.WithValue("CF_API_TOKEN", apiToken), ApiToken(apiToken),
options.WithValue("CF_ACCOUNT_ID", accountID), AccountID(accountID),
options.WithValue("KV_NAMESPACE_ID", kvID), Namespace(kvID),
) )
if err != nil { if err != nil {

View File

@ -0,0 +1,23 @@
package cloudflare
import (
"github.com/micro/go-micro/config/options"
)
// Token sets the cloudflare api token
func ApiToken(t string) options.Option {
// TODO: change to store.cf.api_token
return options.WithValue("CF_API_TOKEN", t)
}
// AccountID sets the cloudflare account id
func AccountID(id string) options.Option {
// TODO: change to store.cf.account_id
return options.WithValue("CF_ACCOUNT_ID", id)
}
// Namespace sets the KV namespace
func Namespace(ns string) options.Option {
// TODO: change to store.cf.namespace
return options.WithValue("KV_NAMESPACE_ID", ns)
}