2018-09-07 19:18:47 -04:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepAttachVolume struct {
|
2018-10-22 19:39:04 -04:00
|
|
|
Index int
|
|
|
|
VolumeName string
|
|
|
|
InstanceInfoKey string
|
2018-09-07 19:18:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachVolume) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*compute.ComputeClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-10-22 19:39:04 -04:00
|
|
|
instanceInfo := state.Get(s.InstanceInfoKey).(*compute.InstanceInfo)
|
2018-09-07 19:18:47 -04:00
|
|
|
|
|
|
|
saClient := client.StorageAttachments()
|
|
|
|
saInput := &compute.CreateStorageAttachmentInput{
|
2018-10-22 19:39:04 -04:00
|
|
|
Index: s.Index,
|
2018-09-07 19:18:47 -04:00
|
|
|
InstanceName: instanceInfo.Name + "/" + instanceInfo.ID,
|
2018-10-22 19:39:04 -04:00
|
|
|
StorageVolumeName: s.VolumeName,
|
2018-09-07 19:18:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sa, err := saClient.CreateStorageAttachment(saInput)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Problem attaching master volume: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-10-22 19:39:04 -04:00
|
|
|
state.Put(s.InstanceInfoKey+"/attachment", sa)
|
2018-09-07 19:18:47 -04:00
|
|
|
|
2018-10-16 17:18:40 -04:00
|
|
|
ui.Message("Volume attached to instance.")
|
2018-09-07 19:18:47 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachVolume) Cleanup(state multistep.StateBag) {
|
2018-10-22 19:39:04 -04:00
|
|
|
sa, ok := state.GetOk(s.InstanceInfoKey + "/attachment")
|
2018-09-07 19:18:47 -04:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client := state.Get("client").(*compute.ComputeClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
saClient := client.StorageAttachments()
|
|
|
|
saI := &compute.DeleteStorageAttachmentInput{
|
|
|
|
Name: sa.(*compute.StorageAttachmentInfo).Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := saClient.DeleteStorageAttachment(saI); err != nil {
|
|
|
|
err = fmt.Errorf("Problem detaching storage volume: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|