Skip to content

Commit 95a6bd7

Browse files
committed
Add allowed_project_ids and excluded_workspace_ids to agent pool data source
1 parent 4612cc9 commit 95a6bd7

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

internal/provider/data_source_agent_pool.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ func dataSourceTFEAgentPool() *schema.Resource {
3737
Computed: true,
3838
Elem: &schema.Schema{Type: schema.TypeString},
3939
},
40+
41+
"allowed_project_ids": {
42+
Type: schema.TypeSet,
43+
Computed: true,
44+
Elem: &schema.Schema{Type: schema.TypeString},
45+
},
46+
47+
"excluded_workspace_ids": {
48+
Type: schema.TypeSet,
49+
Computed: true,
50+
Elem: &schema.Schema{Type: schema.TypeString},
51+
},
4052
},
4153
}
4254
}
@@ -59,11 +71,23 @@ func dataSourceTFEAgentPoolRead(d *schema.ResourceData, meta interface{}) error
5971
d.SetId(pool.ID)
6072
d.Set("organization_scoped", pool.OrganizationScoped)
6173

74+
var allowedProjectIDs []string
75+
for _, allowedProjectID := range pool.AllowedProjects {
76+
allowedProjectIDs = append(allowedProjectIDs, allowedProjectID.ID)
77+
}
78+
d.Set("allowed_project_ids", allowedProjectIDs)
79+
6280
var allowedWorkspaceIDs []string
6381
for _, allowedWorkspaceID := range pool.AllowedWorkspaces {
6482
allowedWorkspaceIDs = append(allowedWorkspaceIDs, allowedWorkspaceID.ID)
6583
}
6684
d.Set("allowed_workspace_ids", allowedWorkspaceIDs)
6785

86+
var excludedWorkspaceIDs []string
87+
for _, excludedWorkspaceID := range pool.ExcludedWorkspaces {
88+
excludedWorkspaceIDs = append(excludedWorkspaceIDs, excludedWorkspaceID.ID)
89+
}
90+
d.Set("excluded_workspace_ids", excludedWorkspaceIDs)
91+
6892
return nil
6993
}

internal/provider/data_source_agent_pool_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,90 @@ func TestAccTFEAgentPoolDataSource_allowed_workspaces(t *testing.T) {
8989
})
9090
}
9191

