Skip to content

Commit

Permalink
Merge pull request #561 from hashicorp/jspiker/import-pool-by-name
Browse files Browse the repository at this point in the history
Allow agent pools to be imported by name
  • Loading branch information
JarrettSpiker committed Aug 2, 2022
2 parents bdb7444 + 9790800 commit 08c9a7d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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 `<ORGANIZATION NAME>/<AGENT POOL NAME>`
* 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))

Expand Down
38 changes: 38 additions & 0 deletions 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
}
36 changes: 5 additions & 31 deletions 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"
)
Expand Down Expand Up @@ -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
}
27 changes: 26 additions & 1 deletion tfe/resource_tfe_agent_pool.go
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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 <ORGANIZATION>/<AGENT POOL NAME> or <AGENT POOL ID>)",
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
}
6 changes: 6 additions & 0 deletions tfe/resource_tfe_agent_pool_test.go
Expand Up @@ -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,
},
},
})
}
Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/agent_pool.html.markdown
Expand Up @@ -47,8 +47,12 @@ The following arguments are supported:

## Import

Agent pools can be imported; use `<AGENT POOL ID>` as the import ID. For example:
Agent pools can be imported; use `<AGENT POOL ID>` or `<ORGANIZATION NAME>/<AGENT POOL NAME>` 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
```

0 comments on commit 08c9a7d

Please sign in to comment.