Skip to content

Commit da2d6b9

Browse files
authored
App data source gets last successful Release & Slug IDs (#376)
* App data source now provides the app's last successful Release ID & Slug ID * Fix broken add-on tests to use Heroku add-on instead of 3rd party
1 parent 7311531 commit da2d6b9

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

docs/data-sources/app.md

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ The following attributes are exported:
5353
* `heroku_hostname` - The hostname for the Heroku application, suitable
5454
for pointing DNS records.
5555

56+
* `last_release_id` - The last successful Release ID for the app. May be empty.
57+
58+
* `last_slug_id` - The Slug ID from the last successful release. May be empty.
59+
5660
* `config_vars` - A map of all configuration variables for the app.
5761

5862
* `acm` - True if Heroku ACM is enabled for this app, false otherwise.

heroku/data_source_heroku_app.go

+33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package heroku
22

33
import (
4+
"context"
5+
"fmt"
6+
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
heroku "github.com/heroku/heroku-go/v5"
59
)
610

711
func dataSourceHerokuApp() *schema.Resource {
@@ -69,6 +73,16 @@ func dataSourceHerokuApp() *schema.Resource {
6973
Computed: true,
7074
},
7175

76+
"last_release_id": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
},
80+
81+
"last_slug_id": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
},
85+
7286
"organization": {
7387
Type: schema.TypeList,
7488
Computed: true,
@@ -126,5 +140,24 @@ func dataSourceHerokuAppRead(d *schema.ResourceData, m interface{}) error {
126140
d.Set("buildpacks", app.Buildpacks)
127141
d.Set("config_vars", app.Vars)
128142

143+
releaseRange := heroku.ListRange{
144+
Field: "version",
145+
Max: 200,
146+
Descending: true,
147+
}
148+
releases, err := client.ReleaseList(context.Background(), app.App.ID, &releaseRange)
149+
if err != nil {
150+
return fmt.Errorf("Failed to fetch releases for app '%s': %s", name, err)
151+
}
152+
for _, r := range releases {
153+
if r.Status == "succeeded" {
154+
d.Set("last_release_id", r.ID)
155+
if r.Slug != nil {
156+
d.Set("last_slug_id", r.Slug.ID)
157+
}
158+
break
159+
}
160+
}
161+
129162
return nil
130163
}

heroku/data_source_heroku_app_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ func TestAccDatasourceHerokuApp_Basic(t *testing.T) {
4040
})
4141
}
4242

