github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/packet/resource_packet_project_test.go (about)

     1  package packet
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/packethost/packngo"
    10  )
    11  
    12  func TestAccPacketProject_Basic(t *testing.T) {
    13  	var project packngo.Project
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckPacketProjectDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCheckPacketProjectConfig_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckPacketProjectExists("packet_project.foobar", &project),
    24  					testAccCheckPacketProjectAttributes(&project),
    25  					resource.TestCheckResourceAttr(
    26  						"packet_project.foobar", "name", "foobar"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckPacketProjectDestroy(s *terraform.State) error {
    34  	client := testAccProvider.Meta().(*packngo.Client)
    35  
    36  	for _, rs := range s.RootModule().Resources {
    37  		if rs.Type != "packet_project" {
    38  			continue
    39  		}
    40  		if _, _, err := client.Projects.Get(rs.Primary.ID); err == nil {
    41  			return fmt.Errorf("Project still exists")
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func testAccCheckPacketProjectAttributes(project *packngo.Project) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		if project.Name != "foobar" {
    51  			return fmt.Errorf("Bad name: %s", project.Name)
    52  		}
    53  		return nil
    54  	}
    55  }
    56  
    57  func testAccCheckPacketProjectExists(n string, project *packngo.Project) resource.TestCheckFunc {
    58  	return func(s *terraform.State) error {
    59  		rs, ok := s.RootModule().Resources[n]
    60  		if !ok {
    61  			return fmt.Errorf("Not found: %s", n)
    62  		}
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No Record ID is set")
    65  		}
    66  
    67  		client := testAccProvider.Meta().(*packngo.Client)
    68  
    69  		foundProject, _, err := client.Projects.Get(rs.Primary.ID)
    70  		if err != nil {
    71  			return err
    72  		}
    73  		if foundProject.ID != rs.Primary.ID {
    74  			return fmt.Errorf("Record not found: %v - %v", rs.Primary.ID, foundProject)
    75  		}
    76  
    77  		*project = *foundProject
    78  
    79  		return nil
    80  	}
    81  }
    82  
    83  var testAccCheckPacketProjectConfig_basic = fmt.Sprintf(`
    84  resource "packet_project" "foobar" {
    85      name = "foobar"
    86  }`)