github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_server_test.go (about) 1 package profitbricks 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/profitbricks/profitbricks-sdk-go" 10 ) 11 12 func TestAccProfitBricksServer_Basic(t *testing.T) { 13 var server profitbricks.Server 14 serverName := "webserver" 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDProfitBricksServerDestroyCheck, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckProfitbricksServerConfig_basic, serverName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckProfitBricksServerExists("profitbricks_server.webserver", &server), 27 testAccCheckProfitBricksServerAttributes("profitbricks_server.webserver", serverName), 28 resource.TestCheckResourceAttr("profitbricks_server.webserver", "name", serverName), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckProfitbricksServerConfig_update, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckProfitBricksServerAttributes("profitbricks_server.webserver", "updated"), 35 resource.TestCheckResourceAttr("profitbricks_server.webserver", "name", "updated"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckDProfitBricksServerDestroyCheck(s *terraform.State) error { 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "profitbricks_datacenter" { 45 continue 46 } 47 48 resp := profitbricks.GetServer(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 49 50 if resp.StatusCode < 299 { 51 return fmt.Errorf("Server still exists %s %s", rs.Primary.ID, resp.Response) 52 } 53 } 54 55 return nil 56 } 57 58 func testAccCheckProfitBricksServerAttributes(n string, name string) resource.TestCheckFunc { 59 return func(s *terraform.State) error { 60 rs, ok := s.RootModule().Resources[n] 61 if !ok { 62 return fmt.Errorf("testAccCheckProfitBricksServerAttributes: Not found: %s", n) 63 } 64 if rs.Primary.Attributes["name"] != name { 65 return fmt.Errorf("Bad name: %s", rs.Primary.Attributes["name"]) 66 } 67 68 return nil 69 } 70 } 71 72 func testAccCheckProfitBricksServerExists(n string, server *profitbricks.Server) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 76 if !ok { 77 return fmt.Errorf("testAccCheckProfitBricksServerExists: Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No Record ID is set") 82 } 83 84 foundServer := profitbricks.GetServer(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 85 86 if foundServer.StatusCode != 200 { 87 return fmt.Errorf("Error occured while fetching Server: %s", rs.Primary.ID) 88 } 89 if foundServer.Id != rs.Primary.ID { 90 return fmt.Errorf("Record not found") 91 } 92 93 server = &foundServer 94 95 return nil 96 } 97 } 98 99 const testAccCheckProfitbricksServerConfig_basic = ` 100 resource "profitbricks_datacenter" "foobar" { 101 name = "server-test" 102 location = "us/las" 103 } 104 105 resource "profitbricks_lan" "webserver_lan" { 106 datacenter_id = "${profitbricks_datacenter.foobar.id}" 107 public = true 108 name = "public" 109 } 110 111 resource "profitbricks_server" "webserver" { 112 name = "%s" 113 datacenter_id = "${profitbricks_datacenter.foobar.id}" 114 cores = 1 115 ram = 1024 116 availability_zone = "ZONE_1" 117 cpu_family = "AMD_OPTERON" 118 volume { 119 name = "system" 120 size = 5 121 disk_type = "SSD" 122 image_name ="ubuntu-16.04" 123 image_password = "K3tTj8G14a3EgKyNeeiY" 124 } 125 nic { 126 lan = "${profitbricks_lan.webserver_lan.id}" 127 dhcp = true 128 firewall_active = true 129 firewall { 130 protocol = "TCP" 131 name = "SSH" 132 port_range_start = 22 133 port_range_end = 22 134 } 135 } 136 }` 137 138 const testAccCheckProfitbricksServerConfig_update = ` 139 resource "profitbricks_datacenter" "foobar" { 140 name = "server-test" 141 location = "us/las" 142 } 143 144 resource "profitbricks_lan" "webserver_lan" { 145 datacenter_id = "${profitbricks_datacenter.foobar.id}" 146 public = true 147 name = "public" 148 } 149 150 resource "profitbricks_server" "webserver" { 151 name = "updated" 152 datacenter_id = "${profitbricks_datacenter.foobar.id}" 153 cores = 1 154 ram = 1024 155 availability_zone = "ZONE_1" 156 cpu_family = "AMD_OPTERON" 157 volume { 158 name = "system" 159 size = 5 160 disk_type = "SSD" 161 image_name ="ubuntu-16.04" 162 image_password = "K3tTj8G14a3EgKyNeeiY" 163 } 164 nic { 165 lan = "${profitbricks_lan.webserver_lan.id}" 166 dhcp = true 167 firewall_active = true 168 firewall { 169 protocol = "TCP" 170 name = "SSH" 171 port_range_start = 22 172 port_range_end = 22 173 } 174 } 175 }`