Fixed tests and updated the provider schema to use a DefaultFunc

The default stack changed from ‘cedar’ to ‘cedar-14’, so updated the
acceptance tests to reflect this.

Updating the schema makes testing easier and gives you a way to
configure the provider using env variables. It also makes the provider
more inline following the TF 0.2 approach.
This commit is contained in:
Sander van Harmelen 2014-11-19 14:25:18 +01:00
parent c1a6a48892
commit 1b6f37b6eb
4 changed files with 23 additions and 57 deletions

View File

@ -3,29 +3,18 @@ package heroku
import (
"log"
"net/http"
"os"
"github.com/cyberdelia/heroku-go/v3"
)
type Config struct {
APIKey string `mapstructure:"api_key"`
Email string `mapstructure:"email"`
Email string
APIKey string
}
// Client() returns a new Service for accessing Heroku.
//
func (c *Config) Client() (*heroku.Service, 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("HEROKU_EMAIL"); v != "" {
c.Email = v
}
if v := os.Getenv("HEROKU_API_KEY"); v != "" {
c.APIKey = v
}
service := heroku.NewService(&http.Client{
Transport: &heroku.Transport{
Username: c.Email,

View File

@ -2,10 +2,10 @@ package heroku
import (
"log"
"os"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure"
)
// Provider returns a terraform.ResourceProvider.
@ -13,13 +13,15 @@ func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"email": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_EMAIL"),
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DefaultFunc: envDefaultFunc("HEROKU_API_KEY"),
},
},
@ -34,11 +36,20 @@ 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{
Email: d.Get("email").(string),
APIKey: d.Get("api_key").(string),
}
log.Println("[INFO] Initializing Heroku client")

View File

@ -4,7 +4,6 @@ import (
"os"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -29,39 +28,6 @@ func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func TestProviderConfigure(t *testing.T) {
var expectedKey string
var expectedEmail string
if v := os.Getenv("HEROKU_EMAIL"); v != "" {
expectedEmail = v
} else {
expectedEmail = "foo"
}
if v := os.Getenv("HEROKU_API_KEY"); v != "" {
expectedKey = v
} else {
expectedKey = "foo"
}
raw := map[string]interface{}{
"api_key": expectedKey,
"email": expectedEmail,
}
rawConfig, err := config.NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
rp := Provider()
err = rp.Configure(terraform.NewResourceConfig(rawConfig))
if err != nil {
t.Fatalf("err: %s", err)
}
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("HEROKU_EMAIL"); v == "" {
t.Fatal("HEROKU_EMAIL must be set for acceptance tests")

View File

@ -128,7 +128,7 @@ func testAccCheckHerokuAppAttributes(app *heroku.App) resource.TestCheckFunc {
return fmt.Errorf("Bad region: %s", app.Region.Name)
}
if app.Stack.Name != "cedar" {
if app.Stack.Name != "cedar-14" {
return fmt.Errorf("Bad stack: %s", app.Stack.Name)
}