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

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccOPCSSHKey_basic(t *testing.T) {
    14  	ruleResourceName := "opc_compute_ssh_key.test"
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccOPCSSHKeyBasic, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccOPCCheckSSHKeyDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccOPCCheckSSHKeyExists,
    27  					resource.TestCheckResourceAttr(ruleResourceName, "key", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Wa2OClh4LDCpR4A1x251PfzeUHvA3uo3Z4joYKIlQXP6242588bq6eh79ihm+HZAuxNoIkkS4OMIelUtiHcYSMYK7niXpato3cUdQHXjwchZjc3wwcXC/hAWK2QJkO7yLgCuYMTqyz2saZ/9zW12QS24rJH1DKFDbq4V40+HF7PQoq6G40Dp0X+slZri223pHJiqHKlyhUZuvMar7QnLZlZ7jenPyqVSpY7IC5KPj6geQSD2tSnVKjRo4TWVkIexSo6iHEu5vzcjVYGBw9RVGhmOd8pCcbB85M01MJFdbqLMjUHREE7/t767hmem3YdSPhMvnbBNPb7VSB+8ZQKn"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccOPCSSHKey_update(t *testing.T) {
    35  	ruleResourceName := "opc_compute_ssh_key.test"
    36  	ri := acctest.RandInt()
    37  	config := fmt.Sprintf(testAccOPCSSHKeyBasic, ri)
    38  	updatedConfig := fmt.Sprintf(testAccOPCSSHKeyUpdated, ri)
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccOPCCheckSSHKeyDestroy,
    44  		Steps: []resource.TestStep{
    45  			{
    46  				Config: config,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccOPCCheckSSHKeyExists,
    49  					resource.TestCheckResourceAttr(ruleResourceName, "key", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Wa2OClh4LDCpR4A1x251PfzeUHvA3uo3Z4joYKIlQXP6242588bq6eh79ihm+HZAuxNoIkkS4OMIelUtiHcYSMYK7niXpato3cUdQHXjwchZjc3wwcXC/hAWK2QJkO7yLgCuYMTqyz2saZ/9zW12QS24rJH1DKFDbq4V40+HF7PQoq6G40Dp0X+slZri223pHJiqHKlyhUZuvMar7QnLZlZ7jenPyqVSpY7IC5KPj6geQSD2tSnVKjRo4TWVkIexSo6iHEu5vzcjVYGBw9RVGhmOd8pCcbB85M01MJFdbqLMjUHREE7/t767hmem3YdSPhMvnbBNPb7VSB+8ZQKn"),
    50  				),
    51  			},
    52  			{
    53  				Config: updatedConfig,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccOPCCheckSSHKeyExists,
    56  					resource.TestCheckResourceAttr(ruleResourceName, "key", "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeXD/4cetIxK3a/mNbE8F0oYFOicK07Am6YyS0tV4Etak29fB2FoRwGAMETN0w7kKa8nKyvjZBH2mTkdAELoSbB70yZLNSufK7GMyLQXRG8c51xFDhTjLXZ92zSN6ZZBrnc7Z7iXCHsfAyXcrTmv9jgm3nE0QF1/AJgHXNa6GqzsyjilKkRjQBhUTqkTQylyVytPJHgM5W/v2vStfFK5wY9h9oDiHJiNACPOxE8v9A+u9MnKaq+E6AuarA0VQJbPWqVzoHWMUXL0ck+WYfZyX17VPB6c18h4Wn27lNxCEE7jaMLIVbMpAW5ICW1UVnrT6/ZoSTseJjEBlukPlZVQu7"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccOPCSSHKey_disable(t *testing.T) {
    64  	ruleResourceName := "opc_compute_ssh_key.test"
    65  	ri := acctest.RandInt()
    66  	config := fmt.Sprintf(testAccOPCSSHKeyBasic, ri)
    67  	updatedConfig := fmt.Sprintf(testAccOPCSSHKeyDisabled, ri)
    68  
    69  	resource.Test(t, resource.TestCase{
    70  		PreCheck:     func() { testAccPreCheck(t) },
    71  		Providers:    testAccProviders,
    72  		CheckDestroy: testAccOPCCheckSSHKeyDestroy,
    73  		Steps: []resource.TestStep{
    74  			{
    75  				Config: config,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccOPCCheckSSHKeyExists,
    78  					resource.TestCheckResourceAttr(ruleResourceName, "enabled", "true"),
    79  				),
    80  			},
    81  			{
    82  				Config: updatedConfig,
    83  				Check: resource.ComposeTestCheckFunc(
    84  					testAccOPCCheckSSHKeyExists,
    85  					resource.TestCheckResourceAttr(ruleResourceName, "enabled", "false"),
    86  				),
    87  			},
    88  		},
    89  	})
    90  }
    91  
    92  func testAccOPCCheckSSHKeyExists(s *terraform.State) error {
    93  	client := testAccProvider.Meta().(*compute.Client).SSHKeys()
    94  
    95  	for _, rs := range s.RootModule().Resources {
    96  		if rs.Type != "opc_compute_ssh_key" {
    97  			continue
    98  		}
    99  
   100  		input := compute.GetSSHKeyInput{
   101  			Name: rs.Primary.Attributes["name"],
   102  		}
   103  		if _, err := client.GetSSHKey(&input); err != nil {
   104  			return fmt.Errorf("Error retrieving state of SSH Key %s: %s", input.Name, err)
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func testAccOPCCheckSSHKeyDestroy(s *terraform.State) error {
   112  	client := testAccProvider.Meta().(*compute.Client).SSHKeys()
   113  
   114  	for _, rs := range s.RootModule().Resources {
   115  		if rs.Type != "opc_compute_ssh_key" {
   116  			continue
   117  		}
   118  
   119  		input := compute.GetSSHKeyInput{
   120  			Name: rs.Primary.Attributes["name"],
   121  		}
   122  		if info, err := client.GetSSHKey(&input); err == nil {
   123  			return fmt.Errorf("SSH Key %s still exists: %#v", input.Name, info)
   124  		}
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  const testAccOPCSSHKeyBasic = `
   131  resource "opc_compute_ssh_key" "test" {
   132  	name    = "acc-ssh-key-%d"
   133  	key     = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Wa2OClh4LDCpR4A1x251PfzeUHvA3uo3Z4joYKIlQXP6242588bq6eh79ihm+HZAuxNoIkkS4OMIelUtiHcYSMYK7niXpato3cUdQHXjwchZjc3wwcXC/hAWK2QJkO7yLgCuYMTqyz2saZ/9zW12QS24rJH1DKFDbq4V40+HF7PQoq6G40Dp0X+slZri223pHJiqHKlyhUZuvMar7QnLZlZ7jenPyqVSpY7IC5KPj6geQSD2tSnVKjRo4TWVkIexSo6iHEu5vzcjVYGBw9RVGhmOd8pCcbB85M01MJFdbqLMjUHREE7/t767hmem3YdSPhMvnbBNPb7VSB+8ZQKn"
   134  	enabled = true
   135  }
   136  `
   137  
   138  const testAccOPCSSHKeyUpdated = `
   139  resource "opc_compute_ssh_key" "test" {
   140  	name    = "acc-ssh-key-%d"
   141  	key     = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeXD/4cetIxK3a/mNbE8F0oYFOicK07Am6YyS0tV4Etak29fB2FoRwGAMETN0w7kKa8nKyvjZBH2mTkdAELoSbB70yZLNSufK7GMyLQXRG8c51xFDhTjLXZ92zSN6ZZBrnc7Z7iXCHsfAyXcrTmv9jgm3nE0QF1/AJgHXNa6GqzsyjilKkRjQBhUTqkTQylyVytPJHgM5W/v2vStfFK5wY9h9oDiHJiNACPOxE8v9A+u9MnKaq+E6AuarA0VQJbPWqVzoHWMUXL0ck+WYfZyX17VPB6c18h4Wn27lNxCEE7jaMLIVbMpAW5ICW1UVnrT6/ZoSTseJjEBlukPlZVQu7"
   142  	enabled = true
   143  }
   144  `
   145  
   146  const testAccOPCSSHKeyDisabled = `
   147  resource "opc_compute_ssh_key" "test" {
   148  	name    = "acc-ssh-key-%d"
   149  	key     = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Wa2OClh4LDCpR4A1x251PfzeUHvA3uo3Z4joYKIlQXP6242588bq6eh79ihm+HZAuxNoIkkS4OMIelUtiHcYSMYK7niXpato3cUdQHXjwchZjc3wwcXC/hAWK2QJkO7yLgCuYMTqyz2saZ/9zW12QS24rJH1DKFDbq4V40+HF7PQoq6G40Dp0X+slZri223pHJiqHKlyhUZuvMar7QnLZlZ7jenPyqVSpY7IC5KPj6geQSD2tSnVKjRo4TWVkIexSo6iHEu5vzcjVYGBw9RVGhmOd8pCcbB85M01MJFdbqLMjUHREE7/t767hmem3YdSPhMvnbBNPb7VSB+8ZQKn"
   150  	enabled = false
   151  }
   152  `