Skip to content

Commit

Permalink
Merge pull request #582 from codenrhoden/enhancement/dobs_translate_u…
Browse files Browse the repository at this point in the history
…nderscores

DOBS translate underscores
  • Loading branch information
akutz authored Jun 26, 2017
2 parents b156064 + 93f388e commit 6939c3b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .docs/user-guide/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ dobs:
statusMaxAttempts: 10
statusInitialDelay: 100ms
statusTimeout: 2m
convertUnderscores: false
```

##### Configuration notes
Expand All @@ -852,6 +853,12 @@ dobs:
- `statusTimeout` is a maximum length of time that polling for volume status can
occur. This serves as a backstop against a stuck request of malfunctioning API
that never returns.
- `convertUnderscores` is a boolean flag that controls whether the driver will
automatically convert underscores to dashes during a volume create request.
Digital Ocean does not allow underscores in the volume name, but some
container orchestrators (e.g. Docker Swarm) automatically prefix volume names
with a string containing a dash. This flag enables such requests to proceed,
but with the volume name modified.

!!! note
The DigitalOcean service currently only supports block storage volumes in
Expand Down
12 changes: 10 additions & 2 deletions drivers/storage/dobs/dobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const (
// Name is the name of the driver
Name = "dobs"

defaultStatusMaxAttempts = 10
defaultStatusInitDelay = "100ms"
defaultStatusMaxAttempts = 10
defaultStatusInitDelay = "100ms"
defaultConvertUnderscores = false

/* This is hard deadline when waiting for the volume status to change to
a desired state. At minimum is has to be more than the expontential
Expand Down Expand Up @@ -54,6 +55,11 @@ const (
// ConfigStatusTimeout is the key for the time duration for a timeout
// on how long to wait for a desired volume status to appears
ConfigStatusTimeout = Name + ".statusTimeout"

// ConfigConvertUnderscores is the key for a boolean flag on whether
// incoming requests that have names with underscores should be
// converted to dashes to satisfy DO naming requirements
ConfigConvertUnderscores = Name + ".convertUnderscores"
)

func init() {
Expand All @@ -72,5 +78,7 @@ func registerConfig() {
ConfigStatusInitDelay)
r.Key(gofig.String, "", defaultStatusTimeout, "Status Timeout",
ConfigStatusTimeout)
r.Key(gofig.Bool, "", defaultConvertUnderscores,
"Convert Underscores", ConfigConvertUnderscores)
gofigCore.Register(r)
}
29 changes: 22 additions & 7 deletions drivers/storage/dobs/storage/dobs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage
import (
"fmt"
"strconv"
"strings"
"time"

gofig "github.com/akutz/gofig/types"
Expand All @@ -25,13 +26,14 @@ const (
)

type driver struct {
name string
config gofig.Config
client *godo.Client
maxAttempts int
statusDelay int64
statusTimeout time.Duration
defaultRegion *string
name string
config gofig.Config
client *godo.Client
maxAttempts int
statusDelay int64
statusTimeout time.Duration
defaultRegion *string
convUnderscore bool
}

func init() {
Expand Down Expand Up @@ -91,6 +93,8 @@ func (d *driver) Init(
}
d.client = client

d.convUnderscore = d.config.GetBool(do.ConfigConvertUnderscores)

ctx.WithFields(fields).Info("storage driver initialized")

return nil
Expand Down Expand Up @@ -169,6 +173,9 @@ func (d *driver) VolumeInspectByName(
if region == nil || *region == "" {
return nil, goof.New("No region provided or configured")
}

volumeName = d.convUnderscores(volumeName)

doVolumes, _, err := d.client.Storage.ListVolumes(
ctx,
&godo.ListVolumeParams{
Expand Down Expand Up @@ -198,6 +205,7 @@ func (d *driver) VolumeCreate(
name string,
opts *types.VolumeCreateOpts) (*types.Volume, error) {

name = d.convUnderscores(name)
fields := map[string]interface{}{
"volumeName": name,
}
Expand Down Expand Up @@ -523,3 +531,10 @@ func (d *driver) mustRegion(ctx types.Context) *string {
}
return d.defaultRegion
}

func (d *driver) convUnderscores(name string) string {
if d.convUnderscore {
name = strings.Replace(name, "_", "-", -1)
}
return name
}

0 comments on commit 6939c3b

Please sign in to comment.