-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_quantum_query_json.go
66 lines (58 loc) · 1.55 KB
/
data_source_quantum_query_json.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
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/tidwall/gjson"
)
func dataSourceQuantumQueryJSON() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Terraform 0.12 supports nested maps. Ex: jsondecode(var.my_variable).attribute1.attribute2",
Read: dataSourceQuantumQueryJSONRead,
Schema: map[string]*schema.Schema{
"json": {
Type: schema.TypeString,
Required: true,
},
"query": {
Type: schema.TypeString,
Required: true,
},
"result_list": {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
},
"result_map": {
Computed: true,
Type: schema.TypeMap,
},
"result": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceQuantumQueryJSONRead(d *schema.ResourceData, m interface{}) error {
json := d.Get("json").(string)
query := d.Get("query").(string)
queryResult := gjson.Get(json, query)
d.SetId(fmt.Sprintf("%d-%d", hashcode.String(json), hashcode.String(query)))
d.Set("result", queryResult.String())
if queryResult.IsArray() {
resultList := []string{}
for _, value := range queryResult.Array() {
resultList = append(resultList, value.String())
}
d.Set("result_list", resultList)
}
if queryResult.IsObject() {
resultMap := map[string]string{}
for key, value := range queryResult.Map() {
resultMap[key] = value.String()
}
d.Set("result_map", resultMap)
}
return nil
}