forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors_rule.go
144 lines (121 loc) · 3.79 KB
/
cors_rule.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
package common
import (
"strings"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func SchemaCorsRule() *pluginsdk.Schema {
allowedMethods := []string{
"DELETE",
"GET",
"HEAD",
"MERGE",
"POST",
"OPTIONS",
"PUT",
"PATCH",
}
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allowed_origins": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 64,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"exposed_headers": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 64,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"allowed_headers": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 64,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
"allowed_methods": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 64,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice(allowedMethods, false),
},
},
"max_age_in_seconds": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 2000000000),
},
},
},
}
}
func ExpandCosmosCorsRule(input []interface{}) *[]documentdb.CorsPolicy {
if len(input) == 0 || input[0] == nil {
return nil
}
corsRules := make([]documentdb.CorsPolicy, 0)
if len(input) == 0 {
return &corsRules
}
for _, attr := range input {
corsRuleAttr := attr.(map[string]interface{})
corsRule := documentdb.CorsPolicy{}
corsRule.AllowedOrigins = utils.String(strings.Join(*utils.ExpandStringSlice(corsRuleAttr["allowed_origins"].([]interface{})), ","))
corsRule.ExposedHeaders = utils.String(strings.Join(*utils.ExpandStringSlice(corsRuleAttr["exposed_headers"].([]interface{})), ","))
corsRule.AllowedHeaders = utils.String(strings.Join(*utils.ExpandStringSlice(corsRuleAttr["allowed_headers"].([]interface{})), ","))
corsRule.AllowedMethods = utils.String(strings.Join(*utils.ExpandStringSlice(corsRuleAttr["allowed_methods"].([]interface{})), ","))
corsRule.MaxAgeInSeconds = utils.Int64(int64(corsRuleAttr["max_age_in_seconds"].(int)))
corsRules = append(corsRules, corsRule)
}
return &corsRules
}
func FlattenCosmosCorsRule(input *[]documentdb.CorsPolicy) []interface{} {
corsRules := make([]interface{}, 0)
if input == nil || len(*input) == 0 {
return corsRules
}
for _, corsRule := range *input {
var maxAgeInSeconds int
if corsRule.MaxAgeInSeconds != nil {
maxAgeInSeconds = int(*corsRule.MaxAgeInSeconds)
}
corsRules = append(corsRules, map[string]interface{}{
"allowed_headers": flattenCorsProperty(corsRule.AllowedHeaders),
"allowed_origins": flattenCorsProperty(corsRule.AllowedOrigins),
"allowed_methods": flattenCorsProperty(corsRule.AllowedMethods),
"exposed_headers": flattenCorsProperty(corsRule.ExposedHeaders),
"max_age_in_seconds": maxAgeInSeconds,
})
}
return corsRules
}
func flattenCorsProperty(input *string) []interface{} {
if input == nil {
return make([]interface{}, 0)
}
results := make([]interface{}, 0, len(*input))
origins := strings.Split(*input, ",")
for _, origin := range origins {
results = append(results, origin)
}
return results
}