/
resource_arm_logic_app_workflow.go
348 lines (286 loc) · 11 KB
/
resource_arm_logic_app_workflow.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package logic
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/logic/mgmt/2019-05-01/logic"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
var logicAppResourceName = "azurerm_logic_app"
func resourceArmLogicAppWorkflow() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogicAppWorkflowCreate,
Read: resourceArmLogicAppWorkflowRead,
Update: resourceArmLogicAppWorkflowUpdate,
Delete: resourceArmLogicAppWorkflowDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": azure.SchemaLocation(),
"resource_group_name": azure.SchemaResourceGroupName(),
// TODO: should Parameters be split out into their own object to allow validation on the different sub-types?
"parameters": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"workflow_schema": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
},
"workflow_version": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "1.0.0.0",
},
"access_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"connector_endpoint_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"connector_outbound_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"workflow_endpoint_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"workflow_outbound_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tags.Schema(),
},
}
}
func resourceArmLogicAppWorkflowCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.WorkflowsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for Logic App Workflow creation.")
name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Logic App Workflow %q (Resource Group %q): %s", name, resourceGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_logic_app_workflow", *existing.ID)
}
}
location := azure.NormalizeLocation(d.Get("location").(string))
parameters := expandLogicAppWorkflowParameters(d.Get("parameters").(map[string]interface{}))
workflowSchema := d.Get("workflow_schema").(string)
workflowVersion := d.Get("workflow_version").(string)
t := d.Get("tags").(map[string]interface{})
properties := logic.Workflow{
Location: utils.String(location),
WorkflowProperties: &logic.WorkflowProperties{
Definition: &map[string]interface{}{
"$schema": workflowSchema,
"contentVersion": workflowVersion,
"actions": make(map[string]interface{}),
"triggers": make(map[string]interface{}),
},
Parameters: parameters,
},
Tags: tags.Expand(t),
}
if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, properties); err != nil {
return fmt.Errorf("[ERROR] Error creating Logic App Workflow %q (Resource Group %q): %+v", name, resourceGroup, err)
}
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("[ERROR] Error making Read request on Logic App Workflow %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("[ERROR] Cannot read Logic App Workflow %q (Resource Group %q) ID", name, resourceGroup)
}
d.SetId(*read.ID)
return resourceArmLogicAppWorkflowRead(d, meta)
}
func resourceArmLogicAppWorkflowUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.WorkflowsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["workflows"]
// lock to prevent against Actions, Parameters or Triggers conflicting
locks.ByName(name, logicAppResourceName)
defer locks.UnlockByName(name, logicAppResourceName)
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error making Read request on Logic App Workflow %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.WorkflowProperties == nil {
return fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties` is nil")
}
location := azure.NormalizeLocation(d.Get("location").(string))
parameters := expandLogicAppWorkflowParameters(d.Get("parameters").(map[string]interface{}))
t := d.Get("tags").(map[string]interface{})
properties := logic.Workflow{
Location: utils.String(location),
WorkflowProperties: &logic.WorkflowProperties{
Definition: read.WorkflowProperties.Definition,
Parameters: parameters,
},
Tags: tags.Expand(t),
}
if _, err = client.CreateOrUpdate(ctx, resourceGroup, name, properties); err != nil {
return fmt.Errorf("Error updating Logic App Workspace %q (Resource Group %q): %+v", name, resourceGroup, err)
}
return resourceArmLogicAppWorkflowRead(d, meta)
}
func resourceArmLogicAppWorkflowRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.WorkflowsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["workflows"]
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Logic App Workflow %q (Resource Group %q) was not found - removing from state", name, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error making Read request on Logic App Workflow %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.WorkflowProperties; props != nil {
parameters := flattenLogicAppWorkflowParameters(props.Parameters)
if err := d.Set("parameters", parameters); err != nil {
return fmt.Errorf("Error setting `parameters`: %+v", err)
}
d.Set("access_endpoint", props.AccessEndpoint)
if props.EndpointsConfiguration == nil || props.EndpointsConfiguration.Connector == nil {
d.Set("connector_endpoint_ip_addresses", []interface{}{})
d.Set("connector_outbound_ip_addresses", []interface{}{})
} else {
d.Set("connector_endpoint_ip_addresses", flattenIPAddresses(props.EndpointsConfiguration.Connector.AccessEndpointIPAddresses))
d.Set("connector_outbound_ip_addresses", flattenIPAddresses(props.EndpointsConfiguration.Connector.OutgoingIPAddresses))
}
if props.EndpointsConfiguration == nil || props.EndpointsConfiguration.Workflow == nil {
d.Set("workflow_endpoint_ip_addresses", []interface{}{})
d.Set("workflow_outbound_ip_addresses", []interface{}{})
} else {
d.Set("workflow_endpoint_ip_addresses", flattenIPAddresses(props.EndpointsConfiguration.Workflow.AccessEndpointIPAddresses))
d.Set("workflow_outbound_ip_addresses", flattenIPAddresses(props.EndpointsConfiguration.Workflow.OutgoingIPAddresses))
}
if definition := props.Definition; definition != nil {
if v, ok := definition.(map[string]interface{}); ok {
d.Set("workflow_schema", v["$schema"].(string))
d.Set("workflow_version", v["contentVersion"].(string))
}
}
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceArmLogicAppWorkflowDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logic.WorkflowsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
name := id.Path["workflows"]
// lock to prevent against Actions, Parameters or Triggers conflicting
locks.ByName(name, logicAppResourceName)
defer locks.UnlockByName(name, logicAppResourceName)
resp, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
}
return fmt.Errorf("Error issuing delete request for Logic App Workflow %q (Resource Group %q): %+v", name, resourceGroup, err)
}
return nil
}
func expandLogicAppWorkflowParameters(input map[string]interface{}) map[string]*logic.WorkflowParameter {
output := make(map[string]*logic.WorkflowParameter)
for k, v := range input {
output[k] = &logic.WorkflowParameter{
Type: logic.ParameterTypeString,
Value: v.(string),
}
}
return output
}
func flattenLogicAppWorkflowParameters(input map[string]*logic.WorkflowParameter) map[string]interface{} {
output := make(map[string]interface{})
for k, v := range input {
if v != nil {
// we only support string parameters at this time
val, ok := v.Value.(string)
if !ok {
log.Printf("[DEBUG] Skipping parameter %q since it's not a string", k)
}
output[k] = val
}
}
return output
}
func flattenIPAddresses(input *[]logic.IPAddress) []interface{} {
if input == nil {
return []interface{}{}
}
var addresses []interface{}
for _, addr := range *input {
addresses = append(addresses, *addr.Address)
}
return addresses
}