Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying initial attachment name when creating addon. #231

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions heroku/resource_heroku_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func resourceHerokuAddon() *schema.Resource {
ValidateFunc: validateCustomAddonName,
},

"attachment_name": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},

"config": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -119,6 +125,10 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
opts.Name = &v
}

if v := d.Get("attachment_name").(string); v != "" {
opts.Attachment.Name = &v
}

log.Printf("[DEBUG] Addon create configuration: %#v, %#v", app, opts)
a, err := client.AddOnCreate(context.TODO(), app, opts)
if err != nil {
Expand Down Expand Up @@ -148,11 +158,20 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Config).Api

// Retrieve the addon
addon, err := resourceHerokuAddonRetrieve(d.Id(), client)
if err != nil {
return err
}

// Retrieve the addon's attachments to set the addon.attachment_name.
// By default, the addon will always have at least one initial attachment to the app that the addon is tied to.
lr := &heroku.ListRange{}
addonAttachments, attachmentReadErr := client.AddOnAttachmentListByAddOn(context.TODO(), addon.ID, lr)
if attachmentReadErr != nil {
return attachmentReadErr
}

// Determine the plan. If we were configured without a specific plan,
// then just avoid the plan altogether (accepting anything that
// Heroku sends down).
Expand All @@ -166,12 +185,15 @@ func resourceHerokuAddonRead(d *schema.ResourceData, meta interface{}) error {
}
}

d.Set("name", addon.Name)
d.Set("app", addon.App.Name)
d.Set("plan", plan)
d.Set("provider_id", addon.ProviderID)
if err := d.Set("config_vars", addon.ConfigVars); err != nil {
return err
var setErr error
setErr = d.Set("name", addon.Name)
setErr = d.Set("app", addon.App.Name)
setErr = d.Set("plan", plan)
setErr = d.Set("provider_id", addon.ProviderID)
setErr = d.Set("config_vars", addon.ConfigVars)
setErr = d.Set("attachment_name", addonAttachments[0].ID) // I *think* it's safe to just read the zero index attachment.
if setErr != nil {
return setErr
}

return nil
Expand Down
16 changes: 10 additions & 6 deletions heroku/resource_heroku_addon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ func TestAccHerokuAddon_CustomName(t *testing.T) {
var addon heroku.AddOn
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
customName := fmt.Sprintf("custom-addonname-%s", acctest.RandString(15))
customAttachmentName := fmt.Sprintf("custom-attachment-name-%s", acctest.RandString(15))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName),
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName, customAttachmentName),
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
testAccCheckHerokuAddonAttributes(&addon, "memcachier:dev"),
Expand All @@ -94,6 +95,8 @@ func TestAccHerokuAddon_CustomName(t *testing.T) {
"heroku_addon.foobar", "plan", "memcachier"),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "name", customName),
resource.TestCheckResourceAttr(
"heroku_addon.foobar", "attachment_name", customAttachmentName),
),
},
},
Expand All @@ -110,7 +113,7 @@ func TestAccHerokuAddon_CustomName_Invalid(t *testing.T) {
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName),
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName, ""),
ExpectError: regexp.MustCompile(`config is invalid: invalid value for name`),
},
},
Expand All @@ -127,7 +130,7 @@ func TestAccHerokuAddon_CustomName_EmptyString(t *testing.T) {
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName),
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName, ""),
ExpectError: regexp.MustCompile(`config is invalid: 2 problems:.*`),
},
},
Expand All @@ -144,7 +147,7 @@ func TestAccHerokuAddon_CustomName_FirstCharNum(t *testing.T) {
CheckDestroy: testAccCheckHerokuAddonDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName),
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName, ""),
ExpectError: regexp.MustCompile(`config is invalid: invalid value for name`),
},
},
Expand Down Expand Up @@ -269,7 +272,7 @@ resource "heroku_addon" "foobar" {
}`, appName)
}

func testAccCheckHerokuAddonConfig_CustomName(appName, customAddonName string) string {
func testAccCheckHerokuAddonConfig_CustomName(appName, customAddonName, customAttachmentName string) string {
return fmt.Sprintf(`
resource "heroku_app" "foobar" {
name = "%s"
Expand All @@ -280,5 +283,6 @@ resource "heroku_addon" "foobar" {
app = "${heroku_app.foobar.name}"
plan = "memcachier"
name = "%s"
}`, appName, customAddonName)
attachment_name = "%s"
}`, appName, customAddonName, customAttachmentName)
}
1 change: 1 addition & 0 deletions website/docs/r/addon.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The following arguments are supported:
* `plan` - (Required) The addon to add.
* `config` - (Optional) Optional plan configuration.
* `name` - (Optional) Globally unique name of the add-on.
* `attachment_name` - (Optional) Unique name for this add-on attachment to this app.

## Attributes Reference

Expand Down