From 6dd629a334ad3e32f4ea27d896ef80e30a49d24b Mon Sep 17 00:00:00 2001 From: Ben Marini Date: Tue, 29 Jul 2014 15:45:12 -0700 Subject: [PATCH] Add heroku drain resource --- .../providers/heroku/resource_heroku_drain.go | 126 ++++++++++++++++++ .../heroku/resource_heroku_drain_test.go | 104 +++++++++++++++ builtin/providers/heroku/resources.go | 8 ++ 3 files changed, 238 insertions(+) create mode 100644 builtin/providers/heroku/resource_heroku_drain.go create mode 100644 builtin/providers/heroku/resource_heroku_drain_test.go diff --git a/builtin/providers/heroku/resource_heroku_drain.go b/builtin/providers/heroku/resource_heroku_drain.go new file mode 100644 index 000000000..908125eea --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_drain.go @@ -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{}, + } +} 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..7ff5d3a37 --- /dev/null +++ b/builtin/providers/heroku/resource_heroku_drain_test.go @@ -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" +}` diff --git a/builtin/providers/heroku/resources.go b/builtin/providers/heroku/resources.go index a208714c6..4c47632f8 100644 --- a/builtin/providers/heroku/resources.go +++ b/builtin/providers/heroku/resources.go @@ -36,6 +36,14 @@ func init() { Diff: resource_heroku_domain_diff, 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, + }, }, } }