-
Notifications
You must be signed in to change notification settings - Fork 26
/
data_worker_pools.go
110 lines (92 loc) · 2.76 KB
/
data_worker_pools.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
package spacelift
import (
"context"
"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"
)
func dataWorkerPools() *schema.Resource {
return &schema.Resource{
Description: "" +
"`spacelift_worker_pools` represents the worker pools assigned to " +
"the Spacelift account.",
ReadContext: dataWorkerPoolsRead,
Schema: map[string]*schema.Schema{
"worker_pools": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"worker_pool_id": {
Type: schema.TypeString,
Description: "ID of the worker pool",
Computed: true,
},
"config": {
Type: schema.TypeString,
Description: "credentials necessary to connect WorkerPool's workers to the control plane",
Sensitive: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "name of the worker pool",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "description of the worker pool",
Computed: true,
},
"space_id": {
Type: schema.TypeString,
Description: "ID (slug) of the space the worker pool is in",
Computed: true,
},
},
},
},
},
}
}
func dataWorkerPoolsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
WorkerPools []*structs.WorkerPool `graphql:"workerPools()"`
}
variables := map[string]interface{}{}
if err := meta.(*internal.Client).Query(ctx, "WorkerPoolsRead", &query, variables); err != nil {
return diag.Errorf("could not query for worker pools: %v", err)
}
d.SetId("spacelift-worker-pools")
workerPools := query.WorkerPools
if workerPools == nil {
d.Set("worker_pools", nil)
return nil
}
wps := flattenDataWorkerPoolsList(workerPools)
if err := d.Set("worker_pools", wps); err != nil {
d.SetId("")
return diag.Errorf("could not set worker pools: %v", err)
}
return nil
}
func flattenDataWorkerPoolsList(workerPools []*structs.WorkerPool) []map[string]interface{} {
wps := make([]map[string]interface{}, len(workerPools))
for index, wp := range workerPools {
var description *string
if wp.Description != nil {
description = wp.Description
} else {
description = nil
}
wps[index] = map[string]interface{}{
"worker_pool_id": wp.ID,
"name": wp.Name,
"config": wp.Config,
"description": description,
"space_id": wp.Space,
}
}
return wps
}