github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/resource_aws_ssm_patch_group.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/service/ssm"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceAwsSsmPatchGroup() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceAwsSsmPatchGroupCreate,
    14  		Read:   resourceAwsSsmPatchGroupRead,
    15  		Delete: resourceAwsSsmPatchGroupDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"baseline_id": {
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  			"patch_group": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourceAwsSsmPatchGroupCreate(d *schema.ResourceData, meta interface{}) error {
    33  	ssmconn := meta.(*AWSClient).ssmconn
    34  
    35  	params := &ssm.RegisterPatchBaselineForPatchGroupInput{
    36  		BaselineId: aws.String(d.Get("baseline_id").(string)),
    37  		PatchGroup: aws.String(d.Get("patch_group").(string)),
    38  	}
    39  
    40  	resp, err := ssmconn.RegisterPatchBaselineForPatchGroup(params)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	d.SetId(*resp.PatchGroup)
    46  	return resourceAwsSsmPatchGroupRead(d, meta)
    47  }
    48  
    49  func resourceAwsSsmPatchGroupRead(d *schema.ResourceData, meta interface{}) error {
    50  	ssmconn := meta.(*AWSClient).ssmconn
    51  
    52  	params := &ssm.DescribePatchGroupsInput{}
    53  
    54  	resp, err := ssmconn.DescribePatchGroups(params)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	found := false
    60  	for _, t := range resp.Mappings {
    61  		if *t.PatchGroup == d.Id() {
    62  			found = true
    63  
    64  			d.Set("patch_group", t.PatchGroup)
    65  			d.Set("baseline_id", t.BaselineIdentity.BaselineId)
    66  		}
    67  	}
    68  
    69  	if !found {
    70  		log.Printf("[INFO] Patch Group not found. Removing from state")
    71  		d.SetId("")
    72  		return nil
    73  	}
    74  
    75  	return nil
    76  
    77  }
    78  
    79  func resourceAwsSsmPatchGroupDelete(d *schema.ResourceData, meta interface{}) error {
    80  	ssmconn := meta.(*AWSClient).ssmconn
    81  
    82  	log.Printf("[INFO] Deleting SSM Patch Group: %s", d.Id())
    83  
    84  	params := &ssm.DeregisterPatchBaselineForPatchGroupInput{
    85  		BaselineId: aws.String(d.Get("baseline_id").(string)),
    86  		PatchGroup: aws.String(d.Get("patch_group").(string)),
    87  	}
    88  
    89  	_, err := ssmconn.DeregisterPatchBaselineForPatchGroup(params)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	return nil
    95  }