From d701adb3e0f2e0c69c0290c512d983db69d53755 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Thu, 11 Jul 2013 11:31:09 +0200 Subject: [PATCH 1/2] builder/digitalocean: use detected env variables for credentials --- builder/digitalocean/builder.go | 11 +++++++++++ .../source/docs/builders/digitalocean.html.markdown | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 88000d20c..65ec8282e 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -12,6 +12,7 @@ import ( "github.com/mitchellh/packer/builder/common" "github.com/mitchellh/packer/packer" "log" + "os" "strconv" "text/template" "time" @@ -64,6 +65,16 @@ func (b *Builder) Prepare(raws ...interface{}) error { // Optional configuration with defaults // + if b.config.APIKey == "" { + // Default to environment variable for api_key, if it exists + b.config.APIKey = os.Getenv("DIGITALOCEAN_API_KEY") + } + + if b.config.ClientID == "" { + // Default to environment variable for client_id, if it exists + b.config.ClientID = os.Getenv("DIGITALOCEAN_CLIENT_ID") + } + if b.config.RegionID == 0 { // Default to Region "New York" b.config.RegionID = 1 diff --git a/website/source/docs/builders/digitalocean.html.markdown b/website/source/docs/builders/digitalocean.html.markdown index 990410dd1..ba4cba4e5 100644 --- a/website/source/docs/builders/digitalocean.html.markdown +++ b/website/source/docs/builders/digitalocean.html.markdown @@ -25,11 +25,13 @@ Required: * `api_key` (string) - The API key to use to access your account. You can retrieve this on the "API" page visible after logging into your account - on DigitalOcean. + on DigitalOcean. Alternatively, the builder looks for the environment + variable `DIGITALOCEAN_API_KEY`. * `client_id` (string) - The client ID to use to access your account. You can find this on the "API" page visible after logging into your account on - DigitalOcean. + DigitalOcean. Alternatively, the builder looks for the environment + variable `DIGITALOCEAN_CLIENT_ID`. Optional: From 490279c6b98d6906736396d5a56d2c04a61a8820 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Fri, 12 Jul 2013 09:47:45 +0200 Subject: [PATCH 2/2] builder/digitalocean: add tests for credentials via env vars --- builder/digitalocean/builder_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/builder/digitalocean/builder_test.go b/builder/digitalocean/builder_test.go index 9a0c3ed07..6fe9217dc 100644 --- a/builder/digitalocean/builder_test.go +++ b/builder/digitalocean/builder_test.go @@ -2,10 +2,17 @@ package digitalocean import ( "github.com/mitchellh/packer/packer" + "os" "strconv" "testing" ) +func init() { + // Clear out the credential env vars + os.Setenv("DIGITALOCEAN_API_KEY", "") + os.Setenv("DIGITALOCEAN_CLIENT_ID", "") +} + func testConfig() map[string]interface{} { return map[string]interface{}{ "client_id": "foo", @@ -55,6 +62,15 @@ func TestBuilderPrepare_APIKey(t *testing.T) { if err == nil { t.Fatal("should have error") } + + // Test env variable + delete(config, "api_key") + os.Setenv("DIGITALOCEAN_API_KEY", "foo") + defer os.Setenv("DIGITALOCEAN_API_KEY", "") + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } } func TestBuilderPrepare_ClientID(t *testing.T) { @@ -79,6 +95,15 @@ func TestBuilderPrepare_ClientID(t *testing.T) { if err == nil { t.Fatal("should have error") } + + // Test env variable + delete(config, "client_id") + os.Setenv("DIGITALOCEAN_CLIENT_ID", "foo") + defer os.Setenv("DIGITALOCEAN_CLIENT_ID", "") + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } } func TestBuilderPrepare_RegionID(t *testing.T) {