Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dex
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
dex
Commits
725a9421
Commit
725a9421
authored
Aug 05, 2016
by
Eric Chiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
storage: add storage with static clients
parent
3110f45c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
+103
-0
static_clients_test.go
storage/memory/static_clients_test.go
+48
-0
static_clients.go
storage/static_clients.go
+55
-0
No files found.
storage/memory/static_clients_test.go
0 → 100644
View file @
725a9421
package
memory
import
(
"reflect"
"testing"
"github.com/coreos/poke/storage"
)
func
TestStaticClients
(
t
*
testing
.
T
)
{
s
:=
New
()
c1
:=
storage
.
Client
{
ID
:
"foo"
,
Secret
:
"foo_secret"
}
c2
:=
storage
.
Client
{
ID
:
"bar"
,
Secret
:
"bar_secret"
}
s
.
CreateClient
(
c1
)
s2
:=
storage
.
WithStaticClients
(
s
,
[]
storage
.
Client
{
c2
})
tests
:=
[]
struct
{
id
string
s
storage
.
Storage
wantErr
bool
wantClient
storage
.
Client
}{
{
"foo"
,
s
,
false
,
c1
},
{
"bar"
,
s
,
true
,
storage
.
Client
{}},
{
"foo"
,
s2
,
true
,
storage
.
Client
{}},
{
"bar"
,
s2
,
false
,
c2
},
}
for
i
,
tc
:=
range
tests
{
gotClient
,
err
:=
tc
.
s
.
GetClient
(
tc
.
id
)
if
err
!=
nil
{
if
!
tc
.
wantErr
{
t
.
Errorf
(
"case %d: GetClient(%q) %v"
,
i
,
tc
.
id
,
err
)
}
continue
}
if
tc
.
wantErr
{
t
.
Errorf
(
"case %d: GetClient(%q) expected error"
,
i
,
tc
.
id
)
continue
}
if
!
reflect
.
DeepEqual
(
tc
.
wantClient
,
gotClient
)
{
t
.
Errorf
(
"case %d: expected=%#v got=%#v"
,
i
,
tc
.
wantClient
,
gotClient
)
}
}
}
storage/static_clients.go
0 → 100644
View file @
725a9421
package
storage
import
"errors"
// Tests for this code are in the "memory" package, since this package doesn't
// define a concrete storage implementation.
// staticClientsStorage is a storage that only allow read-only actions on clients.
// All read actions return from the list of clients stored in memory, not the
// underlying
type
staticClientsStorage
struct
{
Storage
// A read-only set of clients.
clients
[]
Client
clientsByID
map
[
string
]
Client
}
// WithStaticClients returns a storage with a read-only set of clients. Write actions,
// such as creating other clients, will fail.
//
// In the future the returned storage may allow creating and storing additional clients
// in the underlying storage.
func
WithStaticClients
(
s
Storage
,
staticClients
[]
Client
)
Storage
{
clientsByID
:=
make
(
map
[
string
]
Client
,
len
(
staticClients
))
for
_
,
client
:=
range
staticClients
{
clientsByID
[
client
.
ID
]
=
client
}
return
staticClientsStorage
{
s
,
staticClients
,
clientsByID
}
}
func
(
s
staticClientsStorage
)
GetClient
(
id
string
)
(
Client
,
error
)
{
if
client
,
ok
:=
s
.
clientsByID
[
id
];
ok
{
return
client
,
nil
}
return
Client
{},
ErrNotFound
}
func
(
s
staticClientsStorage
)
ListClients
()
([]
Client
,
error
)
{
clients
:=
make
([]
Client
,
len
(
s
.
clients
))
copy
(
clients
,
s
.
clients
)
return
clients
,
nil
}
func
(
s
staticClientsStorage
)
CreateClient
(
c
Client
)
error
{
return
errors
.
New
(
"static clients: read-only cannot create client"
)
}
func
(
s
staticClientsStorage
)
DeleteClient
(
id
string
)
error
{
return
errors
.
New
(
"static clients: read-only cannot delete client"
)
}
func
(
s
staticClientsStorage
)
UpdateClient
(
id
string
,
updater
func
(
old
Client
)
(
Client
,
error
))
error
{
return
errors
.
New
(
"static clients: read-only cannot update client"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment