From b49fe4971acbf3f5df8f3a3ce2196e6e5366e6a8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Aug 2013 20:28:20 -0700 Subject: [PATCH] builder/amazon: modifying more than one AMI attribute type works --- CHANGELOG.md | 2 + .../common/step_modify_ami_attributes.go | 46 ++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7f5c2d57..c8a0fc186 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ BUG FIXES: * windows: file URLs are easier to get right as Packer has better parsing and error handling for Windows file paths. [GH-284] +* builder/amazon/all: Modifying more than one AMI attribute type no longer + crashes. * builder/amazon-instance: send IAM instance profile data. [GH-294] * builder/virtualbox: dowload progress won't be shown until download actually starts. [GH-288] diff --git a/builder/amazon/common/step_modify_ami_attributes.go b/builder/amazon/common/step_modify_ami_attributes.go index 55d2b2d1c..c522b85ef 100644 --- a/builder/amazon/common/step_modify_ami_attributes.go +++ b/builder/amazon/common/step_modify_ami_attributes.go @@ -31,20 +31,44 @@ func (s *StepModifyAMIAttributes) Run(state map[string]interface{}) multistep.St return multistep.ActionContinue } - options := &ec2.ModifyImageAttribute{ - Description: s.Description, - AddUsers: s.Users, - AddGroups: s.Groups, - ProductCodes: s.ProductCodes, + // Construct the modify image attribute requests we're going to make. + // We need to make each separately since the EC2 API only allows changing + // one type at a kind currently. + options := make(map[string]*ec2.ModifyImageAttribute) + if s.Description != "" { + options["description"] = &ec2.ModifyImageAttribute{ + Description: s.Description, + } + } + + if len(s.Groups) > 0 { + options["groups"] = &ec2.ModifyImageAttribute{ + AddGroups: s.Groups, + } + } + + if len(s.Users) > 0 { + options["users"] = &ec2.ModifyImageAttribute{ + AddUsers: s.Users, + } + } + + if len(s.ProductCodes) > 0 { + options["product codes"] = &ec2.ModifyImageAttribute{ + ProductCodes: s.ProductCodes, + } } ui.Say("Modifying AMI attributes...") - _, err := ec2conn.ModifyImageAttribute(ami, options) - if err != nil { - err := fmt.Errorf("Error modify AMI attributes: %s", err) - state["error"] = err - ui.Error(err.Error()) - return multistep.ActionHalt + for name, opts := range options { + ui.Message(fmt.Sprintf("Modifying: %s", name)) + _, err := ec2conn.ModifyImageAttribute(ami, opts) + if err != nil { + err := fmt.Errorf("Error modify AMI attributes: %s", err) + state["error"] = err + ui.Error(err.Error()) + return multistep.ActionHalt + } } return multistep.ActionContinue