Skip to content

Commit

Permalink
Replace calls to DescribeSecurityGroups with single result with `fi…
Browse files Browse the repository at this point in the history
…nder.SecurityGroup...()`
  • Loading branch information
gdavison committed Mar 26, 2021
1 parent 44b9f1f commit e7674c3
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 416 deletions.
31 changes: 9 additions & 22 deletions aws/resource_aws_default_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/finder"
)

func TestAccAWSDefaultSecurityGroup_Vpc_basic(t *testing.T) {
Expand Down Expand Up @@ -181,24 +182,19 @@ func testAccCheckAWSDefaultSecurityGroupExists(n string, group *ec2.SecurityGrou
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Security Group is set")
return fmt.Errorf("No EC2 Default Security Group ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).ec2conn
req := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{aws.String(rs.Primary.ID)},
}
resp, err := conn.DescribeSecurityGroups(req)

sg, err := finder.SecurityGroupByID(conn, rs.Primary.ID)
if err != nil {
return err
}

if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupId == rs.Primary.ID {
*group = *resp.SecurityGroups[0]
return nil
}
*group = *sg

return fmt.Errorf("Security Group not found")
return nil
}
}

Expand All @@ -215,21 +211,12 @@ func testAccCheckAWSDefaultSecurityGroupEc2ClassicExists(n string, group *ec2.Se

conn := testAccProviderEc2Classic.Meta().(*AWSClient).ec2conn

input := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{aws.String(rs.Primary.ID)},
}

resp, err := conn.DescribeSecurityGroups(input)

sg, err := finder.SecurityGroupByID(conn, rs.Primary.ID)
if err != nil {
return fmt.Errorf("error describing EC2 Default Security Group (%s): %w", rs.Primary.ID, err)
}

if len(resp.SecurityGroups) == 0 || aws.StringValue(resp.SecurityGroups[0].GroupId) != rs.Primary.ID {
return fmt.Errorf("EC2 Default Security Group (%s) not found", rs.Primary.ID)
return err
}

*group = *resp.SecurityGroups[0]
*group = *sg

return nil
}
Expand Down
68 changes: 20 additions & 48 deletions aws/resource_aws_emr_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/emr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/finder"
)

