-
Notifications
You must be signed in to change notification settings - Fork 147
/
data_source_variable_set.go
116 lines (94 loc) · 2.75 KB
/
data_source_variable_set.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
package tfe
import (
"fmt"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTFEVariableSet() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEVariableSetRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"global": {
Type: schema.TypeBool,
Computed: true,
},
"workspace_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"variable_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceTFEVariableSetRead(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.
options := tfe.VariableSetListOptions{}
for {
// Variable Set relations, vars and workspaces, are omitted from the querying until
// we find the desired variable set.
l, err := tfeClient.VariableSets.List(ctx, organization, &options)
if err != nil {
if err == tfe.ErrResourceNotFound {
return fmt.Errorf("could not find variable set%s/%s", organization, name)
}
return fmt.Errorf("Error retrieving variable set: %v", err)
}
for _, vs := range l.Items {
if vs.Name == name {
d.Set("name", vs.Name)
d.Set("description", vs.Description)
d.Set("global", vs.Global)
//Only now include vars and workspaces to cut down on request load.
readOptions := tfe.VariableSetReadOptions{
Include: &[]tfe.VariableSetIncludeOpt{tfe.VariableSetWorkspaces, tfe.VariableSetVars},
}
vs, err = tfeClient.VariableSets.Read(ctx, vs.ID, &readOptions)
if err != nil {
return fmt.Errorf("Error retrieving variable set relations: %v", err)
}
var workspaces []interface{}
for _, workspace := range vs.Workspaces {
workspaces = append(workspaces, workspace.ID)
}
d.Set("workspace_ids", workspaces)
var variables []interface{}
for _, variable := range vs.Variables {
variables = append(variables, variable.ID)
}
d.Set("variable_ids", variables)
d.SetId(vs.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 variable set %s/%s", organization, name)
}