This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_http_req.go
192 lines (155 loc) · 4.02 KB
/
data_http_req.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
const (
defaultSource = "default"
overrideSource = "override"
requestSource = "request"
urlKey = "url"
methodKey = "method"
responseContentTypeKey = "response_content_type"
responseContentJSONKeyKey = "response_content_json_key"
defaultKey = "default"
overrideKey = "override"
valueKey = "value"
sourceKey = "source"
)
func dataHTTPReq() *schema.Resource {
return &schema.Resource{
Read: dataHTTPReqRead,
Schema: map[string]*schema.Schema{
urlKey: &schema.Schema{
Type: schema.TypeString,
Required: true,
},
methodKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "GET",
},
responseContentTypeKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "text/plain",
},
responseContentJSONKeyKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
defaultKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
overrideKey: &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
valueKey: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
sourceKey: &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataHTTPReqRead(d *schema.ResourceData, m interface{}) error {
url := d.Get(urlKey).(string)
method := d.Get(methodKey).(string)
rspContentType := d.Get(responseContentTypeKey).(string)
rspContentJSONKey := d.Get(responseContentJSONKeyKey).(string)
defaultVal := d.Get(defaultKey).(string)
overrideVal := d.Get(overrideKey).(string)
d.SetId(hashString(url, method, rspContentType, rspContentJSONKey, defaultVal, overrideVal))
// Make override case exceptional and return early
if overrideVal != "" {
d.Set(valueKey, overrideVal)
d.Set(sourceKey, overrideSource)
return nil
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
d.SetId("")
return err
}
client := &http.Client{}
// Only allow default value to be used when the error comes from
// HTTP request failure OR status code is not OK
getDefaultOrErr := func(err error) error {
if defaultVal == "" {
d.SetId("")
return err
}
d.Set(valueKey, defaultVal)
d.Set(sourceKey, defaultSource)
return nil
}
rsp, err := client.Do(req)
if err != nil {
return getDefaultOrErr(err)
}
defer rsp.Body.Close()
// Check for response status code before proceeding
if rsp.StatusCode != http.StatusOK {
return getDefaultOrErr(fmt.Errorf("Response returned status code %d, and default value is not set", rsp.StatusCode))
}
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
d.SetId("")
return err
}
var value string
if rspContentType == "text/plain" {
value = string(body)
} else if rspContentType == "application/json" {
if len(rspContentJSONKey) == 0 {
d.SetId("")
return errors.New("response_content_json_key cannot be empty")
}
if rspContentJSONKey[0] != '.' {
d.SetId("")
return errors.New("response_content_json_key must begin with \".\"")
}
bodyMap := map[string]interface{}{}
if err := json.Unmarshal(body, &bodyMap); err != nil {
d.SetId("")
return err
}
key := rspContentJSONKey[1:]
var ok bool
// string
value, ok = bodyMap[key].(string)
// number
if !ok {
var valueRep float64
valueRep, ok = bodyMap[key].(float64)
value = fmt.Sprintf("%g", valueRep)
// boolean
if !ok {
var valueRep bool
valueRep, ok = bodyMap[key].(bool)
value = strconv.FormatBool(valueRep)
// No other type left, so return error
if !ok {
d.SetId("")
return fmt.Errorf("%s does not contain any value, or has invalid value type", rspContentJSONKey)
}
}
}
}
if err := d.Set(valueKey, value); err != nil {
d.SetId("")
return err
}
d.Set(sourceKey, requestSource)
return nil
}