Updated the provider to make testing a little easier

Also makes the provider more inline with the others following the TF
0.2 approach.
This commit is contained in:
Sander van Harmelen 2014-11-18 15:21:32 +01:00
parent c1a6a48892
commit 2cb1fd8987
3 changed files with 17 additions and 48 deletions

View File

@ -2,25 +2,18 @@ package mailgun
import (
"log"
"os"
"github.com/pearkes/mailgun"
)
type Config struct {
APIKey string `mapstructure:"api_key"`
APIKey string
}
// Client() returns a new client for accessing mailgun.
//
func (c *Config) Client() (*mailgun.Client, error) {
// If we have env vars set (like in the acc) tests,
// we need to override the values passed in here.
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
c.APIKey = v
}
// We don't set a domain right away
client, err := mailgun.NewClient(c.APIKey)

View File

@ -2,10 +2,10 @@ package mailgun
import (
"log"
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure"
)
// Provider returns a terraform.ResourceProvider.
@ -13,8 +13,9 @@ func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"api_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
DefaultFunc: envDefaultFunc("MAILGUN_API_KEY"),
},
},
@ -26,14 +27,21 @@ func Provider() terraform.ResourceProvider {
}
}
func envDefaultFunc(k string) schema.SchemaDefaultFunc {
return func() (interface{}, error) {
if v := os.Getenv(k); v != "" {
return v, nil
}
return nil, nil
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var config Config
configRaw := d.Get("").(map[string]interface{})
if err := mapstructure.Decode(configRaw, &config); err != nil {
return nil, err
config := Config{
APIKey: d.Get("api_key").(string),
}
log.Println("[INFO] Initializing Mailgun client")
return config.Client()
}

View File

@ -4,10 +4,8 @@ import (
"os"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/pearkes/mailgun"
)
var testAccProviders map[string]terraform.ResourceProvider
@ -30,36 +28,6 @@ func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func TestProviderConfigure(t *testing.T) {
var expectedKey string
if v := os.Getenv("MAILGUN_API_KEY"); v != "" {
expectedKey = v
} else {
expectedKey = "foo"
}
raw := map[string]interface{}{
"api_key": expectedKey,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
rp := Provider().(*schema.Provider)
err = rp.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("err: %s", err)
}
config := rp.Meta().(*mailgun.Client)
if config.ApiKey != expectedKey {
t.Fatalf("bad: %#v", config)
}
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("MAILGUN_API_KEY"); v == "" {
t.Fatal("MAILGUN_API_KEY must be set for acceptance tests")