github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/packet/resource_packet_ssh_key_test.go (about)

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