forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_logic_app_trigger_http_request.go
200 lines (162 loc) · 5.13 KB
/
resource_arm_logic_app_trigger_http_request.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
package azurerm
import (
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)
func resourceArmLogicAppTriggerHttpRequest() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogicAppTriggerHttpRequestCreateUpdate,
Read: resourceArmLogicAppTriggerHttpRequestRead,
Update: resourceArmLogicAppTriggerHttpRequestCreateUpdate,
Delete: resourceArmLogicAppTriggerHttpRequestDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error {
relativePath := diff.Get("relative_path").(string)
if relativePath != "" {
method := diff.Get("method").(string)
if method == "" {
return fmt.Errorf("`method` must be specified when `relative_path` is set.")
}
}
return nil
},
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,
},
"schema": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
"method": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(http.MethodDelete),
string(http.MethodGet),
string(http.MethodPatch),
string(http.MethodPost),
string(http.MethodPut),
}, false),
},
"relative_path": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateLogicAppTriggerHttpRequestRelativePath,
},
},
}
}
func resourceArmLogicAppTriggerHttpRequestCreateUpdate(d *schema.ResourceData, meta interface{}) error {
schemaRaw := d.Get("schema").(string)
var schema map[string]interface{}
err := json.Unmarshal([]byte(schemaRaw), &schema)
if err != nil {
return fmt.Errorf("Error unmarshalling JSON from Schema: %+v", err)
}
inputs := map[string]interface{}{
"schema": schema,
}
if v, ok := d.GetOk("method"); ok {
inputs["method"] = v.(string)
}
if v, ok := d.GetOk("relative_path"); ok {
inputs["relativePath"] = v.(string)
}
trigger := map[string]interface{}{
"inputs": inputs,
"kind": "Http",
"type": "Request",
}
logicAppId := d.Get("logic_app_id").(string)
name := d.Get("name").(string)
err = resourceLogicAppTriggerUpdate(d, meta, logicAppId, name, trigger)
if err != nil {
return err
}
return resourceArmLogicAppTriggerHttpRequestRead(d, meta)
}
func resourceArmLogicAppTriggerHttpRequestRead(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["triggers"]
t, app, err := retrieveLogicAppTrigger(meta, resourceGroup, logicAppName, name)
if err != nil {
return err
}
if t == nil {
log.Printf("[DEBUG] Logic App %q (Resource Group %q) does not contain Trigger %q - removing from state", logicAppName, resourceGroup, name)
d.SetId("")
return nil
}
trigger := *t
d.Set("name", name)
d.Set("logic_app_id", app.ID)
v := trigger["inputs"]
if v == nil {
return fmt.Errorf("Error `inputs` was nil for HTTP Trigger %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 Trigger %q (Logic App %q / Resource Group %q)", name, logicAppName, resourceGroup)
}
if method := inputs["method"]; method != nil {
d.Set("method", method.(string))
}
if relativePath := inputs["relativePath"]; relativePath != nil {
d.Set("relative_path", relativePath.(string))
}
if schemaRaw := inputs["schema"]; schemaRaw != nil {
schema, err := json.Marshal(schemaRaw)
if err != nil {
return fmt.Errorf("Error serializing the Schema to JSON: %+v", err)
}
d.Set("schema", string(schema))
}
return nil
}
func resourceArmLogicAppTriggerHttpRequestDelete(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["triggers"]
err = resourceLogicAppTriggerRemove(d, meta, resourceGroup, logicAppName, name)
if err != nil {
return fmt.Errorf("Error removing Trigger %q from Logic App %q (Resource Group %q): %+v", name, logicAppName, resourceGroup, err)
}
return nil
}
func validateLogicAppTriggerHttpRequestRelativePath(v interface{}, _ string) (ws []string, errors []error) {
value := v.(string)
r, _ := regexp.Compile("^[A-Za-z0-9_/}{]+$")
if !r.MatchString(value) {
errors = append(errors, fmt.Errorf("Relative Path can only contain alphanumeric characters, underscores, forward slashes and curly braces."))
}
return ws, errors
}