github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/step_modify_ami_attributes.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/goamz/ec2"
     6  	"github.com/mitchellh/multistep"
     7  	"github.com/mitchellh/packer/packer"
     8  )
     9  
    10  type StepModifyAMIAttributes struct {
    11  	Users        []string
    12  	Groups       []string
    13  	ProductCodes []string
    14  	Description  string
    15  }
    16  
    17  func (s *StepModifyAMIAttributes) Run(state map[string]interface{}) multistep.StepAction {
    18  	ec2conn := state["ec2"].(*ec2.EC2)
    19  	ui := state["ui"].(packer.Ui)
    20  	amis := state["amis"].(map[string]string)
    21  	ami := amis[ec2conn.Region.Name]
    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  	ui.Say("Modifying AMI attributes...")
    63  	for name, opts := range options {
    64  		ui.Message(fmt.Sprintf("Modifying: %s", name))
    65  		_, err := ec2conn.ModifyImageAttribute(ami, opts)
    66  		if err != nil {
    67  			err := fmt.Errorf("Error modify AMI attributes: %s", err)
    68  			state["error"] = err
    69  			ui.Error(err.Error())
    70  			return multistep.ActionHalt
    71  		}
    72  	}
    73  
    74  	return multistep.ActionContinue
    75  }
    76  
    77  func (s *StepModifyAMIAttributes) Cleanup(state map[string]interface{}) {
    78  	// No cleanup...
    79  }