github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_inspector_assessment_target.go (about) 1 package aws 2 3 import ( 4 "log" 5 "time" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/inspector" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceAWSInspectorAssessmentTarget() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsInspectorAssessmentTargetCreate, 17 Read: resourceAwsInspectorAssessmentTargetRead, 18 Update: resourceAwsInspectorAssessmentTargetUpdate, 19 Delete: resourceAwsInspectorAssessmentTargetDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "name": { 23 Type: schema.TypeString, 24 ForceNew: true, 25 Required: true, 26 }, 27 "arn": { 28 Type: schema.TypeString, 29 Computed: true, 30 }, 31 "resource_group_arn": { 32 Type: schema.TypeString, 33 Required: true, 34 }, 35 }, 36 } 37 } 38 39 func resourceAwsInspectorAssessmentTargetCreate(d *schema.ResourceData, meta interface{}) error { 40 conn := meta.(*AWSClient).inspectorconn 41 42 targetName := d.Get("name").(string) 43 resourceGroupArn := d.Get("resource_group_arn").(string) 44 45 resp, err := conn.CreateAssessmentTarget(&inspector.CreateAssessmentTargetInput{ 46 AssessmentTargetName: aws.String(targetName), 47 ResourceGroupArn: aws.String(resourceGroupArn), 48 }) 49 if err != nil { 50 return err 51 } 52 log.Printf("[DEBUG] Inspector Assessment %s created", *resp.AssessmentTargetArn) 53 54 d.Set("arn", resp.AssessmentTargetArn) 55 d.SetId(*resp.AssessmentTargetArn) 56 57 return resourceAwsInspectorAssessmentTargetRead(d, meta) 58 } 59 60 func resourceAwsInspectorAssessmentTargetRead(d *schema.ResourceData, meta interface{}) error { 61 conn := meta.(*AWSClient).inspectorconn 62 63 resp, err := conn.DescribeAssessmentTargets(&inspector.DescribeAssessmentTargetsInput{ 64 AssessmentTargetArns: []*string{ 65 aws.String(d.Id()), 66 }, 67 }) 68 69 if err != nil { 70 if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "InvalidInputException" { 71 return nil 72 } else { 73 log.Printf("[ERROR] Error finding Inspector Assessment Target: %s", err) 74 return err 75 } 76 } 77 78 if resp.AssessmentTargets != nil && len(resp.AssessmentTargets) > 0 { 79 d.Set("name", resp.AssessmentTargets[0].Name) 80 } 81 82 return nil 83 } 84 85 func resourceAwsInspectorAssessmentTargetUpdate(d *schema.ResourceData, meta interface{}) error { 86 conn := meta.(*AWSClient).inspectorconn 87 88 input := inspector.UpdateAssessmentTargetInput{ 89 AssessmentTargetArn: aws.String(d.Id()), 90 AssessmentTargetName: aws.String(d.Get("name").(string)), 91 ResourceGroupArn: aws.String(d.Get("resource_group_arn").(string)), 92 } 93 94 _, err := conn.UpdateAssessmentTarget(&input) 95 if err != nil { 96 return err 97 } 98 99 log.Println("[DEBUG] Inspector Assessment Target updated") 100 101 return resourceAwsInspectorAssessmentTargetRead(d, meta) 102 } 103 104 func resourceAwsInspectorAssessmentTargetDelete(d *schema.ResourceData, meta interface{}) error { 105 conn := meta.(*AWSClient).inspectorconn 106 107 return resource.Retry(60*time.Minute, func() *resource.RetryError { 108 _, err := conn.DeleteAssessmentTarget(&inspector.DeleteAssessmentTargetInput{ 109 AssessmentTargetArn: aws.String(d.Id()), 110 }) 111 if err != nil { 112 if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "AssessmentRunInProgressException" { 113 log.Printf("[ERROR] Assement Run in progress: %s", err) 114 return resource.RetryableError(err) 115 } else { 116 log.Printf("[ERROR] Error deleting Assement Target: %s", err) 117 return resource.NonRetryableError(err) 118 } 119 } 120 return nil 121 }) 122 123 }