github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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: []string{"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 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "FileSystemNotFound" { 104 log.Printf("[WARN] EFS File System (%s) not found, error code (404)", d.Id()) 105 d.SetId("") 106 return nil 107 } 108 return err 109 } 110 if len(resp.FileSystems) < 1 { 111 return fmt.Errorf("EFS file system %q not found", d.Id()) 112 } 113 114 tagsResp, err := conn.DescribeTags(&efs.DescribeTagsInput{ 115 FileSystemId: aws.String(d.Id()), 116 }) 117 if err != nil { 118 return err 119 } 120 121 d.Set("tags", tagsToMapEFS(tagsResp.Tags)) 122 123 return nil 124 } 125 126 func resourceAwsEfsFileSystemDelete(d *schema.ResourceData, meta interface{}) error { 127 conn := meta.(*AWSClient).efsconn 128 129 log.Printf("[DEBUG] Deleting EFS file system %s", d.Id()) 130 _, err := conn.DeleteFileSystem(&efs.DeleteFileSystemInput{ 131 FileSystemId: aws.String(d.Id()), 132 }) 133 stateConf := &resource.StateChangeConf{ 134 Pending: []string{"available", "deleting"}, 135 Target: []string{}, 136 Refresh: func() (interface{}, string, error) { 137 resp, err := conn.DescribeFileSystems(&efs.DescribeFileSystemsInput{ 138 FileSystemId: aws.String(d.Id()), 139 }) 140 if err != nil { 141 efsErr, ok := err.(awserr.Error) 142 if ok && efsErr.Code() == "FileSystemNotFound" { 143 return nil, "", nil 144 } 145 return nil, "error", err 146 } 147 148 if len(resp.FileSystems) < 1 { 149 return nil, "", nil 150 } 151 152 fs := resp.FileSystems[0] 153 log.Printf("[DEBUG] current status of %q: %q", 154 *fs.FileSystemId, *fs.LifeCycleState) 155 return fs, *fs.LifeCycleState, nil 156 }, 157 Timeout: 10 * time.Minute, 158 Delay: 2 * time.Second, 159 MinTimeout: 3 * time.Second, 160 } 161 162 _, err = stateConf.WaitForState() 163 if err != nil { 164 return err 165 } 166 167 log.Printf("[DEBUG] EFS file system %q deleted.", d.Id()) 168 169 return nil 170 }