-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
user_resource_mapping.go
69 lines (60 loc) · 2.25 KB
/
user_resource_mapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package platform
import (
"context"
"errors"
"fmt"
)
type UserType string
type ResourceType string
// available user resource types.
const (
Owner UserType = "owner"
Member UserType = "member"
DashboardResourceType ResourceType = "dashboard"
BucketResourceType ResourceType = "bucket"
TaskResourceType ResourceType = "task"
OrgResourceType ResourceType = "org"
ViewResourceType ResourceType = "view"
TelegrafResourceType ResourceType = "telegraf"
)
// UserResourceMappingService maps the relationships between users and resources
type UserResourceMappingService interface {
// FindUserResourceMappings returns a list of UserResourceMappings that match filter and the total count of matching mappings.
FindUserResourceMappings(ctx context.Context, filter UserResourceMappingFilter, opt ...FindOptions) ([]*UserResourceMapping, int, error)
// CreateUserResourceMapping creates a user resource mapping
CreateUserResourceMapping(ctx context.Context, m *UserResourceMapping) error
// DeleteUserResourceMapping deletes a user resource mapping
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"`
ResourceType ResourceType `json:"resource_type"`
UserID ID `json:"user_id"`
UserType UserType `json:"user_type"`
}
// Validate reports any validation errors for the mapping.
func (m UserResourceMapping) Validate() error {
if !m.ResourceID.Valid() {
return errors.New("resourceID is required")
}
if !m.UserID.Valid() {
return errors.New("userID is required")
}
if m.UserType != Owner && m.UserType != Member {
return errors.New("a valid user type is required")
}
switch m.ResourceType {
case DashboardResourceType, BucketResourceType, TaskResourceType, OrgResourceType, ViewResourceType, TelegrafResourceType:
default:
return fmt.Errorf("a valid resource type is required")
}
return nil
}
// UserResourceMapping represents a set of filters that restrict the returned results.
type UserResourceMappingFilter struct {
ResourceID ID
ResourceType ResourceType
UserID ID
UserType UserType
}