github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/vcd/resource_vcd_firewall_rules_test.go (about) 1 package vcd 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/hmrc/vmware-govcd" 12 ) 13 14 func TestAccVcdFirewallRules_basic(t *testing.T) { 15 16 var existingRules, fwRules govcd.EdgeGateway 17 newConfig := createFirewallRulesConfigs(&existingRules) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: newConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckVcdFirewallRulesExists("vcd_firewall_rules.bar", &fwRules), 27 testAccCheckVcdFirewallRulesAttributes(&fwRules, &existingRules), 28 ), 29 }, 30 }, 31 }) 32 33 } 34 35 func testAccCheckVcdFirewallRulesExists(n string, gateway *govcd.EdgeGateway) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[n] 38 39 if !ok { 40 return fmt.Errorf("Not found: %s", n) 41 } 42 43 if rs.Primary.ID == "" { 44 return fmt.Errorf("No Record ID is set") 45 } 46 47 conn := testAccProvider.Meta().(*VCDClient) 48 49 resp, err := conn.OrgVdc.FindEdgeGateway(rs.Primary.ID) 50 if err != nil { 51 return fmt.Errorf("Edge Gateway does not exist.") 52 } 53 54 *gateway = resp 55 56 return nil 57 } 58 } 59 60 func testAccCheckVcdFirewallRulesAttributes(newRules, existingRules *govcd.EdgeGateway) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 63 if len(newRules.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule) != len(existingRules.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule)+1 { 64 return fmt.Errorf("New firewall rule not added: %d != %d", 65 len(newRules.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule), 66 len(existingRules.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule)+1) 67 } 68 69 return nil 70 } 71 } 72 73 func createFirewallRulesConfigs(existingRules *govcd.EdgeGateway) string { 74 config := Config{ 75 User: os.Getenv("VCD_USER"), 76 Password: os.Getenv("VCD_PASSWORD"), 77 Org: os.Getenv("VCD_ORG"), 78 Href: os.Getenv("VCD_URL"), 79 VDC: os.Getenv("VCD_VDC"), 80 MaxRetryTimeout: 240, 81 } 82 conn, err := config.Client() 83 if err != nil { 84 return fmt.Sprintf(testAccCheckVcdFirewallRules_add, "", "") 85 } 86 edgeGateway, _ := conn.OrgVdc.FindEdgeGateway(os.Getenv("VCD_EDGE_GATWEWAY")) 87 *existingRules = edgeGateway 88 log.Printf("[DEBUG] Edge gateway: %#v", edgeGateway) 89 firewallRules := *edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService 90 return fmt.Sprintf(testAccCheckVcdFirewallRules_add, os.Getenv("VCD_EDGE_GATEWAY"), firewallRules.DefaultAction) 91 } 92 93 const testAccCheckVcdFirewallRules_add = ` 94 resource "vcd_firewall_rules" "bar" { 95 edge_gateway = "%s" 96 default_action = "%s" 97 98 rule { 99 description = "Test rule" 100 policy = "allow" 101 protocol = "any" 102 destination_port = "any" 103 destination_ip = "any" 104 source_port = "any" 105 source_ip = "any" 106 } 107 } 108 `