-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provider/aws: Handle missing EFS mount target in aws_efs_mount_target. #8529
Merged
stack72
merged 4 commits into
hashicorp:master
from
kwilczynski:fix/handle-missing-mount-aws_efs_mount_target
Aug 30, 2016
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5569046
Handle missing EFS mount target in aws_efs_mount_target.
kwilczynski 4df4bb6
Add EFS mount target DNS helper function.
kwilczynski 918555c
Add EFS mount target response helper.
kwilczynski 88de0c8
Add acceptance test to check for non-empty plan.
kwilczynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ func resourceAwsEfsMountTargetCreate(d *schema.ResourceData, meta interface{}) e | |
// So we make it fail by calling 1 request per AZ at a time. | ||
az, err := getAzFromSubnetId(subnetId, meta.(*AWSClient).ec2conn) | ||
if err != nil { | ||
return fmt.Errorf("Failed getting AZ from subnet ID (%s): %s", subnetId, err) | ||
return fmt.Errorf("Failed getting Availability Zone from subnet ID (%s): %s", subnetId, err) | ||
} | ||
mtKey := "efs-mt-" + fsId + "-" + az | ||
awsMutexKV.Lock(mtKey) | ||
|
@@ -102,6 +102,7 @@ func resourceAwsEfsMountTargetCreate(d *schema.ResourceData, meta interface{}) e | |
} | ||
|
||
d.SetId(*mt.MountTargetId) | ||
log.Printf("[INFO] EFS mount target ID: %s", d.Id()) | ||
|
||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"creating"}, | ||
|
@@ -114,8 +115,8 @@ func resourceAwsEfsMountTargetCreate(d *schema.ResourceData, meta interface{}) e | |
return nil, "error", err | ||
} | ||
|
||
if len(resp.MountTargets) < 1 { | ||
return nil, "error", fmt.Errorf("EFS mount target %q not found", d.Id()) | ||
if hasEmptyMountTargets(resp) { | ||
return nil, "error", fmt.Errorf("EFS mount target %q could not be found.", d.Id()) | ||
} | ||
|
||
mt := resp.MountTargets[0] | ||
|
@@ -161,22 +162,30 @@ func resourceAwsEfsMountTargetRead(d *schema.ResourceData, meta interface{}) err | |
MountTargetId: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
return err | ||
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "MountTargetNotFound" { | ||
// The EFS mount target could not be found, | ||
// which would indicate that it might be | ||
// already deleted. | ||
log.Printf("[WARN] EFS mount target %q could not be found.", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading EFS mount target %s: %s", d.Id(), err) | ||
} | ||
|
||
if len(resp.MountTargets) < 1 { | ||
return fmt.Errorf("EFS mount target %q not found", d.Id()) | ||
if hasEmptyMountTargets(resp) { | ||
return fmt.Errorf("EFS mount target %q could not be found.", d.Id()) | ||
} | ||
|
||
mt := resp.MountTargets[0] | ||
|
||
log.Printf("[DEBUG] Found EFS mount target: %#v", mt) | ||
|
||
d.SetId(*mt.MountTargetId) | ||
d.Set("file_system_id", *mt.FileSystemId) | ||
d.Set("ip_address", *mt.IpAddress) | ||
d.Set("subnet_id", *mt.SubnetId) | ||
d.Set("network_interface_id", *mt.NetworkInterfaceId) | ||
d.Set("file_system_id", mt.FileSystemId) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice changes! |
||
d.Set("ip_address", mt.IpAddress) | ||
d.Set("subnet_id", mt.SubnetId) | ||
d.Set("network_interface_id", mt.NetworkInterfaceId) | ||
|
||
sgResp, err := conn.DescribeMountTargetSecurityGroups(&efs.DescribeMountTargetSecurityGroupsInput{ | ||
MountTargetId: aws.String(d.Id()), | ||
|
@@ -185,15 +194,22 @@ func resourceAwsEfsMountTargetRead(d *schema.ResourceData, meta interface{}) err | |
return err | ||
} | ||
|
||
d.Set("security_groups", schema.NewSet(schema.HashString, flattenStringList(sgResp.SecurityGroups))) | ||
err = d.Set("security_groups", schema.NewSet(schema.HashString, flattenStringList(sgResp.SecurityGroups))) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// DNS name per http://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html | ||
az, err := getAzFromSubnetId(*mt.SubnetId, meta.(*AWSClient).ec2conn) | ||
if err != nil { | ||
return fmt.Errorf("Failed getting AZ from subnet ID (%s): %s", *mt.SubnetId, err) | ||
return fmt.Errorf("Failed getting Availability Zone from subnet ID (%s): %s", *mt.SubnetId, err) | ||
} | ||
|
||
region := meta.(*AWSClient).region | ||
d.Set("dns_name", fmt.Sprintf("%s.%s.efs.%s.amazonaws.com", az, *mt.FileSystemId, region)) | ||
err = d.Set("dns_name", resourceAwsEfsMountTargetDnsName(az, *mt.FileSystemId, region)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
@@ -207,8 +223,8 @@ func getAzFromSubnetId(subnetId string, conn *ec2.EC2) (string, error) { | |
return "", err | ||
} | ||
|
||
if len(out.Subnets) != 1 { | ||
return "", fmt.Errorf("Expected exactly 1 subnet returned for %q", subnetId) | ||
if l := len(out.Subnets); l != 1 { | ||
return "", fmt.Errorf("Expected exactly 1 subnet returned for %q, got: %d", subnetId, l) | ||
} | ||
|
||
return *out.Subnets[0].AvailabilityZone, nil | ||
|
@@ -245,7 +261,7 @@ func resourceAwsEfsMountTargetDelete(d *schema.ResourceData, meta interface{}) e | |
return nil, "error", awsErr | ||
} | ||
|
||
if len(resp.MountTargets) < 1 { | ||
if hasEmptyMountTargets(resp) { | ||
return nil, "", nil | ||
} | ||
|
||
|
@@ -261,11 +277,22 @@ func resourceAwsEfsMountTargetDelete(d *schema.ResourceData, meta interface{}) e | |
|
||
_, err = stateConf.WaitForState() | ||
if err != nil { | ||
return fmt.Errorf("Error waiting for EFS mount target (%q) to delete: %q", | ||
return fmt.Errorf("Error waiting for EFS mount target (%q) to delete: %s", | ||
d.Id(), err.Error()) | ||
} | ||
|
||
log.Printf("[DEBUG] EFS mount target %q deleted.", d.Id()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsEfsMountTargetDnsName(az, fileSystemId, region string) string { | ||
return fmt.Sprintf("%s.%s.efs.%s.amazonaws.com", az, fileSystemId, region) | ||
} | ||
|
||
func hasEmptyMountTargets(mto *efs.DescribeMountTargetsOutput) bool { | ||
if mto != nil && len(mto.MountTargets) > 0 { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a test for _disappears - look at how db_instance tests work for this if you fancy trying it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!