-
Notifications
You must be signed in to change notification settings - Fork 26
/
data_contexts.go
150 lines (125 loc) · 3.72 KB
/
data_contexts.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
148
149
150
package spacelift
import (
"context"
"slices"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs/search/predicates"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/validations"
)
func dataContexts() *schema.Resource {
return &schema.Resource{
Description: "" +
"`spacelift_contexts` represents all the contexts in the Spacelift " +
"account visible to the API user.",
ReadContext: dataContextsRead,
Schema: map[string]*schema.Schema{
"labels": predicates.StringField("Require contexts to have one of the labels", 0),
"contexts": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"context_id": {
Type: schema.TypeString,
Description: "immutable ID (slug) of the context",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"description": {
Type: schema.TypeString,
Description: "free-form context description for users",
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "name of the context",
Computed: true,
},
"space_id": {
Type: schema.TypeString,
Description: "ID (slug) of the space the context is in",
Computed: true,
},
},
},
},
},
}
}
func dataContextsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
Contexts []*structs.Context `graphql:"contexts()"`
}
variables := map[string]interface{}{}
if err := meta.(*internal.Client).Query(ctx, "ContextsRead", &query, variables); err != nil {
return diag.Errorf("could not query for contexts: %v", err)
}
d.SetId("spacelift-contexts")
contexts := query.Contexts
if contexts == nil {
d.Set("contexts", nil)
return nil
}
var labelFilters [][]string
for _, labelFilter := range d.Get("labels").([]interface{}) {
possibleValues := labelFilter.(map[string]interface{})["any_of"]
if possibleValues == nil {
continue
}
var labels []string
for _, label := range possibleValues.([]interface{}) {
labels = append(labels, label.(string))
}
labelFilters = append(labelFilters, labels)
}
wps := flattenDataContextsList(contexts, labelFilters)
if err := d.Set("contexts", wps); err != nil {
d.SetId("")
return diag.Errorf("could not set contexts: %v", err)
}
return nil
}
func flattenDataContextsList(contexts []*structs.Context, labelFilters [][]string) []map[string]interface{} {
wps := []map[string]interface{}{}
for _, ctx := range contexts {
if !matchesLabels(ctx.Labels, labelFilters) {
continue
}
wps = append(wps, map[string]interface{}{
"context_id": ctx.ID,
"description": ctx.Description,
"labels": ctx.Labels,
"name": ctx.Name,
"space_id": ctx.Space,
})
}
return wps
}
func matchesLabels(labelSet []string, labelFilters [][]string) bool {
if len(labelFilters) == 0 {
return true
}
for _, labelFilter := range labelFilters {
if len(labelFilter) == 0 {
continue
}
var matchesFilter bool
for _, label := range labelFilter {
if slices.Contains(labelSet, label) {
matchesFilter = true
}
}
if !matchesFilter {
return false
}
}
return true
}