Move collection to a new file.

This commit is contained in:
Mariano Cano 2019-03-05 14:28:32 -08:00
parent 4b2b6ffe32
commit dd0376657c
2 changed files with 48 additions and 35 deletions

View file

@ -0,0 +1,41 @@
package provisioner
import (
"sync"
"github.com/pkg/errors"
)
// Collection is a memory map of provisioners.
type Collection struct {
byID *sync.Map
}
// NewCollection initializes a collection of provisioners.
func NewCollection() *Collection {
return &Collection{
byID: new(sync.Map),
}
}
// Load a provisioner by the ID.
func (c *Collection) Load(id string) (*Provisioner, bool) {
i, ok := c.byID.Load(id)
if !ok {
return nil, false
}
p, ok := i.(*Provisioner)
if !ok {
return nil, false
}
return p, true
}
// Store adds a provisioner to the collection, it makes sure two provisioner
// does not have the same ID.
func (c *Collection) Store(p *Provisioner) error {
if _, loaded := c.byID.LoadOrStore(p.ID(), p); loaded == false {
return errors.New("cannot add multiple provisioners with the same id")
}
return nil
}

View file

@ -3,7 +3,6 @@ package provisioner
import ( import (
"encoding/json" "encoding/json"
"strings" "strings"
"sync"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -44,6 +43,11 @@ func (p *Provisioner) ID() string {
return p.base.ID() return p.base.ID()
} }
// Type return the provisioners type.
func (p *Provisioner) Type() Type {
return p.typ
}
// Init initializes the base provisioner with the given claims. // Init initializes the base provisioner with the given claims.
func (p *Provisioner) Init(claims *Claims) error { func (p *Provisioner) Init(claims *Claims) error {
return p.base.Init(claims) return p.base.Init(claims)
@ -70,8 +74,10 @@ func (p *Provisioner) UnmarshalJSON(data []byte) error {
switch strings.ToLower(typ.Type) { switch strings.ToLower(typ.Type) {
case "jwt": case "jwt":
p.typ = TypeJWK
p.base = &JWT{} p.base = &JWT{}
case "oidc": case "oidc":
p.typ = TypeOIDC
p.base = &OIDC{} p.base = &OIDC{}
default: default:
return errors.New("provisioner type not supported") return errors.New("provisioner type not supported")
@ -81,37 +87,3 @@ func (p *Provisioner) UnmarshalJSON(data []byte) error {
} }
return nil return nil
} }
// Collection is a memory map of provisioners.
type Collection struct {
byID *sync.Map
}
// NewCollection initializes a collection of provisioners.
func NewCollection() *Collection {
return &Collection{
byID: new(sync.Map),
}
}
// Load a provisioner by the ID.
func (c *Collection) Load(id string) (*Provisioner, bool) {
i, ok := c.byID.Load(id)
if !ok {
return nil, false
}
p, ok := i.(*Provisioner)
if !ok {
return nil, false
}
return p, true
}
// Store adds a provisioner to the collection, it makes sure two provisioner
// does not have the same ID.
func (c *Collection) Store(p *Provisioner) error {
if _, loaded := c.byID.LoadOrStore(p.ID(), p); loaded == false {
return errors.New("cannot add multiple provisioners with the same id")
}
return nil
}