github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/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 return fmt.Errorf("Firewall still exists %s %s", rs.Primary.ID, resp.Response) 52 } 53 } 54 55 return nil 56 } 57 58 func testAccCheckProfitBricksLoadbalancerAttributes(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("testAccCheckProfitBricksLoadbalancerAttributes: 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 testAccCheckProfitBricksLoadbalancerExists(n string, loadbalancer *profitbricks.Loadbalancer) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 76 if !ok { 77 return fmt.Errorf("testAccCheckProfitBricksLoadbalancerExists: Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No Record ID is set") 82 } 83 84 foundLB := profitbricks.GetLoadbalancer(rs.Primary.Attributes["datacenter_id"], rs.Primary.ID) 85 86 if foundLB.StatusCode != 200 { 87 return fmt.Errorf("Error occured while fetching Loadbalancer: %s", rs.Primary.ID) 88 } 89 if foundLB.Id != rs.Primary.ID { 90 return fmt.Errorf("Record not found") 91 } 92 93 loadbalancer = &foundLB 94 95 return nil 96 } 97 } 98 99 const testAccCheckProfitbricksLoadbalancerConfig_basic = ` 100 resource "profitbricks_datacenter" "foobar" { 101 name = "loadbalancer-test" 102 location = "us/las" 103 } 104 105 resource "profitbricks_server" "webserver" { 106 name = "webserver" 107 datacenter_id = "${profitbricks_datacenter.foobar.id}" 108 cores = 1 109 ram = 1024 110 availability_zone = "ZONE_1" 111 cpu_family = "AMD_OPTERON" 112 volume { 113 name = "system" 114 size = 5 115 disk_type = "SSD" 116 image_name ="ubuntu-16.04" 117 image_password = "test1234" 118 } 119 nic { 120 lan = "1" 121 dhcp = true 122 firewall_active = true 123 firewall { 124 protocol = "TCP" 125 name = "SSH" 126 port_range_start = 22 127 port_range_end = 22 128 } 129 } 130 } 131 132 resource "profitbricks_nic" "database_nic" { 133 datacenter_id = "${profitbricks_datacenter.foobar.id}" 134 server_id = "${profitbricks_server.webserver.id}" 135 lan = "2" 136 dhcp = true 137 firewall_active = true 138 name = "updated" 139 } 140 141 resource "profitbricks_loadbalancer" "example" { 142 datacenter_id = "${profitbricks_datacenter.foobar.id}" 143 nic_id = "${profitbricks_nic.database_nic.id}" 144 name = "%s" 145 dhcp = true 146 }` 147 148 const testAccCheckProfitbricksLoadbalancerConfig_update = ` 149 resource "profitbricks_datacenter" "foobar" { 150 name = "loadbalancer-test" 151 location = "us/las" 152 } 153 154 resource "profitbricks_server" "webserver" { 155 name = "webserver" 156 datacenter_id = "${profitbricks_datacenter.foobar.id}" 157 cores = 1 158 ram = 1024 159 availability_zone = "ZONE_1" 160 cpu_family = "AMD_OPTERON" 161 volume { 162 name = "system" 163 size = 5 164 disk_type = "SSD" 165 image_name ="ubuntu-16.04" 166 image_password = "test1234" 167 } 168 nic { 169 lan = "1" 170 dhcp = true 171 firewall_active = true 172 firewall { 173 protocol = "TCP" 174 name = "SSH" 175 port_range_start = 22 176 port_range_end = 22 177 } 178 } 179 } 180 181 resource "profitbricks_nic" "database_nic" { 182 datacenter_id = "${profitbricks_datacenter.foobar.id}" 183 server_id = "${profitbricks_server.webserver.id}" 184 lan = "2" 185 dhcp = true 186 firewall_active = true 187 name = "updated" 188 } 189 190 resource "profitbricks_loadbalancer" "example" { 191 datacenter_id = "${profitbricks_datacenter.foobar.id}" 192 nic_id = "${profitbricks_nic.database_nic.id}" 193 name = "updated" 194 dhcp = true 195 }`