2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2014-08-25 22:56:40 +02:00
|
|
|
"fmt"
|
2014-08-25 20:48:20 +02:00
|
|
|
"testing"
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
"code.google.com/p/google-api-go-client/compute/v1"
|
2014-08-25 20:48:20 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-25 22:56:40 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-08-25 20:48:20 +02:00
|
|
|
)
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
func TestAccComputeAddress_basic(t *testing.T) {
|
2014-08-25 22:56:40 +02:00
|
|
|
var addr compute.Address
|
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2014-08-25 22:56:40 +02:00
|
|
|
CheckDestroy: testAccCheckComputeAddressDestroy,
|
2014-08-25 20:48:20 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2014-08-25 21:55:08 +02:00
|
|
|
Config: testAccComputeAddress_basic,
|
2014-08-25 20:48:20 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2014-08-25 22:56:40 +02:00
|
|
|
testAccCheckComputeAddressExists(
|
|
|
|
"google_compute_address.foobar", &addr),
|
2014-08-25 20:48:20 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
func testAccCheckComputeAddressDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
2014-08-25 20:48:20 +02:00
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
2014-08-25 22:56:40 +02:00
|
|
|
if rs.Type != "google_compute_address" {
|
2014-08-25 20:48:20 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
_, err := config.clientCompute.Addresses.Get(
|
|
|
|
config.Project, config.Region, rs.ID).Do()
|
2014-08-25 20:48:20 +02:00
|
|
|
if err == nil {
|
2014-08-25 22:56:40 +02:00
|
|
|
return fmt.Errorf("Address still exists")
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
func testAccCheckComputeAddressExists(n string, addr *compute.Address) resource.TestCheckFunc {
|
2014-08-25 20:48:20 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.ID == "" {
|
2014-08-25 22:56:40 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
config := testAccProvider.Meta().(*Config)
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
found, err := config.clientCompute.Addresses.Get(
|
|
|
|
config.Project, config.Region, rs.ID).Do()
|
2014-08-25 20:48:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
if found.Name != rs.ID {
|
|
|
|
return fmt.Errorf("Addr not found")
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
|
2014-08-25 22:56:40 +02:00
|
|
|
*addr = *found
|
2014-08-25 20:48:20 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
const testAccComputeAddress_basic = `
|
|
|
|
resource "google_compute_address" "foobar" {
|
|
|
|
name = "terraform-test"
|
2014-08-25 20:48:20 +02:00
|
|
|
}`
|