From 0c80f77f52e362dcb1003c932886ec9f581555c7 Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Wed, 9 Jan 2019 15:34:16 -0500 Subject: [PATCH] Add tests for overriding DigitalOcean API URL. --- digitalocean/config.go | 10 ++++---- digitalocean/provider_test.go | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/digitalocean/config.go b/digitalocean/config.go index 8aeb77b1..c3ac918f 100644 --- a/digitalocean/config.go +++ b/digitalocean/config.go @@ -62,7 +62,7 @@ func (c *Config) Client() (*CombinedConfig, error) { userAgent := fmt.Sprintf("Terraform/%s", terraform.VersionString()) - do, err := godo.New(oauth2.NewClient(oauth2.NoContext, tokenSrc), godo.SetUserAgent(userAgent)) + godoClient, err := godo.New(oauth2.NewClient(oauth2.NoContext, tokenSrc), godo.SetUserAgent(userAgent)) if err != nil { return nil, err } @@ -71,16 +71,16 @@ func (c *Config) Client() (*CombinedConfig, error) { if err != nil { return nil, err } - client.BaseURL = apiURL + godoClient.BaseURL = apiURL if logging.IsDebugOrHigher() { - do.OnRequestCompleted(logRequestAndResponse) + godoClient.OnRequestCompleted(logRequestAndResponse) } - log.Printf("[INFO] DigitalOcean Client configured for URL: %s", do.BaseURL.String()) + log.Printf("[INFO] DigitalOcean Client configured for URL: %s", godoClient.BaseURL.String()) return &CombinedConfig{ - client: do, + client: godoClient, accessID: c.AccessID, secretKey: c.SecretKey, }, nil diff --git a/digitalocean/provider_test.go b/digitalocean/provider_test.go index fc5f78a2..b5b0eba1 100644 --- a/digitalocean/provider_test.go +++ b/digitalocean/provider_test.go @@ -4,6 +4,7 @@ import ( "os" "testing" + "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -33,3 +34,50 @@ func testAccPreCheck(t *testing.T) { t.Fatal("DIGITALOCEAN_TOKEN must be set for acceptance tests") } } + +func TestURLOverride(t *testing.T) { + customEndpoint := "https://mock-api.internal.example.com/" + + rawProvider := Provider() + raw := map[string]interface{}{ + "token": "12345", + "api_endpoint": customEndpoint, + } + + rawConfig, err := config.NewRawConfig(raw) + if err != nil { + t.Fatalf("err: %s", err) + } + + err = rawProvider.Configure(terraform.NewResourceConfig(rawConfig)) + meta := rawProvider.(*schema.Provider).Meta() + if meta == nil { + t.Fatalf("Expected metadata, got nil: err: %s", err) + } + client := meta.(*CombinedConfig).godoClient() + if client.BaseURL.String() != customEndpoint { + t.Fatalf("Expected %s, got %s", customEndpoint, client.BaseURL.String()) + } +} + +func TestURLDefault(t *testing.T) { + rawProvider := Provider() + raw := map[string]interface{}{ + "token": "12345", + } + + rawConfig, err := config.NewRawConfig(raw) + if err != nil { + t.Fatalf("err: %s", err) + } + + err = rawProvider.Configure(terraform.NewResourceConfig(rawConfig)) + meta := rawProvider.(*schema.Provider).Meta() + if meta == nil { + t.Fatalf("Expected metadata, got nil: err: %s", err) + } + client := meta.(*CombinedConfig).godoClient() + if client.BaseURL.String() != "https://api.digitalocean.com" { + t.Fatalf("Expected %s, got %s", "https://api.digitalocean.com", client.BaseURL.String()) + } +}