github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair_test.go (about)

     1  package cloudstack
     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/xanzy/go-cloudstack/cloudstack"
    11  )
    12  
    13  func TestAccCloudStackSSHKeyPair_basic(t *testing.T) {
    14  	var sshkey cloudstack.SSHKeyPair
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCloudStackSSHKeyPair_create,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey),
    25  					testAccCheckCloudStackSSHKeyPairAttributes(&sshkey),
    26  					testAccCheckCloudStackSSHKeyPairCreateAttributes("terraform-test-keypair"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccCloudStackSSHKeyPair_register(t *testing.T) {
    34  	var sshkey cloudstack.SSHKeyPair
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccCloudStackSSHKeyPair_register,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey),
    45  					testAccCheckCloudStackSSHKeyPairAttributes(&sshkey),
    46  					resource.TestCheckResourceAttr(
    47  						"cloudstack_ssh_keypair.foo",
    48  						"public_key",
    49  						CLOUDSTACK_SSH_PUBLIC_KEY),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func testAccCheckCloudStackSSHKeyPairExists(n string, sshkey *cloudstack.SSHKeyPair) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[n]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", n)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No key pair ID is set")
    65  		}
    66  
    67  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    68  		p := cs.SSH.NewListSSHKeyPairsParams()
    69  		p.SetName(rs.Primary.ID)
    70  
    71  		list, err := cs.SSH.ListSSHKeyPairs(p)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		if list.Count != 1 || list.SSHKeyPairs[0].Name != rs.Primary.ID {
    77  			return fmt.Errorf("Key pair not found")
    78  		}
    79  
    80  		*sshkey = *list.SSHKeyPairs[0]
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  func testAccCheckCloudStackSSHKeyPairAttributes(
    87  	keypair *cloudstack.SSHKeyPair) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  
    90  		fpLen := len(keypair.Fingerprint)
    91  		if fpLen != 47 {
    92  			return fmt.Errorf("SSH key: Attribute private_key expected length 47, got %d", fpLen)
    93  		}
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  func testAccCheckCloudStackSSHKeyPairCreateAttributes(name string) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		found := false
   102  
   103  		for _, rs := range s.RootModule().Resources {
   104  			if rs.Type != "cloudstack_ssh_keypair" {
   105  				continue
   106  			}
   107  
   108  			if rs.Primary.ID != name {
   109  				continue
   110  			}
   111  
   112  			if !strings.Contains(rs.Primary.Attributes["private_key"], "PRIVATE KEY") {
   113  				return fmt.Errorf(
   114  					"SSH key: Attribute private_key expected 'PRIVATE KEY' to be present, got %s",
   115  					rs.Primary.Attributes["private_key"])
   116  			}
   117  
   118  			found = true
   119  			break
   120  		}
   121  
   122  		if !found {
   123  			return fmt.Errorf("Could not find key pair %s", name)
   124  		}
   125  
   126  		return nil
   127  	}
   128  }
   129  
   130  func testAccCheckCloudStackSSHKeyPairDestroy(s *terraform.State) error {
   131  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   132  
   133  	for _, rs := range s.RootModule().Resources {
   134  		if rs.Type != "cloudstack_ssh_keypair" {
   135  			continue
   136  		}
   137  
   138  		if rs.Primary.ID == "" {
   139  			return fmt.Errorf("No key pair ID is set")
   140  		}
   141  
   142  		p := cs.SSH.NewListSSHKeyPairsParams()
   143  		p.SetName(rs.Primary.ID)
   144  
   145  		list, err := cs.SSH.ListSSHKeyPairs(p)
   146  		if err != nil {
   147  			return err
   148  		}
   149  
   150  		for _, keyPair := range list.SSHKeyPairs {
   151  			if keyPair.Name == rs.Primary.ID {
   152  				return fmt.Errorf("Key pair %s still exists", rs.Primary.ID)
   153  			}
   154  		}
   155  	}
   156  
   157  	return nil
   158  }
   159  
   160  var testAccCloudStackSSHKeyPair_create = fmt.Sprintf(`
   161  resource "cloudstack_ssh_keypair" "foo" {
   162    name = "terraform-test-keypair"
   163  }`)
   164  
   165  var testAccCloudStackSSHKeyPair_register = fmt.Sprintf(`
   166  resource "cloudstack_ssh_keypair" "foo" {
   167    name = "terraform-test-keypair"
   168    public_key = "%s"
   169  }`, CLOUDSTACK_SSH_PUBLIC_KEY)