-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
resource_aws_sfn_state_machine.go
225 lines (185 loc) · 6.09 KB
/
resource_aws_sfn_state_machine.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sfn"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsSfnStateMachine() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSfnStateMachineCreate,
Read: resourceAwsSfnStateMachineRead,
Update: resourceAwsSfnStateMachineUpdate,
Delete: resourceAwsSfnStateMachineDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"definition": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 1024*1024), // 1048576
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateSfnStateMachineName,
},
"role_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Print("[DEBUG] Creating Step Function State Machine")
params := &sfn.CreateStateMachineInput{
Definition: aws.String(d.Get("definition").(string)),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
}
var activity *sfn.CreateStateMachineOutput
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
var err error
activity, err = conn.CreateStateMachine(params)
if err != nil {
// Note: the instance may be in a deleting mode, hence the retry
// when creating the step function. This can happen when we are
// updating the resource (since there is no update API call).
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "StateMachineDeleting" {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error creating Step Function State Machine: %s", err)
}
d.SetId(*activity.StateMachineArn)
if v, ok := d.GetOk("tags"); ok {
input := &sfn.TagResourceInput{
ResourceArn: aws.String(d.Id()),
Tags: tagsFromMapSfn(v.(map[string]interface{})),
}
log.Printf("[DEBUG] Tagging SFN State Machine: %s", input)
_, err := conn.TagResource(input)
if err != nil {
return fmt.Errorf("error tagging SFN State Machine (%s): %s", d.Id(), input)
}
}
return resourceAwsSfnStateMachineRead(d, meta)
}
func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Printf("[DEBUG] Reading Step Function State Machine: %s", d.Id())
sm, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
StateMachineArn: aws.String(d.Id()),
})
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "NotFoundException" || awserr.Code() == "StateMachineDoesNotExist" {
d.SetId("")
return nil
}
}
return err
}
d.Set("definition", sm.Definition)
d.Set("name", sm.Name)
d.Set("role_arn", sm.RoleArn)
d.Set("status", sm.Status)
if err := d.Set("creation_date", sm.CreationDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting creation_date: %s", err)
}
tagsResp, err := conn.ListTagsForResource(
&sfn.ListTagsForResourceInput{
ResourceArn: aws.String(d.Id()),
},
)
if err != nil {
return fmt.Errorf("error listing SFN Activity (%s) tags: %s", d.Id(), err)
}
if err := d.Set("tags", tagsToMapSfn(tagsResp.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
return nil
}
func resourceAwsSfnStateMachineUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
params := &sfn.UpdateStateMachineInput{
StateMachineArn: aws.String(d.Id()),
Definition: aws.String(d.Get("definition").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
}
_, err := conn.UpdateStateMachine(params)
log.Printf("[DEBUG] Updating Step Function State Machine: %#v", params)
if err != nil {
if isAWSErr(err, "StateMachineDoesNotExist", "State Machine Does Not Exist") {
return fmt.Errorf("Error updating Step Function State Machine: %s", err)
}
return err
}
if d.HasChange("tags") {
oldTagsRaw, newTagsRaw := d.GetChange("tags")
oldTagsMap := oldTagsRaw.(map[string]interface{})
newTagsMap := newTagsRaw.(map[string]interface{})
createTags, removeTags := diffTagsSfn(tagsFromMapSfn(oldTagsMap), tagsFromMapSfn(newTagsMap))
if len(removeTags) > 0 {
removeTagKeys := make([]*string, len(removeTags))
for i, removeTag := range removeTags {
removeTagKeys[i] = removeTag.Key
}
input := &sfn.UntagResourceInput{
ResourceArn: aws.String(d.Id()),
TagKeys: removeTagKeys,
}
log.Printf("[DEBUG] Untagging State Function: %s", input)
if _, err := conn.UntagResource(input); err != nil {
return fmt.Errorf("error untagging State Function (%s): %s", d.Id(), err)
}
}
if len(createTags) > 0 {
input := &sfn.TagResourceInput{
ResourceArn: aws.String(d.Id()),
Tags: createTags,
}
log.Printf("[DEBUG] Tagging State Function: %s", input)
if _, err := conn.TagResource(input); err != nil {
return fmt.Errorf("error tagging State Function (%s): %s", d.Id(), err)
}
}
}
return resourceAwsSfnStateMachineRead(d, meta)
}
func resourceAwsSfnStateMachineDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sfnconn
log.Printf("[DEBUG] Deleting Step Function State Machine: %s", d.Id())
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteStateMachine(&sfn.DeleteStateMachineInput{
StateMachineArn: aws.String(d.Id()),
})
if err == nil {
return nil
}
return resource.NonRetryableError(err)
})
}