func init() {
Expand All @@ -26,7 +26,7 @@ func init() {
func testSweepEmrClusters(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).emrconn

Expand Down Expand Up @@ -71,7 +71,7 @@ func testSweepEmrClusters(region string) error {
log.Printf("[WARN] Skipping EMR Cluster sweep for %s: %s", region, err)
return nil
}
return fmt.Errorf("error retrieving EMR Clusters: %s", err)
return fmt.Errorf("error retrieving EMR Clusters: %w", err)
}

return nil
Expand Down Expand Up @@ -1440,25 +1440,24 @@ func testAccCheckAWSEmrDestroy(s *terraform.State) error {
continue
}

params := &emr.DescribeClusterInput{
input := &emr.DescribeClusterInput{
ClusterId: aws.String(rs.Primary.ID),
}

describe, err := conn.DescribeCluster(params)

if err == nil {
if describe.Cluster != nil &&
*describe.Cluster.Status.State == "WAITING" {
return fmt.Errorf("EMR Cluster still exists")
}
output, err := conn.DescribeCluster(input)
if err != nil {
return err
}

providerErr, ok := err.(awserr.Error)
if !ok {
return err
// if output.Cluster != nil &&
// *output.Cluster.Status.State == "WAITING" {
// return fmt.Errorf("EMR Cluster still exists")
// }
if output.Cluster == nil || output.Cluster.Status == nil || aws.StringValue(output.Cluster.Status.State) == emr.ClusterStateTerminated {
continue
}

log.Printf("[ERROR] %v", providerErr)
return fmt.Errorf("EMR Cluster still exists")
}

return nil
Expand All @@ -1478,7 +1477,7 @@ func testAccCheckAWSEmrClusterExists(n string, v *emr.Cluster) resource.TestChec
ClusterId: aws.String(rs.Primary.ID),
})
if err != nil {
return fmt.Errorf("EMR error: %v", err)
return fmt.Errorf("EMR error: %w", err)
}

if describe.Cluster == nil || *describe.Cluster.Id != rs.Primary.ID {
Expand Down Expand Up @@ -1549,7 +1548,7 @@ func testAccCheckAWSEmrClusterDisappears(cluster *emr.Cluster) resource.TestChec
}

if err != nil {
return fmt.Errorf("error waiting for EMR Cluster (%s) Instances to drain: %s", id, err)
return fmt.Errorf("error waiting for EMR Cluster (%s) Instances to drain: %w", id, err)
}

return nil
Expand Down Expand Up @@ -1584,10 +1583,10 @@ func testAccEmrDeleteManagedSecurityGroups(conn *ec2.EC2, vpc *ec2.Vpc) error {
}

for groupName := range managedSecurityGroups {
securityGroup, err := testAccEmrDescribeManagedSecurityGroup(conn, vpc, groupName)
securityGroup, err := finder.SecurityGroupByNameAndVpcID(conn, groupName, aws.StringValue(vpc.VpcId))

if err != nil {
return fmt.Errorf("error describing EMR Managed Security Group (%s): %s", groupName, err)
return fmt.Errorf("error describing EMR Managed Security Group (%s): %w", groupName, err)
}

managedSecurityGroups[groupName] = securityGroup
Expand All @@ -1603,7 +1602,7 @@ func testAccEmrDeleteManagedSecurityGroups(conn *ec2.EC2, vpc *ec2.Vpc) error {
err := testAccEmrRevokeManagedSecurityGroup(conn, securityGroup)

if err != nil {
return fmt.Errorf("error revoking EMR Managed Security Group (%s): %s", groupName, err)
return fmt.Errorf("error revoking EMR Managed Security Group (%s): %w", groupName, err)
}
}

Expand All @@ -1615,40 +1614,13 @@ func testAccEmrDeleteManagedSecurityGroups(conn *ec2.EC2, vpc *ec2.Vpc) error {
err := testAccEmrDeleteManagedSecurityGroup(conn, securityGroup)

if err != nil {
return fmt.Errorf("error deleting EMR Managed Security Group (%s): %s", groupName, err)
return fmt.Errorf("error deleting EMR Managed Security Group (%s): %w", groupName, err)
}
}

return nil
}

func testAccEmrDescribeManagedSecurityGroup(conn *ec2.EC2, vpc *ec2.Vpc, securityGroupName string) (*ec2.SecurityGroup, error) {
input := &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("group-name"),
Values: aws.StringSlice([]string{securityGroupName}),
},
{
Name: aws.String("vpc-id"),
Values: []*string{vpc.VpcId},
},
},
}

output, err := conn.DescribeSecurityGroups(input)

if err != nil {
return nil, err
}

if output == nil || len(output.SecurityGroups) != 1 {
return nil, nil
}

return output.SecurityGroups[0], nil
}

func testAccEmrRevokeManagedSecurityGroup(conn *ec2.EC2, securityGroup *ec2.SecurityGroup) error {
input := &ec2.RevokeSecurityGroupIngressInput{
GroupId: securityGroup.GroupId,
Expand Down
23 changes: 8 additions & 15 deletions aws/resource_aws_globalaccelerator_endpoint_group_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"errors"
"fmt"
"regexp"
"testing"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
ec2finder "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/globalaccelerator/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)
Expand Down Expand Up @@ -469,27 +471,18 @@ func testAccCheckGlobalAcceleratorEndpointGroupDeleteGlobalAcceleratorSecurityGr
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := &ec2.DescribeSecurityGroupsInput{
Filters: buildEC2AttributeFilterList(
map[string]string{
"group-name": "GlobalAccelerator",
"vpc-id": aws.StringValue(vpc.VpcId),
},
),
sg, err := ec2finder.SecurityGroupByNameAndVpcID(conn, "GlobalAccelerator", aws.StringValue(vpc.VpcId))
var nfe *resource.NotFoundError
if errors.As(err, &nfe) {
// Already gone.
return nil
}

output, err := conn.DescribeSecurityGroups(input)
if err != nil {
return err
}

if len(output.SecurityGroups) == 0 {
// Already gone.
return nil
}

_, err = conn.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{
GroupId: output.SecurityGroups[0].GroupId,
GroupId: sg.GroupId,
})
if err != nil {
return err
Expand Down
Loading

0 comments on commit e7674c3

Please sign in to comment.