From ec4e95493d4fab7b1bffa0cfd3a276a4ce0aa5a3 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 5 Feb 2016 12:57:43 +0000 Subject: [PATCH] builder/digitalocean: add ability to specify api url This is useful in case of using DigitalOcean compatibility api hosting. Signed-off-by: Vasiliy Tolstov --- builder/digitalocean/builder.go | 8 ++++++++ builder/digitalocean/config.go | 5 ++++- website/source/docs/builders/digitalocean.html.markdown | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index ec57ed2b2..0c2cf9686 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -6,6 +6,7 @@ package digitalocean import ( "fmt" "log" + "net/url" "github.com/digitalocean/godo" "github.com/mitchellh/multistep" @@ -37,6 +38,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe client := godo.NewClient(oauth2.NewClient(oauth2.NoContext, &apiTokenSource{ AccessToken: b.config.APIToken, })) + if b.config.APIURL != "" { + u, err := url.Parse(b.config.APIURL) + if err != nil { + return nil, fmt.Errorf("DigitalOcean: Invalid API URL, %s.", err) + } + client.BaseURL = u + } // Set up the state state := new(multistep.BasicStateBag) diff --git a/builder/digitalocean/config.go b/builder/digitalocean/config.go index 617b88b68..a32970f40 100644 --- a/builder/digitalocean/config.go +++ b/builder/digitalocean/config.go @@ -20,6 +20,7 @@ type Config struct { Comm communicator.Config `mapstructure:",squash"` APIToken string `mapstructure:"api_token"` + APIURL string `mapstructure:"api_url"` Region string `mapstructure:"region"` Size string `mapstructure:"size"` @@ -57,7 +58,9 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { // Default to environment variable for api_token, if it exists c.APIToken = os.Getenv("DIGITALOCEAN_API_TOKEN") } - + if c.APIURL == "" { + c.APIURL = os.Getenv("DIGITALOCEAN_API_URL") + } if c.SnapshotName == "" { def, err := interpolate.Render("packer-{{timestamp}}", nil) if err != nil { diff --git a/website/source/docs/builders/digitalocean.html.markdown b/website/source/docs/builders/digitalocean.html.markdown index 17cb859a6..6aef71ee7 100644 --- a/website/source/docs/builders/digitalocean.html.markdown +++ b/website/source/docs/builders/digitalocean.html.markdown @@ -55,6 +55,10 @@ builder. ### Optional: +- `api_url` (string) - Non standard api endpoint URL. Set this if you are + using a DigitalOcean API compatible service. It can also be specified via + environment variable `DIGITALOCEAN_API_URL`. + - `droplet_name` (string) - The name assigned to the droplet. DigitalOcean sets the hostname of the machine to this value.