github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  
    41  		_, _, err := client.Projects.Get(rs.Primary.ID)
    42  
    43  		if err == nil {
    44  			fmt.Errorf("Project cstill exists")
    45  		}
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func testAccCheckPacketProjectAttributes(project *packngo.Project) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  
    54  		if project.Name != "foobar" {
    55  			return fmt.Errorf("Bad name: %s", project.Name)
    56  		}
    57  
    58  		return nil
    59  	}
    60  }
    61  
    62  func testAccCheckPacketProjectExists(n string, project *packngo.Project) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		rs, ok := s.RootModule().Resources[n]
    65  
    66  		if !ok {
    67  			return fmt.Errorf("Not found: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No Record ID is set")
    72  		}
    73  
    74  		client := testAccProvider.Meta().(*packngo.Client)
    75  
    76  		foundProject, _, err := client.Projects.Get(rs.Primary.ID)
    77  
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		if foundProject.ID != rs.Primary.ID {
    83  			return fmt.Errorf("Record not found: %v - %v", rs.Primary.ID, foundProject)
    84  		}
    85  
    86  		*project = *foundProject
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  var testAccCheckPacketProjectConfig_basic = fmt.Sprintf(`
    93  resource "packet_project" "foobar" {
    94      name = "foobar"
    95  }`)