92+
func TestAccTFEAgentPoolDataSource_allowed_projects(t *testing.T) {
93+
skipIfEnterprise(t)
94+
95+
tfeClient, err := getClientUsingEnv()
96+
if err != nil {
97+
t.Fatal(err)
98+
}
99+
100+
org, orgCleanup := createBusinessOrganization(t, tfeClient)
101+
t.Cleanup(orgCleanup)
102+
103+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
104+
105+
ws, err := tfeClient.Projects.Create(ctx, org.Name, tfe.ProjectCreateOptions{
106+
Name: fmt.Sprintf("tst-proj-test-%d", rInt),
107+
})
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
resource.Test(t, resource.TestCase{
113+
PreCheck: func() { testAccPreCheck(t) },
114+
ProtoV5ProviderFactories: testAccMuxedProviders,
115+
Steps: []resource.TestStep{
116+
{
117+
Config: testAccTFEAgentPoolDataSourceAllowedProjectsConfig(org.Name, rInt, ws.ID),
118+
Check: resource.ComposeAggregateTestCheckFunc(
119+
resource.TestCheckResourceAttrSet("data.tfe_agent_pool.foobar", "id"),
120+
resource.TestCheckResourceAttr(
121+
"data.tfe_agent_pool.foobar", "name", fmt.Sprintf("agent-pool-test-%d", rInt)),
122+
resource.TestCheckResourceAttr(
123+
"data.tfe_agent_pool.foobar", "organization", org.Name),
124+
resource.TestCheckResourceAttr(
125+
"data.tfe_agent_pool.foobar", "organization_scoped", "false"),
126+
resource.TestCheckResourceAttr(
127+
"data.tfe_agent_pool.foobar", "allowed_project_ids.0", ws.ID),
128+
),
129+
},
130+
},
131+
})
132+
}
133+
134+
func TestAccTFEAgentPoolDataSource_excluded_workspaces(t *testing.T) {
135+
skipIfEnterprise(t)
136+
137+
tfeClient, err := getClientUsingEnv()
138+
if err != nil {
139+
t.Fatal(err)
140+
}
141+
142+
org, orgCleanup := createBusinessOrganization(t, tfeClient)
143+
t.Cleanup(orgCleanup)
144+
145+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
146+
147+
ws, err := tfeClient.Workspaces.Create(ctx, org.Name, tfe.WorkspaceCreateOptions{
148+
Name: tfe.String(fmt.Sprintf("tst-workspace-test-%d", rInt)),
149+
})
150+
if err != nil {
151+
t.Fatal(err)
152+
}
153+
154+
resource.Test(t, resource.TestCase{
155+
PreCheck: func() { testAccPreCheck(t) },
156+
ProtoV5ProviderFactories: testAccMuxedProviders,
157+
Steps: []resource.TestStep{
158+
{
159+
Config: testAccTFEAgentPoolDataSourceExcludedWorkspacesConfig(org.Name, rInt, ws.ID),
160+
Check: resource.ComposeAggregateTestCheckFunc(
161+
resource.TestCheckResourceAttrSet("data.tfe_agent_pool.foobar", "id"),
162+
resource.TestCheckResourceAttr(
163+
"data.tfe_agent_pool.foobar", "name", fmt.Sprintf("agent-pool-test-%d", rInt)),
164+
resource.TestCheckResourceAttr(
165+
"data.tfe_agent_pool.foobar", "organization", org.Name),
166+
resource.TestCheckResourceAttr(
167+
"data.tfe_agent_pool.foobar", "organization_scoped", "false"),
168+
resource.TestCheckResourceAttr(
169+
"data.tfe_agent_pool.foobar", "excluded_workspace_ids.0", ws.ID),
170+
),
171+
},
172+
},
173+
})
174+
}
175+
92176
func testAccTFEAgentPoolDataSourceConfig(organization string, rInt int) string {
93177
return fmt.Sprintf(`
94178
resource "tfe_agent_pool" "foobar" {
@@ -121,3 +205,43 @@ data "tfe_agent_pool" "foobar" {
121205
depends_on = [ tfe_agent_pool_allowed_workspaces.foobar ]
122206
}`, rInt, organization, workspaceID, organization)
123207
}
208+
209+
func testAccTFEAgentPoolDataSourceAllowedProjectsConfig(organization string, rInt int, projectID string) string {
210+
return fmt.Sprintf(`
211+
resource "tfe_agent_pool" "foobar" {
212+
name = "agent-pool-test-%d"
213+
organization = "%s"
214+
organization_scoped = false
215+
}
216+
217+
resource "tfe_agent_pool_allowed_projects" "foobar" {
218+
agent_pool_id = tfe_agent_pool.foobar.id
219+
allowed_project_ids = ["%s"]
220+
}
221+
222+
data "tfe_agent_pool" "foobar" {
223+
name = tfe_agent_pool.foobar.name
224+
organization = "%s"
225+
depends_on = [ tfe_agent_pool_allowed_projects.foobar ]
226+
}`, rInt, organization, projectID, organization)
227+
}
228+
229+
func testAccTFEAgentPoolDataSourceExcludedWorkspacesConfig(organization string, rInt int, workspaceID string) string {
230+
return fmt.Sprintf(`
231+
resource "tfe_agent_pool" "foobar" {
232+
name = "agent-pool-test-%d"
233+
organization = "%s"
234+
organization_scoped = false
235+
}
236+
237+
resource "tfe_agent_pool_excluded_workspaces" "foobar" {
238+
agent_pool_id = tfe_agent_pool.foobar.id
239+
excluded_workspace_ids = ["%s"]
240+
}
241+
242+
data "tfe_agent_pool" "foobar" {
243+
name = tfe_agent_pool.foobar.name
244+
organization = "%s"
245+
depends_on = [ tfe_agent_pool_excluded_workspaces.foobar ]
246+
}`, rInt, organization, workspaceID, organization)
247+
}

0 commit comments

Comments
 (0)