github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/softlayer/resource_softlayer_ssh_key_test.go (about)

     1  package softlayer
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	datatypes "github.com/maximilien/softlayer-go/data_types"
    12  )
    13  
    14  func TestAccSoftLayerSSHKey_Basic(t *testing.T) {
    15  	var key datatypes.SoftLayer_Security_Ssh_Key
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckSoftLayerSSHKeyDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccCheckSoftLayerSSHKeyConfig_basic,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckSoftLayerSSHKeyExists("softlayer_ssh_key.testacc_foobar", &key),
    26  					testAccCheckSoftLayerSSHKeyAttributes(&key),
    27  					resource.TestCheckResourceAttr(
    28  						"softlayer_ssh_key.testacc_foobar", "name", "testacc_foobar"),
    29  					resource.TestCheckResourceAttr(
    30  						"softlayer_ssh_key.testacc_foobar", "public_key", testAccValidPublicKey),
    31  					resource.TestCheckResourceAttr(
    32  						"softlayer_ssh_key.testacc_foobar", "notes", "first_note"),
    33  				),
    34  			},
    35  
    36  			resource.TestStep{
    37  				Config: testAccCheckSoftLayerSSHKeyConfig_updated,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckSoftLayerSSHKeyExists("softlayer_ssh_key.testacc_foobar", &key),
    40  					resource.TestCheckResourceAttr(
    41  						"softlayer_ssh_key.testacc_foobar", "name", "changed_name"),
    42  					resource.TestCheckResourceAttr(
    43  						"softlayer_ssh_key.testacc_foobar", "public_key", testAccValidPublicKey),
    44  					resource.TestCheckResourceAttr(
    45  						"softlayer_ssh_key.testacc_foobar", "notes", "changed_note"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func testAccCheckSoftLayerSSHKeyDestroy(s *terraform.State) error {
    53  	client := testAccProvider.Meta().(*Client).sshKeyService
    54  
    55  	for _, rs := range s.RootModule().Resources {
    56  		if rs.Type != "softlayer_ssh_key" {
    57  			continue
    58  		}
    59  
    60  		keyId, _ := strconv.Atoi(rs.Primary.ID)
    61  
    62  		// Try to find the key
    63  		_, err := client.GetObject(keyId)
    64  
    65  		if err == nil {
    66  			return fmt.Errorf("SSH key still exists")
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckSoftLayerSSHKeyAttributes(key *datatypes.SoftLayer_Security_Ssh_Key) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  
    76  		if key.Label != "testacc_foobar" {
    77  			return fmt.Errorf("Bad name: %s", key.Label)
    78  		}
    79  
    80  		return nil
    81  	}
    82  }
    83  
    84  func testAccCheckSoftLayerSSHKeyExists(n string, key *datatypes.SoftLayer_Security_Ssh_Key) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", n)
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No Record ID is set")
    94  		}
    95  
    96  		keyId, _ := strconv.Atoi(rs.Primary.ID)
    97  
    98  		client := testAccProvider.Meta().(*Client).sshKeyService
    99  		foundKey, err := client.GetObject(keyId)
   100  
   101  		if err != nil {
   102  			return err
   103  		}
   104  
   105  		if strconv.Itoa(int(foundKey.Id)) != rs.Primary.ID {
   106  			return fmt.Errorf("Record not found")
   107  		}
   108  
   109  		*key = foundKey
   110  
   111  		return nil
   112  	}
   113  }
   114  
   115  var testAccCheckSoftLayerSSHKeyConfig_basic = fmt.Sprintf(`
   116  resource "softlayer_ssh_key" "testacc_foobar" {
   117      name = "testacc_foobar"
   118      notes = "first_note"
   119      public_key = "%s"
   120  }`, testAccValidPublicKey)
   121  
   122  var testAccCheckSoftLayerSSHKeyConfig_updated = fmt.Sprintf(`
   123  resource "softlayer_ssh_key" "testacc_foobar" {
   124      name = "changed_name"
   125      notes = "changed_note"
   126      public_key = "%s"
   127  }`, testAccValidPublicKey)
   128  
   129  var testAccValidPublicKey = strings.TrimSpace(`
   130  ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
   131  `)