github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/builder/amazon/common/step_modify_ami_attributes.go (about) 1 package common 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/packer" 10 ) 11 12 type StepModifyAMIAttributes struct { 13 Users []string 14 Groups []string 15 ProductCodes []string 16 Description string 17 } 18 19 func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAction { 20 ec2conn := state.Get("ec2").(*ec2.EC2) 21 ui := state.Get("ui").(packer.Ui) 22 amis := state.Get("amis").(map[string]string) 23 24 // Determine if there is any work to do. 25 valid := false 26 valid = valid || s.Description != "" 27 valid = valid || (s.Users != nil && len(s.Users) > 0) 28 valid = valid || (s.Groups != nil && len(s.Groups) > 0) 29 valid = valid || (s.ProductCodes != nil && len(s.ProductCodes) > 0) 30 31 if !valid { 32 return multistep.ActionContinue 33 } 34 35 // Construct the modify image attribute requests we're going to make. 36 // We need to make each separately since the EC2 API only allows changing 37 // one type at a kind currently. 38 options := make(map[string]*ec2.ModifyImageAttributeInput) 39 if s.Description != "" { 40 options["description"] = &ec2.ModifyImageAttributeInput{ 41 Description: &ec2.AttributeValue{Value: &s.Description}, 42 } 43 } 44 45 if len(s.Groups) > 0 { 46 groups := make([]*string, len(s.Groups)) 47 adds := make([]*ec2.LaunchPermission, len(s.Groups)) 48 addGroups := &ec2.ModifyImageAttributeInput{ 49 LaunchPermission: &ec2.LaunchPermissionModifications{}, 50 } 51 52 for i, g := range s.Groups { 53 groups[i] = aws.String(g) 54 adds[i] = &ec2.LaunchPermission{ 55 Group: aws.String(g), 56 } 57 } 58 addGroups.UserGroups = groups 59 addGroups.LaunchPermission.Add = adds 60 61 options["groups"] = addGroups 62 } 63 64 if len(s.Users) > 0 { 65 users := make([]*string, len(s.Users)) 66 adds := make([]*ec2.LaunchPermission, len(s.Users)) 67 for i, u := range s.Users { 68 users[i] = aws.String(u) 69 adds[i] = &ec2.LaunchPermission{UserID: aws.String(u)} 70 } 71 options["users"] = &ec2.ModifyImageAttributeInput{ 72 UserIDs: users, 73 LaunchPermission: &ec2.LaunchPermissionModifications{ 74 Add: adds, 75 }, 76 } 77 } 78 79 if len(s.ProductCodes) > 0 { 80 codes := make([]*string, len(s.ProductCodes)) 81 for i, c := range s.ProductCodes { 82 codes[i] = &c 83 } 84 options["product codes"] = &ec2.ModifyImageAttributeInput{ 85 ProductCodes: codes, 86 } 87 } 88 89 for region, ami := range amis { 90 ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami)) 91 regionconn := ec2.New(&aws.Config{ 92 Credentials: ec2conn.Config.Credentials, 93 Region: region, 94 }) 95 for name, input := range options { 96 ui.Message(fmt.Sprintf("Modifying: %s", name)) 97 input.ImageID = &ami 98 _, err := regionconn.ModifyImageAttribute(input) 99 if err != nil { 100 err := fmt.Errorf("Error modify AMI attributes: %s", err) 101 state.Put("error", err) 102 ui.Error(err.Error()) 103 return multistep.ActionHalt 104 } 105 } 106 } 107 108 return multistep.ActionContinue 109 } 110 111 func (s *StepModifyAMIAttributes) Cleanup(state multistep.StateBag) { 112 // No cleanup... 113 }