diff --git a/owner.go b/owner.go deleted file mode 100644 index 4687f1a68b..0000000000 --- a/owner.go +++ /dev/null @@ -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 -} diff --git a/owner_mapping.go b/owner_mapping.go deleted file mode 100644 index 2eae1e9e4d..0000000000 --- a/owner_mapping.go +++ /dev/null @@ -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 -} diff --git a/owner_mapping_test.go b/owner_mapping_test.go deleted file mode 100644 index 17b6df64e1..0000000000 --- a/owner_mapping_test.go +++ /dev/null @@ -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) - } - }) - } -} diff --git a/task.go b/task.go index c1cf3b5ce3..c5183c368c 100644 --- a/task.go +++ b/task.go @@ -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"` diff --git a/user_resource_mapping.go b/user_resource_mapping.go new file mode 100644 index 0000000000..40a8a73a54 --- /dev/null +++ b/user_resource_mapping.go @@ -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 +} diff --git a/user_resource_mapping_test.go b/user_resource_mapping_test.go new file mode 100644 index 0000000000..5010001974 --- /dev/null +++ b/user_resource_mapping_test.go @@ -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) + } + }) + } +}