forked from hashicorp/terraform-provider-aws
/
resource_aws_datasync_location_efs.go
215 lines (177 loc) · 5.9 KB
/
resource_aws_datasync_location_efs.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
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/datasync"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsDataSyncLocationEfs() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDataSyncLocationEfsCreate,
Read: resourceAwsDataSyncLocationEfsRead,
Update: resourceAwsDataSyncLocationEfsUpdate,
Delete: resourceAwsDataSyncLocationEfsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"ec2_config": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"security_group_arns": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"subnet_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
},
},
},
"efs_file_system_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"subdirectory": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "/",
// Ignore missing trailing slash
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if new == "/" {
return false
}
if strings.TrimSuffix(old, "/") == strings.TrimSuffix(new, "/") {
return true
}
return false
},
},
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsDataSyncLocationEfsCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
input := &datasync.CreateLocationEfsInput{
Ec2Config: expandDataSyncEc2Config(d.Get("ec2_config").([]interface{})),
EfsFilesystemArn: aws.String(d.Get("efs_file_system_arn").(string)),
Subdirectory: aws.String(d.Get("subdirectory").(string)),
Tags: expandDataSyncTagListEntry(d.Get("tags").(map[string]interface{})),
}
log.Printf("[DEBUG] Creating DataSync Location EFS: %s", input)
output, err := conn.CreateLocationEfs(input)
if err != nil {
return fmt.Errorf("error creating DataSync Location EFS: %s", err)
}
d.SetId(aws.StringValue(output.LocationArn))
return resourceAwsDataSyncLocationEfsRead(d, meta)
}
func resourceAwsDataSyncLocationEfsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
input := &datasync.DescribeLocationEfsInput{
LocationArn: aws.String(d.Id()),
}
log.Printf("[DEBUG] Reading DataSync Location EFS: %s", input)
output, err := conn.DescribeLocationEfs(input)
if isAWSErr(err, "InvalidRequestException", "not found") {
log.Printf("[WARN] DataSync Location EFS %q not found - removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading DataSync Location EFS (%s): %s", d.Id(), err)
}
tagsInput := &datasync.ListTagsForResourceInput{
ResourceArn: output.LocationArn,
}
log.Printf("[DEBUG] Reading DataSync Location EFS tags: %s", tagsInput)
tagsOutput, err := conn.ListTagsForResource(tagsInput)
if err != nil {
return fmt.Errorf("error reading DataSync Location EFS (%s) tags: %s", d.Id(), err)
}
subdirectory, err := dataSyncParseLocationURI(aws.StringValue(output.LocationUri))
if err != nil {
return fmt.Errorf("error parsing Location EFS (%s) URI (%s): %s", d.Id(), aws.StringValue(output.LocationUri), err)
}
d.Set("arn", output.LocationArn)
if err := d.Set("ec2_config", flattenDataSyncEc2Config(output.Ec2Config)); err != nil {
return fmt.Errorf("error setting ec2_config: %s", err)
}
d.Set("subdirectory", subdirectory)
if err := d.Set("tags", flattenDataSyncTagListEntry(tagsOutput.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("uri", output.LocationUri)
return nil
}
func resourceAwsDataSyncLocationEfsUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
if d.HasChange("tags") {
oldRaw, newRaw := d.GetChange("tags")
createTags, removeTags := dataSyncTagsDiff(expandDataSyncTagListEntry(oldRaw.(map[string]interface{})), expandDataSyncTagListEntry(newRaw.(map[string]interface{})))
if len(removeTags) > 0 {
input := &datasync.UntagResourceInput{
Keys: dataSyncTagsKeys(removeTags),
ResourceArn: aws.String(d.Id()),
}
log.Printf("[DEBUG] Untagging DataSync Location EFS: %s", input)
if _, err := conn.UntagResource(input); err != nil {
return fmt.Errorf("error untagging DataSync Location EFS (%s): %s", d.Id(), err)
}
}
if len(createTags) > 0 {
input := &datasync.TagResourceInput{
ResourceArn: aws.String(d.Id()),
Tags: createTags,
}
log.Printf("[DEBUG] Tagging DataSync Location EFS: %s", input)
if _, err := conn.TagResource(input); err != nil {
return fmt.Errorf("error tagging DataSync Location EFS (%s): %s", d.Id(), err)
}
}
}
return resourceAwsDataSyncLocationEfsRead(d, meta)
}
func resourceAwsDataSyncLocationEfsDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
input := &datasync.DeleteLocationInput{
LocationArn: aws.String(d.Id()),
}
log.Printf("[DEBUG] Deleting DataSync Location EFS: %s", input)
_, err := conn.DeleteLocation(input)
if isAWSErr(err, "InvalidRequestException", "not found") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting DataSync Location EFS (%s): %s", d.Id(), err)
}
return nil
}