-
Notifications
You must be signed in to change notification settings - Fork 6
/
defaultPolicyAssignmentValues.go
95 lines (81 loc) · 3.75 KB
/
defaultPolicyAssignmentValues.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
90
91
92
93
94
95
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package alzlib
import (
"maps"
"slices"
mapset "github.com/deckarep/golang-set/v2"
)
// PolicyAssignmentsParameterValues is a map of default names to DefaultPolicyAssignmentValuesValue.
// It is used to map a single value to multiple policy assignments.
type DefaultPolicyAssignmentValues map[string]DefaultPolicyAssignmentValuesValue
// DefaultPolicyAssignmentValuesValue is a map of assignments names to parameter names.
type DefaultPolicyAssignmentValuesValue struct {
assignment2Parameters map[string]mapset.Set[string]
description string
}
func NewDefaultPolicyAssignmentValuesValue(description string) DefaultPolicyAssignmentValuesValue {
return DefaultPolicyAssignmentValuesValue{
assignment2Parameters: make(map[string]mapset.Set[string]),
description: description,
}
}
// AssignmentParameterComboExists checks if a given assignment name and parameter name combination exists in the DefaultPolicyAssignmentValues.
// It iterates through each assignment in the DefaultPolicyAssignmentValues and checks if the assignment contains the specified assignment name.
// If the assignment contains the assignment name, it then checks if the assignment's parameters contain the specified parameter name.
// If the combination exists, it returns true. Otherwise, it returns false.
func (d DefaultPolicyAssignmentValues) AssignmentParameterComboExists(wantAssignmentName, wantParameterName string) bool {
for _, assignment := range d {
if parameters, exists := assignment.assignment2Parameters[wantAssignmentName]; exists && parameters.Contains(wantParameterName) {
return true
}
}
return false
}
// Add adds a new default policy assignment value to the DefaultPolicyAssignmentValues.
// It takes the defaultName, assignmentName, and parameterNames as input parameters.
// If the defaultName does not exist in the DefaultPolicyAssignmentValues, it creates a new entry.
// If the assignmentName does not exist under the defaultName, it creates a new entry.
// Finally, it appends the parameterNames to the assignmentName.
func (d DefaultPolicyAssignmentValues) Add(defaultName, assignmentName, description string, parameterNames ...string) {
if _, exists := d[defaultName]; !exists {
d[defaultName] = NewDefaultPolicyAssignmentValuesValue(description)
}
if _, exists := d[defaultName].assignment2Parameters[assignmentName]; !exists {
d[defaultName].assignment2Parameters[assignmentName] = mapset.NewThreadUnsafeSet[string]()
}
d[defaultName].assignment2Parameters[assignmentName].Append(parameterNames...)
}
func (d DefaultPolicyAssignmentValuesValue) copy() DefaultPolicyAssignmentValuesValue {
newVal := NewDefaultPolicyAssignmentValuesValue(d.description)
for k, v := range d.assignment2Parameters {
newVal.assignment2Parameters[k] = v.Clone()
}
return newVal
}
// Assignments returns a sorted list of assignment names.
func (d DefaultPolicyAssignmentValuesValue) Assignments() []string {
result := make([]string, 0, len(d.assignment2Parameters))
for s := range maps.Keys(d.assignment2Parameters) {
result = append(result, s)
}
slices.Sort(result)
return result
}
// Assignments returns a sorted list of parameter names.
func (d DefaultPolicyAssignmentValuesValue) AssignmentParameters(name string) []string {
if _, ok := d.assignment2Parameters[name]; !ok {
return nil
}
v := d.assignment2Parameters[name]
s := v.ToSlice()
slices.Sort(s)
return s
}
// Description returns the description of the DefaultPolicyAssignmentValuesValue.
func (d DefaultPolicyAssignmentValuesValue) Description() string {
return d.description
}
func (d DefaultPolicyAssignmentValuesValue) PolicyAssignment2ParameterMap() map[string]mapset.Set[string] {
return d.assignment2Parameters
}