Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dobs support for VolumeInspectByName #581

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions drivers/storage/dobs/storage/dobs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type driver struct {
maxAttempts int
statusDelay int64
statusTimeout time.Duration
defaultRegion *string
}

func init() {
Expand Down Expand Up @@ -71,7 +72,11 @@ func (d *driver) Init(
"statusDelay": fmt.Sprintf(
"%v", time.Duration(d.statusDelay)*time.Nanosecond),
"statusTimeout": d.statusTimeout,
"region": d.config.GetString(do.ConfigRegion),
}

if region := d.config.GetString(do.ConfigRegion); region != "" {
d.defaultRegion = &region
fields["region"] = region
}

if token == "" {
Expand Down Expand Up @@ -155,6 +160,39 @@ func (d *driver) VolumeInspect(
return volume, nil
}

func (d *driver) VolumeInspectByName(
ctx types.Context,
volumeName string,
opts *types.VolumeInspectOpts) (*types.Volume, error) {

region := d.mustRegion(ctx)
if region == nil || *region == "" {
return nil, goof.New("No region provided or configured")
}
doVolumes, _, err := d.client.Storage.ListVolumes(
ctx,
&godo.ListVolumeParams{
Region: *region,
Name: volumeName,
},
)
if err != nil {
return nil, err
}
if len(doVolumes) == 0 {
return nil, apiUtils.NewNotFoundError(volumeName)
}
if len(doVolumes) > 1 {
return nil, goof.New("too many volumes returned")
}

volume, err := d.toTypesVolume(ctx, &doVolumes[0], opts.Attachments)
if err != nil {
return nil, goof.New("error converting to types.Volume")
}
return volume, nil
}

func (d *driver) VolumeCreate(
ctx types.Context,
name string,
Expand All @@ -165,12 +203,13 @@ func (d *driver) VolumeCreate(
}

if opts.AvailabilityZone == nil || *opts.AvailabilityZone == "" {
instance, err := d.InstanceInspect(ctx, nil)
if err != nil {
return nil, err
opts.AvailabilityZone = d.mustRegion(ctx)
if opts.AvailabilityZone == nil || *opts.AvailabilityZone == "" {
return nil, goof.WithFields(fields,
"No region for volume create")
}
opts.AvailabilityZone = &instance.Region
}
fields["region"] = *opts.AvailabilityZone

if opts.Size == nil {
size := int64(minSizeGiB)
Expand All @@ -192,6 +231,8 @@ func (d *driver) VolumeCreate(

volume, _, err := d.client.Storage.CreateVolume(ctx, volumeReq)
if err != nil {
ctx.WithFields(fields).WithError(err).Error(
"error returned from create volume")
return nil, err
}

Expand Down Expand Up @@ -473,3 +514,12 @@ func (d *driver) waitForAction(
}
return nil
}

func (d *driver) mustRegion(ctx types.Context) *string {
if iid, ok := context.InstanceID(ctx); ok {
if v, ok := iid.Fields[do.InstanceIDFieldRegion]; ok && v != "" {
return &v
}
}
return d.defaultRegion
}