|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfe |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "math/rand" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + tfe "github.com/hashicorp/go-tfe" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAccTFETeamsDataSource_basic(t *testing.T) { |
| 19 | + tfeClient, err := getClientUsingEnv() |
| 20 | + if err != nil { |
| 21 | + t.Fatal(err) |
| 22 | + } |
| 23 | + |
| 24 | + rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int() |
| 25 | + |
| 26 | + org, orgCleanup := createOrganization(t, tfeClient, tfe.OrganizationCreateOptions{ |
| 27 | + Name: tfe.String(fmt.Sprintf("tst-terraform-%d", rInt)), |
| 28 | + Email: tfe. String( fmt. Sprintf( "%[email protected]", randomString( t))), |
| 29 | + }) |
| 30 | + t.Cleanup(orgCleanup) |
| 31 | + |
| 32 | + resource.Test(t, resource.TestCase{ |
| 33 | + PreCheck: func() { testAccPreCheck(t) }, |
| 34 | + Providers: testAccProviders, |
| 35 | + Steps: []resource.TestStep{ |
| 36 | + { |
| 37 | + Config: testAccTFETeamsDataSourceConfig_basic_resource(rInt, org.Name), |
| 38 | + }, |
| 39 | + { |
| 40 | + Config: testAccTFETeamsDataSourceConfig_basic_data(org.Name), |
| 41 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 42 | + testAccCheckTFETeamsHasNames("data.tfe_teams.foobar", []string{ |
| 43 | + fmt.Sprintf("team-foo-%d", rInt), |
| 44 | + fmt.Sprintf("team-bar-%d", rInt), |
| 45 | + }), |
| 46 | + testAccCheckTFETeamsHasIDs("data.tfe_teams.foobar", []string{ |
| 47 | + fmt.Sprintf("team-foo-%d", rInt), |
| 48 | + fmt.Sprintf("team-bar-%d", rInt), |
| 49 | + }), |
| 50 | + ), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +func testAccCheckTFETeamsHasNames(teamsData string, teamNames []string) resource.TestCheckFunc { |
| 57 | + return func(s *terraform.State) error { |
| 58 | + teams, ok := s.RootModule().Resources[teamsData] |
| 59 | + if !ok { |
| 60 | + return fmt.Errorf("Teams data '%s' not found.", teamsData) |
| 61 | + } |
| 62 | + numTeamsStr := teams.Primary.Attributes["names.#"] |
| 63 | + numTeams, _ := strconv.Atoi(numTeamsStr) |
| 64 | + |
| 65 | + if numTeams < len(teamNames) { |
| 66 | + return fmt.Errorf("expected %d organizations, but found %d.", len(teamNames), numTeams) |
| 67 | + } |
| 68 | + |
| 69 | + teamsMap := map[string]struct{}{} |
| 70 | + for i := 0; i < numTeams; i++ { |
| 71 | + teamName := teams.Primary.Attributes[fmt.Sprintf("names.%d", i)] |
| 72 | + teamsMap[teamName] = struct{}{} |
| 73 | + } |
| 74 | + |
| 75 | + for _, teamName := range teamNames { |
| 76 | + _, ok := teamsMap[teamName] |
| 77 | + if !ok { |
| 78 | + return fmt.Errorf("expected to find team name %s, but did not.", teamName) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func testAccCheckTFETeamsHasIDs(teamsData string, teamNames []string) resource.TestCheckFunc { |
| 87 | + return func(s *terraform.State) error { |
| 88 | + teams, ok := s.RootModule().Resources[teamsData] |
| 89 | + if !ok { |
| 90 | + return fmt.Errorf("Teams data '%s' not found.", teamsData) |
| 91 | + } |
| 92 | + |
| 93 | + for _, teamName := range teamNames { |
| 94 | + id := fmt.Sprintf("ids.%s", teamName) |
| 95 | + _, ok := teams.Primary.Attributes[id] |
| 96 | + if !ok { |
| 97 | + return fmt.Errorf("expected to find team id %s, but did not.", id) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func testAccTFETeamsDataSourceConfig_basic_resource(rInt int, organization string) string { |
| 106 | + return fmt.Sprintf(` |
| 107 | +resource "tfe_team" "foo" { |
| 108 | + name = "team-foo-%d" |
| 109 | + organization = "%s" |
| 110 | +} |
| 111 | +
|
| 112 | +resource "tfe_team" "bar" { |
| 113 | + name = "team-bar-%d" |
| 114 | + organization = "%s" |
| 115 | +}`, rInt, organization, rInt, organization) |
| 116 | +} |
| 117 | + |
| 118 | +func testAccTFETeamsDataSourceConfig_basic_data(organization string) string { |
| 119 | + return fmt.Sprintf(` |
| 120 | + data "tfe_teams" "foobar" { |
| 121 | + organization = "%s" |
| 122 | + }`, organization) |
| 123 | +} |
0 commit comments