-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_app_config_property.go
286 lines (263 loc) · 8.52 KB
/
data_source_ibm_app_config_property.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
package appconfiguration
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/appconfiguration-go-admin-sdk/appconfigurationv1"
)
func DataSourceIBMAppConfigProperty() *schema.Resource {
return &schema.Resource{
Read: dataSourceIbmAppConfigPropertyRead,
Schema: map[string]*schema.Schema{
"guid": {
Type: schema.TypeString,
Required: true,
Description: "GUID of the App Configuration service. Get it from the service instance credentials section of the dashboard.",
},
"environment_id": {
Type: schema.TypeString,
Required: true,
Description: "Environment Id.",
},
"property_id": {
Type: schema.TypeString,
Required: true,
Description: "Property Id.",
},
"include": {
Type: schema.TypeString,
Optional: true,
Description: "Include the associated collections in the response.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Property name.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Property description.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the Property (BOOLEAN, STRING, NUMERIC).",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Value of the Property. The value can be Boolean, String or a Numeric value as per the `type` attribute.",
},
"tags": {
Type: schema.TypeString,
Computed: true,
Description: "Tags associated with the property.",
},
"segment_rules": {
Type: schema.TypeList,
Computed: true,
Description: "Specify the targeting rules that is used to set different property values for different segments.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rules": {
Type: schema.TypeList,
Computed: true,
Description: "Rules array.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"segments": {
Type: schema.TypeList,
Computed: true,
Description: "List of segment ids that are used for targeting using the rule.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Value to be used for evaluation for this rule. The value can be Boolean, String or a Numeric value as per the `type` attribute.",
},
"order": {
Type: schema.TypeInt,
Computed: true,
Description: "Order of the rule, used during evaluation. The evaluation is performed in the order defined and the value associated with the first matching rule is used for evaluation.",
},
},
},
},
"segment_exists": {
Type: schema.TypeBool,
Computed: true,
Description: "Denotes if the targeting rules are specified for the property.",
},
"collections": {
Type: schema.TypeList,
Computed: true,
Description: "List of collection id representing the collections that are associated with the specified property.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"collection_id": {
Type: schema.TypeString,
Computed: true,
Description: "Collection id.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the collection.",
},
},
},
},
"created_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time of the property.",
},
"updated_time": {
Type: schema.TypeString,
Computed: true,
Description: "Last modified time of the property data.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "Property URL.",
},
},
}
}
func dataSourceIbmAppConfigPropertyRead(d *schema.ResourceData, meta interface{}) error {
guid := d.Get("guid").(string)
appconfigClient, err := getAppConfigClient(meta, guid)
if err != nil {
return fmt.Errorf("getAppConfigClient failed %s", err)
}
options := &appconfigurationv1.GetPropertyOptions{}
options.SetEnvironmentID(d.Get("environment_id").(string))
options.SetPropertyID(d.Get("property_id").(string))
if _, ok := d.GetOk("include"); ok {
options.SetInclude(d.Get("include").(string))
}
property, response, err := appconfigClient.GetProperty(options)
if err != nil {
return fmt.Errorf("GetProperty failed %s\n%s", err, response)
}
d.SetId(fmt.Sprintf("%s/%s/%s", guid, *options.EnvironmentID, *property.PropertyID))
if property.Name != nil {
if err = d.Set("name", property.Name); err != nil {
return fmt.Errorf("error setting name: %s", err)
}
}
if property.Description != nil {
if err = d.Set("description", property.Description); err != nil {
return fmt.Errorf("error setting description: %s", err)
}
}
if property.Type != nil {
if err = d.Set("type", property.Type); err != nil {
return fmt.Errorf("error setting type: %s", err)
}
}
if property.Value != nil {
value := property.Value
switch value.(interface{}).(type) {
case string:
d.Set("value", value.(string))
case float64:
d.Set("value", fmt.Sprintf("%v", value))
case bool:
d.Set("value", strconv.FormatBool(value.(bool)))
}
}
if property.Tags != nil {
if err = d.Set("tags", property.Tags); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
}
if property.SegmentRules != nil {
err = d.Set("segment_rules", dataSourcePropertyFlattenSegmentRules(property.SegmentRules))
if err != nil {
return fmt.Errorf("error setting segment_rules %s", err)
}
}
if property.SegmentExists != nil {
if err = d.Set("segment_exists", property.SegmentExists); err != nil {
return fmt.Errorf("error setting segment_exists: %s", err)
}
}
if property.Collections != nil {
err = d.Set("collections", dataSourcePropertyFlattenCollections(property.Collections))
if err != nil {
return fmt.Errorf("error setting collections %s", err)
}
}
if property.CreatedTime != nil {
if err = d.Set("created_time", property.CreatedTime.String()); err != nil {
return fmt.Errorf("error setting created_time: %s", err)
}
}
if property.UpdatedTime != nil {
if err = d.Set("updated_time", property.UpdatedTime.String()); err != nil {
return fmt.Errorf("error setting updated_time: %s", err)
}
}
if property.Href != nil {
if err = d.Set("href", property.Href); err != nil {
return fmt.Errorf("error setting href: %s", err)
}
}
return nil
}
func dataSourcePropertyFlattenCollections(result []appconfigurationv1.CollectionRef) (collections []map[string]interface{}) {
for _, collectionsItem := range result {
collections = append(collections, dataSourcePropertyCollectionsToMap(collectionsItem))
}
return collections
}
func dataSourcePropertyCollectionsToMap(collectionsItem appconfigurationv1.CollectionRef) (collectionsMap map[string]interface{}) {
collectionsMap = map[string]interface{}{}
if collectionsItem.CollectionID != nil {
collectionsMap["collection_id"] = collectionsItem.CollectionID
}
if collectionsItem.Name != nil {
collectionsMap["name"] = collectionsItem.Name
}
return collectionsMap
}
func dataSourcePropertyFlattenSegmentRules(result []appconfigurationv1.SegmentRule) (segmentRules []map[string]interface{}) {
for _, segmentRulesItem := range result {
segmentRules = append(segmentRules, dataSourcePropertySegmentRulesToMap(segmentRulesItem))
}
return segmentRules
}
func dataSourcePropertySegmentRulesToMap(segmentRulesItem appconfigurationv1.SegmentRule) (segmentRulesMap map[string]interface{}) {
segmentRulesMap = map[string]interface{}{}
if segmentRulesItem.Rules != nil {
rulesList := []map[string]interface{}{}
for _, rulesItem := range segmentRulesItem.Rules {
rulesList = append(rulesList, dataSourceFeatureSegmentRulesRulesToMap(rulesItem))
}
segmentRulesMap["rules"] = rulesList
}
if segmentRulesItem.Value != nil {
segmentValue := segmentRulesItem.Value
switch segmentValue.(interface{}).(type) {
case string:
segmentRulesMap["value"] = segmentValue.(string)
case float64:
segmentRulesMap["value"] = fmt.Sprintf("%v", segmentValue)
case bool:
segmentRulesMap["value"] = strconv.FormatBool(segmentValue.(bool))
}
}
if segmentRulesItem.Order != nil {
segmentRulesMap["order"] = segmentRulesItem.Order
}
return segmentRulesMap
}