forked from openshift/terraform-provider-azurerm
/
logic_apps.go
183 lines (140 loc) · 7.11 KB
/
logic_apps.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
package azurerm
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/logic/mgmt/2016-06-01/logic"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
// NOTE: this file is not a recommended way of developing Terraform resources; this exists to work around the fact that this API is dynamic (by it's nature)
func resourceLogicAppActionUpdate(d *schema.ResourceData, meta interface{}, logicAppId string, name string, vals map[string]interface{}) error {
return resourceLogicAppComponentUpdate(d, meta, "Action", "actions", logicAppId, name, vals)
}
func resourceLogicAppTriggerUpdate(d *schema.ResourceData, meta interface{}, logicAppId string, name string, vals map[string]interface{}) error {
return resourceLogicAppComponentUpdate(d, meta, "Trigger", "triggers", logicAppId, name, vals)
}
func resourceLogicAppComponentUpdate(d *schema.ResourceData, meta interface{}, kind string, propertyName string, logicAppId string, name string, vals map[string]interface{}) error {
client := meta.(*ArmClient).logicWorkflowsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(logicAppId)
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
logicAppName := id.Path["workflows"]
log.Printf("[DEBUG] Preparing arguments for Logic App Workspace %q (Resource Group %q) %s %q", logicAppName, resourceGroup, kind, name)
// lock to prevent against Actions or Triggers conflicting
azureRMLockByName(logicAppName, logicAppResourceName)
defer azureRMUnlockByName(logicAppName, logicAppResourceName)
read, err := client.Get(ctx, resourceGroup, logicAppName)
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return fmt.Errorf("[ERROR] Logic App Workflow %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("[ERROR] Error making Read request on Logic App Workflow %q (Resource Group %q): %+v", logicAppName, resourceGroup, err)
}
if read.WorkflowProperties == nil {
return fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties` is nil")
}
if read.WorkflowProperties.Definition == nil {
return fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties.Definition` is nil")
}
definition := read.WorkflowProperties.Definition.(map[string]interface{})
vs := definition[propertyName].(map[string]interface{})
vs[name] = vals
definition[propertyName] = vs
properties := logic.Workflow{
Location: read.Location,
WorkflowProperties: &logic.WorkflowProperties{
Definition: definition,
Parameters: read.WorkflowProperties.Parameters,
},
Tags: read.Tags,
}
if _, err = client.CreateOrUpdate(ctx, resourceGroup, logicAppName, properties); err != nil {
return fmt.Errorf("Error updating Logic App Workspace %q (Resource Group %q) for %s %q: %+v", logicAppName, resourceGroup, kind, name, err)
}
if d.IsNewResource() {
d.SetId(fmt.Sprintf("%s/%s/%s", *read.ID, propertyName, name))
}
return nil
}
func resourceLogicAppActionRemove(d *schema.ResourceData, meta interface{}, resourceGroup, logicAppName, name string) error {
return resourceLogicAppComponentRemove(d, meta, "Action", "actions", resourceGroup, logicAppName, name)
}
func resourceLogicAppTriggerRemove(d *schema.ResourceData, meta interface{}, resourceGroup, logicAppName, name string) error {
return resourceLogicAppComponentRemove(d, meta, "Trigger", "triggers", resourceGroup, logicAppName, name)
}
func resourceLogicAppComponentRemove(d *schema.ResourceData, meta interface{}, kind, propertyName, resourceGroup, logicAppName, name string) error {
client := meta.(*ArmClient).logicWorkflowsClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[DEBUG] Preparing arguments for Logic App Workspace %q (Resource Group %q) %s %q Deletion", logicAppName, resourceGroup, kind, name)
// lock to prevent against Actions, Parameters or Actions conflicting
azureRMLockByName(logicAppName, logicAppResourceName)
defer azureRMUnlockByName(logicAppName, logicAppResourceName)
read, err := client.Get(ctx, resourceGroup, logicAppName)
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", logicAppName, resourceGroup, err)
}
if read.WorkflowProperties == nil {
return fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties` is nil")
}
if read.WorkflowProperties.Definition == nil {
return fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties.Definition` is nil")
}
definition := read.WorkflowProperties.Definition.(map[string]interface{})
vs := definition[propertyName].(map[string]interface{})
delete(vs, name)
definition[propertyName] = vs
properties := logic.Workflow{
Location: read.Location,
WorkflowProperties: &logic.WorkflowProperties{
Definition: definition,
Parameters: read.WorkflowProperties.Parameters,
},
Tags: read.Tags,
}
if _, err = client.CreateOrUpdate(ctx, resourceGroup, logicAppName, properties); err != nil {
return fmt.Errorf("Error removing %s %q from Logic App Workspace %q (Resource Group %q): %+v", kind, name, logicAppName, resourceGroup, err)
}
return nil
}
func retrieveLogicAppAction(meta interface{}, resourceGroup, logicAppName, name string) (*map[string]interface{}, *logic.Workflow, error) {
return retrieveLogicAppComponent(meta, resourceGroup, "Action", "actions", logicAppName, name)
}
func retrieveLogicAppTrigger(meta interface{}, resourceGroup, logicAppName, name string) (*map[string]interface{}, *logic.Workflow, error) {
return retrieveLogicAppComponent(meta, resourceGroup, "Trigger", "triggers", logicAppName, name)
}
func retrieveLogicAppComponent(meta interface{}, resourceGroup, kind, propertyName, logicAppName, name string) (*map[string]interface{}, *logic.Workflow, error) {
client := meta.(*ArmClient).logicWorkflowsClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[DEBUG] Preparing arguments for Logic App Workspace %q (Resource Group %q) %s %q", logicAppName, resourceGroup, kind, name)
// lock to prevent against Actions, Parameters or Actions conflicting
azureRMLockByName(logicAppName, logicAppResourceName)
defer azureRMUnlockByName(logicAppName, logicAppResourceName)
read, err := client.Get(ctx, resourceGroup, logicAppName)
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
return nil, nil, nil
}
return nil, nil, fmt.Errorf("[ERROR] Error making Read request on Logic App Workflow %q (Resource Group %q): %+v", logicAppName, resourceGroup, err)
}
if read.WorkflowProperties == nil {
return nil, nil, fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties` is nil")
}
if read.WorkflowProperties.Definition == nil {
return nil, nil, fmt.Errorf("[ERROR] Error parsing Logic App Workflow - `WorkflowProperties.Definition` is nil")
}
definition := read.WorkflowProperties.Definition.(map[string]interface{})
vs := definition[propertyName].(map[string]interface{})
v := vs[name]
if v == nil {
return nil, nil, nil
}
result := v.(map[string]interface{})
return &result, &read, nil
}