diff --git a/CHANGELOG.md b/CHANGELOG.md index c89b57c49..e3f3bb7ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ BUG FIXES: FEATURES: * d/agent_pool: Improve efficiency of reading agent pool data when the target organization has more than 20 agent pools ([#508](https://github.com/hashicorp/terraform-provider-tfe/pull/508)) +* r/agent_pool: Agent Pools can now be imported using `/` * Added warning logs for 404 error responses ([#538](https://github.com/hashicorp/terraform-provider-tfe/pull/538)) * r/tfe_registry_module: Add ability to create both public and private `registry_modules` without VCS. ([#546](https://github.com/hashicorp/terraform-provider-tfe/pull/546)) diff --git a/tfe/agent_pool_helpers.go b/tfe/agent_pool_helpers.go new file mode 100644 index 000000000..01a6724de --- /dev/null +++ b/tfe/agent_pool_helpers.go @@ -0,0 +1,38 @@ +package tfe + +import ( + "fmt" + + tfe "github.com/hashicorp/go-tfe" +) + +func fetchAgentPoolID(orgName string, poolName string, client *tfe.Client) (string, error) { + // to reduce the number of pages returned, search based on the name. TFE instances which + // do not support agent pool search will just ignore the query parameter + options := tfe.AgentPoolListOptions{ + Query: poolName, + } + + for { + l, err := client.AgentPools.List(ctx, orgName, &options) + if err != nil { + return "", fmt.Errorf("Error retrieving agent pools: %w", err) + } + + for _, k := range l.Items { + if k.Name == poolName { + return k.ID, nil + } + } + + // Exit the loop when we've seen all pages. + if l.CurrentPage >= l.TotalPages { + break + } + + // Update the page number to get the next page. + options.PageNumber = l.NextPage + } + + return "", tfe.ErrResourceNotFound +} diff --git a/tfe/data_source_agent_pool.go b/tfe/data_source_agent_pool.go index 2fdeb58d5..1e0cf2887 100644 --- a/tfe/data_source_agent_pool.go +++ b/tfe/data_source_agent_pool.go @@ -1,8 +1,6 @@ package tfe import ( - "fmt" - tfe "github.com/hashicorp/go-tfe" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -32,34 +30,10 @@ func dataSourceTFEAgentPoolRead(d *schema.ResourceData, meta interface{}) error name := d.Get("name").(string) organization := d.Get("organization").(string) - // Create an options struct. - // to reduce the number of pages returned, search based on the name. TFE instances which - // do not support agent pool search will just ignore the query parameter - options := tfe.AgentPoolListOptions{ - Query: name, + id, err := fetchAgentPoolID(organization, name, tfeClient) + if err != nil { + return err } - - for { - l, err := tfeClient.AgentPools.List(ctx, organization, &options) - if err != nil { - return fmt.Errorf("Error retrieving agent pools: %w", err) - } - - for _, k := range l.Items { - if k.Name == name { - d.SetId(k.ID) - return nil - } - } - - // Exit the loop when we've seen all pages. - if l.CurrentPage >= l.TotalPages { - break - } - - // Update the page number to get the next page. - options.PageNumber = l.NextPage - } - - return fmt.Errorf("Could not find agent pool %s/%s", organization, name) + d.SetId(id) + return nil } diff --git a/tfe/resource_tfe_agent_pool.go b/tfe/resource_tfe_agent_pool.go index f1e961624..4f84b942c 100644 --- a/tfe/resource_tfe_agent_pool.go +++ b/tfe/resource_tfe_agent_pool.go @@ -3,6 +3,7 @@ package tfe import ( "fmt" "log" + "strings" tfe "github.com/hashicorp/go-tfe" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -15,7 +16,7 @@ func resourceTFEAgentPool() *schema.Resource { Update: resourceTFEAgentPoolUpdate, Delete: resourceTFEAgentPoolDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceTFEAgentPoolImporter, }, Schema: map[string]*schema.Schema{ @@ -109,3 +110,27 @@ func resourceTFEAgentPoolDelete(d *schema.ResourceData, meta interface{}) error return nil } + +func resourceTFEAgentPoolImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + tfeClient := meta.(*tfe.Client) + + s := strings.Split(d.Id(), "/") + if len(s) >= 3 { + return nil, fmt.Errorf( + "invalid agent pool input format: %s (expected / or )", + d.Id(), + ) + } else if len(s) == 2 { + org := s[0] + poolName := s[1] + poolID, err := fetchAgentPoolID(org, poolName, tfeClient) + if err != nil { + return nil, fmt.Errorf( + "error retrieving agent pool with name %s from organization %s %w", poolName, org, err) + } + + d.SetId(poolID) + } + + return []*schema.ResourceData{d}, nil +} diff --git a/tfe/resource_tfe_agent_pool_test.go b/tfe/resource_tfe_agent_pool_test.go index b31019c17..91a5a610f 100644 --- a/tfe/resource_tfe_agent_pool_test.go +++ b/tfe/resource_tfe_agent_pool_test.go @@ -94,6 +94,12 @@ func TestAccTFEAgentPool_import(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + ResourceName: "tfe_agent_pool.foobar", + ImportState: true, + ImportStateId: fmt.Sprintf("tst-terraform-%d/agent-pool-test", rInt), + ImportStateVerify: true, + }, }, }) } diff --git a/website/docs/r/agent_pool.html.markdown b/website/docs/r/agent_pool.html.markdown index 4d281c127..fa42318e3 100644 --- a/website/docs/r/agent_pool.html.markdown +++ b/website/docs/r/agent_pool.html.markdown @@ -47,8 +47,12 @@ The following arguments are supported: ## Import -Agent pools can be imported; use `` as the import ID. For example: +Agent pools can be imported; use `` or `/` as the import ID. For example: ```shell terraform import tfe_agent_pool.test apool-rW0KoLSlnuNb5adB ``` + +```shell +terraform import tfe_workspace.test my-org-name/my-agent-pool-name +``` \ No newline at end of file