Merge pull request #97 from bmarini/add-heroku-drain-resource

Add heroku drain resource
This commit is contained in:
Jack Pearkes 2014-07-29 19:15:17 -04:00
commit d3e8454ceb
3 changed files with 238 additions and 0 deletions

View File

@ -0,0 +1,126 @@
package heroku
import (
"fmt"
"log"
"github.com/bgentry/heroku-go"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform"
)
func resource_heroku_drain_create(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client
// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)
app := rs.Attributes["app"]
url := rs.Attributes["url"]
log.Printf("[DEBUG] Drain create configuration: %#v, %#v", app, url)
dr, err := client.LogDrainCreate(app, url)
if err != nil {
return s, err
}
rs.ID = dr.Id
rs.Attributes["url"] = dr.URL
rs.Attributes["token"] = dr.Token
log.Printf("[INFO] Drain ID: %s", rs.ID)
return rs, nil
}
func resource_heroku_drain_update(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
panic("Cannot update drain")
return nil, nil
}
func resource_heroku_drain_destroy(
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client
log.Printf("[INFO] Deleting drain: %s", s.ID)
// Destroy the app
err := client.LogDrainDelete(s.Attributes["app"], s.ID)
if err != nil {
return fmt.Errorf("Error deleting drain: %s", err)
}
return nil
}
func resource_heroku_drain_refresh(
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client
drain, err := resource_heroku_drain_retrieve(s.Attributes["app"], s.ID, client)
if err != nil {
return nil, err
}
s.Attributes["url"] = drain.URL
s.Attributes["token"] = drain.Token
return s, nil
}
func resource_heroku_drain_diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {
b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"url": diff.AttrTypeCreate,
"app": diff.AttrTypeCreate,
},
ComputedAttrs: []string{
"token",
},
}
return b.Diff(s, c)
}
func resource_heroku_drain_retrieve(app string, id string, client *heroku.Client) (*heroku.LogDrain, error) {
drain, err := client.LogDrainInfo(app, id)
if err != nil {
return nil, fmt.Errorf("Error retrieving drain: %s", err)
}
return drain, nil
}
func resource_heroku_drain_validation() *config.Validator {
return &config.Validator{
Required: []string{
"url",
"app",
},
Optional: []string{},
}
}

View File

@ -0,0 +1,104 @@
package heroku
import (
"fmt"
"testing"
"github.com/bgentry/heroku-go"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccHerokuDrain_Basic(t *testing.T) {
var drain heroku.LogDrain
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHerokuDrainDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckHerokuDrainConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckHerokuDrainExists("heroku_drain.foobar", &drain),
testAccCheckHerokuDrainAttributes(&drain),
resource.TestCheckResourceAttr(
"heroku_drain.foobar", "url", "syslog://terraform.example.com:1234"),
resource.TestCheckResourceAttr(
"heroku_drain.foobar", "app", "terraform-test-app"),
resource.TestCheckResourceAttr(
"heroku_drain.foobar", "token", "foo-bar-baz-qux"),
),
},
},
})
}
func testAccCheckHerokuDrainDestroy(s *terraform.State) error {
client := testAccProvider.client
for _, rs := range s.Resources {
if rs.Type != "heroku_drain" {
continue
}
_, err := client.LogDrainInfo(rs.Attributes["app"], rs.ID)
if err == nil {
return fmt.Errorf("Drain still exists")
}
}
return nil
}
func testAccCheckHerokuDrainAttributes(Drain *heroku.LogDrain) resource.TestCheckFunc {
return func(s *terraform.State) error {
if Drain.URL != "syslog://terraform.example.com:1234" {
return fmt.Errorf("Bad URL: %s", Drain.URL)
}
return nil
}
}
func testAccCheckHerokuDrainExists(n string, Drain *heroku.LogDrain) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No Drain ID is set")
}
client := testAccProvider.client
foundDrain, err := client.LogDrainInfo(rs.Attributes["app"], rs.ID)
if err != nil {
return err
}
if foundDrain.Id != rs.ID {
return fmt.Errorf("Drain not found")
}
*Drain = *foundDrain
return nil
}
}
const testAccCheckHerokuDrainConfig_basic = `
resource "heroku_app" "foobar" {
name = "terraform-test-app"
}
resource "heroku_drain" "foobar" {
app = "${heroku_app.foobar.name}"
url = "syslog://terraform.example.com:1234"
}`

View File

@ -36,6 +36,14 @@ func init() {
Diff: resource_heroku_domain_diff, Diff: resource_heroku_domain_diff,
Refresh: resource_heroku_domain_refresh, Refresh: resource_heroku_domain_refresh,
}, },
"heroku_drain": resource.Resource{
ConfigValidator: resource_heroku_drain_validation(),
Create: resource_heroku_drain_create,
Destroy: resource_heroku_drain_destroy,
Diff: resource_heroku_drain_diff,
Refresh: resource_heroku_drain_refresh,
},
}, },
} }
} }