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

r/aws_ssm_service_setting: fix panic on empty result #27232

Merged
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/27232.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ssm_service_setting: Prevent panic during status read
```
13 changes: 10 additions & 3 deletions internal/service/ssm/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,26 @@ func FindPatchGroup(conn *ssm.SSM, patchGroup, baselineId string) (*ssm.PatchGro
return result, err
}

func FindServiceSettingByARN(conn *ssm.SSM, arn string) (*ssm.ServiceSetting, error) {
func FindServiceSettingByID(conn *ssm.SSM, id string) (*ssm.ServiceSetting, error) {
input := &ssm.GetServiceSettingInput{
SettingId: aws.String(arn),
SettingId: aws.String(id),
}

output, err := conn.GetServiceSetting(input)

if tfawserr.ErrCodeContains(err, ssm.ErrCodeServiceSettingNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.ServiceSetting == nil {
return nil, fmt.Errorf("finding %s: empty result", arn)
return nil, tfresource.NewEmptyResultError(input)
}

return output.ServiceSetting, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ssm/service_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func resourceServiceSettingRead(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] Reading SSM Activation: %s", d.Id())

output, err := FindServiceSettingByARN(conn, d.Id())
output, err := FindServiceSettingByID(conn, d.Id())
if err != nil {
return create.Error(names.SSM, create.ErrActionReading, ResNameServiceSetting, d.Id(), err)
}
Expand Down
31 changes: 20 additions & 11 deletions internal/service/ssm/service_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ssm"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/create"
tfssm "github.com/hashicorp/terraform-provider-aws/internal/service/ssm"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -57,37 +57,46 @@ func testAccServiceSettingDestroy(s *terraform.State) error {
continue
}

output, err := conn.GetServiceSetting(&ssm.GetServiceSettingInput{
SettingId: aws.String(rs.Primary.Attributes["setting_id"]),
})
_, ok := err.(awserr.Error)
if !ok {
output, err := tfssm.FindServiceSettingByID(conn, rs.Primary.Attributes["setting_id"])

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}
if output.ServiceSetting.Status != aws.String("default") {
return create.Error(names.SSM, create.ErrActionCheckingDestroyed, tfssm.ResNameServiceSetting, rs.Primary.Attributes["setting_id"], err)

if aws.StringValue(output.Status) == "Default" {
continue
}

return create.Error(names.SSM, create.ErrActionCheckingDestroyed, tfssm.ResNameServiceSetting, rs.Primary.Attributes["setting_id"], err)
}

return nil
}

func testAccServiceSettingExists(n string, res *ssm.ServiceSetting) resource.TestCheckFunc {
func testAccServiceSettingExists(n string, v *ssm.ServiceSetting) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No SSM Service Setting ID is set")
}

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

output, err := tfssm.FindServiceSettingByARN(conn, rs.Primary.Attributes["setting_id"])
output, err := tfssm.FindServiceSettingByID(conn, rs.Primary.Attributes["setting_id"])

if err != nil {
return create.Error(names.SSM, create.ErrActionReading, tfssm.ResNameServiceSetting, rs.Primary.Attributes["setting_id"], err)
}

*res = *output
*v = *output

return nil
}
Expand Down
8 changes: 6 additions & 2 deletions internal/service/ssm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ func statusDocument(conn *ssm.SSM, name string) resource.StateRefreshFunc {
}
}

func statusServiceSetting(conn *ssm.SSM, arn string) resource.StateRefreshFunc {
func statusServiceSetting(conn *ssm.SSM, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindServiceSettingByARN(conn, arn)
output, err := FindServiceSettingByID(conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
}

if err != nil {
return nil, "", err
}

return output, aws.StringValue(output.Status), nil
}
}
8 changes: 4 additions & 4 deletions internal/service/ssm/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func waitDocumentActive(conn *ssm.SSM, name string) (*ssm.DocumentDescription, e
return nil, err
}

func waitServiceSettingUpdated(conn *ssm.SSM, arn string, timeout time.Duration) (*ssm.ServiceSetting, error) {
func waitServiceSettingUpdated(conn *ssm.SSM, id string, timeout time.Duration) (*ssm.ServiceSetting, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"PendingUpdate", ""},
Target: []string{"Customized", "Default"},
Refresh: statusServiceSetting(conn, arn),
Refresh: statusServiceSetting(conn, id),
Timeout: timeout,
}

Expand All @@ -88,11 +88,11 @@ func waitServiceSettingUpdated(conn *ssm.SSM, arn string, timeout time.Duration)
return nil, err
}

func waitServiceSettingReset(conn *ssm.SSM, arn string, timeout time.Duration) error {
func waitServiceSettingReset(conn *ssm.SSM, id string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"Customized", "PendingUpdate", ""},
Target: []string{"Default"},
Refresh: statusServiceSetting(conn, arn),
Refresh: statusServiceSetting(conn, id),
Timeout: timeout,
}

Expand Down