github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_efs_mount_target.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 resourceAwsEfsMountTarget() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsEfsMountTargetCreate,
    18  		Read:   resourceAwsEfsMountTargetRead,
    19  		Update: resourceAwsEfsMountTargetUpdate,
    20  		Delete: resourceAwsEfsMountTargetDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"file_system_id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"ip_address": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Computed: true,
    32  				Optional: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"security_groups": &schema.Schema{
    37  				Type:     schema.TypeSet,
    38  				Elem:     &schema.Schema{Type: schema.TypeString},
    39  				Set:      schema.HashString,
    40  				Computed: true,
    41  				Optional: true,
    42  			},
    43  
    44  			"subnet_id": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Required: true,
    47  				ForceNew: true,
    48  			},
    49  
    50  			"network_interface_id": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Computed: true,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func resourceAwsEfsMountTargetCreate(d *schema.ResourceData, meta interface{}) error {
    59  	conn := meta.(*AWSClient).efsconn
    60  
    61  	input := efs.CreateMountTargetInput{
    62  		FileSystemId: aws.String(d.Get("file_system_id").(string)),
    63  		SubnetId:     aws.String(d.Get("subnet_id").(string)),
    64  	}
    65  
    66  	if v, ok := d.GetOk("ip_address"); ok {
    67  		input.IpAddress = aws.String(v.(string))
    68  	}
    69  	if v, ok := d.GetOk("security_groups"); ok {
    70  		input.SecurityGroups = expandStringList(v.(*schema.Set).List())
    71  	}
    72  
    73  	log.Printf("[DEBUG] Creating EFS mount target: %#v", input)
    74  
    75  	mt, err := conn.CreateMountTarget(&input)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	d.SetId(*mt.MountTargetId)
    81  
    82  	stateConf := &resource.StateChangeConf{
    83  		Pending: []string{"creating"},
    84  		Target:  "available",
    85  		Refresh: func() (interface{}, string, error) {
    86  			resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
    87  				MountTargetId: aws.String(d.Id()),
    88  			})
    89  			if err != nil {
    90  				return nil, "error", err
    91  			}
    92  
    93  			if len(resp.MountTargets) < 1 {
    94  				return nil, "error", fmt.Errorf("EFS mount target %q not found", d.Id())
    95  			}
    96  
    97  			mt := resp.MountTargets[0]
    98  
    99  			log.Printf("[DEBUG] Current status of %q: %q", *mt.MountTargetId, *mt.LifeCycleState)
   100  			return mt, *mt.LifeCycleState, nil
   101  		},
   102  		Timeout:    10 * time.Minute,
   103  		Delay:      2 * time.Second,
   104  		MinTimeout: 3 * time.Second,
   105  	}
   106  
   107  	_, err = stateConf.WaitForState()
   108  	if err != nil {
   109  		return fmt.Errorf("Error waiting for EFS mount target (%s) to create: %s", d.Id(), err)
   110  	}
   111  
   112  	log.Printf("[DEBUG] EFS mount target created: %s", *mt.MountTargetId)
   113  
   114  	return resourceAwsEfsMountTargetRead(d, meta)
   115  }
   116  
   117  func resourceAwsEfsMountTargetUpdate(d *schema.ResourceData, meta interface{}) error {
   118  	conn := meta.(*AWSClient).efsconn
   119  
   120  	if d.HasChange("security_groups") {
   121  		input := efs.ModifyMountTargetSecurityGroupsInput{
   122  			MountTargetId:  aws.String(d.Id()),
   123  			SecurityGroups: expandStringList(d.Get("security_groups").(*schema.Set).List()),
   124  		}
   125  		_, err := conn.ModifyMountTargetSecurityGroups(&input)
   126  		if err != nil {
   127  			return err
   128  		}
   129  	}
   130  
   131  	return resourceAwsEfsMountTargetRead(d, meta)
   132  }
   133  
   134  func resourceAwsEfsMountTargetRead(d *schema.ResourceData, meta interface{}) error {
   135  	conn := meta.(*AWSClient).efsconn
   136  	resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
   137  		MountTargetId: aws.String(d.Id()),
   138  	})
   139  	if err != nil {
   140  		return err
   141  	}
   142  
   143  	if len(resp.MountTargets) < 1 {
   144  		return fmt.Errorf("EFS mount target %q not found", d.Id())
   145  	}
   146  
   147  	mt := resp.MountTargets[0]
   148  
   149  	log.Printf("[DEBUG] Found EFS mount target: %#v", mt)
   150  
   151  	d.SetId(*mt.MountTargetId)
   152  	d.Set("file_system_id", *mt.FileSystemId)
   153  	d.Set("ip_address", *mt.IpAddress)
   154  	d.Set("subnet_id", *mt.SubnetId)
   155  	d.Set("network_interface_id", *mt.NetworkInterfaceId)
   156  
   157  	sgResp, err := conn.DescribeMountTargetSecurityGroups(&efs.DescribeMountTargetSecurityGroupsInput{
   158  		MountTargetId: aws.String(d.Id()),
   159  	})
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	d.Set("security_groups", schema.NewSet(schema.HashString, flattenStringList(sgResp.SecurityGroups)))
   165  
   166  	return nil
   167  }
   168  
   169  func resourceAwsEfsMountTargetDelete(d *schema.ResourceData, meta interface{}) error {
   170  	conn := meta.(*AWSClient).efsconn
   171  
   172  	log.Printf("[DEBUG] Deleting EFS mount target %q", d.Id())
   173  	_, err := conn.DeleteMountTarget(&efs.DeleteMountTargetInput{
   174  		MountTargetId: aws.String(d.Id()),
   175  	})
   176  	if err != nil {
   177  		return err
   178  	}
   179  
   180  	stateConf := &resource.StateChangeConf{
   181  		Pending: []string{"available", "deleting", "deleted"},
   182  		Target:  "",
   183  		Refresh: func() (interface{}, string, error) {
   184  			resp, err := conn.DescribeMountTargets(&efs.DescribeMountTargetsInput{
   185  				MountTargetId: aws.String(d.Id()),
   186  			})
   187  			if err != nil {
   188  				awsErr, ok := err.(awserr.Error)
   189  				if !ok {
   190  					return nil, "error", err
   191  				}
   192  
   193  				if awsErr.Code() == "MountTargetNotFound" {
   194  					return nil, "", nil
   195  				}
   196  
   197  				return nil, "error", awsErr
   198  			}
   199  
   200  			if len(resp.MountTargets) < 1 {
   201  				return nil, "", nil
   202  			}
   203  
   204  			mt := resp.MountTargets[0]
   205  
   206  			log.Printf("[DEBUG] Current status of %q: %q", *mt.MountTargetId, *mt.LifeCycleState)
   207  			return mt, *mt.LifeCycleState, nil
   208  		},
   209  		Timeout:    10 * time.Minute,
   210  		Delay:      2 * time.Second,
   211  		MinTimeout: 3 * time.Second,
   212  	}
   213  
   214  	_, err = stateConf.WaitForState()
   215  	if err != nil {
   216  		return fmt.Errorf("Error waiting for EFS mount target (%q) to delete: %q",
   217  			d.Id(), err.Error())
   218  	}
   219  
   220  	log.Printf("[DEBUG] EFS mount target %q deleted.", d.Id())
   221  
   222  	return nil
   223  }