github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/packet/resource_packet_ssh_key_test.go (about)

     1  package packet
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     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  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckPacketSSHKeyDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCheckPacketSSHKeyConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckPacketSSHKeyExists("packet_ssh_key.foobar", &key),
    25  					testAccCheckPacketSSHKeyAttributes(&key),
    26  					resource.TestCheckResourceAttr(
    27  						"packet_ssh_key.foobar", "name", "foobar"),
    28  					resource.TestCheckResourceAttr(
    29  						"packet_ssh_key.foobar", "public_key", testAccValidPublicKey),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckPacketSSHKeyDestroy(s *terraform.State) error {
    37  	client := testAccProvider.Meta().(*packngo.Client)
    38  
    39  	for _, rs := range s.RootModule().Resources {
    40  		if rs.Type != "packet_ssh_key" {
    41  			continue
    42  		}
    43  		if _, _, err := client.SSHKeys.Get(rs.Primary.ID); err == nil {
    44  			return fmt.Errorf("SSH key still exists")
    45  		}
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func testAccCheckPacketSSHKeyAttributes(key *packngo.SSHKey) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		if key.Label != "foobar" {
    54  			return fmt.Errorf("Bad name: %s", key.Label)
    55  		}
    56  		return nil
    57  	}
    58  }
    59  
    60  func testAccCheckPacketSSHKeyExists(n string, key *packngo.SSHKey) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  		if rs.Primary.ID == "" {
    67  			return fmt.Errorf("No Record ID is set")
    68  		}
    69  
    70  		client := testAccProvider.Meta().(*packngo.Client)
    71  
    72  		foundKey, _, err := client.SSHKeys.Get(rs.Primary.ID)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		if foundKey.ID != rs.Primary.ID {
    77  			return fmt.Errorf("SSh Key not found: %v - %v", rs.Primary.ID, foundKey)
    78  		}
    79  
    80  		*key = *foundKey
    81  
    82  		fmt.Printf("key: %v", key)
    83  		return nil
    84  	}
    85  }
    86  
    87  var testAccCheckPacketSSHKeyConfig_basic = fmt.Sprintf(`
    88  resource "packet_ssh_key" "foobar" {
    89      name = "foobar"
    90      public_key = "%s"
    91  }`, testAccValidPublicKey)
    92  
    93  var testAccValidPublicKey = strings.TrimSpace(`
    94  ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
    95  `)