-
Notifications
You must be signed in to change notification settings - Fork 72
/
data_source.go
168 lines (154 loc) · 4.45 KB
/
data_source.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package datacenter
import (
"context"
"crypto/sha1"
"fmt"
"sort"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/hcclient"
)
const (
// DataSourceType is the type name of the Hetzner Cloud Datacenter
// datasource.
DataSourceType = "hcloud_datacenter"
// DatacentersDataSourceType is the type name of the Hetzner Cloud
// Datacenters datasource.
DatacentersDataSourceType = "hcloud_datacenters"
)
// DataSource creates a new Terraform schema for the Hetzner Cloud Datacenter
// data source.
func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceHcloudDatacenterRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeMap,
Computed: true,
},
"supported_server_type_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
"available_server_type_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func dataSourceHcloudDatacenterRead(ctx context.Context, data *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcloud.Client)
if id, ok := data.GetOk("id"); ok {
d, _, err := client.Datacenter.GetByID(ctx, id.(int))
if err != nil {
return hcclient.ErrorToDiag(err)
}
if d == nil {
return diag.Errorf("no datacenter found with id %d", id)
}
setDatacenterSchema(data, d)
return nil
}
if name, ok := data.GetOk("name"); ok {
d, _, err := client.Datacenter.GetByName(ctx, name.(string))
if err != nil {
return hcclient.ErrorToDiag(err)
}
if d == nil {
return diag.Errorf("no datacenter found with name %v", name)
}
setDatacenterSchema(data, d)
return nil
}
return diag.Errorf("please specify an id, or a name to lookup for a datacenter")
}
func setDatacenterSchema(d *schema.ResourceData, dc *hcloud.Datacenter) {
d.SetId(strconv.Itoa(dc.ID))
d.Set("name", dc.Name)
d.Set("description", dc.Description)
d.Set("location", map[string]string{
"id": strconv.Itoa(dc.Location.ID),
"name": dc.Location.Name,
"description": dc.Location.Description,
"country": dc.Location.Country,
"city": dc.Location.City,
"latitude": fmt.Sprintf("%f", dc.Location.Latitude),
"longitude": fmt.Sprintf("%f", dc.Location.Longitude),
})
supported := make([]int, len(dc.ServerTypes.Supported))
for i, v := range dc.ServerTypes.Supported {
supported[i] = v.ID
}
available := make([]int, len(dc.ServerTypes.Available))
for i, v := range dc.ServerTypes.Available {
available[i] = v.ID
}
sort.Ints(available)
sort.Ints(supported)
d.Set("supported_server_type_ids", supported)
d.Set("available_server_type_ids", available)
}
// DatacentersDataSource creates a new Terraform schema for the Hetzner Cloud
// Datacenters data source.
func DatacentersDataSource() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceHcloudDatacentersRead,
Schema: map[string]*schema.Schema{
"datacenter_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"descriptions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceHcloudDatacentersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcloud.Client)
dcs, err := client.Datacenter.All(ctx)
if err != nil {
return hcclient.ErrorToDiag(err)
}
names := make([]string, 0, len(dcs))
descriptions := make([]string, 0, len(dcs))
ids := make([]string, 0, len(dcs))
for _, v := range dcs {
ids = append(ids, strconv.Itoa(v.ID))
descriptions = append(descriptions, v.Description)
names = append(names, v.Name)
}
d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, "")))))
d.Set("datacenter_ids", ids)
d.Set("names", names)
d.Set("descriptions", descriptions)
return nil
}