Skip to content

Commit

Permalink
completely change up config.go to return Config struct [add-support-f…
Browse files Browse the repository at this point in the history
…or-netrc]
  • Loading branch information
davidji99 committed Sep 10, 2018
1 parent a3ab463 commit a2aba83
Show file tree
Hide file tree
Showing 42 changed files with 291 additions and 291 deletions.
8 changes: 5 additions & 3 deletions heroku/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ type Config struct {
Email string
APIKey string
Headers http.Header

Api *heroku.Service
}

// Client returns a new Service for accessing Heroku.
func (c *Config) Client() (*heroku.Service, error) {
func (c *Config) loadAndInitialize() error {
var debugHTTP = false
if logging.IsDebugOrHigher() {
debugHTTP = true
}
service := heroku.NewService(&http.Client{
c.Api = heroku.NewService(&http.Client{
Transport: &heroku.Transport{
Username: c.Email,
Password: c.APIKey,
Expand All @@ -32,5 +34,5 @@ func (c *Config) Client() (*heroku.Service, error) {

log.Printf("[INFO] Heroku Client configured for user: %s", c.Email)

return service, nil
return nil
}
3 changes: 1 addition & 2 deletions heroku/data_source_heroku_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/hashicorp/terraform/helper/schema"
"github.com/heroku/heroku-go/v3"
)

func dataSourceHerokuApp() *schema.Resource {
Expand Down Expand Up @@ -99,7 +98,7 @@ func dataSourceHerokuApp() *schema.Resource {
}

func dataSourceHerokuAppRead(d *schema.ResourceData, m interface{}) error {
client := m.(*heroku.Service)
client := m.(*Config)

name := d.Get("name").(string)
app, err := resourceHerokuAppRetrieve(name, true, client)
Expand Down
3 changes: 1 addition & 2 deletions heroku/data_source_heroku_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package heroku

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/heroku/heroku-go/v3"
)

func dataSourceHerokuSpace() *schema.Resource {
Expand Down Expand Up @@ -56,7 +55,7 @@ func dataSourceHerokuSpace() *schema.Resource {
}

func dataSourceHerokuSpaceRead(d *schema.ResourceData, m interface{}) error {
client := m.(*heroku.Service)
client := m.(*Config)

name := d.Get("name").(string)
spaceRaw, _, err := SpaceStateRefreshFunc(client, name)()
Expand Down
5 changes: 2 additions & 3 deletions heroku/data_source_heroku_space_peering_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/hashicorp/terraform/helper/schema"
"github.com/heroku/heroku-go/v3"
)

func dataSourceHerokuSpacePeeringInfo() *schema.Resource {
Expand Down Expand Up @@ -56,12 +55,12 @@ func dataSourceHerokuSpacePeeringInfo() *schema.Resource {
}

func dataSourceHerokuSpacePeeringInfoRead(d *schema.ResourceData, m interface{}) error {
client := m.(*heroku.Service)
client := m.(*Config)

name := d.Get("name").(string)
d.SetId(name)

peeringInfo, err := client.PeeringInfoInfo(context.TODO(), name)
peeringInfo, err := client.Api.PeeringInfoInfo(context.TODO(), name)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions heroku/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func getEmail(d *schema.ResourceData) string {
return email
}

func doesHerokuAppExist(appName string, client *heroku.Service) (*heroku.App, error) {
app, err := client.AppInfo(context.TODO(), appName)
func doesHerokuAppExist(appName string, client *Config) (*heroku.App, error) {
app, err := client.Api.AppInfo(context.TODO(), appName)

if err != nil {
log.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion heroku/import_heroku_pipeline_coupling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
heroku "github.com/heroku/heroku-go/v3"
"github.com/heroku/heroku-go/v3"
)

func TestAccHerokuPipelineCoupling_importBasic(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion heroku/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}

log.Println("[INFO] Initializing Heroku client")
return config.Client()
if err := config.loadAndInitialize(); err != nil {
return nil, err
}

return &config, nil
}

func buildCompositeID(a, b string) string {
Expand Down
19 changes: 9 additions & 10 deletions heroku/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"net/http/httptest"
"testing"

"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/heroku/heroku-go/v3"
"github.com/stretchr/testify/assert"
helper "github.com/terraform-providers/terraform-provider-heroku/helper/test"
"os"
"io/ioutil"
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/stretchr/testify/assert"
"os"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand Down Expand Up @@ -58,10 +57,10 @@ func TestProviderConfigureUsesHeadersForClient(t *testing.T) {
}))
defer srv.Close()

c := client.(*heroku.Service)
c.URL = srv.URL
c := client.(*Config)
c.Api.URL = srv.URL

_, err = c.AppInfo(context.Background(), "does-not-matter")
_, err = c.Api.AppInfo(context.Background(), "does-not-matter")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -90,8 +89,8 @@ func TestProviderConfigureUseNetrc(t *testing.T) {
}
configuration := meta.(*Config)

assert.Equal(t, "email_login", configuration.Email)
assert.Equal(t, "api_key", configuration.APIKey)
assert.Equal(t, "email_login", configuration)
//assert.Equal(t, "api_key", configuration.APIKey)
}

func testAccPreCheck(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions heroku/resource_heroku_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
addonLock.Lock()
defer addonLock.Unlock()

client := meta.(*heroku.Service)
client := meta.(*Config)

app := d.Get("app").(string)
opts := heroku.AddOnCreateOpts{
Expand All @@ -100,7 +100,7 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
}

log.Printf("[DEBUG] Addon create configuration: %#v, %#v", app, opts)
a, err := client.AddOnCreate(context.TODO(), app, opts)
a, err := client.Api.AddOnCreate(context.TODO(), app, opts)
if err != nil {
return err
}
Expand All @@ -126,7 +126,7 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
}

func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

addon, err := resourceHerokuAddonRetrieve(d.Id(), client)
if err != nil {
Expand Down Expand Up @@ -158,12 +158,12 @@ func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceHerokuAddonUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

app := d.Get("app").(string)

if d.HasChange("plan") {
ad, err := client.AddOnUpdate(
ad, err := client.Api.AddOnUpdate(
context.TODO(), app, d.Id(), heroku.AddOnUpdateOpts{Plan: d.Get("plan").(string)})
if err != nil {
return err
Expand All @@ -177,12 +177,12 @@ func resourceHerokuAddonUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceHerokuAddonDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

log.Printf("[INFO] Deleting Addon: %s", d.Id())

// Destroy the app
_, err := client.AddOnDelete(context.TODO(), d.Get("app").(string), d.Id())
_, err := client.Api.AddOnDelete(context.TODO(), d.Get("app").(string), d.Id())
if err != nil {
return fmt.Errorf("Error deleting addon: %s", err)
}
Expand All @@ -192,9 +192,9 @@ func resourceHerokuAddonDelete(d *schema.ResourceData, meta interface{}) error {
}

func resourceHerokuAddonExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*heroku.Service)
client := meta.(*Config)

_, err := client.AddOnInfo(context.TODO(), d.Id())
_, err := client.Api.AddOnInfo(context.TODO(), d.Id())
if err != nil {
if herr, ok := err.(*url.Error).Err.(heroku.Error); ok && herr.ID == "not_found" {
return false, nil
Expand All @@ -205,8 +205,8 @@ func resourceHerokuAddonExists(d *schema.ResourceData, meta interface{}) (bool,
return true, nil
}

func resourceHerokuAddonRetrieve(id string, client *heroku.Service) (*heroku.AddOn, error) {
addon, err := client.AddOnInfo(context.TODO(), id)
func resourceHerokuAddonRetrieve(id string, client *Config) (*heroku.AddOn, error) {
addon, err := client.Api.AddOnInfo(context.TODO(), id)

if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
Expand All @@ -215,8 +215,8 @@ func resourceHerokuAddonRetrieve(id string, client *heroku.Service) (*heroku.Add
return addon, nil
}

func resourceHerokuAddonRetrieveByApp(app string, id string, client *heroku.Service) (*heroku.AddOn, error) {
addon, err := client.AddOnInfoByApp(context.TODO(), app, id)
func resourceHerokuAddonRetrieveByApp(app string, id string, client *Config) (*heroku.AddOn, error) {
addon, err := client.Api.AddOnInfoByApp(context.TODO(), app, id)

if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
Expand All @@ -227,7 +227,7 @@ func resourceHerokuAddonRetrieveByApp(app string, id string, client *heroku.Serv

// AddOnStateRefreshFunc returns a resource.StateRefreshFunc that is used to
// watch an AddOn.
func AddOnStateRefreshFunc(client *heroku.Service, appID, addOnID string) resource.StateRefreshFunc {
func AddOnStateRefreshFunc(client *Config, appID, addOnID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
addon, err := resourceHerokuAddonRetrieveByApp(appID, addOnID, client)

Expand Down
12 changes: 6 additions & 6 deletions heroku/resource_heroku_addon_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func resourceHerokuAddonAttachment() *schema.Resource {
}

func resourceHerokuAddonAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

opts := heroku.AddOnAttachmentCreateOpts{Addon: d.Get("addon_id").(string), App: d.Get("app_id").(string)}

Expand All @@ -56,7 +56,7 @@ func resourceHerokuAddonAttachmentCreate(d *schema.ResourceData, meta interface{
}

log.Printf("[DEBUG] Addon Attachment create configuration: %#v", opts)
a, err := client.AddOnAttachmentCreate(context.TODO(), opts)
a, err := client.Api.AddOnAttachmentCreate(context.TODO(), opts)
if err != nil {
return err
}
Expand All @@ -68,14 +68,14 @@ func resourceHerokuAddonAttachmentCreate(d *schema.ResourceData, meta interface{
}

func resourceHerokuAddonAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

match, err := regexp.MatchString(`^[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+$`, d.Id())
if !match {
return fmt.Errorf("You can only import addon attachments by their unique ID")
}

addonattachment, err := client.AddOnAttachmentInfo(context.TODO(), d.Id())
addonattachment, err := client.Api.AddOnAttachmentInfo(context.TODO(), d.Id())
if err != nil {
return fmt.Errorf("Error retrieving addon attachment: %s", err)
}
Expand All @@ -88,12 +88,12 @@ func resourceHerokuAddonAttachmentRead(d *schema.ResourceData, meta interface{})
}

func resourceHerokuAddonAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
client := meta.(*Config)

log.Printf("[INFO] Deleting Addon Attachment: %s", d.Id())

// Destroy the app
_, err := client.AddOnAttachmentDelete(context.TODO(), d.Id())
_, err := client.Api.AddOnAttachmentDelete(context.TODO(), d.Id())
if err != nil {
return fmt.Errorf("Error deleting addon attachment: %s", err)
}
Expand Down
7 changes: 3 additions & 4 deletions heroku/resource_heroku_addon_attachment_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"
"github.com/hashicorp/terraform/terraform"
"github.com/heroku/heroku-go/v3"
"log"
)

func resourceHerokuAddonAttachmentMigrateState(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
conn := meta.(*heroku.Service)
conn := meta.(*Config)

log.Printf("[DEBUG] Current version of state file is: v%v", v)

Expand All @@ -23,7 +22,7 @@ func resourceHerokuAddonAttachmentMigrateState(v int, is *terraform.InstanceStat
}

// Migrate attachment addons in state file to use the UUID for addon_id instead of the NAME for addon_id
func migrateAddonAttachmentStateV0toV1(is *terraform.InstanceState, client *heroku.Service) (*terraform.InstanceState, error) {
func migrateAddonAttachmentStateV0toV1(is *terraform.InstanceState, client *Config) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
log.Println("[DEBUG] Empty Heroku Addon Attachment State; nothing to migrate.")
return is, nil
Expand All @@ -34,7 +33,7 @@ func migrateAddonAttachmentStateV0toV1(is *terraform.InstanceState, client *hero
attachmentAppId := is.Attributes["app_id"]
attachmentAddOnId := is.Attributes["id"]

addon, err := client.AddOnInfoByApp(context.TODO(), attachmentAppId, attachmentAddOnId)
addon, err := client.Api.AddOnInfoByApp(context.TODO(), attachmentAppId, attachmentAddOnId)
if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
}
Expand Down
7 changes: 3 additions & 4 deletions heroku/resource_heroku_addon_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"
"github.com/hashicorp/terraform/terraform"
"github.com/heroku/heroku-go/v3"
"log"
)

func resourceHerokuAddonMigrate(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
conn := meta.(*heroku.Service)
conn := meta.(*Config)

log.Printf("[DEBUG] Current version of state file is: v%v", v)

Expand All @@ -22,7 +21,7 @@ func resourceHerokuAddonMigrate(v int, is *terraform.InstanceState, meta interfa
}
}

func migrateAddonIdsStateV0toV1(is *terraform.InstanceState, client *heroku.Service) (*terraform.InstanceState, error) {
func migrateAddonIdsStateV0toV1(is *terraform.InstanceState, client *Config) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
log.Println("[DEBUG] Empty Heroku Addon State; nothing to migrate.")
return is, nil
Expand All @@ -34,7 +33,7 @@ func migrateAddonIdsStateV0toV1(is *terraform.InstanceState, client *heroku.Serv
currentAddonId := is.ID
addonAppId := is.Attributes["app"]

addon, err := client.AddOnInfoByApp(context.TODO(), addonAppId, currentAddonId)
addon, err := client.Api.AddOnInfoByApp(context.TODO(), addonAppId, currentAddonId)
if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
}
Expand Down
Loading

0 comments on commit a2aba83

Please sign in to comment.