providers/heroku: heroku_drain

This commit is contained in:
Mitchell Hashimoto 2014-08-19 09:18:45 -07:00
parent 27ee254447
commit 43405c270b
3 changed files with 192 additions and 0 deletions

View File

@ -26,6 +26,7 @@ func Provider() *schema.Provider {
"heroku_app": resourceHerokuApp(), "heroku_app": resourceHerokuApp(),
"heroku_addon": resourceHerokuAddon(), "heroku_addon": resourceHerokuAddon(),
"heroku_domain": resourceHerokuDomain(), "heroku_domain": resourceHerokuDomain(),
"heroku_drain": resourceHerokuDrain(),
}, },
ConfigureFunc: providerConfigure, ConfigureFunc: providerConfigure,

View File

@ -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
}

View File

@ -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"
}`