github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/softlayer/resource_softlayer_virtual_guest_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 TestAccSoftLayerVirtualGuest_Basic(t *testing.T) {
    15  	var guest datatypes.SoftLayer_Virtual_Guest
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckSoftLayerVirtualGuestDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config:  testAccCheckSoftLayerVirtualGuestConfig_basic,
    24  				Destroy: false,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-1", &guest),
    27  					resource.TestCheckResourceAttr(
    28  						"softlayer_virtual_guest.terraform-acceptance-test-1", "name", "terraform-test"),
    29  					resource.TestCheckResourceAttr(
    30  						"softlayer_virtual_guest.terraform-acceptance-test-1", "domain", "bar.example.com"),
    31  					resource.TestCheckResourceAttr(
    32  						"softlayer_virtual_guest.terraform-acceptance-test-1", "region", "ams01"),
    33  					resource.TestCheckResourceAttr(
    34  						"softlayer_virtual_guest.terraform-acceptance-test-1", "public_network_speed", "10"),
    35  					resource.TestCheckResourceAttr(
    36  						"softlayer_virtual_guest.terraform-acceptance-test-1", "hourly_billing", "true"),
    37  					resource.TestCheckResourceAttr(
    38  						"softlayer_virtual_guest.terraform-acceptance-test-1", "private_network_only", "false"),
    39  					resource.TestCheckResourceAttr(
    40  						"softlayer_virtual_guest.terraform-acceptance-test-1", "cpu", "1"),
    41  					resource.TestCheckResourceAttr(
    42  						"softlayer_virtual_guest.terraform-acceptance-test-1", "ram", "1024"),
    43  					resource.TestCheckResourceAttr(
    44  						"softlayer_virtual_guest.terraform-acceptance-test-1", "disks.0", "25"),
    45  					resource.TestCheckResourceAttr(
    46  						"softlayer_virtual_guest.terraform-acceptance-test-1", "disks.1", "10"),
    47  					resource.TestCheckResourceAttr(
    48  						"softlayer_virtual_guest.terraform-acceptance-test-1", "disks.2", "20"),
    49  					resource.TestCheckResourceAttr(
    50  						"softlayer_virtual_guest.terraform-acceptance-test-1", "user_data", "{\"value\":\"newvalue\"}"),
    51  					resource.TestCheckResourceAttr(
    52  						"softlayer_virtual_guest.terraform-acceptance-test-1", "local_disk", "false"),
    53  					resource.TestCheckResourceAttr(
    54  						"softlayer_virtual_guest.terraform-acceptance-test-1", "dedicated_acct_host_only", "true"),
    55  
    56  					// TODO: As agreed, will be enabled when VLAN support is implemented: https://github.com/TheWeatherCompany/softlayer-go/issues/3
    57  					//					resource.TestCheckResourceAttr(
    58  					//						"softlayer_virtual_guest.terraform-acceptance-test-1", "frontend_vlan_id", "1085155"),
    59  					//					resource.TestCheckResourceAttr(
    60  					//						"softlayer_virtual_guest.terraform-acceptance-test-1", "backend_vlan_id", "1085157"),
    61  				),
    62  			},
    63  
    64  			resource.TestStep{
    65  				Config:  testAccCheckSoftLayerVirtualGuestConfig_userDataUpdate,
    66  				Destroy: false,
    67  				Check: resource.ComposeTestCheckFunc(
    68  					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-1", &guest),
    69  					resource.TestCheckResourceAttr(
    70  						"softlayer_virtual_guest.terraform-acceptance-test-1", "user_data", "updatedData"),
    71  				),
    72  			},
    73  
    74  			resource.TestStep{
    75  				Config: testAccCheckSoftLayerVirtualGuestConfig_upgradeMemoryNetworkSpeed,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-1", &guest),
    78  					resource.TestCheckResourceAttr(
    79  						"softlayer_virtual_guest.terraform-acceptance-test-1", "ram", "2048"),
    80  					resource.TestCheckResourceAttr(
    81  						"softlayer_virtual_guest.terraform-acceptance-test-1", "public_network_speed", "100"),
    82  				),
    83  			},
    84  
    85  			// TODO: currently CPU upgrade test is disabled, due to unexpected behavior of field "dedicated_acct_host_only".
    86  			// TODO: For some reason it is reset by SoftLayer to "false". Daniel Bright reported corresponding issue to SoftLayer team.
    87  			//			resource.TestStep{
    88  			//				Config: testAccCheckSoftLayerVirtualGuestConfig_vmUpgradeCPUs,
    89  			//				Check: resource.ComposeTestCheckFunc(
    90  			//					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-1", &guest),
    91  			//					resource.TestCheckResourceAttr(
    92  			//						"softlayer_virtual_guest.terraform-acceptance-test-1", "cpu", "2"),
    93  			//				),
    94  			//			},
    95  
    96  		},
    97  	})
    98  }
    99  
   100  func TestAccSoftLayerVirtualGuest_BlockDeviceTemplateGroup(t *testing.T) {
   101  	var guest datatypes.SoftLayer_Virtual_Guest
   102  
   103  	resource.Test(t, resource.TestCase{
   104  		PreCheck:     func() { testAccPreCheck(t) },
   105  		Providers:    testAccProviders,
   106  		CheckDestroy: testAccCheckSoftLayerVirtualGuestDestroy,
   107  		Steps: []resource.TestStep{
   108  			resource.TestStep{
   109  				Config: testAccCheckSoftLayerVirtualGuestConfig_blockDeviceTemplateGroup,
   110  				Check: resource.ComposeTestCheckFunc(
   111  					// block_device_template_group_gid value is hardcoded. If it's valid then virtual guest will be created well
   112  					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-BDTGroup", &guest),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func TestAccSoftLayerVirtualGuest_postInstallScriptUri(t *testing.T) {
   120  	var guest datatypes.SoftLayer_Virtual_Guest
   121  
   122  	resource.Test(t, resource.TestCase{
   123  		PreCheck:     func() { testAccPreCheck(t) },
   124  		Providers:    testAccProviders,
   125  		CheckDestroy: testAccCheckSoftLayerVirtualGuestDestroy,
   126  		Steps: []resource.TestStep{
   127  			resource.TestStep{
   128  				Config: testAccCheckSoftLayerVirtualGuestConfig_postInstallScriptUri,
   129  				Check: resource.ComposeTestCheckFunc(
   130  					// block_device_template_group_gid value is hardcoded. If it's valid then virtual guest will be created well
   131  					testAccCheckSoftLayerVirtualGuestExists("softlayer_virtual_guest.terraform-acceptance-test-pISU", &guest),
   132  				),
   133  			},
   134  		},
   135  	})
   136  }
   137  
   138  func testAccCheckSoftLayerVirtualGuestDestroy(s *terraform.State) error {
   139  	client := testAccProvider.Meta().(*Client).virtualGuestService
   140  
   141  	for _, rs := range s.RootModule().Resources {
   142  		if rs.Type != "softlayer_virtual_guest" {
   143  			continue
   144  		}
   145  
   146  		guestId, _ := strconv.Atoi(rs.Primary.ID)
   147  
   148  		// Try to find the guest
   149  		_, err := client.GetObject(guestId)
   150  
   151  		// Wait
   152  
   153  		if err != nil && !strings.Contains(err.Error(), "404") {
   154  			return fmt.Errorf(
   155  				"Error waiting for virtual guest (%s) to be destroyed: %s",
   156  				rs.Primary.ID, err)
   157  		}
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func testAccCheckSoftLayerVirtualGuestExists(n string, guest *datatypes.SoftLayer_Virtual_Guest) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  		rs, ok := s.RootModule().Resources[n]
   166  		if !ok {
   167  			return fmt.Errorf("Not found: %s", n)
   168  		}
   169  
   170  		if rs.Primary.ID == "" {
   171  			return fmt.Errorf("No virtual guest ID is set")
   172  		}
   173  
   174  		id, err := strconv.Atoi(rs.Primary.ID)
   175  
   176  		if err != nil {
   177  			return err
   178  		}
   179  
   180  		client := testAccProvider.Meta().(*Client).virtualGuestService
   181  		retrieveVirtGuest, err := client.GetObject(id)
   182  
   183  		if err != nil {
   184  			return err
   185  		}
   186  
   187  		fmt.Printf("The ID is %d", id)
   188  
   189  		if retrieveVirtGuest.Id != id {
   190  			return fmt.Errorf("Virtual guest not found")
   191  		}
   192  
   193  		*guest = retrieveVirtGuest
   194  
   195  		return nil
   196  	}
   197  }
   198  
   199  const testAccCheckSoftLayerVirtualGuestConfig_basic = `
   200  resource "softlayer_virtual_guest" "terraform-acceptance-test-1" {
   201      name = "terraform-test"
   202      domain = "bar.example.com"
   203      image = "DEBIAN_7_64"
   204      region = "ams01"
   205      public_network_speed = 10
   206      hourly_billing = true
   207  	private_network_only = false
   208      cpu = 1
   209      ram = 1024
   210      disks = [25, 10, 20]
   211      user_data = "{\"value\":\"newvalue\"}"
   212      dedicated_acct_host_only = true
   213      local_disk = false
   214  }
   215  `
   216  
   217  const testAccCheckSoftLayerVirtualGuestConfig_userDataUpdate = `
   218  resource "softlayer_virtual_guest" "terraform-acceptance-test-1" {
   219      name = "terraform-test"
   220      domain = "bar.example.com"
   221      image = "DEBIAN_7_64"
   222      region = "ams01"
   223      public_network_speed = 10
   224      hourly_billing = true
   225      cpu = 1
   226      ram = 1024
   227      disks = [25, 10, 20]
   228      user_data = "updatedData"
   229      dedicated_acct_host_only = true
   230      local_disk = false
   231  }
   232  `
   233  
   234  const testAccCheckSoftLayerVirtualGuestConfig_upgradeMemoryNetworkSpeed = `
   235  resource "softlayer_virtual_guest" "terraform-acceptance-test-1" {
   236      name = "terraform-test"
   237      domain = "bar.example.com"
   238      image = "DEBIAN_7_64"
   239      region = "ams01"
   240      public_network_speed = 100
   241      hourly_billing = true
   242      cpu = 1
   243      ram = 2048
   244      disks = [25, 10, 20]
   245      user_data = "updatedData"
   246      dedicated_acct_host_only = true
   247      local_disk = false
   248  }
   249  `
   250  
   251  const testAccCheckSoftLayerVirtualGuestConfig_vmUpgradeCPUs = `
   252  resource "softlayer_virtual_guest" "terraform-acceptance-test-1" {
   253      name = "terraform-test"
   254      domain = "bar.example.com"
   255      image = "DEBIAN_7_64"
   256      region = "ams01"
   257      public_network_speed = 100
   258      hourly_billing = true
   259      cpu = 2
   260      ram = 2048
   261      disks = [25, 10, 20]
   262      user_data = "updatedData"
   263      dedicated_acct_host_only = true
   264      local_disk = false
   265  }
   266  `
   267  
   268  const testAccCheckSoftLayerVirtualGuestConfig_postInstallScriptUri = `
   269  resource "softlayer_virtual_guest" "terraform-acceptance-test-pISU" {
   270      name = "terraform-test-pISU"
   271      domain = "bar.example.com"
   272      image = "DEBIAN_7_64"
   273      region = "ams01"
   274      public_network_speed = 10
   275      hourly_billing = true
   276  	private_network_only = false
   277      cpu = 1
   278      ram = 1024
   279      disks = [25, 10, 20]
   280      user_data = "{\"value\":\"newvalue\"}"
   281      dedicated_acct_host_only = true
   282      local_disk = false
   283      post_install_script_uri = "https://www.google.com"
   284  }
   285  `
   286  
   287  const testAccCheckSoftLayerVirtualGuestConfig_blockDeviceTemplateGroup = `
   288  resource "softlayer_virtual_guest" "terraform-acceptance-test-BDTGroup" {
   289      name = "terraform-test-blockDeviceTemplateGroup"
   290      domain = "bar.example.com"
   291      region = "ams01"
   292      public_network_speed = 10
   293      hourly_billing = false
   294      cpu = 1
   295      ram = 1024
   296      local_disk = false
   297      block_device_template_group_gid = "ac2b413c-9893-4178-8e62-a24cbe2864db"
   298  }
   299  `