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

skaff: Use names for errors, improve data source #25738

Merged
merged 6 commits into from
Jul 7, 2022
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
3 changes: 3 additions & 0 deletions .changelog/25738.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_config_remediation_configuration: Add `parameter.*.static_values` attribute for a list of values
```
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestAccConfigService_serial(t *testing.T) {
"disappears": testAccRemediationConfiguration_disappears,
"recreates": testAccRemediationConfiguration_recreates,
"updates": testAccRemediationConfiguration_updates,
"values": testAccRemediationConfiguration_values,
},
}

Expand Down
31 changes: 17 additions & 14 deletions internal/service/configservice/organization_custom_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package configservice

import (
"errors"
"fmt"
"log"
"time"

Expand Down Expand Up @@ -126,6 +125,10 @@ func ResourceOrganizationCustomRule() *schema.Resource {
}
}

const (
ResNameOrganizationCustomRule = "Organization Custom Rule"
)

func resourceOrganizationCustomRuleCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).ConfigServiceConn
name := d.Get("name").(string)
Expand Down Expand Up @@ -173,13 +176,13 @@ func resourceOrganizationCustomRuleCreate(d *schema.ResourceData, meta interface
_, err := conn.PutOrganizationConfigRule(input)

if err != nil {
return fmt.Errorf("error creating Config Organization Custom Rule (%s): %s", name, err)
return names.Error(names.ConfigService, names.ErrActionCreating, ResNameOrganizationCustomRule, name, err)
}

d.SetId(name)

if err := waitForOrganizationRuleStatusCreateSuccessful(conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return fmt.Errorf("error waiting for Config Organization Custom Rule (%s) creation: %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionWaitingForCreation, ResNameOrganizationCustomRule, d.Id(), err)
}

return resourceOrganizationCustomRuleRead(d, meta)
Expand All @@ -197,7 +200,7 @@ func resourceOrganizationCustomRuleRead(d *schema.ResourceData, meta interface{}
}

if err != nil {
return fmt.Errorf("error describing Config Organization Custom Rule (%s): %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionReading, ResNameOrganizationCustomRule, d.Id(), err)
}

if !d.IsNewResource() && rule == nil {
Expand All @@ -207,22 +210,22 @@ func resourceOrganizationCustomRuleRead(d *schema.ResourceData, meta interface{}
}

if d.IsNewResource() && rule == nil {
return names.Error(names.ConfigService, names.ErrActionReading, "Organization Custom Rule", d.Id(), errors.New("empty rule after creation"))
return names.Error(names.ConfigService, names.ErrActionReading, ResNameOrganizationCustomRule, d.Id(), errors.New("empty rule after creation"))
}

if rule.OrganizationManagedRuleMetadata != nil {
return fmt.Errorf("expected Config Organization Custom Rule, found Config Organization Custom Rule: %s", d.Id())
return names.Error(names.ConfigService, names.ErrActionReading, ResNameOrganizationCustomRule, d.Id(), errors.New("expected Organization Custom Rule, found Organization Managed Rule"))
}

if rule.OrganizationCustomRuleMetadata == nil {
return fmt.Errorf("error describing Config Organization Custom Rule (%s): empty metadata", d.Id())
return names.Error(names.ConfigService, names.ErrActionReading, ResNameOrganizationCustomRule, d.Id(), errors.New("empty metadata"))
}

d.Set("arn", rule.OrganizationConfigRuleArn)
d.Set("description", rule.OrganizationCustomRuleMetadata.Description)

if err := d.Set("excluded_accounts", aws.StringValueSlice(rule.ExcludedAccounts)); err != nil {
return fmt.Errorf("error setting excluded_accounts: %s", err)
return names.Error(names.ConfigService, names.ErrActionSetting, ResNameOrganizationCustomRule, d.Id(), err)
}

d.Set("input_parameters", rule.OrganizationCustomRuleMetadata.InputParameters)
Expand All @@ -232,14 +235,14 @@ func resourceOrganizationCustomRuleRead(d *schema.ResourceData, meta interface{}
d.Set("resource_id_scope", rule.OrganizationCustomRuleMetadata.ResourceIdScope)

if err := d.Set("resource_types_scope", aws.StringValueSlice(rule.OrganizationCustomRuleMetadata.ResourceTypesScope)); err != nil {
return fmt.Errorf("error setting resource_types_scope: %s", err)
return names.Error(names.ConfigService, names.ErrActionSetting, ResNameOrganizationCustomRule, d.Id(), err)
}

d.Set("tag_key_scope", rule.OrganizationCustomRuleMetadata.TagKeyScope)
d.Set("tag_value_scope", rule.OrganizationCustomRuleMetadata.TagValueScope)

if err := d.Set("trigger_types", aws.StringValueSlice(rule.OrganizationCustomRuleMetadata.OrganizationConfigRuleTriggerTypes)); err != nil {
return fmt.Errorf("error setting trigger_types: %s", err)
return names.Error(names.ConfigService, names.ErrActionSetting, ResNameOrganizationCustomRule, d.Id(), err)
}

return nil
Expand Down Expand Up @@ -291,11 +294,11 @@ func resourceOrganizationCustomRuleUpdate(d *schema.ResourceData, meta interface
_, err := conn.PutOrganizationConfigRule(input)

if err != nil {
return fmt.Errorf("error updating Config Organization Custom Rule (%s): %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionUpdating, ResNameOrganizationCustomRule, d.Id(), err)
}

if err := waitForOrganizationRuleStatusUpdateSuccessful(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for Config Organization Custom Rule (%s) update: %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionWaitingForUpdate, ResNameOrganizationCustomRule, d.Id(), err)
}

return resourceOrganizationCustomRuleRead(d, meta)
Expand All @@ -311,11 +314,11 @@ func resourceOrganizationCustomRuleDelete(d *schema.ResourceData, meta interface
_, err := conn.DeleteOrganizationConfigRule(input)

if err != nil {
return fmt.Errorf("error deleting Config Organization Custom Rule (%s): %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionDeleting, ResNameOrganizationCustomRule, d.Id(), err)
}

if err := waitForOrganizationRuleStatusDeleteSuccessful(conn, d.Id(), d.Timeout(schema.TimeoutDelete)); err != nil {
return fmt.Errorf("error waiting for Config Organization Custom Rule (%s) deletion: %s", d.Id(), err)
return names.Error(names.ConfigService, names.ErrActionWaitingForDeletion, ResNameOrganizationCustomRule, d.Id(), err)
}

return nil
Expand Down
20 changes: 11 additions & 9 deletions internal/service/configservice/organization_custom_rule_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configservice_test

import (
"errors"
"fmt"
"regexp"
"testing"
Expand All @@ -12,7 +13,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfconfig "github.com/hashicorp/terraform-provider-aws/internal/service/configservice"
tfconfigservice "github.com/hashicorp/terraform-provider-aws/internal/service/configservice"
"github.com/hashicorp/terraform-provider-aws/names"
)

func testAccOrganizationCustomRule_basic(t *testing.T) {
Expand Down Expand Up @@ -69,7 +71,7 @@ func testAccOrganizationCustomRule_disappears(t *testing.T) {
Config: testAccOrganizationCustomRuleConfig_triggerTypes1(rName, "ConfigurationItemChangeNotification"),
Check: resource.ComposeTestCheckFunc(
testAccCheckOrganizationCustomRuleExists(resourceName, &rule),
acctest.CheckResourceDisappears(acctest.Provider, tfconfig.ResourceOrganizationCustomRule(), resourceName),
acctest.CheckResourceDisappears(acctest.Provider, tfconfigservice.ResourceOrganizationCustomRule(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -443,19 +445,19 @@ func testAccCheckOrganizationCustomRuleExists(resourceName string, ocr *configse
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not Found: %s", resourceName)
return names.Error(names.ConfigService, names.ErrActionCheckingExistence, tfconfigservice.ResNameOrganizationCustomRule, resourceName, errors.New("not found"))
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ConfigServiceConn

rule, err := tfconfig.DescribeOrganizationConfigRule(conn, rs.Primary.ID)
rule, err := tfconfigservice.DescribeOrganizationConfigRule(conn, rs.Primary.ID)

if err != nil {
return fmt.Errorf("error describing Config Organization Managed Rule (%s): %s", rs.Primary.ID, err)
return names.Error(names.ConfigService, names.ErrActionCheckingExistence, tfconfigservice.ResNameOrganizationCustomRule, resourceName, err)
}

if rule == nil {
return fmt.Errorf("Config Organization Managed Rule (%s) not found", rs.Primary.ID)
return names.Error(names.ConfigService, names.ErrActionCheckingExistence, tfconfigservice.ResNameOrganizationCustomRule, resourceName, errors.New("empty response"))
}

*ocr = *rule
Expand All @@ -472,18 +474,18 @@ func testAccCheckOrganizationCustomRuleDestroy(s *terraform.State) error {
continue
}

rule, err := tfconfig.DescribeOrganizationConfigRule(conn, rs.Primary.ID)
rule, err := tfconfigservice.DescribeOrganizationConfigRule(conn, rs.Primary.ID)

if tfawserr.ErrCodeEquals(err, configservice.ErrCodeNoSuchOrganizationConfigRuleException) {
continue
}

if err != nil {
return fmt.Errorf("error describing Config Organization Managed Rule (%s): %s", rs.Primary.ID, err)
return names.Error(names.ConfigService, names.ErrActionCheckingDestroyed, tfconfigservice.ResNameOrganizationCustomRule, rs.Primary.ID, err)
}

if rule != nil {
return fmt.Errorf("Config Organization Managed Rule (%s) still exists", rs.Primary.ID)
return names.Error(names.ConfigService, names.ErrActionCheckingDestroyed, tfconfigservice.ResNameOrganizationCustomRule, rs.Primary.ID, errors.New("still exists"))
}
}

Expand Down
Loading