Skip to content
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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions builtin/providers/aws/resource_aws_efs_mount_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,8 +114,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 resp.MountTargets != nil && len(resp.MountTargets) < 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be refactored into a helper method? We tend to repeat this process a lot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean a lot in here, or a lot all over the place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe it's used 3 or 4 times in this file right?

return nil, "error", fmt.Errorf("EFS mount target %q could not be found.", d.Id())
}

mt := resp.MountTargets[0]
Expand Down Expand Up @@ -161,22 +161,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" {
Copy link
Contributor

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 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

// 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 resp.MountTargets != nil && len(resp.MountTargets) < 1 {
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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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()),
Expand All @@ -185,15 +193,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", fmt.Sprintf("%s.%s.efs.%s.amazonaws.com", az, *mt.FileSystemId, region))
Copy link
Contributor

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 this refactored into a helper rather than done inline so that we can test it

I suggest something like buildEfsMountDnsName

if err != nil {
return err
}

return nil
}
Expand All @@ -207,8 +222,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
Expand Down Expand Up @@ -245,7 +260,7 @@ func resourceAwsEfsMountTargetDelete(d *schema.ResourceData, meta interface{}) e
return nil, "error", awsErr
}

if len(resp.MountTargets) < 1 {
if resp.MountTargets != nil && len(resp.MountTargets) < 1 {
return nil, "", nil
}

Expand All @@ -261,7 +276,7 @@ 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())
}

Expand Down