Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow agent pools to be imported by name #561

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

ENHANCEMENTS:
* 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))

## 0.33.0 (July 8th, 2022)
Expand Down
38 changes: 38 additions & 0 deletions tfe/agent_pool_helpers.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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)
}
JarrettSpiker marked this conversation as resolved.
Show resolved Hide resolved

return []*schema.ResourceData{d}, nil
}
6 changes: 6 additions & 0 deletions tfe/resource_tfe_agent_pool_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
```