/
data_source_metal_hardware_reservation.go
124 lines (111 loc) · 3.53 KB
/
data_source_metal_hardware_reservation.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
package equinix
import (
"fmt"
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"
"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/packethost/packngo"
)
func dataSourceMetalHardwareReservation() *schema.Resource {
return &schema.Resource{
Read: dataSourceMetalHardwareReservationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "ID of the hardware reservation to look up",
},
"device_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "UUID of device occupying the reservation",
},
"short_id": {
Type: schema.TypeString,
Computed: true,
Description: "Reservation short ID",
},
"project_id": {
Type: schema.TypeString,
Computed: true,
Description: "UUID of project this reservation is scoped to",
},
"plan": {
Type: schema.TypeString,
Computed: true,
Description: "Plan type for the reservation",
},
"facility": {
Type: schema.TypeString,
Computed: true,
Description: "Facility for the reservation",
Deprecated: "Use metro instead of facility. For more information, read the migration guide: https://registry.terraform.io/providers/equinix/equinix/latest/docs/guides/migration_guide_facilities_to_metros_devices",
},
"provisionable": {
Type: schema.TypeBool,
Computed: true,
Description: "Flag indicating whether the reserved server is provisionable or not. Spare devices can't be provisioned unless they are activated first",
},
"spare": {
Type: schema.TypeBool,
Computed: true,
Description: "Flag indicating whether the Hardware Reservation is a spare. Spare Hardware Reservations are used when a Hardware Reservations requires service from Metal Equinix",
},
"switch_uuid": {
Type: schema.TypeString,
Computed: true,
Description: "Switch short ID, can be used to determine if two devices are connected to the same switch",
},
},
}
}
func dataSourceMetalHardwareReservationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*config.Config).Metal
hrIdRaw, hrIdOk := d.GetOk("id")
dIdRaw, dIdOk := d.GetOk("device_id")
if dIdOk == hrIdOk {
return fmt.Errorf("You must set one of id and device_id")
}
var deviceId string
var hr *packngo.HardwareReservation
if dIdOk {
deviceId = dIdRaw.(string)
includes := []string{
"hardware_reservation.project",
"hardware_reservation.facility",
}
d, _, err := client.Devices.Get(deviceId, &packngo.GetOptions{Includes: includes})
if err != nil {
return err
}
if d.HardwareReservation == nil {
return fmt.Errorf("Device %s is not in a hardware reservation", deviceId)
}
hr = d.HardwareReservation
} else {
var err error
hr, _, err = client.HardwareReservations.Get(
hrIdRaw.(string),
&packngo.GetOptions{Includes: []string{"project", "facility", "device"}})
if err != nil {
return err
}
if hr.Device != nil {
deviceId = hr.Device.ID
}
}
m := map[string]interface{}{
"short_id": hr.ShortID,
"project_id": hr.Project.ID,
"device_id": deviceId,
"plan": hr.Plan.Slug,
"facility": hr.Facility.Code,
"provisionable": hr.Provisionable,
"spare": hr.Spare,
"switch_uuid": hr.SwitchUUID,
}
d.SetId(hr.ID)
return equinix_schema.SetMap(d, m)
}