github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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,
    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(s *terraform.State) error {
    70  	client := testAccProvider.Meta().(*Client).scaleway
    71  
    72  	for _, rs := range s.RootModule().Resources {
    73  		if rs.Type != "scaleway" {
    74  			continue
    75  		}
    76  
    77  		groups, err := client.GetSecurityGroups()
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		all_err := true
    83  		for _, group := range groups.SecurityGroups {
    84  			_, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
    85  			all_err = all_err && err != nil
    86  		}
    87  
    88  		if !all_err {
    89  			return fmt.Errorf("Security Group still exists")
    90  		}
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  func testAccCheckScalewaySecurityGroupRuleAttributes(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		rs, ok := s.RootModule().Resources[n]
    99  		if !ok {
   100  			return fmt.Errorf("Unknown resource: %s", n)
   101  		}
   102  
   103  		client := testAccProvider.Meta().(*Client).scaleway
   104  		rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		if rule.Rules.Action != "accept" {
   110  			return fmt.Errorf("Wrong rule action")
   111  		}
   112  		if rule.Rules.Direction != "inbound" {
   113  			return fmt.Errorf("wrong rule direction")
   114  		}
   115  		if rule.Rules.IPRange != "0.0.0.0/0" {
   116  			return fmt.Errorf("wrong rule IP Range")
   117  		}
   118  		if rule.Rules.Protocol != "TCP" {
   119  			return fmt.Errorf("wrong rule protocol")
   120  		}
   121  		if rule.Rules.DestPortFrom != 80 {
   122  			return fmt.Errorf("Wrong port")
   123  		}
   124  
   125  		return nil
   126  	}
   127  }
   128  
   129  func testAccCheckScalewaySecurityGroupRuleExists(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[n]
   132  
   133  		if !ok {
   134  			return fmt.Errorf("Security Group Rule Not found: %s", n)
   135  		}
   136  
   137  		if rs.Primary.ID == "" {
   138  			return fmt.Errorf("No Security Group Rule ID is set")
   139  		}
   140  
   141  		client := testAccProvider.Meta().(*Client).scaleway
   142  		rule, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID)
   143  
   144  		if err != nil {
   145  			return err
   146  		}
   147  
   148  		if rule.Rules.ID != rs.Primary.ID {
   149  			return fmt.Errorf("Record not found")
   150  		}
   151  
   152  		return nil
   153  	}
   154  }
   155  
   156  var testAccCheckScalewaySecurityGroupRuleConfig = `
   157  resource "scaleway_security_group" "base" {
   158    name = "public"
   159    description = "public gateway"
   160  }
   161  
   162  resource "scaleway_security_group_rule" "http" {
   163    security_group = "${scaleway_security_group.base.id}"
   164  
   165    action = "accept"
   166    direction = "inbound"
   167    ip_range = "0.0.0.0/0"
   168    protocol = "TCP"
   169    port = 80
   170  }
   171  
   172  resource "scaleway_security_group_rule" "https" {
   173    security_group = "${scaleway_security_group.base.id}"
   174  
   175    action = "accept"
   176    direction = "inbound"
   177    ip_range = "0.0.0.0/0"
   178    protocol = "TCP"
   179    port = 443
   180  }
   181  `