43+
func TestAccDatasourceHerokuApp_ReleaseIDAndSlugID(t *testing.T) {
44+
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
45+
46+
resource.Test(t, resource.TestCase{
47+
PreCheck: func() { testAccPreCheck(t) },
48+
Providers: testAccProviders,
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccCheckHerokuAppWithDatasource_slugRelease(appName),
52+
Check: resource.ComposeTestCheckFunc(
53+
resource.TestCheckResourceAttrSet(
54+
"data.heroku_app.foobar", "last_release_id"),
55+
resource.TestCheckResourceAttrSet(
56+
"data.heroku_app.foobar", "last_slug_id"),
57+
),
58+
},
59+
},
60+
})
61+
}
62+
4363
func TestAccDatasourceHerokuApp_Organization(t *testing.T) {
4464
appName := fmt.Sprintf("tftest-%s", acctest.RandString(10))
4565
org := os.Getenv("HEROKU_SPACES_ORGANIZATION")
@@ -108,6 +128,35 @@ data "heroku_app" "foobar" {
108128
`, appName)
109129
}
110130

131+
func testAccCheckHerokuAppWithDatasource_slugRelease(appName string) string {
132+
return fmt.Sprintf(`
133+
resource "heroku_app" "foobar" {
134+
name = "%s"
135+
region = "us"
136+
}
137+
138+
resource "heroku_slug" "foobar" {
139+
app_id = heroku_app.foobar.id
140+
buildpack_provided_description = "Ruby"
141+
file_path = "test-fixtures/slug.tgz"
142+
process_types = {
143+
web = "ruby server.rb"
144+
}
145+
}
146+
147+
resource "heroku_app_release" "foobar" {
148+
app_id = heroku_app.foobar.id
149+
slug_id = heroku_slug.foobar.id
150+
}
151+
152+
data "heroku_app" "foobar" {
153+
name = "${heroku_app.foobar.name}"
154+
155+
depends_on = [heroku_app_release.foobar]
156+
}
157+
`, appName)
158+
}
159+
111160
func testAccCheckHerokuApp_organization(appName, orgName string) string {
112161
return fmt.Sprintf(`
113162
resource "heroku_app" "foobar" {

heroku/resource_heroku_addon_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ func TestAccHerokuAddon_noPlan(t *testing.T) {
5050
Config: testAccCheckHerokuAddonConfig_no_plan(appName),
5151
Check: resource.ComposeTestCheckFunc(
5252
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
53-
testAccCheckHerokuAddonPlan(&addon, "memcachier:dev"),
53+
testAccCheckHerokuAddonPlan(&addon, "heroku-postgresql:mini"),
5454
resource.TestCheckResourceAttrSet(
5555
"heroku_addon.foobar", "app_id"),
5656
resource.TestCheckResourceAttr(
57-
"heroku_addon.foobar", "plan", "memcachier"),
57+
"heroku_addon.foobar", "plan", "heroku-postgresql:mini"),
5858
),
5959
},
6060
{
6161
Config: testAccCheckHerokuAddonConfig_no_plan(appName),
6262
Check: resource.ComposeTestCheckFunc(
6363
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
64-
testAccCheckHerokuAddonPlan(&addon, "memcachier:dev"),
64+
testAccCheckHerokuAddonPlan(&addon, "heroku-postgresql:mini"),
6565
resource.TestCheckResourceAttrSet(
6666
"heroku_addon.foobar", "app_id"),
6767
resource.TestCheckResourceAttr(
68-
"heroku_addon.foobar", "plan", "memcachier"),
68+
"heroku_addon.foobar", "plan", "heroku-postgresql:mini"),
6969
),
7070
},
7171
},
@@ -123,11 +123,11 @@ func TestAccHerokuAddon_CustomName(t *testing.T) {
123123
Config: testAccCheckHerokuAddonConfig_CustomName(appName, customName),
124124
Check: resource.ComposeTestCheckFunc(
125125
testAccCheckHerokuAddonExists("heroku_addon.foobar", &addon),
126-
testAccCheckHerokuAddonPlan(&addon, "memcachier:dev"),
126+
testAccCheckHerokuAddonPlan(&addon, "heroku-postgresql:mini"),
127127
resource.TestCheckResourceAttrSet(
128128
"heroku_addon.foobar", "app_id"),
129129
resource.TestCheckResourceAttr(
130-
"heroku_addon.foobar", "plan", "memcachier"),
130+
"heroku_addon.foobar", "plan", "heroku-postgresql:mini"),
131131
resource.TestCheckResourceAttr(
132132
"heroku_addon.foobar", "name", customName),
133133
),
@@ -357,7 +357,7 @@ resource "heroku_app" "foobar" {
357357
358358
resource "heroku_addon" "foobar" {
359359
app_id = heroku_app.foobar.id
360-
plan = "memcachier"
360+
plan = "heroku-postgresql:mini"
361361
}`, appName)
362362
}
363363

@@ -370,7 +370,7 @@ resource "heroku_app" "foobar" {
370370
371371
resource "heroku_addon" "foobar" {
372372
app_id = heroku_app.foobar.id
373-
plan = "memcachier"
373+
plan = "heroku-postgresql:mini"
374374
name = "%s"
375375
}`, appName, customAddonName)
376376
}

0 commit comments

Comments
 (0)