-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathindexing_policy.go
300 lines (237 loc) · 8.28 KB
/
indexing_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package common
import (
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
func expandAzureRmCosmosDBIndexingPolicyIncludedPaths(input []interface{}) *[]documentdb.IncludedPath {
if len(input) == 0 {
return nil
}
var includedPaths []documentdb.IncludedPath
for _, v := range input {
includedPath := v.(map[string]interface{})
path := documentdb.IncludedPath{
Path: utils.String(includedPath["path"].(string)),
}
includedPaths = append(includedPaths, path)
}
return &includedPaths
}
func expandAzureRmCosmosDBIndexingPolicyExcludedPaths(input []interface{}) *[]documentdb.ExcludedPath {
if len(input) == 0 {
return nil
}
var paths []documentdb.ExcludedPath
for _, v := range input {
block := v.(map[string]interface{})
paths = append(paths, documentdb.ExcludedPath{
Path: utils.String(block["path"].(string)),
})
}
return &paths
}
func ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(input []interface{}) *[][]documentdb.CompositePath {
indexes := make([][]documentdb.CompositePath, 0)
for _, i := range input {
indexPairs := make([]documentdb.CompositePath, 0)
indexPair := i.(map[string]interface{})
for _, idxPair := range indexPair["index"].([]interface{}) {
data := idxPair.(map[string]interface{})
index := documentdb.CompositePath{
Path: utils.String(data["path"].(string)),
Order: documentdb.CompositePathSortOrder(strings.ToLower(data["order"].(string))),
}
indexPairs = append(indexPairs, index)
}
indexes = append(indexes, indexPairs)
}
return &indexes
}
func ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input []interface{}) *[]documentdb.SpatialSpec {
if len(input) == 0 || input[0] == nil {
return nil
}
indexes := make([]documentdb.SpatialSpec, 0)
// no matter what spatial types are updated, all types will be set and returned from service
spatialTypes := []documentdb.SpatialType{
documentdb.SpatialTypeLineString,
documentdb.SpatialTypeMultiPolygon,
documentdb.SpatialTypePoint,
documentdb.SpatialTypePolygon,
}
for _, i := range input {
indexPair := i.(map[string]interface{})
indexes = append(indexes, documentdb.SpatialSpec{
Types: &spatialTypes,
Path: utils.String(indexPair["path"].(string)),
})
}
return &indexes
}
func ExpandAzureRmCosmosDbIndexingPolicy(d *pluginsdk.ResourceData) *documentdb.IndexingPolicy {
i := d.Get("indexing_policy").([]interface{})
if len(i) == 0 || i[0] == nil {
return nil
}
input := i[0].(map[string]interface{})
policy := &documentdb.IndexingPolicy{}
policy.IndexingMode = documentdb.IndexingMode(strings.ToLower(input["indexing_mode"].(string)))
if v, ok := input["included_path"].([]interface{}); ok {
policy.IncludedPaths = expandAzureRmCosmosDBIndexingPolicyIncludedPaths(v)
}
if v, ok := input["excluded_path"].([]interface{}); ok {
policy.ExcludedPaths = expandAzureRmCosmosDBIndexingPolicyExcludedPaths(v)
}
if v, ok := input["composite_index"].([]interface{}); ok {
policy.CompositeIndexes = ExpandAzureRmCosmosDBIndexingPolicyCompositeIndexes(v)
}
policy.SpatialIndexes = ExpandAzureRmCosmosDBIndexingPolicySpatialIndexes(input["spatial_index"].([]interface{}))
return policy
}
func flattenCosmosDBIndexingPolicyExcludedPaths(input *[]documentdb.ExcludedPath) []interface{} {
if input == nil {
return nil
}
excludedPaths := make([]interface{}, 0)
for _, v := range *input {
// _etag is automatically added by the server and should be excluded on flattening
// as the user isn't setting it and it will show changes in state.
if *v.Path == "/\"_etag\"/?" {
continue
}
block := make(map[string]interface{})
block["path"] = v.Path
excludedPaths = append(excludedPaths, block)
}
return excludedPaths
}
func flattenCosmosDBIndexingPolicyCompositeIndex(input []documentdb.CompositePath) []interface{} {
if input == nil {
return []interface{}{}
}
indexPairs := make([]interface{}, 0)
for _, v := range input {
path := ""
if v.Path != nil {
path = *v.Path
}
block := make(map[string]interface{})
block["path"] = path
block["order"] = strings.Title(string(v.Order))
indexPairs = append(indexPairs, block)
}
return indexPairs
}
func FlattenCosmosDBIndexingPolicyCompositeIndexes(input *[][]documentdb.CompositePath) []interface{} {
if input == nil {
return []interface{}{}
}
indexes := make([]interface{}, 0)
for _, v := range *input {
block := make(map[string][]interface{})
block["index"] = flattenCosmosDBIndexingPolicyCompositeIndex(v)
indexes = append(indexes, block)
}
return indexes
}
func flattenCosmosDBIndexingPolicyIncludedPaths(input *[]documentdb.IncludedPath) []interface{} {
if input == nil {
return nil
}
includedPaths := make([]interface{}, 0)
for _, v := range *input {
block := make(map[string]interface{})
block["path"] = v.Path
includedPaths = append(includedPaths, block)
}
return includedPaths
}
func FlattenCosmosDBIndexingPolicySpatialIndexes(input *[]documentdb.SpatialSpec) []interface{} {
if input == nil {
return []interface{}{}
}
indexes := make([]interface{}, 0)
for _, v := range *input {
var path string
if v.Path != nil {
path = *v.Path
}
indexes = append(indexes, map[string]interface{}{
"path": path,
"types": flattenCosmosDBIndexingPolicySpatialIndexesTypes(v.Types),
})
}
return indexes
}
func flattenCosmosDBIndexingPolicySpatialIndexesTypes(input *[]documentdb.SpatialType) []interface{} {
if input == nil {
return nil
}
types := make([]interface{}, 0)
for _, v := range *input {
types = append(types, string(v))
}
return types
}
func FlattenAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPolicy) []interface{} {
results := make([]interface{}, 0)
if indexingPolicy == nil {
return results
}
result := make(map[string]interface{})
result["indexing_mode"] = strings.Title(string(indexingPolicy.IndexingMode))
result["included_path"] = flattenCosmosDBIndexingPolicyIncludedPaths(indexingPolicy.IncludedPaths)
result["excluded_path"] = flattenCosmosDBIndexingPolicyExcludedPaths(indexingPolicy.ExcludedPaths)
result["composite_index"] = FlattenCosmosDBIndexingPolicyCompositeIndexes(indexingPolicy.CompositeIndexes)
result["spatial_index"] = FlattenCosmosDBIndexingPolicySpatialIndexes(indexingPolicy.SpatialIndexes)
results = append(results, result)
return results
}
func ValidateAzureRmCosmosDbIndexingPolicy(indexingPolicy *documentdb.IndexingPolicy) error {
if indexingPolicy == nil {
return nil
}
// Ensure includedPaths or excludedPaths are not set if indexingMode is "None".
if indexingPolicy.IndexingMode == documentdb.IndexingModeNone {
if indexingPolicy.IncludedPaths != nil {
return fmt.Errorf("included_path must not be set if indexing_mode is %q", strings.Title(string(documentdb.IndexingModeNone)))
}
if indexingPolicy.ExcludedPaths != nil {
return fmt.Errorf("excluded_path must not be set if indexing_mode is %q", strings.Title(string(documentdb.IndexingModeNone)))
}
}
// Any indexing policy has to include the root path /* as either an included or an excluded path.
rootPath := "/*"
includedPathsDefined := indexingPolicy.IncludedPaths != nil
includedPathsContainRootPath := false
if includedPathsDefined {
for _, includedPath := range *indexingPolicy.IncludedPaths {
if includedPathsContainRootPath {
break
}
includedPathsContainRootPath = *includedPath.Path == rootPath
}
}
excludedPathsContainRootPath := false
excludedPathsDefined := indexingPolicy.ExcludedPaths != nil
if excludedPathsDefined {
for _, excludedPath := range *indexingPolicy.ExcludedPaths {
if excludedPathsContainRootPath {
break
}
excludedPathsContainRootPath = *excludedPath.Path == rootPath
}
}
// The root path can't be included and excluded at the same time.
if includedPathsContainRootPath && excludedPathsContainRootPath {
return fmt.Errorf("only one of included_path or excluded_path may include the path %q", rootPath)
}
// The root path must be included or excluded
if (includedPathsDefined || excludedPathsDefined) && !(includedPathsContainRootPath || excludedPathsContainRootPath) {
return fmt.Errorf("either included_path or excluded_path must include the path %q", rootPath)
}
return nil
}