Skip to content

Commit 2cb1fd8

Browse files
author
Sander van Harmelen
committed
Updated the provider to make testing a little easier
Also makes the provider more inline with the others following the TF 0.2 approach.
1 parent c1a6a48 commit 2cb1fd8

3 files changed

Lines changed: 17 additions & 48 deletions

File tree

builtin/providers/mailgun/config.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@ package mailgun
22

33
import (
44
"log"
5-
"os"
65

76
"github.com/pearkes/mailgun"
87
)
98

109
type Config struct {
11-
APIKey string `mapstructure:"api_key"`
10+
APIKey string
1211
}
1312

1413
// Client() returns a new client for accessing mailgun.
1514
//
1615
func (c *Config) Client() (*mailgun.Client, error) {
1716

18-
// If we have env vars set (like in the acc) tests,
19-
// we need to override the values passed in here.
20-
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
21-
c.APIKey = v
22-
}
23-
2417
// We don't set a domain right away
2518
client, err := mailgun.NewClient(c.APIKey)
2619

builtin/providers/mailgun/provider.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ package mailgun
22

33
import (
44
"log"
5+
"os"
56

67
"github.com/hashicorp/terraform/helper/schema"
78
"github.com/hashicorp/terraform/terraform"
8-
"github.com/mitchellh/mapstructure"
99
)
1010

1111
// Provider returns a terraform.ResourceProvider.
1212
func Provider() terraform.ResourceProvider {
1313
return &schema.Provider{
1414
Schema: map[string]*schema.Schema{
1515
"api_key": &schema.Schema{
16-
Type: schema.TypeString,
17-
Required: true,
16+
Type: schema.TypeString,
17+
Required: true,
18+
DefaultFunc: envDefaultFunc("MAILGUN_API_KEY"),
1819
},
1920
},
2021

@@ -26,14 +27,21 @@ func Provider() terraform.ResourceProvider {
2627
}
2728
}
2829

30+
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
31+
return func() (interface{}, error) {
32+
if v := os.Getenv(k); v != "" {
33+
return v, nil
34+
}
35+
36+
return nil, nil
37+
}
38+
}
39+
2940
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
30-
var config Config
31-
configRaw := d.Get("").(map[string]interface{})
32-
if err := mapstructure.Decode(configRaw, &config); err != nil {
33-
return nil, err
41+
config := Config{
42+
APIKey: d.Get("api_key").(string),
3443
}
3544

3645
log.Println("[INFO] Initializing Mailgun client")
37-
3846
return config.Client()
3947
}

builtin/providers/mailgun/provider_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"os"
55
"testing"
66

7-
"github.com/hashicorp/terraform/config"
87
"github.com/hashicorp/terraform/helper/schema"
98
"github.com/hashicorp/terraform/terraform"
10-
"github.com/pearkes/mailgun"
119
)
1210

1311
var testAccProviders map[string]terraform.ResourceProvider
@@ -30,36 +28,6 @@ func TestProvider_impl(t *testing.T) {
3028
var _ terraform.ResourceProvider = Provider()
3129
}
3230

33-
func TestProviderConfigure(t *testing.T) {
34-
var expectedKey string
35-
36-
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
37-
expectedKey = v
38-
} else {
39-
expectedKey = "foo"
40-
}
41-
42-
raw := map[string]interface{}{
43-
"api_key": expectedKey,
44-
}
45-
46-
rawConfig, err := config.NewRawConfig(raw)
47-
if err != nil {
48-
t.Fatalf("err: %s", err)
49-
}
50-
51-
rp := Provider().(*schema.Provider)
52-
err = rp.Configure(terraform.NewResourceConfig(rawConfig))
53-
if err != nil {
54-
t.Fatalf("err: %s", err)
55-
}
56-
57-
config := rp.Meta().(*mailgun.Client)
58-
if config.ApiKey != expectedKey {
59-
t.Fatalf("bad: %#v", config)
60-
}
61-
}
62-
6331
func testAccPreCheck(t *testing.T) {
6432
if v := os.Getenv("MAILGUN_API_KEY"); v == "" {
6533
t.Fatal("MAILGUN_API_KEY must be set for acceptance tests")

0 commit comments

Comments
 (0)