github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_ami_launch_permission.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/aws/aws-sdk-go/aws"
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceAwsAmiLaunchPermission() *schema.Resource {
    11  	return &schema.Resource{
    12  		Exists: resourceAwsAmiLaunchPermissionExists,
    13  		Create: resourceAwsAmiLaunchPermissionCreate,
    14  		Read:   resourceAwsAmiLaunchPermissionRead,
    15  		Delete: resourceAwsAmiLaunchPermissionDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"image_id": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  			"account_id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourceAwsAmiLaunchPermissionExists(d *schema.ResourceData, meta interface{}) (bool, error) {
    33  	conn := meta.(*AWSClient).ec2conn
    34  
    35  	image_id := d.Get("image_id").(string)
    36  	account_id := d.Get("account_id").(string)
    37  	return hasLaunchPermission(conn, image_id, account_id)
    38  }
    39  
    40  func resourceAwsAmiLaunchPermissionCreate(d *schema.ResourceData, meta interface{}) error {
    41  	conn := meta.(*AWSClient).ec2conn
    42  
    43  	image_id := d.Get("image_id").(string)
    44  	account_id := d.Get("account_id").(string)
    45  
    46  	_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
    47  		ImageId:   aws.String(image_id),
    48  		Attribute: aws.String("launchPermission"),
    49  		LaunchPermission: &ec2.LaunchPermissionModifications{
    50  			Add: []*ec2.LaunchPermission{
    51  				&ec2.LaunchPermission{UserId: aws.String(account_id)},
    52  			},
    53  		},
    54  	})
    55  	if err != nil {
    56  		return fmt.Errorf("error creating ami launch permission: %s", err)
    57  	}
    58  
    59  	d.SetId(fmt.Sprintf("%s-%s", image_id, account_id))
    60  	return nil
    61  }
    62  
    63  func resourceAwsAmiLaunchPermissionRead(d *schema.ResourceData, meta interface{}) error {
    64  	return nil
    65  }
    66  
    67  func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface{}) error {
    68  	conn := meta.(*AWSClient).ec2conn
    69  
    70  	image_id := d.Get("image_id").(string)
    71  	account_id := d.Get("account_id").(string)
    72  
    73  	_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{
    74  		ImageId:   aws.String(image_id),
    75  		Attribute: aws.String("launchPermission"),
    76  		LaunchPermission: &ec2.LaunchPermissionModifications{
    77  			Remove: []*ec2.LaunchPermission{
    78  				&ec2.LaunchPermission{UserId: aws.String(account_id)},
    79  			},
    80  		},
    81  	})
    82  	if err != nil {
    83  		return fmt.Errorf("error removing ami launch permission: %s", err)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string) (bool, error) {
    90  	attrs, err := conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{
    91  		ImageId:   aws.String(image_id),
    92  		Attribute: aws.String("launchPermission"),
    93  	})
    94  	if err != nil {
    95  		return false, err
    96  	}
    97  
    98  	for _, lp := range attrs.LaunchPermissions {
    99  		if *lp.UserId == account_id {
   100  			return true, nil
   101  		}
   102  	}
   103  	return false, nil
   104  }