Skip to content

Commit fceb3ac

Browse files
committed
Maintenance and clean-up for aws_efs_file_system.
This commit is purely a maintenance and clean-up of the resource. Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
1 parent 4519ba4 commit fceb3ac

2 files changed

Lines changed: 62 additions & 44 deletions

File tree

‎builtin/providers/aws/resource_aws_efs_file_system.go‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func resourceAwsEfsFileSystem() *schema.Resource {
2424
},
2525

2626
Schema: map[string]*schema.Schema{
27-
"creation_token": &schema.Schema{
27+
"creation_token": {
2828
Type: schema.TypeString,
2929
Optional: true,
3030
ForceNew: true,
3131
ValidateFunc: validateMaxLength(64),
3232
},
3333

34-
"reference_name": &schema.Schema{
34+
"reference_name": {
3535
Type: schema.TypeString,
3636
Optional: true,
3737
ForceNew: true,
@@ -40,7 +40,7 @@ func resourceAwsEfsFileSystem() *schema.Resource {
4040
ValidateFunc: validateReferenceName,
4141
},
4242

43-
"performance_mode": &schema.Schema{
43+
"performance_mode": {
4444
Type: schema.TypeString,
4545
Optional: true,
4646
ForceNew: true,
@@ -95,8 +95,8 @@ func resourceAwsEfsFileSystemCreate(d *schema.ResourceData, meta interface{}) er
9595
return nil, "error", err
9696
}
9797

98-
if len(resp.FileSystems) < 1 {
99-
return nil, "not-found", fmt.Errorf("EFS file system %q not found", d.Id())
98+
if hasEmptyFileSystems(resp) {
99+
return nil, "not-found", fmt.Errorf("EFS file system %q could not be found.", d.Id())
100100
}
101101

102102
fs := resp.FileSystems[0]
@@ -110,7 +110,7 @@ func resourceAwsEfsFileSystemCreate(d *schema.ResourceData, meta interface{}) er
110110

111111
_, err = stateConf.WaitForState()
112112
if err != nil {
113-
return fmt.Errorf("Error waiting for EFS file system (%q) to create: %q",
113+
return fmt.Errorf("Error waiting for EFS file system (%q) to create: %s",
114114
d.Id(), err.Error())
115115
}
116116
log.Printf("[DEBUG] EFS file system %q created.", d.Id())
@@ -122,7 +122,7 @@ func resourceAwsEfsFileSystemUpdate(d *schema.ResourceData, meta interface{}) er
122122
conn := meta.(*AWSClient).efsconn
123123
err := setTagsEFS(conn, d)
124124
if err != nil {
125-
return fmt.Errorf("Error setting EC2 tags for EFS file system (%q): %q",
125+
return fmt.Errorf("Error setting EC2 tags for EFS file system (%q): %s",
126126
d.Id(), err.Error())
127127
}
128128

@@ -137,25 +137,29 @@ func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) erro
137137
})
138138
if err != nil {
139139
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "FileSystemNotFound" {
140-
log.Printf("[WARN] EFS File System (%s) not found, error code (404)", d.Id())
140+
log.Printf("[WARN] EFS file system (%s) could not be found.", d.Id())
141141
d.SetId("")
142142
return nil
143143
}
144144
return err
145145
}
146-
if len(resp.FileSystems) < 1 {
147-
return fmt.Errorf("EFS file system %q not found", d.Id())
146+
147+
if hasEmptyFileSystems(resp) {
148+
return fmt.Errorf("EFS file system %q could not be found.", d.Id())
148149
}
149150

150151
tagsResp, err := conn.DescribeTags(&efs.DescribeTagsInput{
151152
FileSystemId: aws.String(d.Id()),
152153
})
153154
if err != nil {
154-
return fmt.Errorf("Error retrieving EC2 tags for EFS file system (%q): %q",
155+
return fmt.Errorf("Error retrieving EC2 tags for EFS file system (%q): %s",
155156
d.Id(), err.Error())
156157
}
157158

158-
d.Set("tags", tagsToMapEFS(tagsResp.Tags))
159+
err = d.Set("tags", tagsToMapEFS(tagsResp.Tags))
160+
if err != nil {
161+
return err
162+
}
159163

160164
return nil
161165
}
@@ -182,7 +186,7 @@ func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) er
182186
return nil, "error", err
183187
}
184188

185-
if len(resp.FileSystems) < 1 {
189+
if hasEmptyFileSystems(resp) {
186190
return nil, "", nil
187191
}
188192

@@ -197,7 +201,7 @@ func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) er
197201

198202
_, err = stateConf.WaitForState()
199203
if err != nil {
200-
return fmt.Errorf("Error waiting for EFS file system (%q) to delete: %q",
204+
return fmt.Errorf("Error waiting for EFS file system (%q) to delete: %s",
201205
d.Id(), err.Error())
202206
}
203207

@@ -218,10 +222,17 @@ func validateReferenceName(v interface{}, k string) (ws []string, errors []error
218222

219223
func validatePerformanceModeType(v interface{}, k string) (ws []string, errors []error) {
220224
value := v.(string)
221-
if value != "generalPurpose" && value != "maxIO" {
225+
if value != efs.PerformanceModeGeneralPurpose && value != efs.PerformanceModeMaxIo {
222226
errors = append(errors, fmt.Errorf(
223-
"%q contains an invalid Performance Mode %q. Valid modes are either %q or %q",
224-
k, value, "generalPurpose", "maxIO"))
227+
"%q contains an invalid Performance Mode %q. Valid modes are either %q or %q.",
228+
k, value, efs.PerformanceModeGeneralPurpose, efs.PerformanceModeMaxIo))
225229
}
226230
return
227231
}
232+
233+
func hasEmptyFileSystems(fs *efs.DescribeFileSystemsOutput) bool {
234+
if fs != nil && len(fs.FileSystems) > 0 {
235+
return false
236+
}
237+
return true
238+
}

‎builtin/providers/aws/resource_aws_efs_file_system_test.go‎

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/hashicorp/terraform/terraform"
1515
)
1616

17-
func TestResourceAWSEFSReferenceName_validation(t *testing.T) {
17+
func TestResourceAWSEFSFileSystem_validateReferenceName(t *testing.T) {
1818
var value string
1919
var errors []error
2020

@@ -27,35 +27,20 @@ func TestResourceAWSEFSReferenceName_validation(t *testing.T) {
2727
value = acctest.RandString(32)
2828
_, errors = validateReferenceName(value, "reference_name")
2929
if len(errors) != 0 {
30-
t.Fatalf("Expected to trigger a validation error")
30+
t.Fatalf("Expected not to trigger a validation error")
3131
}
3232
}
3333

34-
func TestResourceAWSEFSPerformanceMode_validation(t *testing.T) {
35-
type testCase struct {
36-
Value string
37-
ErrCount int
38-
}
39-
40-
invalidCases := []testCase{
41-
{
42-
Value: "garrusVakarian",
43-
ErrCount: 1,
44-
},
45-
{
46-
Value: acctest.RandString(80),
47-
ErrCount: 1,
48-
},
49-
}
50-
51-
for _, tc := range invalidCases {
52-
_, errors := validatePerformanceModeType(tc.Value, "performance_mode")
53-
if len(errors) != tc.ErrCount {
54-
t.Fatalf("Expected to trigger a validation error")
55-
}
34+
func TestResourceAWSEFSFileSystem_validatePerformanceModeType(t *testing.T) {
35+
_, errors := validatePerformanceModeType("incorrect", "performance_mode")
36+
if len(errors) == 0 {
37+
t.Fatalf("Expected to trigger a validation error")
5638
}
5739

58-
validCases := []testCase{
40+
var testCases = []struct {
41+
Value string
42+
ErrCount int
43+
}{
5944
{
6045
Value: "generalPurpose",
6146
ErrCount: 0,
@@ -66,14 +51,36 @@ func TestResourceAWSEFSPerformanceMode_validation(t *testing.T) {
6651
},
6752
}
6853

69-
for _, tc := range validCases {
70-
_, errors := validatePerformanceModeType(tc.Value, "aws_efs_file_system")
54+
for _, tc := range testCases {
55+
_, errors := validatePerformanceModeType(tc.Value, "performance_mode")
7156
if len(errors) != tc.ErrCount {
7257
t.Fatalf("Expected not to trigger a validation error")
7358
}
7459
}
7560
}
7661

62+
func TestResourceAWSEFSFileSystem_hasEmptyFileSystems(t *testing.T) {
63+
fs := &efs.DescribeFileSystemsOutput{
64+
FileSystems: []*efs.FileSystemDescription{},
65+
}
66+
67+
var actual bool
68+
69+
actual = hasEmptyFileSystems(fs)
70+
if !actual {
71+
t.Fatalf("Expected return value to be true, got %t", actual)
72+
}
73+
74+
// Add an empty file system.
75+
fs.FileSystems = append(fs.FileSystems, &efs.FileSystemDescription{})
76+
77+
actual = hasEmptyFileSystems(fs)
78+
if actual {
79+
t.Fatalf("Expected return value to be false, got %t", actual)
80+
}
81+
82+
}
83+
7784
func TestAccAWSEFSFileSystem_basic(t *testing.T) {
7885
resource.Test(t, resource.TestCase{
7986
PreCheck: func() { testAccPreCheck(t) },

0 commit comments

Comments
 (0)