forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_logic_app_action_http.go
195 lines (157 loc) · 4.63 KB
/
resource_arm_logic_app_action_http.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
package azurerm
import (
"fmt"
"log"
"strings"
"net/http"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)
func resourceArmLogicAppActionHTTP() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogicAppActionHTTPCreateUpdate,
Read: resourceArmLogicAppActionHTTPRead,
Update: resourceArmLogicAppActionHTTPCreateUpdate,
Delete: resourceArmLogicAppActionHTTPDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"logic_app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"method": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(http.MethodDelete),
string(http.MethodGet),
string(http.MethodPatch),
string(http.MethodPost),
string(http.MethodPut),
}, false),
},
"uri": {
Type: schema.TypeString,
Required: true,
},
"body": {
Type: schema.TypeString,
Optional: true,
},
"headers": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceArmLogicAppActionHTTPCreateUpdate(d *schema.ResourceData, meta interface{}) error {
headersRaw := d.Get("headers").(map[string]interface{})
headers, err := expandLogicAppActionHttpHeaders(headersRaw)
if err != nil {
return err
}
inputs := map[string]interface{}{
"method": d.Get("method").(string),
"uri": d.Get("uri").(string),
"headers": headers,
}
if v, ok := d.GetOk("body"); ok {
inputs["body"] = v.(string)
}
action := map[string]interface{}{
"inputs": inputs,
"type": "http",
}
logicAppId := d.Get("logic_app_id").(string)
name := d.Get("name").(string)
err = resourceLogicAppActionUpdate(d, meta, logicAppId, name, action)
if err != nil {
return err
}
return resourceArmLogicAppActionHTTPRead(d, meta)
}
func resourceArmLogicAppActionHTTPRead(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
logicAppName := id.Path["workflows"]
name := id.Path["actions"]
t, app, err := retrieveLogicAppAction(meta, resourceGroup, logicAppName, name)
if err != nil {
return err
}
if t == nil {
log.Printf("[DEBUG] Logic App %q (Resource Group %q) does not contain Action %q - removing from state", logicAppName, resourceGroup, name)
d.SetId("")
return nil
}
action := *t
d.Set("name", name)
d.Set("logic_app_id", app.ID)
actionType := action["type"].(string)
if !strings.EqualFold(actionType, "http") {
return fmt.Errorf("Expected an HTTP Action for Action %q (Logic App %q / Resource Group %q) - got %q", name, logicAppName, resourceGroup, actionType)
}
v := action["inputs"]
if v == nil {
return fmt.Errorf("Error`inputs` was nil for HTTP Action %q (Logic App %q / Resource Group %q)", name, logicAppName, resourceGroup)
}
inputs, ok := v.(map[string]interface{})
if !ok {
return fmt.Errorf("Error parsing `inputs` for HTTP Action %q (Logic App %q / Resource Group %q)", name, logicAppName, resourceGroup)
}
if uri := inputs["uri"]; uri != nil {
d.Set("uri", uri.(string))
}
if method := inputs["method"]; method != nil {
d.Set("method", method.(string))
}
if body := inputs["body"]; body != nil {
d.Set("body", body.(string))
}
if headers := inputs["headers"]; headers != nil {
hv := headers.(map[string]interface{})
if err := d.Set("headers", hv); err != nil {
return fmt.Errorf("Error flattening `headers` for HTTP Action %q: %+v", name, err)
}
}
return nil
}
func resourceArmLogicAppActionHTTPDelete(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
logicAppName := id.Path["workflows"]
name := id.Path["actions"]
err = resourceLogicAppActionRemove(d, meta, resourceGroup, logicAppName, name)
if err != nil {
return fmt.Errorf("Error removing Action %q from Logic App %q (Resource Group %q): %+v", name, logicAppName, resourceGroup, err)
}
return nil
}
func expandLogicAppActionHttpHeaders(headersRaw map[string]interface{}) (*map[string]string, error) {
headers := make(map[string]string, 0)
for i, v := range headersRaw {
value, err := tagValueToString(v)
if err != nil {
return nil, err
}
headers[i] = value
}
return &headers, nil
}