github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_efs_file_system.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/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/efs"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsEfsFileSystem() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsEfsFileSystemCreate,
    18  		Read:   resourceAwsEfsFileSystemRead,
    19  		Update: resourceAwsEfsFileSystemUpdate,
    20  		Delete: resourceAwsEfsFileSystemDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"reference_name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"tags": tagsSchema(),
    30  		},
    31  	}
    32  }
    33  
    34  func resourceAwsEfsFileSystemCreate(d *schema.ResourceData, meta interface{}) error {
    35  	conn := meta.(*AWSClient).efsconn
    36  
    37  	referenceName := ""
    38  	if v, ok := d.GetOk("reference_name"); ok {
    39  		referenceName = v.(string) + "-"
    40  	}
    41  	token := referenceName + resource.UniqueId()
    42  	fs, err := conn.CreateFileSystem(&efs.CreateFileSystemInput{
    43  		CreationToken: aws.String(token),
    44  	})
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	log.Printf("[DEBUG] Creating EFS file system: %s", *fs)
    50  	d.SetId(*fs.FileSystemId)
    51  
    52  	stateConf := &resource.StateChangeConf{
    53  		Pending: []string{"creating"},
    54  		Target:  "available",
    55  		Refresh: func() (interface{}, string, error) {
    56  			resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
    57  				FileSystemId: aws.String(d.Id()),
    58  			})
    59  			if err != nil {
    60  				return nil, "error", err
    61  			}
    62  
    63  			if len(resp.FileSystems) < 1 {
    64  				return nil, "not-found", fmt.Errorf("EFS file system %q not found", d.Id())
    65  			}
    66  
    67  			fs := resp.FileSystems[0]
    68  			log.Printf("[DEBUG] current status of %q: %q", *fs.FileSystemId, *fs.LifeCycleState)
    69  			return fs, *fs.LifeCycleState, nil
    70  		},
    71  		Timeout:    10 * time.Minute,
    72  		Delay:      2 * time.Second,
    73  		MinTimeout: 3 * time.Second,
    74  	}
    75  
    76  	_, err = stateConf.WaitForState()
    77  	if err != nil {
    78  		return fmt.Errorf("Error waiting for EFS file system (%q) to create: %q",
    79  			d.Id(), err.Error())
    80  	}
    81  	log.Printf("[DEBUG] EFS file system created: %q", *fs.FileSystemId)
    82  
    83  	return resourceAwsEfsFileSystemUpdate(d, meta)
    84  }
    85  
    86  func resourceAwsEfsFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
    87  	conn := meta.(*AWSClient).efsconn
    88  	err := setTagsEFS(conn, d)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	return resourceAwsEfsFileSystemRead(d, meta)
    94  }
    95  
    96  func resourceAwsEfsFileSystemRead(d *schema.ResourceData, meta interface{}) error {
    97  	conn := meta.(*AWSClient).efsconn
    98  
    99  	resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   100  		FileSystemId: aws.String(d.Id()),
   101  	})
   102  	if err != nil {
   103  		return err
   104  	}
   105  	if len(resp.FileSystems) < 1 {
   106  		return fmt.Errorf("EFS file system %q not found", d.Id())
   107  	}
   108  
   109  	tagsResp, err := conn.DescribeTags(&efs.DescribeTagsInput{
   110  		FileSystemId: aws.String(d.Id()),
   111  	})
   112  	if err != nil {
   113  		return err
   114  	}
   115  
   116  	d.Set("tags", tagsToMapEFS(tagsResp.Tags))
   117  
   118  	return nil
   119  }
   120  
   121  func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) error {
   122  	conn := meta.(*AWSClient).efsconn
   123  
   124  	log.Printf("[DEBUG] Deleting EFS file system %s", d.Id())
   125  	_, err := conn.DeleteFileSystem(&efs.DeleteFileSystemInput{
   126  		FileSystemId: aws.String(d.Id()),
   127  	})
   128  	stateConf := &resource.StateChangeConf{
   129  		Pending: []string{"available", "deleting"},
   130  		Target:  "",
   131  		Refresh: func() (interface{}, string, error) {
   132  			resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{
   133  				FileSystemId: aws.String(d.Id()),
   134  			})
   135  			if err != nil {
   136  				efsErr, ok := err.(awserr.Error)
   137  				if ok && efsErr.Code() == "FileSystemNotFound" {
   138  					return nil, "", nil
   139  				}
   140  				return nil, "error", err
   141  			}
   142  
   143  			if len(resp.FileSystems) < 1 {
   144  				return nil, "", nil
   145  			}
   146  
   147  			fs := resp.FileSystems[0]
   148  			log.Printf("[DEBUG] current status of %q: %q",
   149  				*fs.FileSystemId, *fs.LifeCycleState)
   150  			return fs, *fs.LifeCycleState, nil
   151  		},
   152  		Timeout:    10 * time.Minute,
   153  		Delay:      2 * time.Second,
   154  		MinTimeout: 3 * time.Second,
   155  	}
   156  
   157  	_, err = stateConf.WaitForState()
   158  	if err != nil {
   159  		return err
   160  	}
   161  
   162  	log.Printf("[DEBUG] EFS file system %q deleted.", d.Id())
   163  
   164  	return nil
   165  }