github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/oneandone/resource_oneandone_firewall_policy_test.go (about) 1 package oneandone 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/1and1/oneandone-cloudserver-sdk-go" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "os" 11 "time" 12 ) 13 14 func TestAccOneandoneFirewall_Basic(t *testing.T) { 15 var firewall oneandone.FirewallPolicy 16 17 name := "test" 18 name_updated := "test1" 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { 22 testAccPreCheck(t) 23 }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDOneandoneFirewallDestroyCheck, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: fmt.Sprintf(testAccCheckOneandoneFirewall_basic, name), 29 30 Check: resource.ComposeTestCheckFunc( 31 func(*terraform.State) error { 32 time.Sleep(10 * time.Second) 33 return nil 34 }, 35 testAccCheckOneandoneFirewallExists("oneandone_firewall_policy.fw", &firewall), 36 testAccCheckOneandoneFirewallAttributes("oneandone_firewall_policy.fw", name), 37 resource.TestCheckResourceAttr("oneandone_firewall_policy.fw", "name", name), 38 ), 39 }, 40 resource.TestStep{ 41 Config: fmt.Sprintf(testAccCheckOneandoneFirewall_update, name_updated), 42 43 Check: resource.ComposeTestCheckFunc( 44 func(*terraform.State) error { 45 time.Sleep(10 * time.Second) 46 return nil 47 }, 48 testAccCheckOneandoneFirewallExists("oneandone_firewall_policy.fw", &firewall), 49 testAccCheckOneandoneFirewallAttributes("oneandone_firewall_policy.fw", name_updated), 50 resource.TestCheckResourceAttr("oneandone_firewall_policy.fw", "name", name_updated), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func testAccCheckDOneandoneFirewallDestroyCheck(s *terraform.State) error { 58 for _, rs := range s.RootModule().Resources { 59 if rs.Type != "oneandone_firewall_policy.fw" { 60 continue 61 } 62 63 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 64 65 _, err := api.GetFirewallPolicy(rs.Primary.ID) 66 67 if err == nil { 68 return fmt.Errorf("Firewall Policy still exists %s %s", rs.Primary.ID, err.Error()) 69 } 70 } 71 72 return nil 73 } 74 func testAccCheckOneandoneFirewallAttributes(n string, reverse_dns string) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 rs, ok := s.RootModule().Resources[n] 77 if !ok { 78 return fmt.Errorf("Not found: %s", n) 79 } 80 if rs.Primary.Attributes["name"] != reverse_dns { 81 return fmt.Errorf("Bad name: expected %s : found %s ", reverse_dns, rs.Primary.Attributes["name"]) 82 } 83 84 return nil 85 } 86 } 87 88 func testAccCheckOneandoneFirewallExists(n string, fw_p *oneandone.FirewallPolicy) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 rs, ok := s.RootModule().Resources[n] 91 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No Record ID is set") 98 } 99 100 api := oneandone.New(os.Getenv("ONEANDONE_TOKEN"), oneandone.BaseUrl) 101 102 found_fw, err := api.GetFirewallPolicy(rs.Primary.ID) 103 104 if err != nil { 105 return fmt.Errorf("Error occured while fetching Firewall Policy: %s", rs.Primary.ID) 106 } 107 if found_fw.Id != rs.Primary.ID { 108 return fmt.Errorf("Record not found") 109 } 110 fw_p = found_fw 111 112 return nil 113 } 114 } 115 116 const testAccCheckOneandoneFirewall_basic = ` 117 resource "oneandone_firewall_policy" "fw" { 118 name = "%s" 119 rules = [ 120 { 121 "protocol" = "TCP" 122 "port_from" = 80 123 "port_to" = 80 124 "source_ip" = "0.0.0.0" 125 }, 126 { 127 "protocol" = "ICMP" 128 "source_ip" = "0.0.0.0" 129 }, 130 { 131 "protocol" = "TCP" 132 "port_from" = 43 133 "port_to" = 43 134 "source_ip" = "0.0.0.0" 135 }, 136 { 137 "protocol" = "TCP" 138 "port_from" = 22 139 "port_to" = 22 140 "source_ip" = "0.0.0.0" 141 } 142 ] 143 }` 144 145 const testAccCheckOneandoneFirewall_update = ` 146 resource "oneandone_firewall_policy" "fw" { 147 name = "%s" 148 rules = [ 149 { 150 "protocol" = "TCP" 151 "port_from" = 80 152 "port_to" = 80 153 "source_ip" = "0.0.0.0" 154 }, 155 { 156 "protocol" = "ICMP" 157 "source_ip" = "0.0.0.0" 158 }, 159 { 160 "protocol" = "TCP" 161 "port_from" = 43 162 "port_to" = 43 163 "source_ip" = "0.0.0.0" 164 }, 165 { 166 "protocol" = "TCP" 167 "port_from" = 22 168 "port_to" = 22 169 "source_ip" = "0.0.0.0" 170 }, 171 { 172 "protocol" = "TCP" 173 "port_from" = 88 174 "port_to" = 88 175 "source_ip" = "0.0.0.0" 176 }, 177 ] 178 }`