github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/scaleway/resource_scaleway_security_group_rule_test.go (about) 1 package scaleway 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/scaleway/scaleway-cli/pkg/api" 10 ) 11 12 func TestAccScalewaySecurityGroupRule_Basic(t *testing.T) { 13 var group api.ScalewaySecurityGroups 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckScalewaySecurityGroupRuleDestroy(&group), 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccCheckScalewaySecurityGroupRuleConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckScalewaySecurityGroupsExists("scaleway_security_group.base", &group), 24 resource.TestCheckResourceAttr("scaleway_security_group_rule.http", "action", "drop"), 25 resource.TestCheckResourceAttr("scaleway_security_group_rule.http", "direction", "inbound"), 26 resource.TestCheckResourceAttr("scaleway_security_group_rule.http", "ip_range", "0.0.0.0/0"), 27 resource.TestCheckResourceAttr("scaleway_security_group_rule.http", "protocol", "TCP"), 28 testAccCheckScalewaySecurityGroupRuleExists("scaleway_security_group_rule.http", &group), 29 testAccCheckScalewaySecurityGroupRuleAttributes("scaleway_security_group_rule.http", &group), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func testAccCheckScalewaySecurityGroupsExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc { 37 return func(s *terraform.State) error { 38 rs, ok := s.RootModule().Resources[n] 39 if !ok { 40 return fmt.Errorf("Security Group Not found: %s", n) 41 } 42 43 if rs.Primary.ID == "" { 44 return fmt.Errorf("No Security Group is set") 45 } 46 47 conn := testAccProvider.Meta().(*Client).scaleway 48 resp, err := conn.GetASecurityGroup(rs.Primary.ID) 49 50 if err != nil { 51 return err 52 } 53 54 if resp.SecurityGroups.ID == rs.Primary.ID { 55 *group = resp.SecurityGroups 56 return nil 57 } 58 59 return fmt.Errorf("Security Group not found") 60 } 61 } 62 63 func testAccCheckScalewaySecurityGroupRuleDestroy(group *api.ScalewaySecurityGroups) func(*terraform.State) error { 64 return func(s *terraform.State) error { 65 client := testAccProvider.Meta().(*Client).scaleway 66 67 for _, rs := range s.RootModule().Resources { 68 if rs.Type != "scaleway" { 69 continue 70 } 71 72 _, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID) 73 74 if err == nil { 75 return fmt.Errorf("Security Group still exists") 76 } 77 } 78 79 return nil 80 } 81 } 82 83 func testAccCheckScalewaySecurityGroupRuleAttributes(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc { 84 return func(s *terraform.State) error { 85 rs, ok := s.RootModule().Resources[n] 86 if !ok { 87 return fmt.Errorf("Unknown resource: %s", n) 88 } 89 90 client := testAccProvider.Meta().(*Client).scaleway 91 rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID) 92 if err != nil { 93 return err 94 } 95 96 if rule.Rules.Action != "drop" { 97 return fmt.Errorf("Wrong rule action") 98 } 99 if rule.Rules.Direction != "inbound" { 100 return fmt.Errorf("wrong rule direction") 101 } 102 if rule.Rules.IPRange != "0.0.0.0/0" { 103 return fmt.Errorf("wrong rule IP Range") 104 } 105 if rule.Rules.Protocol != "TCP" { 106 return fmt.Errorf("wrong rule protocol") 107 } 108 if rule.Rules.DestPortFrom != 80 { 109 return fmt.Errorf("Wrong port") 110 } 111 112 return nil 113 } 114 } 115 116 func testAccCheckScalewaySecurityGroupRuleExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc { 117 return func(s *terraform.State) error { 118 rs, ok := s.RootModule().Resources[n] 119 120 if !ok { 121 return fmt.Errorf("Security Group Rule Not found: %s", n) 122 } 123 124 if rs.Primary.ID == "" { 125 return fmt.Errorf("No Security Group Rule ID is set") 126 } 127 128 client := testAccProvider.Meta().(*Client).scaleway 129 rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID) 130 131 if err != nil { 132 return err 133 } 134 135 if rule.Rules.ID != rs.Primary.ID { 136 return fmt.Errorf("Record not found") 137 } 138 139 return nil 140 } 141 } 142 143 var testAccCheckScalewaySecurityGroupRuleConfig = ` 144 resource "scaleway_security_group" "base" { 145 name = "public" 146 description = "public gateway" 147 } 148 149 resource "scaleway_security_group_rule" "http" { 150 security_group = "${scaleway_security_group.base.id}" 151 152 action = "drop" 153 direction = "inbound" 154 ip_range = "0.0.0.0/0" 155 protocol = "TCP" 156 port = 80 157 } 158 `