-
Notifications
You must be signed in to change notification settings - Fork 84
/
staticgroup.go
89 lines (70 loc) · 2.13 KB
/
staticgroup.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tenantmapping
import (
"context"
"io/ioutil"
"strings"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
)
// StaticGroup missing godoc
type StaticGroup struct {
GroupName string `json:"groupname"`
Scopes []string `json:"scopes"`
}
// StaticGroups missing godoc
type StaticGroups []StaticGroup
// StaticGroupRepository missing godoc
//
//go:generate mockery --name=StaticGroupRepository --output=automock --outpkg=automock --case=underscore --disable-version-string
type StaticGroupRepository interface {
Get(ctx context.Context, groupnames []string) StaticGroups
}
type staticGroupRepository struct {
data map[string]StaticGroup
}
// NewStaticGroupRepository missing godoc
func NewStaticGroupRepository(srcPath string) (*staticGroupRepository, error) {
staticGroupsBytes, err := ioutil.ReadFile(srcPath)
if err != nil {
return nil, errors.Wrap(err, "while reading static groups file")
}
var staticGroups []StaticGroup
if err := yaml.UnmarshalStrict(staticGroupsBytes, &staticGroups, yaml.DisallowUnknownFields); err != nil {
return nil, errors.Wrap(err, "while unmarshalling static groups YAML")
}
data := make(map[string]StaticGroup)
for _, sg := range staticGroups {
data[sg.GroupName] = sg
}
return &staticGroupRepository{
data: data,
}, nil
}
// Get missing godoc
func (r *staticGroupRepository) Get(ctx context.Context, groupnames []string) StaticGroups {
result := []StaticGroup{}
for _, groupname := range groupnames {
if staticGroup, ok := r.data[groupname]; ok {
result = append(result, staticGroup)
} else {
log.C(ctx).Warnf("Static group with name %s not found", groupname)
}
}
return result
}
// GetGroupScopes get all scopes from group array, without duplicates
func (groups StaticGroups) GetGroupScopes() string {
scopeMap := make(map[string]bool)
filteredScopes := []string{}
for _, group := range groups {
for _, scope := range group.Scopes {
_, ok := scopeMap[scope]
if !ok {
filteredScopes = append(filteredScopes, scope)
scopeMap[scope] = true
}
}
}
return strings.Join(filteredScopes, " ")
}