-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_location.go
161 lines (132 loc) · 3.99 KB
/
data_source_location.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
package ionoscloud
import (
"context"
"fmt"
"log"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
)
func dataSourceLocation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceLocationRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"feature": {
Type: schema.TypeString,
Optional: 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,
},
},
},
},
"image_aliases": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceLocationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
name, nameOk := d.GetOk("name")
feature, featureOk := d.GetOk("feature")
if !nameOk && !featureOk {
return diag.FromErr(fmt.Errorf("either 'name' or 'feature' must be provided"))
}
request := client.LocationsApi.LocationsGet(ctx).Depth(1)
if featureOk {
request = request.Filter("features", feature.(string))
}
locations, apiResponse, err := request.Execute()
logApiRequestTime(apiResponse)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching locations: %w", err))
}
var results []ionoscloud.Location
if nameOk && locations.Items != nil {
for _, loc := range *locations.Items {
if loc.Properties != nil && loc.Properties.Name != nil && *loc.Properties.Name == name.(string) {
results = append(results, loc)
}
}
}
log.Printf("[INFO] Results length %d *************", len(results))
var location ionoscloud.Location
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no location found with the specified criteria: name = %s, feature = %s", name.(string), feature.(string)))
} else {
location = results[0]
}
if err := setLocationData(d, &location); err != nil {
return diag.FromErr(err)
}
return nil
}
func setLocationData(d *schema.ResourceData, location *ionoscloud.Location) error {
if location.Id != nil {
d.SetId(*location.Id)
}
if location.Properties != nil {
var cpuArchitectures []interface{}
for _, cpuArchitecture := range *location.Properties.CpuArchitecture {
architectureEntry := make(map[string]interface{})
if cpuArchitecture.CpuFamily != nil {
architectureEntry["cpu_family"] = *cpuArchitecture.CpuFamily
}
if cpuArchitecture.MaxCores != nil {
architectureEntry["max_cores"] = *cpuArchitecture.MaxCores
}
if cpuArchitecture.MaxRam != nil {
architectureEntry["max_ram"] = *cpuArchitecture.MaxRam
}
if cpuArchitecture.Vendor != nil {
architectureEntry["vendor"] = *cpuArchitecture.Vendor
}
cpuArchitectures = append(cpuArchitectures, architectureEntry)
}
if len(cpuArchitectures) > 0 {
if err := d.Set("cpu_architecture", cpuArchitectures); err != nil {
return fmt.Errorf("error while setting cpu_architecture property for datacenter %s: %w", d.Id(), err)
}
}
var imageAliases []string
for _, imageAlias := range *location.Properties.ImageAliases {
imageAliases = append(imageAliases, imageAlias)
}
if len(imageAliases) > 0 {
if err := d.Set("image_aliases", imageAliases); err != nil {
return fmt.Errorf("error while setting image_aliases property for datacenter %s: %w", d.Id(), err)
}
}
}
return nil
}