diff --git a/builtin/providers/heroku/provider.go b/builtin/providers/heroku/provider.go index 7fe0e2dad..f1cf6085c 100644 --- a/builtin/providers/heroku/provider.go +++ b/builtin/providers/heroku/provider.go @@ -26,6 +26,7 @@ func Provider() *schema.Provider { "heroku_app": resourceHerokuApp(), "heroku_addon": resourceHerokuAddon(), "heroku_domain": resourceHerokuDomain(), + "heroku_drain": resourceHerokuDrain(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/heroku/resource_heroku_drain.go b/builtin/providers/heroku/resource_heroku_drain.go new file mode 100644 index 000000000..a02e423cd --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_drain.go @@ -0,0 +1,85 @@ +package heroku + +import ( + "fmt" + "log" + + "github.com/bgentry/heroku-go" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceHerokuDrain() *schema.Resource { + return &schema.Resource{ + Create: resourceHerokuDrainCreate, + Read: resourceHerokuDrainRead, + Delete: resourceHerokuDrainDelete, + + Schema: map[string]*schema.Schema{ + "url": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "app": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "token": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceHerokuDrainCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*heroku.Client) + + app := d.Get("app").(string) + url := d.Get("url").(string) + + log.Printf("[DEBUG] Drain create configuration: %#v, %#v", app, url) + + dr, err := client.LogDrainCreate(app, url) + if err != nil { + return err + } + + d.SetId(dr.Id) + d.Set("url", dr.URL) + d.Set("token", dr.Token) + + log.Printf("[INFO] Drain ID: %s", d.Id()) + return nil +} + +func resourceHerokuDrainDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*heroku.Client) + + log.Printf("[INFO] Deleting drain: %s", d.Id()) + + // Destroy the drain + err := client.LogDrainDelete(d.Get("app").(string), d.Id()) + if err != nil { + return fmt.Errorf("Error deleting drain: %s", err) + } + + return nil +} + +func resourceHerokuDrainRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*heroku.Client) + + dr, err := client.LogDrainInfo(d.Get("app").(string), d.Id()) + if err != nil { + return fmt.Errorf("Error retrieving drain: %s", err) + } + + d.Set("url", dr.URL) + d.Set("token", dr.Token) + + return nil +} diff --git a/builtin/providers/heroku/resource_heroku_drain_test.go b/builtin/providers/heroku/resource_heroku_drain_test.go new file mode 100644 index 000000000..beaf83625 --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_drain_test.go @@ -0,0 +1,106 @@ +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"), + ), + }, + }, + }) +} + +func testAccCheckHerokuDrainDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*heroku.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) + } + + if Drain.Token == "" { + return fmt.Errorf("No token: %#v", Drain) + } + + 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.Meta().(*heroku.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" +}`