/
data_source_workspace_ids.go
147 lines (121 loc) · 3.47 KB
/
data_source_workspace_ids.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package tfe
import (
"fmt"
"strings"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTFEWorkspaceIDs() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEWorkspaceIDsRead,
Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
AtLeastOneOf: []string{"names", "tag_names"},
},
"tag_names": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"exclude_tags": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
},
"ids": {
Type: schema.TypeMap,
Computed: true,
},
"full_names": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceTFEWorkspaceIDsRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the organization.
organization := d.Get("organization").(string)
// Create a map with all the names we are looking for.
var id string
names := make(map[string]bool)
for _, name := range d.Get("names").([]interface{}) {
// ignore empty strings
if name == nil {
continue
}
id += name.(string)
names[name.(string)] = true
}
isWildcard := names["*"]
// Create two maps to hold the results.
fullNames := make(map[string]string, len(names))
ids := make(map[string]string, len(names))
options := &tfe.WorkspaceListOptions{}
excludeTagLookupMap := make(map[string]bool)
var excludeTagBuf strings.Builder
for _, excludedTag := range d.Get("exclude_tags").(*schema.Set).List() {
if exTag, ok := excludedTag.(string); ok && len(strings.TrimSpace(exTag)) != 0 {
excludeTagLookupMap[exTag] = true
if excludeTagBuf.Len() > 0 {
excludeTagBuf.WriteByte(',')
}
excludeTagBuf.WriteString(exTag)
}
}
if excludeTagBuf.Len() > 0 {
options.ExcludeTags = excludeTagBuf.String()
}
// Create a search string with all the tag names we are looking for.
var tagSearchParts []string
for _, tagName := range d.Get("tag_names").([]interface{}) {
if name, ok := tagName.(string); ok && len(strings.TrimSpace(name)) != 0 {
id += name // add to the state id
tagSearchParts = append(tagSearchParts, name)
}
}
if len(tagSearchParts) > 0 {
tagSearch := strings.Join(tagSearchParts, ",")
options.Tags = tagSearch
}
hasOnlyTags := len(tagSearchParts) > 0 && len(names) == 0
for {
wl, err := tfeClient.Workspaces.List(ctx, organization, options)
if err != nil {
return fmt.Errorf("Error retrieving workspaces: %w", err)
}
for _, w := range wl.Items {
nameIncluded := isWildcard || names[w.Name]
// fallback for tfe instances that don't yet support exclude-tags
hasExcludedTag := false
for _, tag := range w.TagNames {
if _, ok := excludeTagLookupMap[tag]; ok {
hasExcludedTag = true
break
}
}
if (hasOnlyTags || nameIncluded) && !hasExcludedTag {
fullNames[w.Name] = organization + "/" + w.Name
ids[w.Name] = w.ID
}
}
// Exit the loop when we've seen all pages.
if wl.CurrentPage >= wl.TotalPages {
break
}
// Update the page number to get the next page.
options.PageNumber = wl.NextPage
}
d.Set("ids", ids)
d.Set("full_names", fullNames)
d.SetId(fmt.Sprintf("%s/%d", organization, schema.HashString(id)))
return nil
}