github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/resource_profitbricks_firewall_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 TestAccProfitBricksFirewall_Basic(t *testing.T) { 13 var firewall profitbricks.FirewallRule 14 firewallName := "firewall" 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckDProfitBricksFirewallDestroyCheck, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: fmt.Sprintf(testAccCheckProfitbricksFirewallConfig_basic, firewallName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckProfitBricksFirewallExists("profitbricks_firewall.webserver_http", &firewall), 27 testAccCheckProfitBricksFirewallAttributes("profitbricks_firewall.webserver_http", firewallName), 28 resource.TestCheckResourceAttr("profitbricks_firewall.webserver_http", "name", firewallName), 29 ), 30 }, 31 resource.TestStep{ 32 Config: testAccCheckProfitbricksFirewallConfig_update, 33 Check: resource.ComposeTestCheckFunc( 34 testAccCheckProfitBricksFirewallAttributes("profitbricks_firewall.webserver_http", "updated"), 35 resource.TestCheckResourceAttr("profitbricks_firewall.webserver_http", "name", "updated"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func testAccCheckDProfitBricksFirewallDestroyCheck(s *terraform.State) error { 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "profitbricks_firewall" { 45 continue 46 } 47 48 resp := profitbricks.GetFirewallRule(rs.Primary.Attributes["datacenter_id"], rs.Primary.Attributes["server_id"], rs.Primary.Attributes["nic_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 testAccCheckProfitBricksFirewallAttributes(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("testAccCheckProfitBricksFirewallAttributes: 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 testAccCheckProfitBricksFirewallExists(n string, firewall *profitbricks.FirewallRule) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 76 if !ok { 77 return fmt.Errorf("testAccCheckProfitBricksFirewallExists: 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.GetFirewallRule(rs.Primary.Attributes["datacenter_id"], rs.Primary.Attributes["server_id"], rs.Primary.Attributes["nic_id"], rs.Primary.ID) 85 86 if foundServer.StatusCode != 200 { 87 return fmt.Errorf("Error occured while fetching Firewall rule: %s", rs.Primary.ID) 88 } 89 if foundServer.Id != rs.Primary.ID { 90 return fmt.Errorf("Record not found") 91 } 92 93 firewall = &foundServer 94 95 return nil 96 } 97 } 98 99 const testAccCheckProfitbricksFirewallConfig_basic = ` 100 resource "profitbricks_datacenter" "foobar" { 101 name = "firewall-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 = "K3tTj8G14a3EgKyNeeiY" 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_firewall" "webserver_http" { 142 datacenter_id = "${profitbricks_datacenter.foobar.id}" 143 server_id = "${profitbricks_server.webserver.id}" 144 nic_id = "${profitbricks_nic.database_nic.id}" 145 protocol = "TCP" 146 name = "%s" 147 port_range_start = 80 148 port_range_end = 80 149 }` 150 151 const testAccCheckProfitbricksFirewallConfig_update = ` 152 resource "profitbricks_datacenter" "foobar" { 153 name = "firewall-test" 154 location = "us/las" 155 } 156 157 resource "profitbricks_server" "webserver" { 158 name = "webserver" 159 datacenter_id = "${profitbricks_datacenter.foobar.id}" 160 cores = 1 161 ram = 1024 162 availability_zone = "ZONE_1" 163 cpu_family = "AMD_OPTERON" 164 volume { 165 name = "system" 166 size = 5 167 disk_type = "SSD" 168 image_name ="ubuntu-16.04" 169 image_password = "test1234" 170 } 171 nic { 172 lan = "1" 173 dhcp = true 174 firewall_active = true 175 firewall { 176 protocol = "TCP" 177 name = "SSH" 178 port_range_start = 22 179 port_range_end = 22 180 } 181 } 182 } 183 184 resource "profitbricks_nic" "database_nic" { 185 datacenter_id = "${profitbricks_datacenter.foobar.id}" 186 server_id = "${profitbricks_server.webserver.id}" 187 lan = 2 188 dhcp = true 189 firewall_active = true 190 name = "updated" 191 } 192 193 resource "profitbricks_firewall" "webserver_http" { 194 datacenter_id = "${profitbricks_datacenter.foobar.id}" 195 server_id = "${profitbricks_server.webserver.id}" 196 nic_id = "${profitbricks_nic.database_nic.id}" 197 protocol = "TCP" 198 name = "updated" 199 port_range_start = 80 200 port_range_end = 80 201 }`