-
Notifications
You must be signed in to change notification settings - Fork 1
/
entitlement.go
73 lines (61 loc) · 1.89 KB
/
entitlement.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
70
71
72
73
package entitlement
import (
"fmt"
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"google.golang.org/protobuf/proto"
)
type EntitlementOption func(*v2.Entitlement)
func WithAnnotation(msgs ...proto.Message) EntitlementOption {
return func(e *v2.Entitlement) {
annos := annotations.Annotations(e.Annotations)
for _, msg := range msgs {
annos.Append(msg)
}
e.Annotations = annos
}
}
func WithGrantableTo(grantableTo ...*v2.ResourceType) EntitlementOption {
return func(g *v2.Entitlement) {
g.GrantableTo = grantableTo
}
}
func WithDisplayName(displayName string) EntitlementOption {
return func(g *v2.Entitlement) {
g.DisplayName = displayName
}
}
func WithDescription(description string) EntitlementOption {
return func(g *v2.Entitlement) {
g.Description = description
}
}
func NewEntitlementID(resource *v2.Resource, permission string) string {
return fmt.Sprintf("%s:%s:%s", resource.Id.ResourceType, resource.Id.Resource, permission)
}
func NewPermissionEntitlement(resource *v2.Resource, name string, entitlementOptions ...EntitlementOption) *v2.Entitlement {
entitlement := &v2.Entitlement{
Id: NewEntitlementID(resource, name),
DisplayName: name,
Slug: name,
Purpose: v2.Entitlement_PURPOSE_VALUE_PERMISSION,
Resource: resource,
}
for _, entitlementOption := range entitlementOptions {
entitlementOption(entitlement)
}
return entitlement
}
func NewAssignmentEntitlement(resource *v2.Resource, name string, entitlementOptions ...EntitlementOption) *v2.Entitlement {
entitlement := &v2.Entitlement{
Id: NewEntitlementID(resource, name),
DisplayName: name,
Slug: name,
Purpose: v2.Entitlement_PURPOSE_VALUE_ASSIGNMENT,
Resource: resource,
}
for _, entitlementOption := range entitlementOptions {
entitlementOption(entitlement)
}
return entitlement
}