-
Notifications
You must be signed in to change notification settings - Fork 92
/
datasource.go
106 lines (86 loc) · 3.03 KB
/
datasource.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
package instance
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/linode/helper"
)
var filterConfig = helper.FilterConfig{
"group": {APIFilterable: true, TypeFunc: helper.FilterTypeString},
"id": {APIFilterable: true, TypeFunc: helper.FilterTypeInt},
"image": {APIFilterable: true, TypeFunc: helper.FilterTypeString},
"label": {APIFilterable: true, TypeFunc: helper.FilterTypeString},
"region": {APIFilterable: true, TypeFunc: helper.FilterTypeString},
// Tags must be filtered on the client
"tags": {TypeFunc: helper.FilterTypeString},
"status": {TypeFunc: helper.FilterTypeString},
"type": {TypeFunc: helper.FilterTypeString},
"watchdog_enabled": {TypeFunc: helper.FilterTypeBool},
}
func dataSourceInstance() *schema.Resource {
return &schema.Resource{
Schema: instanceDataSourceSchema,
}
}
func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: readDataSource,
Schema: map[string]*schema.Schema{
"filter": filterConfig.FilterSchema(),
"order_by": filterConfig.OrderBySchema(),
"order": filterConfig.OrderSchema(),
"instances": {
Type: schema.TypeList,
Description: "The returned list of Instances.",
Computed: true,
Elem: dataSourceInstance(),
},
},
}
}
func readDataSource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*helper.ProviderMeta).Client
filterID, err := filterConfig.GetFilterID(d)
if err != nil {
return diag.Errorf("failed to generate filter id: %s", err)
}
filter, err := filterConfig.ConstructFilterString(d)
if err != nil {
return diag.Errorf("failed to construct filter: %s", err)
}
instances, err := client.ListInstances(ctx, &linodego.ListOptions{
Filter: filter,
})
if err != nil {
return diag.Errorf("failed to get instances: %s", err)
}
instanceIDMap := make(map[int]linodego.Instance, len(instances))
// Create a list of filterable instance maps
flattenedInstances := make([]interface{}, len(instances))
for i, instance := range instances {
instance := instance
instanceIDMap[instance.ID] = instance
instanceMap, err := flattenInstanceSimple(&instance)
if err != nil {
return diag.Errorf("failed to translate instance to filterable map: %s", err)
}
flattenedInstances[i] = instanceMap
}
instancesFiltered, err := filterConfig.FilterResults(d, flattenedInstances)
if err != nil {
return diag.Errorf("failed to filter returned instances: %s", err)
}
// Fully populate returned instances
for i, instance := range instancesFiltered {
instanceObject := instanceIDMap[instance["id"].(int)]
instanceMap, err := flattenInstance(ctx, &client, &instanceObject)
if err != nil {
return diag.Errorf("failed to translate instance to map: %s", err)
}
instancesFiltered[i] = instanceMap
}
d.SetId(filterID)
d.Set("instances", instancesFiltered)
return nil
}