Skip to content

Commit

Permalink
address ci findings
Browse files Browse the repository at this point in the history
address ci findings

address ci findings

address ci findings
  • Loading branch information
albsilv-aws committed Sep 27, 2022
1 parent 8d05133 commit 53f72d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
8 changes: 5 additions & 3 deletions internal/service/controltower/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ func resourceControlRead(ctx context.Context, d *schema.ResourceData, meta inter
}

for _, c := range output.EnabledControls {
if *c.ControlIdentifier == d.Get("control_identifier") {
if aws.StringValue(c.ControlIdentifier) == d.Get("control_identifier") {
return nil
}
}

// control identifier not found in response
log.Printf("[WARN] ControlTower Control (%s) not found, removing from state", d.Id())
d.SetId("")
if !d.IsNewResource() {
log.Printf("[WARN] ControlTower Control (%s) not found, removing from state", d.Id())
d.SetId("")
}
return nil
}

Expand Down
33 changes: 32 additions & 1 deletion internal/service/controltower/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

"github.com/aws/aws-sdk-go/service/controltower"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func TestAccControlTowerControl_basic(t *testing.T) {
Expand All @@ -21,6 +23,7 @@ func TestAccControlTowerControl_basic(t *testing.T) {
acctest.PreCheckControlTowerDeployed(t)
},
ErrorCheck: acctest.ErrorCheck(t, controltower.EndpointsID),
CheckDestroy: testAccCheckControlDisable,
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -37,18 +40,46 @@ func testAccControlConfig_basic(controlName string, ouName string) string {
return fmt.Sprintf(`
data "aws_region" "current" {}
data "aws_partition" "current" {}
data "aws_organizations_organization" "test" {}
data "aws_organizations_organizational_units" "test" {
parent_id = data.aws_organizations_organization.test.roots[0].id
}
resource "aws_controltower_control" "test" {
control_identifier = "arn:aws:controltower:${data.aws_region.current.name}::control/%[1]s"
control_identifier = "arn:${data.aws_partition.current.partition}:controltower:${data.aws_region.current.name}::control/%[1]s"
target_identifier = [
for x in data.aws_organizations_organizational_units.test.children :
x.arn if x.name == "%[2]s"
][0]
}
`, controlName, ouName)
}

func testAccCheckControlDisable(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_controltower_control" {
continue
}
conn := acctest.Provider.Meta().(*conns.AWSClient).ControlTowerConn
input := &controltower.ListEnabledControlsInput{
TargetIdentifier: &rs.Primary.ID,
}

output, err := conn.ListEnabledControls(input)

if err != nil {
return err
}

for _, c := range output.EnabledControls {
if *c.ControlIdentifier == rs.Primary.Attributes["control_identifier"] {
return fmt.Errorf("ControlTower Control still enabled: %s", rs.Primary.ID)
}
}
}

return nil
}
3 changes: 2 additions & 1 deletion internal/service/controltower/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
controlTimeout = 60 * time.Minute
)

//nolint:unparam //linter is producing false positive
func waitControl(ctx context.Context, conn *controltower.ControlTower, operation_identifier string) (*controltower.ControlOperation, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{controltower.ControlOperationStatusInProgress},
Expand All @@ -21,7 +22,7 @@ func waitControl(ctx context.Context, conn *controltower.ControlTower, operation
Timeout: controlTimeout,
}

outputRaw, err := stateConf.WaitForState()
outputRaw, err := stateConf.WaitForStateContext(ctx)

if v, ok := outputRaw.(*controltower.ControlOperation); ok {
return v, err
Expand Down

0 comments on commit 53f72d0

Please sign in to comment.