Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `scalr_wokspace_ids` data source must request only those attributes it uses. ([#437](https://github.com/Scalr/terraform-provider-scalr/pull/437))

### Added

- `scalr_agent_pool`: new attribute `environments` ([#438](https://github.com/Scalr/terraform-provider-scalr/pull/438))

## [3.4.0] - 2025-07-11

### Added
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/agent_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ data "scalr_agent_pool" "example2" {

### Read-Only

- `environments` (Set of String) The list of the environment identifiers that the agent pool is shared to, or `["*"]` if shared with all environments.
- `workspace_ids` (List of String) The list of IDs of linked workspaces.
1 change: 1 addition & 0 deletions docs/resources/agent_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "scalr_agent_pool" "default" {

- `account_id` (String, Deprecated) ID of the account.
- `environment_id` (String, Deprecated) ID of the environment.
- `environments` (Set of String) The list of the environment identifiers that the agent pool is shared to. Use `["*"]` to share with all environments.
- `vcs_enabled` (Boolean) Indicates whether the VCS support is enabled for agents in the pool.

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
github.com/hashicorp/terraform-svchost v0.1.1
github.com/scalr/go-scalr v0.0.0-20250712001519-9f42032f4be3
github.com/scalr/go-scalr v0.0.0-20250714124754-189ab0cece4d
)

require (
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,8 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/scalr/go-scalr v0.0.0-20250709084620-8b3fc4d46833 h1:ecfQzig7ry0tSQEPo5/dIG8kLP3qjWeI6MpKt5lppnY=
github.com/scalr/go-scalr v0.0.0-20250709084620-8b3fc4d46833/go.mod h1:Om3UIkHGeOcUK72OzjWI1GlS/hFyY8RRLxgbO0ZPsn4=
github.com/scalr/go-scalr v0.0.0-20250712001519-9f42032f4be3 h1:adgID8xOmBR3uu7gpUo0mZlzNEkhpsQkPdPfCDwSIsw=
github.com/scalr/go-scalr v0.0.0-20250712001519-9f42032f4be3/go.mod h1:Om3UIkHGeOcUK72OzjWI1GlS/hFyY8RRLxgbO0ZPsn4=
github.com/scalr/go-scalr v0.0.0-20250714124754-189ab0cece4d h1:nwJNe//SghT1/80fnuMr9OEZ8hmqAPv1rHeXa91mKJ4=
github.com/scalr/go-scalr v0.0.0-20250714124754-189ab0cece4d/go.mod h1:Om3UIkHGeOcUK72OzjWI1GlS/hFyY8RRLxgbO0ZPsn4=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
Expand Down
17 changes: 17 additions & 0 deletions internal/provider/data_source_scalr_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func dataSourceScalrAgentPool() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"environments": {
Description: "The list of the environment identifiers that the agent pool is shared to, or `[\"*\"]` if shared with all environments.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand Down Expand Up @@ -115,6 +122,16 @@ func dataSourceScalrAgentPoolRead(ctx context.Context, d *schema.ResourceData, m
log.Printf("[DEBUG] agent pool %s workspaces: %+v", agentPool.ID, workspaces)
_ = d.Set("workspace_ids", workspaces)
}

if agentPool.IsShared {
_ = d.Set("environments", []string{"*"})
} else {
environmentIDs := make([]string, 0)
for _, environment := range agentPool.Environments {
environmentIDs = append(environmentIDs, environment.ID)
}
_ = d.Set("environments", environmentIDs)
}
_ = d.Set("vcs_enabled", agentPool.VcsEnabled)
_ = d.Set("name", agentPool.Name)
d.SetId(agentPool.ID)
Expand Down
14 changes: 13 additions & 1 deletion internal/provider/data_source_scalr_agent_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func TestAccScalrAgentPoolDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.scalr_agent_pool.test", "id"),
resource.TestCheckResourceAttr("data.scalr_agent_pool.test", "name", "ds-agent_pool-test-acc"),
resource.TestCheckResourceAttr("data.scalr_agent_pool.test", "account_id", defaultAccount),
resource.TestCheckResourceAttr(
"data.scalr_agent_pool.test", "environments.#", "1"),
resource.TestCheckResourceAttrPair(
"data.scalr_agent_pool.test",
"environments.0",
"scalr_environment.test-new",
"id"),
),
},
{
Expand Down Expand Up @@ -77,9 +84,14 @@ func TestAccScalrAgentPoolDataSource_basic_env(t *testing.T) {
}

var testAccScalrAgentPoolAccountDataSourceByIDConfig = fmt.Sprintf(`
resource scalr_environment test-new {
name = "test-env-new-for-pool"
account_id = "%s"
}

resource "scalr_agent_pool" "test" {
name = "ds-agent_pool-test-acc"
account_id = "%s"
environments = [scalr_environment.test-new.id]
}

data "scalr_agent_pool" "test" {
Expand Down
90 changes: 80 additions & 10 deletions internal/provider/resource_scalr_agent_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func resourceScalrAgentPool() *schema.Resource {
Optional: true,
Default: false,
},

"environments": {
Description: "The list of the environment identifiers that the agent pool is shared to. Use `[\"*\"]` to share with all environments.",
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -69,9 +77,34 @@ func resourceScalrAgentPoolCreate(ctx context.Context, d *schema.ResourceData, m
VcsEnabled: ptr(vcsEnabled),
}

if envID, ok := d.GetOk("environment_id"); ok {
if v, ok := d.GetOk("environment_id"); ok {
envID = v.(string)
options.Environment = &scalr.Environment{
ID: envID.(string),
ID: v.(string),
}
}

if environmentsI, ok := d.GetOk("environments"); ok {
environments := environmentsI.(*schema.Set).List()
if (len(environments) == 1) && (environments[0].(string) == "*") {
options.IsShared = ptr(true)
} else if len(environments) > 0 {
environmentValues := make([]*scalr.Environment, 0)
for _, env := range environments {
if env.(string) == "*" {
return diag.Errorf(
"You cannot simultaneously enable the agent poool for all and a limited list of environments. Please remove either wildcard or environment identifiers.",
)
}
environmentValues = append(environmentValues, &scalr.Environment{ID: env.(string)})
}
options.Environments = environmentValues
options.IsShared = ptr(false)
if envID != "" {
return diag.Errorf(
"Environmnet scope agent pool cannot have environments linkage.",
)
}
}
}

Expand Down Expand Up @@ -109,6 +142,16 @@ func resourceScalrAgentPoolRead(ctx context.Context, d *schema.ResourceData, met
} else {
_ = d.Set("environment_id", nil)
}

if agentPool.IsShared {
_ = d.Set("environments", []string{"*"})
} else {
environmentIDs := make([]string, 0)
for _, environment := range agentPool.Environments {
environmentIDs = append(environmentIDs, environment.ID)
}
_ = d.Set("environments", environmentIDs)
}
return nil
}

Expand All @@ -121,18 +164,45 @@ func resourceScalrAgentPoolUpdate(ctx context.Context, d *schema.ResourceData, m
return diag.Errorf("Error updating agentPool %s: %v", id, "vcs_enabled attribute is readonly.")
}

if d.HasChange("name") {
// Create a new options struct
options := scalr.AgentPoolUpdateOptions{
Name: ptr(d.Get("name").(string)),
options := scalr.AgentPoolUpdateOptions{
Name: ptr(d.Get("name").(string)),
}

if environmentsI, ok := d.GetOk("environments"); ok {
environments := environmentsI.(*schema.Set).List()
if (len(environments) == 1) && (environments[0].(string) == "*") {
options.IsShared = ptr(true)
options.Environments = make([]*scalr.Environment, 0)
} else {
options.IsShared = ptr(false)
environmentValues := make([]*scalr.Environment, 0)
for _, env := range environments {
if env.(string) == "*" {
return diag.Errorf(
"You cannot simultaneously enable the agent pool for all and a limited list of environments. Please remove either wildcard or environment identifiers.",
)
}
environmentValues = append(environmentValues, &scalr.Environment{ID: env.(string)})
}
options.Environments = environmentValues
}

log.Printf("[DEBUG] Update agent pool %s", id)
_, err := scalrClient.AgentPools.Update(ctx, id, options)
if err != nil {
if _, ok := d.GetOk("environment_id"); ok {
return diag.Errorf(
"Error updating agentPool %s: %v", id, err)
"Environmnet scope agent pool cannot have environments linkage.",
)
}

} else {
options.IsShared = ptr(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I unshare the agent pool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty environments = [] attribute

Copy link
Member

@petroprotsakh petroprotsakh Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't work, it will fall into this conditional block and pass "is-shared": true in the payload

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, added default value of ["*"] shared agent pool to preserve current behavior.

options.Environments = make([]*scalr.Environment, 0)
}

log.Printf("[DEBUG] Update agent pool %s", id)
_, err := scalrClient.AgentPools.Update(ctx, id, options)
if err != nil {
return diag.Errorf(
"Error updating agentPool %s: %v", id, err)
}

return resourceScalrAgentPoolRead(ctx, d, meta)
Expand Down
10 changes: 2 additions & 8 deletions internal/provider/resource_scalr_agent_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestAccScalrAgentPool_basic(t *testing.T) {
"scalr_agent_pool.test", "name", fmt.Sprintf("agent_pool-test-%d", rInt),
),
resource.TestCheckResourceAttr("scalr_agent_pool.test", "account_id", defaultAccount),
resource.TestCheckResourceAttr("scalr_agent_pool.test", "environments.0", "*"),
),
},
},
Expand Down Expand Up @@ -131,16 +132,9 @@ func testAccCheckScalrAgentPoolDestroy(s *terraform.State) error {

func testAccScalrAgentPoolBasic(rInt int) string {
return fmt.Sprintf(`
resource "scalr_environment" "test" {
name = "agent_pool-test-%d"
account_id = "%s"

}

resource "scalr_agent_pool" "test" {
name = "agent_pool-test-%d"
environment_id = scalr_environment.test.id
}`, rInt, defaultAccount, rInt)
}`, rInt)
}

func testAccScalrAgentPoolUpdate() string {
Expand Down
Loading