2018-09-07 19:18:47 -04:00
|
|
|
package classic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-oracle-terraform/compute"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2018-09-07 19:18:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepAttachVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.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
|
|
|
|
}
|
2018-10-24 17:08:11 -04:00
|
|
|
client := state.Get("client").(*compute.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-09-07 19:18:47 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|