github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_inspector_assessment_template.go (about) 1 package aws 2 3 import ( 4 "log" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/aws/awserr" 8 "github.com/aws/aws-sdk-go/service/inspector" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAWSInspectorAssessmentTemplate() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsInspectorAssessmentTemplateCreate, 15 Read: resourceAwsInspectorAssessmentTemplateRead, 16 Delete: resourceAwsInspectorAssessmentTemplateDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": &schema.Schema{ 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 "target_arn": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "arn": &schema.Schema{ 30 Type: schema.TypeString, 31 Computed: true, 32 ForceNew: true, 33 }, 34 "duration": &schema.Schema{ 35 Type: schema.TypeInt, 36 Required: true, 37 ForceNew: true, 38 }, 39 "rules_package_arns": &schema.Schema{ 40 Type: schema.TypeSet, 41 Elem: &schema.Schema{Type: schema.TypeString}, 42 Set: schema.HashString, 43 Required: true, 44 ForceNew: true, 45 }, 46 }, 47 } 48 } 49 50 func resourceAwsInspectorAssessmentTemplateCreate(d *schema.ResourceData, meta interface{}) error { 51 conn := meta.(*AWSClient).inspectorconn 52 53 rules := []*string{} 54 if attr := d.Get("rules_package_arns").(*schema.Set); attr.Len() > 0 { 55 rules = expandStringList(attr.List()) 56 } 57 58 targetArn := d.Get("target_arn").(string) 59 templateName := d.Get("name").(string) 60 duration := int64(d.Get("duration").(int)) 61 62 resp, err := conn.CreateAssessmentTemplate(&inspector.CreateAssessmentTemplateInput{ 63 AssessmentTargetArn: aws.String(targetArn), 64 AssessmentTemplateName: aws.String(templateName), 65 DurationInSeconds: aws.Int64(duration), 66 RulesPackageArns: rules, 67 }) 68 if err != nil { 69 return err 70 } 71 log.Printf("[DEBUG] Inspector Assessment Template %s created", *resp.AssessmentTemplateArn) 72 73 d.Set("arn", resp.AssessmentTemplateArn) 74 75 d.SetId(*resp.AssessmentTemplateArn) 76 77 return resourceAwsInspectorAssessmentTemplateRead(d, meta) 78 } 79 80 func resourceAwsInspectorAssessmentTemplateRead(d *schema.ResourceData, meta interface{}) error { 81 conn := meta.(*AWSClient).inspectorconn 82 83 resp, err := conn.DescribeAssessmentTemplates(&inspector.DescribeAssessmentTemplatesInput{ 84 AssessmentTemplateArns: []*string{ 85 aws.String(d.Id()), 86 }, 87 }, 88 ) 89 if err != nil { 90 if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "InvalidInputException" { 91 return nil 92 } else { 93 log.Printf("[ERROR] Error finding Inspector Assessment Template: %s", err) 94 return err 95 } 96 } 97 98 if resp.AssessmentTemplates != nil && len(resp.AssessmentTemplates) > 0 { 99 d.Set("name", resp.AssessmentTemplates[0].Name) 100 } 101 return nil 102 } 103 104 func resourceAwsInspectorAssessmentTemplateDelete(d *schema.ResourceData, meta interface{}) error { 105 conn := meta.(*AWSClient).inspectorconn 106 107 _, err := conn.DeleteAssessmentTemplate(&inspector.DeleteAssessmentTemplateInput{ 108 AssessmentTemplateArn: aws.String(d.Id()), 109 }) 110 if err != nil { 111 if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "AssessmentRunInProgressException" { 112 log.Printf("[ERROR] Assement Run in progress: %s", err) 113 return err 114 } else { 115 log.Printf("[ERROR] Error deleting Assement Template: %s", err) 116 return err 117 } 118 } 119 120 return nil 121 }