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/aws_inspector_assessment_target: Various enhancements and fixes #7112

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
97 changes: 62 additions & 35 deletions aws/resource_aws_inspector_assessment_target.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/inspector"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -17,6 +17,9 @@ func resourceAWSInspectorAssessmentTarget() *schema.Resource {
Read: resourceAwsInspectorAssessmentTargetRead,
Update: resourceAwsInspectorAssessmentTargetUpdate,
Delete: resourceAwsInspectorAssessmentTargetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -30,7 +33,7 @@ func resourceAWSInspectorAssessmentTarget() *schema.Resource {
},
"resource_group_arn": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
}
Expand All @@ -39,19 +42,19 @@ func resourceAWSInspectorAssessmentTarget() *schema.Resource {
func resourceAwsInspectorAssessmentTargetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).inspectorconn

targetName := d.Get("name").(string)
resourceGroupArn := d.Get("resource_group_arn").(string)
input := &inspector.CreateAssessmentTargetInput{
AssessmentTargetName: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("resource_group_arn"); ok {
input.ResourceGroupArn = aws.String(v.(string))
}

resp, err := conn.CreateAssessmentTarget(&inspector.CreateAssessmentTargetInput{
AssessmentTargetName: aws.String(targetName),
ResourceGroupArn: aws.String(resourceGroupArn),
})
resp, err := conn.CreateAssessmentTarget(input)
if err != nil {
return err
return fmt.Errorf("error creating Inspector Assessment Target: %s", err)
}
log.Printf("[DEBUG] Inspector Assessment %s created", *resp.AssessmentTargetArn)

d.Set("arn", resp.AssessmentTargetArn)
d.SetId(*resp.AssessmentTargetArn)

return resourceAwsInspectorAssessmentTargetRead(d, meta)
Expand All @@ -60,25 +63,22 @@ func resourceAwsInspectorAssessmentTargetCreate(d *schema.ResourceData, meta int
func resourceAwsInspectorAssessmentTargetRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).inspectorconn

resp, err := conn.DescribeAssessmentTargets(&inspector.DescribeAssessmentTargetsInput{
AssessmentTargetArns: []*string{
aws.String(d.Id()),
},
})
assessmentTarget, err := describeInspectorAssessmentTarget(conn, d.Id())

if err != nil {
if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "InvalidInputException" {
return nil
} else {
log.Printf("[ERROR] Error finding Inspector Assessment Target: %s", err)
return err
}
return fmt.Errorf("error describing Inspector Assessment Target (%s): %s", d.Id(), err)
}

if resp.AssessmentTargets != nil && len(resp.AssessmentTargets) > 0 {
d.Set("name", resp.AssessmentTargets[0].Name)
if assessmentTarget == nil {
log.Printf("[WARN] Inspector Assessment Target (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("arn", assessmentTarget.Arn)
d.Set("name", assessmentTarget.Name)
d.Set("resource_group_arn", assessmentTarget.ResourceGroupArn)

return nil
}

Expand All @@ -88,16 +88,17 @@ func resourceAwsInspectorAssessmentTargetUpdate(d *schema.ResourceData, meta int
input := inspector.UpdateAssessmentTargetInput{
AssessmentTargetArn: aws.String(d.Id()),
AssessmentTargetName: aws.String(d.Get("name").(string)),
ResourceGroupArn: aws.String(d.Get("resource_group_arn").(string)),
}

if v, ok := d.GetOk("resource_group_arn"); ok {
input.ResourceGroupArn = aws.String(v.(string))
}

_, err := conn.UpdateAssessmentTarget(&input)
if err != nil {
return err
return fmt.Errorf("error updating Inspector Assessment Target (%s): %s", d.Id(), err)
}

log.Println("[DEBUG] Inspector Assessment Target updated")

return resourceAwsInspectorAssessmentTargetRead(d, meta)
}

Expand All @@ -108,16 +109,42 @@ func resourceAwsInspectorAssessmentTargetDelete(d *schema.ResourceData, meta int
_, err := conn.DeleteAssessmentTarget(&inspector.DeleteAssessmentTargetInput{
AssessmentTargetArn: aws.String(d.Id()),
})

if isAWSErr(err, inspector.ErrCodeAssessmentRunInProgressException, "") {
return resource.RetryableError(err)
}

if err != nil {
if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "AssessmentRunInProgressException" {
log.Printf("[ERROR] Assement Run in progress: %s", err)
return resource.RetryableError(err)
} else {
log.Printf("[ERROR] Error deleting Assement Target: %s", err)
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
}

return nil
})

}

func describeInspectorAssessmentTarget(conn *inspector.Inspector, arn string) (*inspector.AssessmentTarget, error) {
input := &inspector.DescribeAssessmentTargetsInput{
AssessmentTargetArns: []*string{aws.String(arn)},
}

output, err := conn.DescribeAssessmentTargets(input)

if isAWSErr(err, inspector.ErrCodeInvalidInputException, "") {
return nil, nil
}

if err != nil {
return nil, err
}

var assessmentTarget *inspector.AssessmentTarget
for _, target := range output.AssessmentTargets {
if aws.StringValue(target.Arn) == arn {
assessmentTarget = target
break
}
}

return assessmentTarget, nil
}
Loading