Skip to content

Commit

Permalink
Wait for volume to be available (vexxhost#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser authored Aug 26, 2024
1 parent 89be5e0 commit 78f649a
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions internal/target/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ func (t *OpenStack) Connect(ctx context.Context) error {

if errors.Is(err, openstack.ErrorVolumeNotFound) {
log.Info("Creating new volume")
volume, err = volumes.Create(ctx, t.ClientSet.BlockStorage, volumes.CreateOpts{
Name: DiskLabel(t.VirtualMachine, t.Disk),
Size: int(t.Disk.CapacityInBytes) / 1024 / 1024 / 1024,
AvailabilityZone: opts.AvailabilityZone,
VolumeType: opts.VolumeType,
Metadata: volumeMetadata,
}, nil).Extract()
volume, err := t.createVolume(ctx, opts, volumeMetadata)
if err != nil {
return err
}
Expand Down Expand Up @@ -191,6 +185,30 @@ func (t *OpenStack) Connect(ctx context.Context) error {
return nil
}

func (t *OpenStack) createVolume(ctx context.Context, opts *VolumeCreateOpts, metadata map[string]string) (*volumes.Volume, error) {
log.Info("Creating new volume")
volume, err := volumes.Create(ctx, t.ClientSet.BlockStorage, volumes.CreateOpts{
Name: DiskLabel(t.VirtualMachine, t.Disk),
Size: int(t.Disk.CapacityInBytes) / 1024 / 1024 / 1024,
AvailabilityZone: opts.AvailabilityZone,
VolumeType: opts.VolumeType,
Metadata: metadata,
}, nil).Extract()
if err != nil {
return nil, err
}

ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()

err = volumes.WaitForStatus(ctx, t.ClientSet.BlockStorage, volume.ID, "available")
if err != nil {
return nil, errors.Join(errors.New("timed out waiting for volume to be available"), err)
}

return volume, nil
}

func (t *OpenStack) GetPath(ctx context.Context) (string, error) {
volume, err := t.ClientSet.GetVolumeForDisk(ctx, t.VirtualMachine, t.Disk)
if err != nil {
Expand Down

0 comments on commit 78f649a

Please sign in to comment.