github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/builtin/providers/openstack/resource_openstack_fw_rule_v1_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 "time" 8 9 "github.com/gophercloud/gophercloud" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/rules" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccFWRuleV1_basic(t *testing.T) { 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckFWRuleV1Destroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testFirewallRuleMinimalConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckFWRuleV1Exists( 25 "openstack_fw_rule_v1.accept_test_minimal", 26 &rules.Rule{ 27 Protocol: "udp", 28 Action: "deny", 29 IPVersion: 4, 30 Enabled: true, 31 }), 32 ), 33 }, 34 resource.TestStep{ 35 Config: testFirewallRuleConfig, 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckFWRuleV1Exists( 38 "openstack_fw_rule_v1.accept_test", 39 &rules.Rule{ 40 Name: "accept_test", 41 Protocol: "udp", 42 Action: "deny", 43 Description: "Terraform accept test", 44 IPVersion: 4, 45 SourceIPAddress: "1.2.3.4", 46 DestinationIPAddress: "4.3.2.0/24", 47 SourcePort: "444", 48 DestinationPort: "555", 49 Enabled: true, 50 }), 51 ), 52 }, 53 resource.TestStep{ 54 Config: testFirewallRuleUpdateAllFieldsConfig, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckFWRuleV1Exists( 57 "openstack_fw_rule_v1.accept_test", 58 &rules.Rule{ 59 Name: "accept_test_updated_2", 60 Protocol: "tcp", 61 Action: "allow", 62 Description: "Terraform accept test updated", 63 IPVersion: 4, 64 SourceIPAddress: "1.2.3.0/24", 65 DestinationIPAddress: "4.3.2.8", 66 SourcePort: "666", 67 DestinationPort: "777", 68 Enabled: false, 69 }), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func testAccCheckFWRuleV1Destroy(s *terraform.State) error { 77 78 config := testAccProvider.Meta().(*Config) 79 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 80 if err != nil { 81 return fmt.Errorf("(testAccCheckOpenstackFirewallRuleDestroy) Error creating OpenStack networking client: %s", err) 82 } 83 for _, rs := range s.RootModule().Resources { 84 if rs.Type != "openstack_firewall_rule" { 85 continue 86 } 87 _, err = rules.Get(networkingClient, rs.Primary.ID).Extract() 88 if err == nil { 89 return fmt.Errorf("Firewall rule (%s) still exists.", rs.Primary.ID) 90 } 91 if _, ok := err.(gophercloud.ErrDefault404); !ok { 92 return err 93 } 94 } 95 return nil 96 } 97 98 func testAccCheckFWRuleV1Exists(n string, expected *rules.Rule) resource.TestCheckFunc { 99 100 return func(s *terraform.State) error { 101 102 rs, ok := s.RootModule().Resources[n] 103 if !ok { 104 return fmt.Errorf("Not found: %s", n) 105 } 106 107 if rs.Primary.ID == "" { 108 return fmt.Errorf("No ID is set") 109 } 110 111 config := testAccProvider.Meta().(*Config) 112 networkingClient, err := config.networkingV2Client(OS_REGION_NAME) 113 if err != nil { 114 return fmt.Errorf("(testAccCheckFirewallRuleExists) Error creating OpenStack networking client: %s", err) 115 } 116 117 var found *rules.Rule 118 for i := 0; i < 5; i++ { 119 // Firewall rule creation is asynchronous. Retry some times 120 // if we get a 404 error. Fail on any other error. 121 found, err = rules.Get(networkingClient, rs.Primary.ID).Extract() 122 if err != nil { 123 if _, ok := err.(gophercloud.ErrDefault404); ok { 124 time.Sleep(time.Second) 125 continue 126 } 127 return err 128 } 129 break 130 } 131 132 expected.ID = found.ID 133 // Erase the tenant id because we don't want to compare 134 // it as long it is not present in the expected 135 found.TenantID = "" 136 137 if !reflect.DeepEqual(expected, found) { 138 return fmt.Errorf("Expected:\n%#v\nFound:\n%#v", expected, found) 139 } 140 141 return nil 142 } 143 } 144 145 const testFirewallRuleMinimalConfig = ` 146 resource "openstack_fw_rule_v1" "accept_test_minimal" { 147 protocol = "udp" 148 action = "deny" 149 } 150 ` 151 152 const testFirewallRuleConfig = ` 153 resource "openstack_fw_rule_v1" "accept_test" { 154 name = "accept_test" 155 description = "Terraform accept test" 156 protocol = "udp" 157 action = "deny" 158 ip_version = 4 159 source_ip_address = "1.2.3.4" 160 destination_ip_address = "4.3.2.0/24" 161 source_port = "444" 162 destination_port = "555" 163 enabled = true 164 } 165 ` 166 167 const testFirewallRuleUpdateAllFieldsConfig = ` 168 resource "openstack_fw_rule_v1" "accept_test" { 169 name = "accept_test_updated_2" 170 description = "Terraform accept test updated" 171 protocol = "tcp" 172 action = "allow" 173 ip_version = 4 174 source_ip_address = "1.2.3.0/24" 175 destination_ip_address = "4.3.2.8" 176 source_port = "666" 177 destination_port = "777" 178 enabled = false 179 } 180 `