github.com/raghuse92/packer@v1.3.2/builder/openstack/step_add_image_members.go (about)

     1  package openstack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/members"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type stepAddImageMembers struct{}
    13  
    14  func (s *stepAddImageMembers) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    15  	imageId := state.Get("image").(string)
    16  	ui := state.Get("ui").(packer.Ui)
    17  	config := state.Get("config").(*Config)
    18  
    19  	if len(config.ImageMembers) == 0 {
    20  		return multistep.ActionContinue
    21  	}
    22  
    23  	imageClient, err := config.imageV2Client()
    24  	if err != nil {
    25  		err = fmt.Errorf("Error initializing image service client: %s", err)
    26  		state.Put("error", err)
    27  		return multistep.ActionHalt
    28  	}
    29  
    30  	for _, member := range config.ImageMembers {
    31  		ui.Say(fmt.Sprintf("Adding member '%s' to image %s", member, imageId))
    32  		r := members.Create(imageClient, imageId, member)
    33  		if _, err = r.Extract(); err != nil {
    34  			err = fmt.Errorf("Error adding member to image: %s", err)
    35  			state.Put("error", err)
    36  			return multistep.ActionHalt
    37  		}
    38  	}
    39  
    40  	return multistep.ActionContinue
    41  }
    42  
    43  func (s *stepAddImageMembers) Cleanup(multistep.StateBag) {
    44  	// No cleanup...
    45  }