github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/profitbricks/resource_profitbricks_loadbalancer_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 TestAccProfitBricksLoadbalancer_Basic(t *testing.T) { 13 var loadbalancer profitbricks.Loadbalancer 14 lbName := "loadbalancer" 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDProfitBricksLoadbalancerDestroyCheck, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckProfitbricksLoadbalancerConfig_basic, lbName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckProfitBricksLoadbalancerExists("profitbricks_loadbalancer.example", &loadbalancer), 27 testAccCheckProfitBricksLoadbalancerAttributes("profitbricks_loadbalancer.example", lbName), 28 resource.TestCheckResourceAttr("profitbricks_loadbalancer.example", "name", lbName), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckProfitbricksLoadbalancerConfig_update, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckProfitBricksLoadbalancerAttributes("profitbricks_loadbalancer.example", "updated"), 35 resource.TestCheckResourceAttr("profitbricks_loadbalancer.example", "name", "updated"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckDProfitBricksLoadbalancerDestroyCheck(s *terraform.State) error { 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "profitbricks_loadbalancer" { 45 continue 46 } 47 48 resp := profitbricks.GetLoadbalancer(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 49 50 if resp.StatusCode < 299 { 51 resp := profitbricks.DeleteDatacenter(rs.Primary.Attributes["datacenter_id"]) 52 53 if resp.StatusCode > 299 { 54 return fmt.Errorf("Firewall still exists %s %s", rs.Primary.ID, string(resp.Body)) 55 } 56 } 57 } 58 59 return nil 60 } 61 62 func testAccCheckProfitBricksLoadbalancerAttributes(n string, name string) resource.TestCheckFunc { 63 return func(s *terraform.State) error { 64 rs, ok := s.RootModule().Resources[n] 65 if !ok { 66 return fmt.Errorf("testAccCheckProfitBricksLoadbalancerAttributes: Not found: %s", n) 67 } 68 if rs.Primary.Attributes["name"] != name { 69 return fmt.Errorf("Bad name: %s", rs.Primary.Attributes["name"]) 70 } 71 72 return nil 73 } 74 } 75 76 func testAccCheckProfitBricksLoadbalancerExists(n string, loadbalancer *profitbricks.Loadbalancer) resource.TestCheckFunc { 77 return func(s *terraform.State) error { 78 rs, ok := s.RootModule().Resources[n] 79 80 if !ok { 81 return fmt.Errorf("testAccCheckProfitBricksLoadbalancerExists: Not found: %s", n) 82 } 83 84 if rs.Primary.ID == "" { 85 return fmt.Errorf("No Record ID is set") 86 } 87 88 foundLB := profitbricks.GetLoadbalancer(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 89 90 if foundLB.StatusCode != 200 { 91 return fmt.Errorf("Error occured while fetching Loadbalancer: %s", rs.Primary.ID) 92 } 93 if foundLB.Id != rs.Primary.ID { 94 return fmt.Errorf("Record not found") 95 } 96 97 loadbalancer = &foundLB 98 99 return nil 100 } 101 } 102 103 const testAccCheckProfitbricksLoadbalancerConfig_basic = ` 104 resource "profitbricks_datacenter" "foobar" { 105 name = "loadbalancer-test" 106 location = "us/las" 107 } 108 109 resource "profitbricks_server" "webserver" { 110 name = "webserver" 111 datacenter_id = "${profitbricks_datacenter.foobar.id}" 112 cores = 1 113 ram = 1024 114 availability_zone = "ZONE_1" 115 cpu_family = "AMD_OPTERON" 116 volume { 117 name = "system" 118 size = 5 119 disk_type = "SSD" 120 image_name ="ubuntu-16.04" 121 image_password = "K3tTj8G14a3EgKyNeeiY" 122 } 123 nic { 124 lan = "1" 125 dhcp = true 126 firewall_active = true 127 firewall { 128 protocol = "TCP" 129 name = "SSH" 130 port_range_start = 22 131 port_range_end = 22 132 } 133 } 134 } 135 136 resource "profitbricks_nic" "database_nic" { 137 datacenter_id = "${profitbricks_datacenter.foobar.id}" 138 server_id = "${profitbricks_server.webserver.id}" 139 lan = "2" 140 dhcp = true 141 firewall_active = true 142 name = "updated" 143 } 144 145 resource "profitbricks_loadbalancer" "example" { 146 datacenter_id = "${profitbricks_datacenter.foobar.id}" 147 nic_id = "${profitbricks_nic.database_nic.id}" 148 name = "%s" 149 dhcp = true 150 }` 151 152 const testAccCheckProfitbricksLoadbalancerConfig_update = ` 153 resource "profitbricks_datacenter" "foobar" { 154 name = "loadbalancer-test" 155 location = "us/las" 156 } 157 158 resource "profitbricks_server" "webserver" { 159 name = "webserver" 160 datacenter_id = "${profitbricks_datacenter.foobar.id}" 161 cores = 1 162 ram = 1024 163 availability_zone = "ZONE_1" 164 cpu_family = "AMD_OPTERON" 165 volume { 166 name = "system" 167 size = 5 168 disk_type = "SSD" 169 image_name ="ubuntu-16.04" 170 image_password = "K3tTj8G14a3EgKyNeeiY" 171 } 172 nic { 173 lan = "1" 174 dhcp = true 175 firewall_active = true 176 firewall { 177 protocol = "TCP" 178 name = "SSH" 179 port_range_start = 22 180 port_range_end = 22 181 } 182 } 183 } 184 185 resource "profitbricks_nic" "database_nic" { 186 datacenter_id = "${profitbricks_datacenter.foobar.id}" 187 server_id = "${profitbricks_server.webserver.id}" 188 lan = "2" 189 dhcp = true 190 firewall_active = true 191 name = "updated" 192 } 193 194 resource "profitbricks_loadbalancer" "example" { 195 datacenter_id = "${profitbricks_datacenter.foobar.id}" 196 nic_id = "${profitbricks_nic.database_nic.id}" 197 name = "updated" 198 dhcp = true 199 }`