-
Notifications
You must be signed in to change notification settings - Fork 45
/
data_source_metal_devices.go
99 lines (87 loc) · 2.94 KB
/
data_source_metal_devices.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
package equinix
import (
"context"
"fmt"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/equinix/terraform-provider-equinix/internal/datalist"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceMetalDevices() *schema.Resource {
dsmd := dataSourceMetalDevice()
sch := dsmd.Schema
for _, v := range sch {
if v.Optional {
v.Optional = false
}
if v.ConflictsWith != nil {
v.ConflictsWith = nil
}
}
dataListConfig := &datalist.ResourceConfig{
RecordSchema: sch,
ResultAttributeName: "devices",
ResultAttributeDescription: "List of devices that match specified filters",
FlattenRecord: flattenDevice,
GetRecords: getDevices,
ExtraQuerySchema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Description: "The id of the project to query for devices",
Optional: true,
ConflictsWith: []string{"organization_id"},
},
"organization_id": {
Type: schema.TypeString,
Description: "The id of the organization to query for devices",
Optional: true,
ConflictsWith: []string{"project_id"},
},
"search": {
Type: schema.TypeString,
Description: "Search string to filter devices by hostname, description, short_id, reservation short_id, tags, plan name, plan slug, facility code, facility name, operating system name, operating system slug, IP addresses.",
Optional: true,
},
},
}
return datalist.NewResource(dataListConfig)
}
func getDevices(ctx context.Context, d *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
client := meta.(*config.Config).NewMetalClientForSDK(d)
projectID := extra["project_id"].(string)
orgID := extra["organization_id"].(string)
if (len(projectID) == 0) && (len(orgID) == 0) {
return nil, fmt.Errorf("one of project_id or organization_id must be specified")
}
search := extra["search"].(string)
var devices *metalv1.DeviceList
devicesIf := []interface{}{}
var err error
if len(projectID) > 0 {
query := client.DevicesApi.FindProjectDevices(
ctx, projectID).Include(deviceCommonIncludes)
if len(search) > 0 {
query = query.Search(search)
}
devices, _, err = query.Execute()
}
if len(orgID) > 0 {
query := client.DevicesApi.FindOrganizationDevices(
ctx, orgID).Include(deviceCommonIncludes)
if len(search) > 0 {
query = query.Search(search)
}
devices, _, err = query.Execute()
}
for _, d := range devices.Devices {
devicesIf = append(devicesIf, d)
}
return devicesIf, err
}
func flattenDevice(rawDevice interface{}, meta interface{}, extra map[string]interface{}) (map[string]interface{}, error) {
device, ok := rawDevice.(metalv1.Device)
if !ok {
return nil, fmt.Errorf("expected device to be of type *metalv1.Device, got %T", rawDevice)
}
return getDeviceMap(device), nil
}