github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_sfn_state_machine.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/sfn"
    10  	"github.com/hashicorp/errwrap"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsSfnStateMachine() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsSfnStateMachineCreate,
    18  		Read:   resourceAwsSfnStateMachineRead,
    19  		Delete: resourceAwsSfnStateMachineDelete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"definition": {
    26  				Type:         schema.TypeString,
    27  				Required:     true,
    28  				ForceNew:     true,
    29  				ValidateFunc: validateSfnStateMachineDefinition,
    30  			},
    31  
    32  			"name": {
    33  				Type:         schema.TypeString,
    34  				Required:     true,
    35  				ForceNew:     true,
    36  				ValidateFunc: validateSfnStateMachineName,
    37  			},
    38  
    39  			"role_arn": {
    40  				Type:         schema.TypeString,
    41  				Required:     true,
    42  				ForceNew:     true,
    43  				ValidateFunc: validateArn,
    44  			},
    45  
    46  			"creation_date": {
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  
    51  			"status": {
    52  				Type:     schema.TypeString,
    53  				Computed: true,
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{}) error {
    60  	conn := meta.(*AWSClient).sfnconn
    61  	log.Print("[DEBUG] Creating Step Function State Machine")
    62  
    63  	params := &sfn.CreateStateMachineInput{
    64  		Definition: aws.String(d.Get("definition").(string)),
    65  		Name:       aws.String(d.Get("name").(string)),
    66  		RoleArn:    aws.String(d.Get("role_arn").(string)),
    67  	}
    68  
    69  	var activity *sfn.CreateStateMachineOutput
    70  
    71  	err := resource.Retry(5*time.Minute, func() *resource.RetryError {
    72  		var err error
    73  		activity, err = conn.CreateStateMachine(params)
    74  
    75  		if err != nil {
    76  			// Note: the instance may be in a deleting mode, hence the retry
    77  			// when creating the step function. This can happen when we are
    78  			// updating the resource (since there is no update API call).
    79  			if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "StateMachineDeleting" {
    80  				return resource.RetryableError(err)
    81  			}
    82  
    83  			return resource.NonRetryableError(err)
    84  		}
    85  
    86  		return nil
    87  	})
    88  
    89  	if err != nil {
    90  		return errwrap.Wrapf("Error creating Step Function State Machine: {{err}}", err)
    91  	}
    92  
    93  	d.SetId(*activity.StateMachineArn)
    94  
    95  	return resourceAwsSfnStateMachineRead(d, meta)
    96  }
    97  
    98  func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) error {
    99  	conn := meta.(*AWSClient).sfnconn
   100  	log.Printf("[DEBUG] Reading Step Function State Machine: %s", d.Id())
   101  
   102  	sm, err := conn.DescribeStateMachine(&sfn.DescribeStateMachineInput{
   103  		StateMachineArn: aws.String(d.Id()),
   104  	})
   105  	if err != nil {
   106  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   107  			d.SetId("")
   108  			return nil
   109  		}
   110  		return err
   111  	}
   112  
   113  	d.Set("definition", sm.Definition)
   114  	d.Set("name", sm.Name)
   115  	d.Set("role_arn", sm.RoleArn)
   116  	d.Set("status", sm.Status)
   117  
   118  	if err := d.Set("creation_date", sm.CreationDate.Format(time.RFC3339)); err != nil {
   119  		log.Printf("[DEBUG] Error setting creation_date: %s", err)
   120  	}
   121  
   122  	return nil
   123  }
   124  
   125  func resourceAwsSfnStateMachineDelete(d *schema.ResourceData, meta interface{}) error {
   126  	conn := meta.(*AWSClient).sfnconn
   127  	log.Printf("[DEBUG] Deleting Step Function State Machine: %s", d.Id())
   128  
   129  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   130  		_, err := conn.DeleteStateMachine(&sfn.DeleteStateMachineInput{
   131  			StateMachineArn: aws.String(d.Id()),
   132  		})
   133  
   134  		if err == nil {
   135  			return nil
   136  		}
   137  
   138  		return resource.NonRetryableError(err)
   139  	})
   140  }