owner mapping -> user resource mapping

pull/10616/head
Jade McGough 2018-07-30 14:46:30 -07:00
parent faf5408a7b
commit 1896364c7c
6 changed files with 111 additions and 100 deletions

View File

@ -1,19 +0,0 @@
package platform
import "encoding/hex"
// Owner represents a resource owner
type Owner struct {
ID ID
}
// Decode parses b as a hex-encoded byte-slice-string.
func (o *Owner) Decode(b []byte) error {
dst := make([]byte, hex.DecodedLen(len(b)))
_, err := hex.Decode(dst, b)
if err != nil {
return err
}
o.ID = dst
return nil
}

View File

@ -1,35 +0,0 @@
package platform
import (
"context"
"errors"
)
// OwnerMappingService provides a mapping between resources and their owners
type OwnerMappingService interface {
CreateOwnerMapping(ctx context.Context, m *OwnerMapping) error
DeleteOwnerMapping(ctx context.Context, resourceID ID, owner Owner) error
}
// OwnerMapping represents a mapping of a resource to its owner
type OwnerMapping struct {
ResourceID ID `json:"resource_id"`
Owner Owner `json:"owner_id"`
}
// Validate reports any validation errors for the mapping.
func (m OwnerMapping) Validate() error {
if len(m.ResourceID) == 0 {
return errors.New("ResourceID is required")
}
if len(m.Owner.ID) == 0 {
return errors.New("An Owner with an ID is required")
}
return nil
}
// OwnerMappingFilter represents a set of filters that restrict the returned results.
type OwnerMappingFilter struct {
ResourceID ID
Owner *Owner
}

View File

@ -1,45 +0,0 @@
package platform
import (
"testing"
)
func TestOwnerMappingValidate(t *testing.T) {
type fields struct {
ResourceID ID
Owner Owner
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "mapping requires a resourceid",
fields: fields{
Owner: Owner{
ID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
},
},
wantErr: true,
},
{
name: "mapping requires an Owner",
fields: fields{
ResourceID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := OwnerMapping{
ResourceID: tt.fields.ResourceID,
Owner: tt.fields.Owner,
}
if err := m.Validate(); (err != nil) != tt.wantErr {
t.Errorf("OwnerMapping.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -8,7 +8,7 @@ type Task struct {
Organization ID `json:"organizationId"`
Name string `json:"name"`
Status string `json:"status"`
Owner User `json:"owner"`
Owner User `json:"owner"`
Flux string `json:"flux"`
Every string `json:"every,omitempty"`
Cron string `json:"cron,omitempty"`

46
user_resource_mapping.go Normal file
View File

@ -0,0 +1,46 @@
package platform
import (
"context"
"errors"
)
type UserType string
const (
Owner UserType = "owner"
Member UserType = "member"
)
// UserResourceMappingService maps the relationships between users and resources
type UserResourceMappingService interface {
CreateUserResourceMapping(ctx context.Context, m *UserResourceMapping) error
DeleteUserResourceMapping(ctx context.Context, resourceID ID, userID ID) error
}
// UserResourceMapping represents a mapping of a resource to its user
type UserResourceMapping struct {
ResourceID ID `json:"resource_id"`
UserID ID `json:"user_id"`
UserType UserType `json:"user_type"`
}
// Validate reports any validation errors for the mapping.
func (m UserResourceMapping) Validate() error {
if len(m.ResourceID) == 0 {
return errors.New("ResourceID is required")
}
if len(m.UserID) == 0 {
return errors.New("UserID is required")
}
if m.UserType != Owner && m.UserType != Member {
return errors.New("A valid user type is required")
}
return nil
}
// UserResourceMapping represents a set of filters that restrict the returned results.
type UserResourceMappingFilter struct {
ResourceID ID
UserID ID
}

View File

@ -0,0 +1,64 @@
package platform
import (
"testing"
)
func TestOwnerMappingValidate(t *testing.T) {
type fields struct {
ResourceID ID
UserID ID
UserType UserType
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "mapping requires a resourceid",
fields: fields{
UserID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
UserType: Owner,
},
wantErr: true,
},
{
name: "mapping requires an Owner",
fields: fields{
ResourceID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
UserType: Owner,
},
wantErr: true,
},
{
name: "mapping requires a usertype",
fields: fields{
ResourceID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
UserID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
},
wantErr: true,
},
{
name: "the usertype provided must be valid",
fields: fields{
ResourceID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
UserID: []byte{0xde, 0xba, 0xc1, 0xe0, 0xde, 0xad, 0xbe, 0xef},
UserType: "foo",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := UserResourceMapping{
ResourceID: tt.fields.ResourceID,
UserID: tt.fields.UserID,
UserType: tt.fields.UserType,
}
if err := m.Validate(); (err != nil) != tt.wantErr {
t.Errorf("OwnerMapping.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}