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

resource,data_source/aws_servicequotas_service_quota: Handle embedded error in ServiceQuota struct #19722

Merged
merged 3 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions .changelog/19722.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
data-source/aws_servicequotas_service_quota: Correctly handle errors embedded in API struct
```

```release-note:bug
resource/aws_servicequotas_service_quota: Correctly handle errors embedded in API struct
```
10 changes: 9 additions & 1 deletion aws/data_source_aws_servicequotas_service_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,21 @@ func dataSourceAwsServiceQuotasServiceQuotaRead(d *schema.ResourceData, meta int
return fmt.Errorf("error getting Service (%s) Quota (%s): %w", serviceCode, quotaCode, err)
}

if output == nil {
if output == nil || output.Quota == nil {
return fmt.Errorf("error getting Service (%s) Quota (%s): empty result", serviceCode, quotaCode)
}

serviceQuota = output.Quota
}

if serviceQuota.ErrorReason != nil {
return fmt.Errorf("error getting Service (%s) Quota (%s): %s: %s", serviceCode, quotaCode, aws.StringValue(serviceQuota.ErrorReason.ErrorCode), aws.StringValue(serviceQuota.ErrorReason.ErrorMessage))
}

if serviceQuota.Value == nil {
return fmt.Errorf("error getting Service (%s) Quota (%s): empty value", serviceCode, quotaCode)
}

input := &servicequotas.GetAWSDefaultServiceQuotaInput{
QuotaCode: serviceQuota.QuotaCode,
ServiceCode: serviceQuota.ServiceCode,
Expand Down
92 changes: 92 additions & 0 deletions aws/data_source_aws_servicequotas_service_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func TestAccAwsServiceQuotasServiceQuotaDataSource_QuotaCode(t *testing.T) {
})
}

func TestAccAwsServiceQuotasServiceQuotaDataSource_PermissionError_QuotaCode(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceQuotas(t); testAccAssumeRoleARNPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, servicequotas.EndpointsID),
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAwsServiceQuotasServiceQuotaDataSourceConfig_PermissionError_QuotaCode("elasticloadbalancing", "L-53DA6B97"),
ExpectError: regexp.MustCompile(`DEPENDENCY_ACCESS_DENIED_ERROR`),
},
},
})
}

func TestAccAwsServiceQuotasServiceQuotaDataSource_QuotaName(t *testing.T) {
dataSourceName := "data.aws_servicequotas_service_quota.test"

Expand All @@ -61,6 +76,21 @@ func TestAccAwsServiceQuotasServiceQuotaDataSource_QuotaName(t *testing.T) {
})
}

func TestAccAwsServiceQuotasServiceQuotaDataSource_PermissionError_QuotaName(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceQuotas(t); testAccAssumeRoleARNPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, servicequotas.EndpointsID),
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAwsServiceQuotasServiceQuotaDataSourceConfig_PermissionError_QuotaName("elasticloadbalancing", "Application Load Balancers per Region"),
ExpectError: regexp.MustCompile(`DEPENDENCY_ACCESS_DENIED_ERROR`),
},
},
})
}

func testAccAwsServiceQuotasServiceQuotaDataSourceConfigQuotaCode(serviceCode, quotaCode string) string {
return fmt.Sprintf(`
data "aws_servicequotas_service_quota" "test" {
Expand All @@ -70,6 +100,37 @@ data "aws_servicequotas_service_quota" "test" {
`, quotaCode, serviceCode)
}

func testAccAwsServiceQuotasServiceQuotaDataSourceConfig_PermissionError_QuotaCode(serviceCode, quotaCode string) string {
policy := `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"servicequotas:GetServiceQuota"
],
"Resource": "*"
},
{
"Effect": "Deny",
"Action": [
"elasticloadbalancing:*"
],
"Resource": "*"
}
]
}`

return composeConfig(
testAccProviderConfigAssumeRolePolicy(policy),
fmt.Sprintf(`
data "aws_servicequotas_service_quota" "test" {
service_code = %[1]q
quota_code = %[2]q
}
`, serviceCode, quotaCode))
}

func testAccAwsServiceQuotasServiceQuotaDataSourceConfigQuotaName(serviceCode, quotaName string) string {
return fmt.Sprintf(`
data "aws_servicequotas_service_quota" "test" {
Expand All @@ -78,3 +139,34 @@ data "aws_servicequotas_service_quota" "test" {
}
`, quotaName, serviceCode)
}

func testAccAwsServiceQuotasServiceQuotaDataSourceConfig_PermissionError_QuotaName(serviceCode, quotaName string) string {
policy := `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"servicequotas:ListServiceQuotas"
],
"Resource": "*"
},
{
"Effect": "Deny",
"Action": [
"elasticloadbalancing:*"
],
"Resource": "*"
}
]
}`

return composeConfig(
testAccProviderConfigAssumeRolePolicy(policy),
fmt.Sprintf(`
data "aws_servicequotas_service_quota" "test" {
service_code = %[1]q
quota_name = %[2]q
}
`, serviceCode, quotaName))
}
20 changes: 18 additions & 2 deletions aws/resource_aws_servicequotas_service_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ func resourceAwsServiceQuotasServiceQuotaCreate(d *schema.ResourceData, meta int
return fmt.Errorf("error getting Service Quotas Service Quota (%s): %s", d.Id(), err)
}

if output == nil {
if output == nil || output.Quota == nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): empty result", d.Id())
}

if output.Quota.ErrorReason != nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): %s: %s", d.Id(), aws.StringValue(output.Quota.ErrorReason.ErrorCode), aws.StringValue(output.Quota.ErrorReason.ErrorMessage))
}

if output.Quota.Value == nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): empty value", d.Id())
}

if value > aws.Float64Value(output.Quota.Value) {
input := &servicequotas.RequestServiceQuotaIncreaseInput{
DesiredValue: aws.Float64(value),
Expand Down Expand Up @@ -152,10 +160,18 @@ func resourceAwsServiceQuotasServiceQuotaRead(d *schema.ResourceData, meta inter
return fmt.Errorf("error getting Service Quotas Service Quota (%s): %s", d.Id(), err)
}

if output == nil {
if output == nil || output.Quota == nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): empty result", d.Id())
}

if output.Quota.ErrorReason != nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): %s: %s", d.Id(), aws.StringValue(output.Quota.ErrorReason.ErrorCode), aws.StringValue(output.Quota.ErrorReason.ErrorMessage))
}

if output.Quota.Value == nil {
return fmt.Errorf("error getting Service Quotas Service Quota (%s): empty value", d.Id())
}

defaultInput := &servicequotas.GetAWSDefaultServiceQuotaInput{
QuotaCode: aws.String(quotaCode),
ServiceCode: aws.String(serviceCode),
Expand Down
48 changes: 48 additions & 0 deletions aws/resource_aws_servicequotas_service_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"os"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/service/servicequotas"
Expand Down Expand Up @@ -138,6 +139,21 @@ func TestAccAwsServiceQuotasServiceQuota_Value_IncreaseOnUpdate(t *testing.T) {
})
}

func TestAccAwsServiceQuotasServiceQuota_PermissionError(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceQuotas(t); testAccAssumeRoleARNPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, servicequotas.EndpointsID),
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAwsServiceQuotasServiceQuotaConfig_PermissionError("elasticloadbalancing", "L-53DA6B97"),
ExpectError: regexp.MustCompile(`DEPENDENCY_ACCESS_DENIED_ERROR`),
},
},
})
}

func testAccPreCheckAWSServiceQuotas(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).servicequotasconn

Expand Down Expand Up @@ -178,3 +194,35 @@ resource "aws_servicequotas_service_quota" "test" {
}
`, quotaCode, serviceCode, value)
}

func testAccAwsServiceQuotasServiceQuotaConfig_PermissionError(serviceCode, quotaCode string) string {
policy := `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"servicequotas:GetServiceQuota"
],
"Resource": "*"
},
{
"Effect": "Deny",
"Action": [
"elasticloadbalancing:*"
],
"Resource": "*"
}
]
}`

return composeConfig(
testAccProviderConfigAssumeRolePolicy(policy),
fmt.Sprintf(`
resource "aws_servicequotas_service_quota" "test" {
service_code = %[1]q
quota_code = %[2]q
value = 1
}
`, serviceCode, quotaCode))
}