/
data_source_datacenter.go
193 lines (171 loc) · 5.08 KB
/
data_source_datacenter.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package ionoscloud
import (
"context"
"fmt"
"log"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDataCenter() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDataCenterRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"location": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeInt,
Computed: true,
},
"features": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"sec_auth_protection": {
Type: schema.TypeBool,
Computed: true,
},
"cpu_architecture": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cpu_family": {
Type: schema.TypeString,
Computed: true,
},
"max_cores": {
Type: schema.TypeInt,
Computed: true,
},
"max_ram": {
Type: schema.TypeInt,
Computed: true,
},
"vendor": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"ipv6_cidr_block": {
Type: schema.TypeString,
Computed: true,
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceDataCenterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
var name, location string
id, idOk := d.GetOk("id")
t, nameOk := d.GetOk("name")
if nameOk {
name = t.(string)
}
t, locationOk := d.GetOk("location")
if locationOk {
location = t.(string)
}
var datacenter ionoscloud.Datacenter
var err error
var apiResponse *ionoscloud.APIResponse
if !idOk && !nameOk && !locationOk {
return diag.FromErr(fmt.Errorf("either id, location or name must be set"))
}
if idOk {
datacenter, apiResponse, err = client.DataCentersApi.DatacentersFindById(ctx, id.(string)).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting datacenter with id %s %w", id.(string), err))
}
if nameOk {
if *datacenter.Properties.Name != name {
return diag.FromErr(fmt.Errorf("name of dc (UUID=%s, name=%s) does not match expected name: %s",
*datacenter.Id, *datacenter.Properties.Name, name))
}
}
if locationOk {
if *datacenter.Properties.Location != location {
return diag.FromErr(fmt.Errorf("location of dc (UUID=%s, location=%s) does not match expected location: %s",
*datacenter.Id, *datacenter.Properties.Location, location))
}
}
if datacenter.Properties != nil {
log.Printf("[INFO] Got dc [Name=%s, Location=%s]", *datacenter.Properties.Name, *datacenter.Properties.Location)
}
} else {
datacenters, apiResponse, err := client.DataCentersApi.DatacentersGet(ctx).Depth(1).Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occured while fetching datacenters: %w ", err))
}
var results []ionoscloud.Datacenter
if nameOk && datacenters.Items != nil {
var resultsByDatacenter []ionoscloud.Datacenter
for _, dc := range *datacenters.Items {
if dc.Properties != nil && dc.Properties.Name != nil && *dc.Properties.Name == name {
resultsByDatacenter = append(resultsByDatacenter, dc)
}
}
if resultsByDatacenter == nil {
return diag.FromErr(fmt.Errorf("no datacenter found with the specified criteria: name = %s", name))
} else {
results = resultsByDatacenter
}
}
if locationOk {
var resultsByLocation []ionoscloud.Datacenter
if results != nil {
for _, dc := range results {
if dc.Properties.Location != nil && *dc.Properties.Location == location {
resultsByLocation = append(resultsByLocation, dc)
}
}
} else if datacenters.Items != nil {
/* find the first datacenter matching the location */
for _, dc := range *datacenters.Items {
if dc.Properties.Location != nil && *dc.Properties.Location == location {
resultsByLocation = append(resultsByLocation, dc)
}
}
}
if resultsByLocation == nil {
return diag.FromErr(fmt.Errorf("no datacenter found with the specified criteria: location = %s", location))
} else {
results = resultsByLocation
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no datacenter found with the specified criteria: name = %s location = %s", name, location))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one datacenter found with the specified criteria: name = %s location = %s", name, location))
} else {
datacenter = results[0]
}
}
if err := setDatacenterData(d, &datacenter); err != nil {
return diag.FromErr(err)
}
return nil
}