Merge pull request #576 from svanharmelen/f-update-mailgun-provider

providers/mailgun: updated the provider schema to use a DefaultFunc
This commit is contained in:
Armon Dadgar 2014-11-19 13:58:53 -08:00
commit 5b8efd1ba8
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.
@ -15,6 +15,7 @@ func Provider() terraform.ResourceProvider {
"api_key": &schema.Schema{
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")