Skip to content

Commit 92e9804

Browse files
committed
[TF-27883] Add agent execution mode and pool to test config
Introduces 'agent_execution_mode' and 'agent_pool_id' fields to the registry module test_config schema, supporting both agent and remote execution modes. Adds validation to prevent agent_pool_id from being set when execution mode is 'remote', updates create, update, and read logic, and provides comprehensive acceptance tests for new behaviors. Also updates dependencies to latest versions.
1 parent 02ebd7e commit 92e9804

File tree

4 files changed

+439
-6
lines changed

4 files changed

+439
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ BREAKING CHANGES:
2121
All resources have been upgraded to use the [latest Terraform plugin protocol](https://developer.hashicorp.com/terraform/plugin/terraform-plugin-protocol). This provider now requires a Terraform version of v1.0.0 or later.
2222

2323
ENHANCEMENTS:
24+
* `r/tfe_registry_module`: Add support for `agent_execution_mode` and `agent_pool_id` attributes in `test_config` block to enable running registry module tests on custom agent pools. By @hashimoon [#1832](https://github.com/hashicorp/terraform-provider-tfe/pull/1832)
2425
* `r/tfe_oauth_client`: The `oauth_token` attribute no longer triggers resource replacement unless combined with other replacement-triggering attributes. Use `terraform apply -replace` to force replacement. By @lilincmu [#1825](https://github.com/hashicorp/terraform-provider-tfe/pull/1825)
2526

2627
FEATURES:

internal/provider/resource_tfe_registry_module.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ func resourceTFERegistryModule() *schema.Resource {
3333
},
3434

3535
CustomizeDiff: func(c context.Context, d *schema.ResourceDiff, meta interface{}) error {
36-
return validateVcsRepo(d)
36+
if err := validateVcsRepo(d); err != nil {
37+
return err
38+
}
39+
return validateTestConfig(d)
3740
},
3841
Schema: map[string]*schema.Schema{
3942
"organization": {
@@ -146,6 +149,19 @@ func resourceTFERegistryModule() *schema.Resource {
146149
Type: schema.TypeBool,
147150
Optional: true,
148151
},
152+
"agent_execution_mode": {
153+
Type: schema.TypeString,
154+
Optional: true,
155+
Computed: true,
156+
ValidateFunc: validation.StringInSlice(
157+
[]string{"agent", "remote"},
158+
false,
159+
),
160+
},
161+
"agent_pool_id": {
162+
Type: schema.TypeString,
163+
Optional: true,
164+
},
149165
},
150166
},
151167
},
@@ -216,6 +232,16 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
216232
options.TestConfig = &tfe.RegistryModuleTestConfigOptions{
217233
TestsEnabled: tfe.Bool(testsEnabled),
218234
}
235+
236+
if agentExecutionMode, ok := testConfig["agent_execution_mode"].(string); ok && agentExecutionMode != "" {
237+
mode := tfe.AgentExecutionMode(agentExecutionMode)
238+
options.TestConfig.AgentExecutionMode = &mode
239+
}
240+
241+
// Handle agent pool ID - only set if explicitly provided and not empty
242+
if agentPoolID, ok := testConfig["agent_pool_id"].(string); ok && agentPoolID != "" {
243+
options.TestConfig.AgentPoolID = tfe.String(agentPoolID)
244+
}
219245
}
220246

221247
log.Printf("[DEBUG] Create registry module from repository %s", *options.VCSRepo.Identifier)
@@ -357,6 +383,13 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
357383
if testsEnabled, ok := testConfig["tests_enabled"].(bool); ok {
358384
options.TestConfig.TestsEnabled = tfe.Bool(testsEnabled)
359385
}
386+
387+
if agentExecutionMode, ok := testConfig["agent_execution_mode"].(string); ok && agentExecutionMode != "" {
388+
mode := tfe.AgentExecutionMode(agentExecutionMode)
389+
options.TestConfig.AgentExecutionMode = &mode
390+
}
391+
392+
handleAgentPoolID(testConfig, d, options.TestConfig)
360393
}
361394

362395
err = retry.Retry(time.Duration(5)*time.Minute, func() *retry.RetryError {
@@ -434,6 +467,13 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err
434467
"tests_enabled": registryModule.TestConfig.TestsEnabled,
435468
}
436469

470+
if registryModule.TestConfig.AgentExecutionMode != nil && *registryModule.TestConfig.AgentExecutionMode != "" {
471+
testConfigValues["agent_execution_mode"] = *registryModule.TestConfig.AgentExecutionMode
472+
}
473+
474+
if registryModule.TestConfig.AgentPoolID != nil && *registryModule.TestConfig.AgentPoolID != "" {
475+
testConfigValues["agent_pool_id"] = *registryModule.TestConfig.AgentPoolID
476+
}
437477
testConfig = append(testConfig, testConfigValues)
438478
}
439479

@@ -530,3 +570,49 @@ func validateVcsRepo(d *schema.ResourceDiff) error {
530570

531571
return nil
532572
}
573+
574+
func validateTestConfig(d *schema.ResourceDiff) error {
575+
testConfig, ok := d.GetRawConfig().AsValueMap()["test_config"]
576+
if !ok || testConfig.LengthInt() == 0 {
577+
return nil
578+
}
579+
580+
testConfigValue := testConfig.AsValueSlice()[0]
581+
582+
// Check if test_config block is empty (no tests_enabled field)
583+
testsEnabledValue := testConfigValue.GetAttr("tests_enabled")
584+
if testsEnabledValue.IsNull() {
585+
return fmt.Errorf("tests_enabled must be provided when configuring a test_config")
586+
}
587+
588+
agentExecutionModeValue := testConfigValue.GetAttr("agent_execution_mode")
589+
agentPoolIDValue := testConfigValue.GetAttr("agent_pool_id")
590+
591+
// Skip validation if values are unknown (during plan phase)
592+
if !agentExecutionModeValue.IsKnown() || !agentPoolIDValue.IsKnown() {
593+
return nil
594+
}
595+
596+
if !agentExecutionModeValue.IsNull() && !agentPoolIDValue.IsNull() {
597+
executionMode := agentExecutionModeValue.AsString()
598+
agentPoolID := agentPoolIDValue.AsString()
599+
600+
if executionMode == "remote" && agentPoolID != "" {
601+
return fmt.Errorf("agent_pool_id cannot be set when agent_execution_mode is 'remote'")
602+
}
603+
}
604+
605+
return nil
606+
}
607+
608+
func handleAgentPoolID(testConfig map[string]interface{}, d *schema.ResourceData, options *tfe.RegistryModuleTestConfigOptions) {
609+
if agentPoolID, ok := testConfig["agent_pool_id"].(string); ok {
610+
if agentPoolID != "" {
611+
options.AgentPoolID = tfe.String(agentPoolID)
612+
} else if d.HasChange("test_config.0.agent_pool_id") {
613+
options.AgentPoolID = tfe.String("")
614+
}
615+
} else if d.HasChange("test_config.0.agent_pool_id") {
616+
options.AgentPoolID = tfe.String("")
617+
}
618+
}

0 commit comments

Comments
 (0)