/
policy.go
147 lines (115 loc) · 4.41 KB
/
policy.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package policy
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-09-01/policy"
)
func getPolicyDefinitionByDisplayName(ctx context.Context, client *policy.DefinitionsClient, displayName, managementGroupName string) (policy.Definition, error) {
var policyDefinitions policy.DefinitionListResultIterator
var err error
if managementGroupName != "" {
policyDefinitions, err = client.ListByManagementGroupComplete(ctx, managementGroupName)
} else {
policyDefinitions, err = client.ListComplete(ctx)
}
if err != nil {
return policy.Definition{}, fmt.Errorf("failed to load Policy Definition List: %+v", err)
}
var results []policy.Definition
for policyDefinitions.NotDone() {
def := policyDefinitions.Value()
if def.DisplayName != nil && *def.DisplayName == displayName && def.ID != nil {
results = append(results, def)
}
if err := policyDefinitions.NextWithContext(ctx); err != nil {
return policy.Definition{}, fmt.Errorf("failed to load Policy Definition List: %s", err)
}
}
// we found none
if len(results) == 0 {
return policy.Definition{}, fmt.Errorf("failed to load Policy Definition List: could not find policy '%s'", displayName)
}
// we found more than one
if len(results) > 1 {
return policy.Definition{}, fmt.Errorf("failed to load Policy Definition List: found more than one policy '%s'", displayName)
}
return results[0], nil
}
func getPolicyDefinitionByName(ctx context.Context, client *policy.DefinitionsClient, name string, managementGroupName string) (res policy.Definition, err error) {
if managementGroupName == "" {
res, err = client.Get(ctx, name)
} else {
res, err = client.GetAtManagementGroup(ctx, name, managementGroupName)
}
return res, err
}
func getPolicySetDefinitionByName(ctx context.Context, client *policy.SetDefinitionsClient, name string, managementGroupID string) (res policy.SetDefinition, err error) {
if managementGroupID == "" {
res, err = client.Get(ctx, name)
} else {
res, err = client.GetAtManagementGroup(ctx, name, managementGroupID)
}
return res, err
}
func getPolicySetDefinitionByDisplayName(ctx context.Context, client *policy.SetDefinitionsClient, displayName, managementGroupID string) (policy.SetDefinition, error) {
var setDefinitions policy.SetDefinitionListResultIterator
var err error
if managementGroupID != "" {
setDefinitions, err = client.ListByManagementGroupComplete(ctx, managementGroupID)
} else {
setDefinitions, err = client.ListComplete(ctx)
}
if err != nil {
return policy.SetDefinition{}, fmt.Errorf("failed to load Policy Set Definition List: %+v", err)
}
var results []policy.SetDefinition
for setDefinitions.NotDone() {
def := setDefinitions.Value()
if def.DisplayName != nil && *def.DisplayName == displayName && def.ID != nil {
results = append(results, def)
}
if err := setDefinitions.NextWithContext(ctx); err != nil {
return policy.SetDefinition{}, fmt.Errorf("failed to load Policy Set Definition List: %s", err)
}
}
// throw error when we found none
if len(results) == 0 {
return policy.SetDefinition{}, fmt.Errorf("failed to load Policy Set Definition List: could not find policy '%s'", displayName)
}
// throw error when we found more than one
if len(results) > 1 {
return policy.SetDefinition{}, fmt.Errorf("failed to load Policy Set Definition List: found more than one policy set definition '%s'", displayName)
}
return results[0], nil
}
func expandParameterDefinitionsValueFromString(jsonString string) (map[string]*policy.ParameterDefinitionsValue, error) {
var result map[string]*policy.ParameterDefinitionsValue
err := json.Unmarshal([]byte(jsonString), &result)
return result, err
}
func flattenParameterDefintionsValueToString(input map[string]*policy.ParameterDefinitionsValue) (string, error) {
if len(input) == 0 {
return "", nil
}
result, err := json.Marshal(input)
if err != nil {
return "", err
}
return string(result), nil
}
func expandParameterValuesValueFromString(jsonString string) (map[string]*policy.ParameterValuesValue, error) {
var result map[string]*policy.ParameterValuesValue
err := json.Unmarshal([]byte(jsonString), &result)
return result, err
}
func flattenParameterValuesValueToString(input map[string]*policy.ParameterValuesValue) (string, error) {
if len(input) == 0 {
return "", nil
}
result, err := json.Marshal(input)
if err != nil {
return "", err
}
return string(result), nil
}