github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_ssm_activation.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/ssm"
    10  	"github.com/hashicorp/errwrap"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsSsmActivation() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsSsmActivationCreate,
    18  		Read:   resourceAwsSsmActivationRead,
    19  		Delete: resourceAwsSsmActivationDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  				ForceNew: true,
    26  			},
    27  			"description": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  			"expired": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"expiration_date": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				ForceNew: true,
    40  			},
    41  			"iam_role": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  				ForceNew: true,
    45  			},
    46  			"registration_limit": &schema.Schema{
    47  				Type:     schema.TypeInt,
    48  				Optional: true,
    49  				ForceNew: true,
    50  			},
    51  			"registration_count": &schema.Schema{
    52  				Type:     schema.TypeInt,
    53  				Computed: true,
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceAwsSsmActivationCreate(d *schema.ResourceData, meta interface{}) error {
    60  	ssmconn := meta.(*AWSClient).ssmconn
    61  
    62  	log.Printf("[DEBUG] SSM activation create: %s", d.Id())
    63  
    64  	activationInput := &ssm.CreateActivationInput{
    65  		IamRole: aws.String(d.Get("name").(string)),
    66  	}
    67  
    68  	if _, ok := d.GetOk("name"); ok {
    69  		activationInput.DefaultInstanceName = aws.String(d.Get("name").(string))
    70  	}
    71  
    72  	if _, ok := d.GetOk("description"); ok {
    73  		activationInput.Description = aws.String(d.Get("description").(string))
    74  	}
    75  
    76  	if _, ok := d.GetOk("expiration_date"); ok {
    77  		activationInput.ExpirationDate = aws.Time(d.Get("expiration_date").(time.Time))
    78  	}
    79  
    80  	if _, ok := d.GetOk("iam_role"); ok {
    81  		activationInput.IamRole = aws.String(d.Get("iam_role").(string))
    82  	}
    83  
    84  	if _, ok := d.GetOk("registration_limit"); ok {
    85  		activationInput.RegistrationLimit = aws.Int64(int64(d.Get("registration_limit").(int)))
    86  	}
    87  
    88  	// Retry to allow iam_role to be created and policy attachment to take place
    89  	var resp *ssm.CreateActivationOutput
    90  	err := resource.Retry(30*time.Second, func() *resource.RetryError {
    91  		var err error
    92  
    93  		resp, err = ssmconn.CreateActivation(activationInput)
    94  
    95  		if err != nil {
    96  			return resource.RetryableError(err)
    97  		}
    98  
    99  		return resource.NonRetryableError(err)
   100  	})
   101  
   102  	if err != nil {
   103  		return errwrap.Wrapf("[ERROR] Error creating SSM activation: {{err}}", err)
   104  	}
   105  
   106  	if resp.ActivationId == nil {
   107  		return fmt.Errorf("[ERROR] ActivationId was nil")
   108  	}
   109  	d.SetId(*resp.ActivationId)
   110  
   111  	return resourceAwsSsmActivationRead(d, meta)
   112  }
   113  
   114  func resourceAwsSsmActivationRead(d *schema.ResourceData, meta interface{}) error {
   115  	ssmconn := meta.(*AWSClient).ssmconn
   116  
   117  	log.Printf("[DEBUG] Reading SSM Activation: %s", d.Id())
   118  
   119  	params := &ssm.DescribeActivationsInput{
   120  		Filters: []*ssm.DescribeActivationsFilter{
   121  			{
   122  				FilterKey: aws.String("ActivationIds"),
   123  				FilterValues: []*string{
   124  					aws.String(d.Id()),
   125  				},
   126  			},
   127  		},
   128  		MaxResults: aws.Int64(1),
   129  	}
   130  
   131  	resp, err := ssmconn.DescribeActivations(params)
   132  
   133  	if err != nil {
   134  		return errwrap.Wrapf("[ERROR] Error reading SSM activation: {{err}}", err)
   135  	}
   136  	if resp.ActivationList == nil || len(resp.ActivationList) == 0 {
   137  		return fmt.Errorf("[ERROR] ActivationList was nil or empty")
   138  	}
   139  
   140  	activation := resp.ActivationList[0] // Only 1 result as MaxResults is 1 above
   141  	d.Set("name", activation.DefaultInstanceName)
   142  	d.Set("description", activation.Description)
   143  	d.Set("expiration_date", activation.ExpirationDate)
   144  	d.Set("expired", activation.Expired)
   145  	d.Set("iam_role", activation.IamRole)
   146  	d.Set("registration_limit", activation.RegistrationLimit)
   147  	d.Set("registration_count", activation.RegistrationsCount)
   148  
   149  	return nil
   150  }
   151  
   152  func resourceAwsSsmActivationDelete(d *schema.ResourceData, meta interface{}) error {
   153  	ssmconn := meta.(*AWSClient).ssmconn
   154  
   155  	log.Printf("[DEBUG] Deleting SSM Activation: %s", d.Id())
   156  
   157  	params := &ssm.DeleteActivationInput{
   158  		ActivationId: aws.String(d.Id()),
   159  	}
   160  
   161  	_, err := ssmconn.DeleteActivation(params)
   162  
   163  	if err != nil {
   164  		return errwrap.Wrapf("[ERROR] Error deleting SSM activation: {{err}}", err)
   165  	}
   166  
   167  	return nil
   168  }