github.com/jerryclinesmith/packer@v0.3.7/builder/amazon/common/step_modify_ami_attributes.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/goamz/aws" 6 "github.com/mitchellh/goamz/ec2" 7 "github.com/mitchellh/multistep" 8 "github.com/mitchellh/packer/packer" 9 ) 10 11 type StepModifyAMIAttributes struct { 12 Users []string 13 Groups []string 14 ProductCodes []string 15 Description string 16 } 17 18 func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAction { 19 ec2conn := state.Get("ec2").(*ec2.EC2) 20 ui := state.Get("ui").(packer.Ui) 21 amis := state.Get("amis").(map[string]string) 22 23 // Determine if there is any work to do. 24 valid := false 25 valid = valid || s.Description != "" 26 valid = valid || (s.Users != nil && len(s.Users) > 0) 27 valid = valid || (s.Groups != nil && len(s.Groups) > 0) 28 valid = valid || (s.ProductCodes != nil && len(s.ProductCodes) > 0) 29 30 if !valid { 31 return multistep.ActionContinue 32 } 33 34 // Construct the modify image attribute requests we're going to make. 35 // We need to make each separately since the EC2 API only allows changing 36 // one type at a kind currently. 37 options := make(map[string]*ec2.ModifyImageAttribute) 38 if s.Description != "" { 39 options["description"] = &ec2.ModifyImageAttribute{ 40 Description: s.Description, 41 } 42 } 43 44 if len(s.Groups) > 0 { 45 options["groups"] = &ec2.ModifyImageAttribute{ 46 AddGroups: s.Groups, 47 } 48 } 49 50 if len(s.Users) > 0 { 51 options["users"] = &ec2.ModifyImageAttribute{ 52 AddUsers: s.Users, 53 } 54 } 55 56 if len(s.ProductCodes) > 0 { 57 options["product codes"] = &ec2.ModifyImageAttribute{ 58 ProductCodes: s.ProductCodes, 59 } 60 } 61 62 for region, ami := range amis { 63 ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami)) 64 regionconn := ec2.New(ec2conn.Auth, aws.Regions[region]) 65 for name, opts := range options { 66 ui.Message(fmt.Sprintf("Modifying: %s", name)) 67 _, err := regionconn.ModifyImageAttribute(ami, opts) 68 if err != nil { 69 err := fmt.Errorf("Error modify AMI attributes: %s", err) 70 state.Put("error", err) 71 ui.Error(err.Error()) 72 return multistep.ActionHalt 73 } 74 } 75 } 76 77 return multistep.ActionContinue 78 } 79 80 func (s *StepModifyAMIAttributes) Cleanup(state multistep.StateBag) { 81 // No cleanup... 82 }