github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ns1/resource_team_test.go (about)

     1  package ns1
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	ns1 "gopkg.in/ns1/ns1-go.v2/rest"
    12  	"gopkg.in/ns1/ns1-go.v2/rest/model/account"
    13  )
    14  
    15  func TestAccTeam_basic(t *testing.T) {
    16  	var team account.Team
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckTeamDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccTeamBasic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckTeamExists("ns1_team.foobar", &team),
    27  					testAccCheckTeamName(&team, "terraform test"),
    28  					testAccCheckTeamDNSPermission(&team, "view_zones", true),
    29  					testAccCheckTeamDNSPermission(&team, "zones_allow_by_default", true),
    30  					testAccCheckTeamDNSPermissionZones(&team, "zones_allow", []string{"mytest.zone"}),
    31  					testAccCheckTeamDNSPermissionZones(&team, "zones_deny", []string{"myother.zone"}),
    32  					testAccCheckTeamDataPermission(&team, "manage_datasources", true),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccTeam_updated(t *testing.T) {
    40  	var team account.Team
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckTeamDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccTeamBasic,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckTeamExists("ns1_team.foobar", &team),
    51  					testAccCheckTeamName(&team, "terraform test"),
    52  				),
    53  			},
    54  			resource.TestStep{
    55  				Config: testAccTeamUpdated,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckTeamExists("ns1_team.foobar", &team),
    58  					testAccCheckTeamName(&team, "terraform test updated"),
    59  					testAccCheckTeamDNSPermission(&team, "view_zones", true),
    60  					testAccCheckTeamDNSPermission(&team, "zones_allow_by_default", true),
    61  					testAccCheckTeamDNSPermissionZones(&team, "zones_allow", []string{}),
    62  					testAccCheckTeamDNSPermissionZones(&team, "zones_deny", []string{}),
    63  					testAccCheckTeamDataPermission(&team, "manage_datasources", false),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckTeamExists(n string, team *account.Team) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		rs, ok := s.RootModule().Resources[n]
    73  		if !ok {
    74  			return fmt.Errorf("Not found: %s", n)
    75  		}
    76  
    77  		if rs.Primary.ID == "" {
    78  			return fmt.Errorf("NoID is set")
    79  		}
    80  
    81  		client := testAccProvider.Meta().(*ns1.Client)
    82  
    83  		foundTeam, _, err := client.Teams.Get(rs.Primary.Attributes["id"])
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		if foundTeam.Name != rs.Primary.Attributes["name"] {
    89  			return fmt.Errorf("Team not found")
    90  		}
    91  
    92  		*team = *foundTeam
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccCheckTeamDestroy(s *terraform.State) error {
    99  	client := testAccProvider.Meta().(*ns1.Client)
   100  
   101  	for _, rs := range s.RootModule().Resources {
   102  		if rs.Type != "ns1_team" {
   103  			continue
   104  		}
   105  
   106  		team, _, err := client.Teams.Get(rs.Primary.Attributes["id"])
   107  		if err == nil {
   108  			return fmt.Errorf("Team still exists: %#v: %#v", err, team.Name)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func testAccCheckTeamName(team *account.Team, expected string) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		if team.Name != expected {
   118  			return fmt.Errorf("Name: got: %s want: %s", team.Name, expected)
   119  		}
   120  		return nil
   121  	}
   122  }
   123  
   124  func testAccCheckTeamDNSPermission(team *account.Team, perm string, expected bool) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		dns := team.Permissions.DNS
   127  
   128  		switch perm {
   129  		case "view_zones":
   130  			if dns.ViewZones != expected {
   131  				return fmt.Errorf("DNS.ViewZones: got: %t want: %t", dns.ViewZones, expected)
   132  			}
   133  		case "manage_zones":
   134  			if dns.ManageZones != expected {
   135  				return fmt.Errorf("DNS.ManageZones: got: %t want: %t", dns.ManageZones, expected)
   136  			}
   137  		case "zones_allow_by_default":
   138  			if dns.ZonesAllowByDefault != expected {
   139  				return fmt.Errorf("DNS.ZonesAllowByDefault: got: %t want: %t", dns.ZonesAllowByDefault, expected)
   140  			}
   141  		}
   142  
   143  		return nil
   144  	}
   145  }
   146  
   147  func testAccCheckTeamDataPermission(team *account.Team, perm string, expected bool) resource.TestCheckFunc {
   148  	return func(s *terraform.State) error {
   149  		data := team.Permissions.Data
   150  
   151  		switch perm {
   152  		case "push_to_datafeeds":
   153  			if data.PushToDatafeeds != expected {
   154  				return fmt.Errorf("Data.PushToDatafeeds: got: %t want: %t", data.PushToDatafeeds, expected)
   155  			}
   156  		case "manage_datasources":
   157  			if data.ManageDatasources != expected {
   158  				return fmt.Errorf("Data.ManageDatasources: got: %t want: %t", data.ManageDatasources, expected)
   159  			}
   160  		case "manage_datafeeds":
   161  			if data.ManageDatafeeds != expected {
   162  				return fmt.Errorf("Data.ManageDatafeeds: got: %t want: %t", data.ManageDatafeeds, expected)
   163  			}
   164  		}
   165  
   166  		return nil
   167  	}
   168  }
   169  
   170  func testAccCheckTeamDNSPermissionZones(team *account.Team, perm string, expected []string) resource.TestCheckFunc {
   171  	return func(s *terraform.State) error {
   172  		dns := team.Permissions.DNS
   173  
   174  		switch perm {
   175  		case "zones_allow":
   176  			if !reflect.DeepEqual(dns.ZonesAllow, expected) {
   177  				return fmt.Errorf("DNS.ZonesAllow: got: %v want: %v", dns.ZonesAllow, expected)
   178  			}
   179  		case "zones_deny":
   180  			if !reflect.DeepEqual(dns.ZonesDeny, expected) {
   181  				return fmt.Errorf("DNS.ZonesDeny: got: %v want: %v", dns.ZonesDeny, expected)
   182  			}
   183  		}
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  const testAccTeamBasic = `
   190  resource "ns1_team" "foobar" {
   191    name = "terraform test"
   192  
   193    dns_view_zones = true
   194    dns_zones_allow_by_default = true
   195    dns_zones_allow = ["mytest.zone"]
   196    dns_zones_deny = ["myother.zone"]
   197  
   198    data_manage_datasources = true
   199  }`
   200  
   201  const testAccTeamUpdated = `
   202  resource "ns1_team" "foobar" {
   203    name = "terraform test updated"
   204  
   205    dns_view_zones = true
   206    dns_zones_allow_by_default = true
   207  
   208    data_manage_datasources = false
   209  }`