github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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", "accept"),
    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  					resource.TestCheckResourceAttr("scaleway_security_group_rule.http", "port", "80"),
    29  					resource.TestCheckResourceAttr("scaleway_security_group_rule.https", "action", "accept"),
    30  					resource.TestCheckResourceAttr("scaleway_security_group_rule.https", "direction", "inbound"),
    31  					resource.TestCheckResourceAttr("scaleway_security_group_rule.https", "ip_range", "0.0.0.0/0"),
    32  					resource.TestCheckResourceAttr("scaleway_security_group_rule.https", "protocol", "TCP"),
    33  					resource.TestCheckResourceAttr("scaleway_security_group_rule.https", "port", "443"),
    34  					testAccCheckScalewaySecurityGroupRuleExists("scaleway_security_group_rule.http", &group),
    35  					testAccCheckScalewaySecurityGroupRuleAttributes("scaleway_security_group_rule.http", &group),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckScalewaySecurityGroupsExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
    43  	return func(s *terraform.State) error {
    44  		rs, ok := s.RootModule().Resources[n]
    45  		if !ok {
    46  			return fmt.Errorf("Security Group Not found: %s", n)
    47  		}
    48  
    49  		if rs.Primary.ID == "" {
    50  			return fmt.Errorf("No Security Group is set")
    51  		}
    52  
    53  		conn := testAccProvider.Meta().(*Client).scaleway
    54  		resp, err := conn.GetASecurityGroup(rs.Primary.ID)
    55  
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		if resp.SecurityGroups.ID == rs.Primary.ID {
    61  			*group = resp.SecurityGroups
    62  			return nil
    63  		}
    64  
    65  		return fmt.Errorf("Security Group not found")
    66  	}
    67  }
    68  
    69  func testAccCheckScalewaySecurityGroupRuleDestroy(group *api.ScalewaySecurityGroups) func(*terraform.State) error {
    70  	return func(s *terraform.State) error {
    71  		client := testAccProvider.Meta().(*Client).scaleway
    72  
    73  		for _, rs := range s.RootModule().Resources {
    74  			if rs.Type != "scaleway" {
    75  				continue
    76  			}
    77  
    78  			_, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
    79  
    80  			if err == nil {
    81  				return fmt.Errorf("Security Group still exists")
    82  			}
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  func testAccCheckScalewaySecurityGroupRuleAttributes(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		rs, ok := s.RootModule().Resources[n]
    92  		if !ok {
    93  			return fmt.Errorf("Unknown resource: %s", n)
    94  		}
    95  
    96  		client := testAccProvider.Meta().(*Client).scaleway
    97  		rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		if rule.Rules.Action != "accept" {
   103  			return fmt.Errorf("Wrong rule action")
   104  		}
   105  		if rule.Rules.Direction != "inbound" {
   106  			return fmt.Errorf("wrong rule direction")
   107  		}
   108  		if rule.Rules.IPRange != "0.0.0.0/0" {
   109  			return fmt.Errorf("wrong rule IP Range")
   110  		}
   111  		if rule.Rules.Protocol != "TCP" {
   112  			return fmt.Errorf("wrong rule protocol")
   113  		}
   114  		if rule.Rules.DestPortFrom != 80 {
   115  			return fmt.Errorf("Wrong port")
   116  		}
   117  
   118  		return nil
   119  	}
   120  }
   121  
   122  func testAccCheckScalewaySecurityGroupRuleExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rs, ok := s.RootModule().Resources[n]
   125  
   126  		if !ok {
   127  			return fmt.Errorf("Security Group Rule Not found: %s", n)
   128  		}
   129  
   130  		if rs.Primary.ID == "" {
   131  			return fmt.Errorf("No Security Group Rule ID is set")
   132  		}
   133  
   134  		client := testAccProvider.Meta().(*Client).scaleway
   135  		rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
   136  
   137  		if err != nil {
   138  			return err
   139  		}
   140  
   141  		if rule.Rules.ID != rs.Primary.ID {
   142  			return fmt.Errorf("Record not found")
   143  		}
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  var testAccCheckScalewaySecurityGroupRuleConfig = `
   150  resource "scaleway_security_group" "base" {
   151    name = "public"
   152    description = "public gateway"
   153  }
   154  
   155  resource "scaleway_security_group_rule" "http" {
   156    security_group = "${scaleway_security_group.base.id}"
   157  
   158    action = "accept"
   159    direction = "inbound"
   160    ip_range = "0.0.0.0/0"
   161    protocol = "TCP"
   162    port = 80
   163  }
   164  
   165  resource "scaleway_security_group_rule" "https" {
   166    security_group = "${scaleway_security_group.base.id}"
   167  
   168    action = "accept"
   169    direction = "inbound"
   170    ip_range = "0.0.0.0/0"
   171    protocol = "TCP"
   172    port = 443
   173  }
   174  `