github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				ForceNew: true,
    25  				Required: true,
    26  			},
    27  			"arn": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Computed: true,
    30  			},
    31  			"resource_group_arn": &schema.Schema{
    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  	}
    91  
    92  	if d.HasChange("name") {
    93  		_, n := d.GetChange("name")
    94  		input.AssessmentTargetName = aws.String(n.(string))
    95  	}
    96  
    97  	if d.HasChange("resource_group_arn") {
    98  		_, n := d.GetChange("resource_group_arn")
    99  		input.AssessmentTargetName = aws.String(n.(string))
   100  	}
   101  
   102  	_, err := conn.UpdateAssessmentTarget(&input)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	log.Println("[DEBUG] Inspector Assessment Target updated")
   108  
   109  	return resourceAwsInspectorAssessmentTargetRead(d, meta)
   110  }
   111  
   112  func resourceAwsInspectorAssessmentTargetDelete(d *schema.ResourceData, meta interface{}) error {
   113  	conn := meta.(*AWSClient).inspectorconn
   114  
   115  	return resource.Retry(60*time.Minute, func() *resource.RetryError {
   116  		_, err := conn.DeleteAssessmentTarget(&inspector.DeleteAssessmentTargetInput{
   117  			AssessmentTargetArn: aws.String(d.Id()),
   118  		})
   119  		if err != nil {
   120  			if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "AssessmentRunInProgressException" {
   121  				log.Printf("[ERROR] Assement Run in progress: %s", err)
   122  				return resource.RetryableError(err)
   123  			} else {
   124  				log.Printf("[ERROR] Error deleting Assement Target: %s", err)
   125  				return resource.NonRetryableError(err)
   126  			}
   127  		}
   128  		return nil
   129  	})
   130  
   131  }