-
Notifications
You must be signed in to change notification settings - Fork 148
/
data_source_agent_pool.go
65 lines (52 loc) · 1.41 KB
/
data_source_agent_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tfe
import (
"fmt"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTFEAgentPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEAgentPoolRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func dataSourceTFEAgentPoolRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the name and organization.
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,
}
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)
}