forked from equinix/terraform-provider-equinix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_metal_metro.go
85 lines (75 loc) · 1.94 KB
/
data_source_metal_metro.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
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 dataSourceMetalMetro() *schema.Resource {
return &schema.Resource{
Read: dataSourceMetalMetroRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "The ID of this Metro",
Computed: true,
},
"code": {
Type: schema.TypeString,
Description: "The code of the Metro to match",
Required: true,
},
"country": {
Type: schema.TypeString,
Description: "The country of this Metro",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of this Metro",
Computed: true,
},
"capacity": capacitySchema(),
},
}
}
func dataSourceMetalMetroRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*config.Config).Metal
code := d.Get("code").(string)
_, capacityOk := d.GetOk("capacity")
if capacityOk {
ci := getCapacityInput(
d.Get("capacity").([]interface{}),
packngo.ServerInfo{Metro: code},
)
res, _, err := client.CapacityService.CheckMetros(ci)
if err != nil {
return err
}
for _, s := range res.Servers {
if !s.Available {
return fmt.Errorf("not enough capacity in metro %s for %d device(s) of plan %s", s.Metro, s.Quantity, s.Plan)
}
}
if err != nil {
return err
}
}
metros, _, err := client.Metros.List(nil)
if err != nil {
return fmt.Errorf("Error listing Metros: %s", err)
}
for _, m := range metros {
if m.Code == code {
d.SetId(m.ID)
return equinix_schema.SetMap(d, map[string]interface{}{
"id": m.ID,
"code": m.Code,
"name": m.Name,
"country": m.Country,
})
}
}
return fmt.Errorf("Metro %s was not found", code)
}