github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/ovh/resource_ovh_vrack_publiccloud_attachment_test.go (about)

     1  package ovh
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  var testAccVRackPublicCloudAttachmentConfig = fmt.Sprintf(`
    13  resource "ovh_vrack_publiccloud_attachment" "attach" {
    14    vrack_id = "%s"
    15    project_id = "%s"
    16  }
    17  `, os.Getenv("OVH_VRACK"), os.Getenv("OVH_PUBLIC_CLOUD"))
    18  
    19  func TestAccVRackPublicCloudAttachment_basic(t *testing.T) {
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccCheckVRackPublicCloudAttachmentPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckVRackPublicCloudAttachmentDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: testAccVRackPublicCloudAttachmentConfig,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckVRackPublicCloudAttachmentExists("ovh_vrack_publiccloud_attachment.attach", t),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckVRackPublicCloudAttachmentPreCheck(t *testing.T) {
    36  	testAccPreCheck(t)
    37  	testAccCheckVRackExists(t)
    38  	testAccCheckPublicCloudExists(t)
    39  }
    40  
    41  func testAccCheckVRackPublicCloudAttachmentExists(n string, t *testing.T) resource.TestCheckFunc {
    42  	return func(s *terraform.State) error {
    43  		config := testAccProvider.Meta().(*Config)
    44  
    45  		rs, ok := s.RootModule().Resources[n]
    46  		if !ok {
    47  			return fmt.Errorf("Not found: %s", n)
    48  		}
    49  
    50  		if rs.Primary.Attributes["vrack_id"] == "" {
    51  			return fmt.Errorf("No VRack ID is set")
    52  		}
    53  
    54  		if rs.Primary.Attributes["project_id"] == "" {
    55  			return fmt.Errorf("No Project ID is set")
    56  		}
    57  
    58  		return vrackPublicCloudAttachmentExists(rs.Primary.Attributes["vrack_id"], rs.Primary.Attributes["project_id"], config.OVHClient)
    59  	}
    60  }
    61  
    62  func testAccCheckVRackPublicCloudAttachmentDestroy(s *terraform.State) error {
    63  	config := testAccProvider.Meta().(*Config)
    64  	for _, rs := range s.RootModule().Resources {
    65  		if rs.Type != "ovh_vrack_publiccloud_attachment" {
    66  			continue
    67  		}
    68  
    69  		err := vrackPublicCloudAttachmentExists(rs.Primary.Attributes["vrack_id"], rs.Primary.Attributes["project_id"], config.OVHClient)
    70  		if err == nil {
    71  			return fmt.Errorf("VRack > Public Cloud Attachment still exists")
    72  		}
    73  
    74  	}
    75  	return nil
    76  }