Skip to content

Commit

Permalink
fix: refactor all resources to use shared register methods (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws authored Jul 8, 2024
1 parent cc86a67 commit 39332d4
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 253 deletions.
31 changes: 10 additions & 21 deletions nitric/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package nitric

import (
"context"
"net/http"
"path"
"strings"
Expand All @@ -42,7 +41,7 @@ type route struct {
path string
api *api
middleware handler.HttpMiddleware
manager *manager
manager Manager
}

func composeRouteMiddleware(apiMiddleware handler.HttpMiddleware, routeMiddleware []handler.HttpMiddleware) handler.HttpMiddleware {
Expand Down Expand Up @@ -179,23 +178,21 @@ type ApiDetails struct {
type api struct {
name string
routes map[string]Route
manager *manager
manager Manager
securityRules map[string]interface{}
security []OidcOptions
path string
middleware handler.HttpMiddleware
}

func (m *manager) newApi(name string, opts ...ApiOption) (Api, error) {
rsc, err := m.resourceServiceClient()
if err != nil {
return nil, err
}

// NewApi Registers a new API Resource.
//
// The returned API object can be used to register Routes and Methods, with Handlers.
func NewApi(name string, opts ...ApiOption) (Api, error) {
a := &api{
name: name,
routes: map[string]Route{},
manager: m,
manager: defaultManager,
}

// Apply options
Expand All @@ -218,9 +215,8 @@ func (m *manager) newApi(name string, opts ...ApiOption) (Api, error) {
}
}
}

// declare resource
_, err = rsc.Declare(context.TODO(), &resourcev1.ResourceDeclareRequest{
result := <-defaultManager.registerResource(&resourcev1.ResourceDeclareRequest{
Id: &resourcev1.ResourceIdentifier{
Name: name,
Type: resourcev1.ResourceType_Api,
Expand All @@ -229,20 +225,13 @@ func (m *manager) newApi(name string, opts ...ApiOption) (Api, error) {
Api: apiResource,
},
})
if err != nil {
return nil, err
if result.Err != nil {
return nil, result.Err
}

return a, nil
}

// NewApi Registers a new API Resource.
//
// The returned API object can be used to register Routes and Methods, with Handlers.
func NewApi(name string, opts ...ApiOption) (Api, error) {
return defaultManager.newApi(name, opts...)
}

// Get adds a Get method handler to the path with any specified opts.
// Note: to chain middleware use handler.ComposeHttpMiddlware()
func (a *api) Get(match string, handler handler.HttpMiddleware, opts ...MethodOption) {
Expand Down
56 changes: 24 additions & 32 deletions nitric/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package nitric

import (
"context"
"fmt"
"strings"

Expand All @@ -29,8 +28,9 @@ import (
type BucketPermission string

type bucket struct {
name string
manager Manager
name string
manager Manager
registerChan <-chan RegisterResult
}

type Bucket interface {
Expand All @@ -49,42 +49,29 @@ var BucketEverything []BucketPermission = []BucketPermission{BucketRead, BucketW
// NewBucket register this bucket as a required resource for the calling function/container and
// register the permissions required by the currently scoped function for this resource.
func NewBucket(name string) Bucket {
return &bucket{
bucket := &bucket{
name: name,
manager: defaultManager,
}
}

func (b *bucket) Allow(permission BucketPermission, permissions ...BucketPermission) (storage.Bucket, error) {
allPerms := append([]BucketPermission{permission}, permissions...)

return defaultManager.newBucket(b.name, allPerms...)
}

func (m *manager) newBucket(name string, permissions ...BucketPermission) (storage.Bucket, error) {
rsc, err := m.resourceServiceClient()
if err != nil {
return nil, err
}

res := &v1.ResourceIdentifier{
Type: v1.ResourceType_Bucket,
Name: name,
}

dr := &v1.ResourceDeclareRequest{
Id: res,
bucket.registerChan = defaultManager.registerResource(&v1.ResourceDeclareRequest{
Id: &v1.ResourceIdentifier{
Type: v1.ResourceType_Bucket,
Name: name,
},
Config: &v1.ResourceDeclareRequest_Bucket{
Bucket: &v1.BucketResource{},
},
}
_, err = rsc.Declare(context.Background(), dr)
if err != nil {
return nil, err
}
})

return bucket
}

func (b *bucket) Allow(permission BucketPermission, permissions ...BucketPermission) (storage.Bucket, error) {
allPerms := append([]BucketPermission{permission}, permissions...)

actions := []v1.Action{}
for _, perm := range permissions {
for _, perm := range allPerms {
switch perm {
case BucketRead:
actions = append(actions, v1.Action_BucketFileGet, v1.Action_BucketFileList)
Expand All @@ -97,7 +84,12 @@ func (m *manager) newBucket(name string, permissions ...BucketPermission) (stora
}
}

_, err = rsc.Declare(context.Background(), functionResourceDeclareRequest(res, actions))
registerResult := <-b.registerChan
if registerResult.Err != nil {
return nil, registerResult.Err
}

m, err := b.manager.registerPolicy(registerResult.Identifier, actions...)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +101,7 @@ func (m *manager) newBucket(name string, permissions ...BucketPermission) (stora
}
}

return m.storage.Bucket(name), nil
return m.storage.Bucket(b.name), nil
}

func (b *bucket) On(notificationType handler.BlobEventType, notificationPrefixFilter string, middleware ...handler.BlobEventMiddleware) {
Expand Down
62 changes: 28 additions & 34 deletions nitric/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package nitric

import (
"context"
"fmt"

"github.com/nitrictech/go-sdk/api/keyvalue"
Expand All @@ -37,48 +36,37 @@ type KvStore interface {
}

type kvstore struct {
name string
manager Manager
name string
manager Manager
registerChan <-chan RegisterResult
}

func NewKv(name string) *kvstore {
return &kvstore{
name: name,
manager: defaultManager,
kvstore := &kvstore{
name: name,
manager: defaultManager,
registerChan: make(chan RegisterResult),
}

kvstore.registerChan = defaultManager.registerResource(&v1.ResourceDeclareRequest{
Id: &v1.ResourceIdentifier{
Type: v1.ResourceType_KeyValueStore,
Name: name,
},
Config: &v1.ResourceDeclareRequest_KeyValueStore{
KeyValueStore: &v1.KeyValueStoreResource{},
},
})

return kvstore
}

// NewQueue registers this queue as a required resource for the calling function/container.
func (k *kvstore) Allow(permission KvStorePermission, permissions ...KvStorePermission) (keyvalue.Store, error) {
allPerms := append([]KvStorePermission{permission}, permissions...)

return defaultManager.newKv(k.name, allPerms...)
}

func (m *manager) newKv(name string, permissions ...KvStorePermission) (keyvalue.Store, error) {
rsc, err := m.resourceServiceClient()
if err != nil {
return nil, err
}

colRes := &v1.ResourceIdentifier{
Type: v1.ResourceType_KeyValueStore,
Name: name,
}

dr := &v1.ResourceDeclareRequest{
Id: colRes,
Config: &v1.ResourceDeclareRequest_KeyValueStore{
KeyValueStore: &v1.KeyValueStoreResource{},
},
}
_, err = rsc.Declare(context.Background(), dr)
if err != nil {
return nil, err
}

actions := []v1.Action{}
for _, perm := range permissions {
for _, perm := range allPerms {
switch perm {
case KvStoreGet:
actions = append(actions, v1.Action_KeyValueStoreRead)
Expand All @@ -91,7 +79,13 @@ func (m *manager) newKv(name string, permissions ...KvStorePermission) (keyvalue
}
}

_, err = rsc.Declare(context.Background(), functionResourceDeclareRequest(colRes, actions))
registerResult := <-k.registerChan

if registerResult.Err != nil {
return nil, registerResult.Err
}

m, err := k.manager.registerPolicy(registerResult.Identifier, actions...)
if err != nil {
return nil, err
}
Expand All @@ -103,5 +97,5 @@ func (m *manager) newKv(name string, permissions ...KvStorePermission) (keyvalue
}
}

return m.kvstores.Store(name), nil
return m.kvstores.Store(k.name), nil
}
55 changes: 26 additions & 29 deletions nitric/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ type Manager interface {
Run() error
addWorker(name string, s workers.Worker)
resourceServiceClient() (v1.ResourcesClient, error)
registerResource(request *v1.ResourceDeclareRequest, registerResourceChan chan RegisterResult)
registerResource(request *v1.ResourceDeclareRequest) <-chan RegisterResult
registerPolicy(res *v1.ResourceIdentifier, actions ...v1.Action) (*manager, error)

newApi(name string, opts ...ApiOption) (Api, error)
newBucket(name string, permissions ...BucketPermission) (storage.Bucket, error)
newSecret(name string, permissions ...SecretPermission) (secrets.SecretRef, error)
newQueue(name string, permissions ...QueuePermission) (queues.Queue, error)
newSchedule(name string) Schedule
newWebsocket(socket string) (Websocket, error)
newKv(name string, permissions ...KvStorePermission) (keyvalue.Store, error)
newOidcSecurityDefinition(apiName string, options OidcOptions) (OidcSecurityDefinition, error)
}

type RegisterResult struct {
Expand Down Expand Up @@ -110,31 +101,37 @@ func (m *manager) resourceServiceClient() (v1.ResourcesClient, error) {
return m.rsc, nil
}

func (m *manager) registerResource(request *v1.ResourceDeclareRequest, registerResourceChan chan RegisterResult) {
rsc, err := m.resourceServiceClient()
if err != nil {
registerResourceChan <- RegisterResult{
Err: err,
Identifier: nil,
func (m *manager) registerResource(request *v1.ResourceDeclareRequest) <-chan RegisterResult {
registerResourceChan := make(chan RegisterResult)

go func() {
rsc, err := m.resourceServiceClient()
if err != nil {
registerResourceChan <- RegisterResult{
Err: err,
Identifier: nil,
}

return
}

return
}
_, err = rsc.Declare(context.Background(), request)
if err != nil {
registerResourceChan <- RegisterResult{
Err: err,
Identifier: nil,
}

_, err = rsc.Declare(context.Background(), request)
if err != nil {
registerResourceChan <- RegisterResult{
Err: err,
Identifier: nil,
return
}

return
}
registerResourceChan <- RegisterResult{
Err: nil,
Identifier: request.Id,
}
}()

registerResourceChan <- RegisterResult{
Err: nil,
Identifier: request.Id,
}
return registerResourceChan
}

func (m *manager) registerPolicy(res *v1.ResourceIdentifier, actions ...v1.Action) (*manager, error) {
Expand Down
Loading

0 comments on commit 39332d4

Please sign in to comment.