From cc2f4d4146c02faf0f4606917029d3a34d311ffd Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Mon, 18 Mar 2019 08:55:42 -0400 Subject: [PATCH] provider: Fix updated govet composites and nilness checks Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/7992 --- aws/config_test.go | 13 ++++++++++--- aws/resource_aws_db_instance.go | 7 ++----- aws/resource_aws_docdb_cluster_instance.go | 7 ++----- ...resource_aws_egress_only_internet_gateway.go | 4 ---- ...ource_aws_emr_security_configuration_test.go | 17 ++++++++--------- ...urce_aws_gamelift_game_session_queue_test.go | 17 +++++------------ aws/resource_aws_organizations_account_test.go | 9 +++++---- aws/resource_aws_organizations_policy_test.go | 9 +++++---- aws/resource_aws_vpn_connection_test.go | 8 ++------ aws/resource_aws_vpn_gateway_test.go | 13 +------------ 10 files changed, 40 insertions(+), 64 deletions(-) diff --git a/aws/config_test.go b/aws/config_test.go index 1e6594e3d93..603b10f6283 100644 --- a/aws/config_test.go +++ b/aws/config_test.go @@ -11,9 +11,16 @@ import ( func TestGetSupportedEC2Platforms(t *testing.T) { ec2Endpoints := []*awsbase.MockEndpoint{ { - Request: &awsbase.MockRequest{"POST", "/", "Action=DescribeAccountAttributes&" + - "AttributeName.1=supported-platforms&Version=2016-11-15"}, - Response: &awsbase.MockResponse{200, test_ec2_describeAccountAttributes_response, "text/xml"}, + Request: &awsbase.MockRequest{ + Method: "POST", + Uri: "/", + Body: "Action=DescribeAccountAttributes&AttributeName.1=supported-platforms&Version=2016-11-15", + }, + Response: &awsbase.MockResponse{ + StatusCode: 200, + Body: test_ec2_describeAccountAttributes_response, + ContentType: "text/xml", + }, }, } closeFunc, sess, err := awsbase.GetMockedAwsApiSession("EC2", ec2Endpoints) diff --git a/aws/resource_aws_db_instance.go b/aws/resource_aws_db_instance.go index 1a8c7544e2c..b58acad28a7 100644 --- a/aws/resource_aws_db_instance.go +++ b/aws/resource_aws_db_instance.go @@ -1544,11 +1544,8 @@ func resourceAwsDbInstanceRetrieve(id string, conn *rds.RDS) (*rds.DBInstance, e return nil, fmt.Errorf("Error retrieving DB Instances: %s", err) } - if len(resp.DBInstances) != 1 || - *resp.DBInstances[0].DBInstanceIdentifier != id { - if err != nil { - return nil, nil - } + if len(resp.DBInstances) != 1 || resp.DBInstances[0] == nil || aws.StringValue(resp.DBInstances[0].DBInstanceIdentifier) != id { + return nil, nil } return resp.DBInstances[0], nil diff --git a/aws/resource_aws_docdb_cluster_instance.go b/aws/resource_aws_docdb_cluster_instance.go index 510d0bd1de6..997bbed69c7 100644 --- a/aws/resource_aws_docdb_cluster_instance.go +++ b/aws/resource_aws_docdb_cluster_instance.go @@ -456,11 +456,8 @@ func resourceAwsDocDBInstanceRetrieve(id string, conn *docdb.DocDB) (*docdb.DBIn return nil, fmt.Errorf("Error retrieving DB Instances: %s", err) } - if len(resp.DBInstances) != 1 || - *resp.DBInstances[0].DBInstanceIdentifier != id { - if err != nil { - return nil, nil - } + if len(resp.DBInstances) != 1 || resp.DBInstances[0] == nil || aws.StringValue(resp.DBInstances[0].DBInstanceIdentifier) != id { + return nil, nil } return resp.DBInstances[0], nil diff --git a/aws/resource_aws_egress_only_internet_gateway.go b/aws/resource_aws_egress_only_internet_gateway.go index bb4cd863a37..413672d35d1 100644 --- a/aws/resource_aws_egress_only_internet_gateway.go +++ b/aws/resource_aws_egress_only_internet_gateway.go @@ -39,10 +39,6 @@ func resourceAwsEgressOnlyInternetGatewayCreate(d *schema.ResourceData, meta int d.SetId(*resp.EgressOnlyInternetGateway.EgressOnlyInternetGatewayId) - if err != nil { - return fmt.Errorf("%s", err) - } - return resourceAwsEgressOnlyInternetGatewayRead(d, meta) } diff --git a/aws/resource_aws_emr_security_configuration_test.go b/aws/resource_aws_emr_security_configuration_test.go index 71ae7cc8436..23766b4bb05 100644 --- a/aws/resource_aws_emr_security_configuration_test.go +++ b/aws/resource_aws_emr_security_configuration_test.go @@ -58,21 +58,20 @@ func testAccCheckEmrSecurityConfigurationDestroy(s *terraform.State) error { resp, err := conn.DescribeSecurityConfiguration(&emr.DescribeSecurityConfigurationInput{ Name: aws.String(rs.Primary.ID), }) - if err == nil { - if resp.Name != nil && *resp.Name == rs.Primary.ID { - // assume this means the resource still exists - return fmt.Errorf("Error: EMR Security Configuration still exists: %s", *resp.Name) - } + + if isAWSErr(err, "InvalidRequestException", "does not exist") { return nil } - // Verify the error is what we want if err != nil { - if isAWSErr(err, "InvalidRequestException", "does not exist") { - return nil - } return err } + + if resp != nil && aws.StringValue(resp.Name) == rs.Primary.ID { + return fmt.Errorf("Error: EMR Security Configuration still exists: %s", aws.StringValue(resp.Name)) + } + + return nil } return nil diff --git a/aws/resource_aws_gamelift_game_session_queue_test.go b/aws/resource_aws_gamelift_game_session_queue_test.go index ad304a39e43..f42d10f1f3d 100644 --- a/aws/resource_aws_gamelift_game_session_queue_test.go +++ b/aws/resource_aws_gamelift_game_session_queue_test.go @@ -31,11 +31,12 @@ func testSweepGameliftGameSessionQueue(region string) error { out, err := conn.DescribeGameSessionQueues(&gamelift.DescribeGameSessionQueuesInput{}) + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping Gamelife Queue sweep for %s: %s", region, err) + return nil + } + if err != nil { - if testSweepSkipSweepError(err) { - log.Printf("[WARN] Skipping Gamelife Queue sweep for %s: %s", region, err) - return nil - } return fmt.Errorf("error listing Gamelift Session Queue: %s", err) } @@ -57,14 +58,6 @@ func testSweepGameliftGameSessionQueue(region string) error { } } - if err != nil { - if testSweepSkipSweepError(err) { - log.Printf("[WARN] Skipping Gamelift Session Queue sweep for %s: %s", region, err) - return nil - } - return fmt.Errorf("error listing Gamelift Session Queue: %s", err) - } - return nil } diff --git a/aws/resource_aws_organizations_account_test.go b/aws/resource_aws_organizations_account_test.go index 6cb4b47d873..2859fa235b0 100644 --- a/aws/resource_aws_organizations_account_test.go +++ b/aws/resource_aws_organizations_account_test.go @@ -64,14 +64,15 @@ func testAccCheckAwsOrganizationsAccountDestroy(s *terraform.State) error { resp, err := conn.DescribeAccount(params) + if isAWSErr(err, organizations.ErrCodeAccountNotFoundException, "") { + return nil + } + if err != nil { - if isAWSErr(err, organizations.ErrCodeAccountNotFoundException, "") { - return nil - } return err } - if resp == nil && resp.Account != nil { + if resp != nil && resp.Account != nil { return fmt.Errorf("Bad: Account still exists: %q", rs.Primary.ID) } } diff --git a/aws/resource_aws_organizations_policy_test.go b/aws/resource_aws_organizations_policy_test.go index 3c1a4412009..b1ff538521c 100644 --- a/aws/resource_aws_organizations_policy_test.go +++ b/aws/resource_aws_organizations_policy_test.go @@ -98,14 +98,15 @@ func testAccCheckAwsOrganizationsPolicyDestroy(s *terraform.State) error { resp, err := conn.DescribePolicy(input) + if isAWSErr(err, organizations.ErrCodePolicyNotFoundException, "") { + return nil + } + if err != nil { - if isAWSErr(err, organizations.ErrCodePolicyNotFoundException, "") { - return nil - } return err } - if resp == nil && resp.Policy != nil { + if resp != nil && resp.Policy != nil { return fmt.Errorf("Policy %q still exists", rs.Primary.ID) } } diff --git a/aws/resource_aws_vpn_connection_test.go b/aws/resource_aws_vpn_connection_test.go index ece6bd94921..ba326488262 100644 --- a/aws/resource_aws_vpn_connection_test.go +++ b/aws/resource_aws_vpn_connection_test.go @@ -222,13 +222,9 @@ func testAccAWSVpnConnectionDisappears(connection *ec2.VpnConnection) resource.T _, err := conn.DeleteVpnConnection(&ec2.DeleteVpnConnectionInput{ VpnConnectionId: connection.VpnConnectionId, }) + if err != nil { - if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpnConnectionID.NotFound" { - return nil - } - if err != nil { - return err - } + return err } return resource.Retry(40*time.Minute, func() *resource.RetryError { diff --git a/aws/resource_aws_vpn_gateway_test.go b/aws/resource_aws_vpn_gateway_test.go index cef3b03f0aa..0e23fd9b045 100644 --- a/aws/resource_aws_vpn_gateway_test.go +++ b/aws/resource_aws_vpn_gateway_test.go @@ -374,18 +374,7 @@ func testAccAWSVpnGatewayDisappears(gateway *ec2.VpnGateway) resource.TestCheckF VpcId: gateway.VpcAttachments[0].VpcId, }) if err != nil { - ec2err, ok := err.(awserr.Error) - if ok { - if ec2err.Code() == "InvalidVpnGatewayID.NotFound" { - return nil - } else if ec2err.Code() == "InvalidVpnGatewayAttachment.NotFound" { - return nil - } - } - - if err != nil { - return err - } + return err } opts := &ec2.DeleteVpnGatewayInput{