-
Notifications
You must be signed in to change notification settings - Fork 24
/
datasource.go
300 lines (268 loc) · 8.27 KB
/
datasource.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package instance
import (
"context"
"errors"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
exo "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/utils"
)
// DataSourceSchema returns a schema for a single Compute instance data source.
func DataSourceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
AttrAntiAffinityGroupIDs: {
Description: "The list of attached [exoscale_anti_affinity_group](../resources/anti_affinity_group.md) (IDs).",
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
},
AttrCreatedAt: {
Description: "The compute instance creation date.",
Type: schema.TypeString,
Computed: true,
},
AttrDeployTargetID: {
Description: "A deploy target ID.",
Type: schema.TypeString,
Computed: true,
},
AttrDiskSize: {
Description: "The instance disk size (GiB).",
Type: schema.TypeInt,
Computed: true,
},
AttrElasticIPIDs: {
Description: "The list of attached [exoscale_elastic_ip](../resources/elastic_ip.md) (IDs).",
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
},
AttrID: {
Description: "The compute instance ID to match (conflicts with `name`).",
Type: schema.TypeString,
Optional: true,
},
AttrIPv6: {
Description: "Whether IPv6 is enabled on the instance.",
Type: schema.TypeBool,
Computed: true,
},
AttrIPv6Address: {
Description: "The instance (main network interface) IPv6 address (if enabled).",
Type: schema.TypeString,
Computed: true,
},
AttrLabels: {
Description: "A map of key/value labels.",
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
AttrManagerID: {
Description: "The instance manager ID, if any.",
Type: schema.TypeString,
Computed: true,
},
AttrManagerType: {
Description: "The instance manager type (instance pool, SKS node pool, etc.), if any.",
Type: schema.TypeString,
Computed: true,
},
AttrName: {
Description: "The instance name to match (conflicts with `id`).",
Type: schema.TypeString,
Optional: true,
},
AttrPrivateNetworkIDs: {
Description: "The list of attached [exoscale_private_network](../resources/private_network.md) (IDs).",
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
},
AttrPublicIPAddress: {
Description: "The instance (main network interface) IPv4 address.",
Type: schema.TypeString,
Computed: true,
},
AttrReverseDNS: {
Description: "Domain name for reverse DNS record.",
Type: schema.TypeString,
Computed: true,
},
AttrSSHKey: {
Description: "The [exoscale_ssh_key](../resources/ssh_key.md) (name) authorized on the instance.",
Type: schema.TypeString,
Computed: true,
},
AttrSecurityGroupIDs: {
Description: "The list of attached [exoscale_security_group](../resources/security_group.md) (IDs).",
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
},
AttrState: {
Description: "The instance state.",
Type: schema.TypeString,
Computed: true,
},
AttrTemplateID: {
Description: "The instance [exoscale_template](./template.md) ID.",
Type: schema.TypeString,
Computed: true,
},
AttrType: {
Description: "The instance type.",
Type: schema.TypeString,
Computed: true,
},
AttrUserData: {
Description: "The instance [cloud-init](http://cloudinit.readthedocs.io/en/latest/) configuration.",
Type: schema.TypeString,
Computed: true,
},
AttrZone: {
Description: "The Exoscale [Zone](https://www.exoscale.com/datacenters/) name.",
Type: schema.TypeString,
Required: true,
},
}
}
func DataSource() *schema.Resource {
return &schema.Resource{
Description: `Fetch Exoscale [Compute Instances](https://community.exoscale.com/documentation/compute/) data.
Corresponding resource: [exoscale_compute_instance](../resources/compute_instance.md).`,
Schema: func() map[string]*schema.Schema {
schema := DataSourceSchema()
// adding context-aware schema settings here so getDataSourceComputeInstanceSchema can be used elsewhere
schema[AttrID].ConflictsWith = []string{AttrName}
schema[AttrName].ConflictsWith = []string{AttrID}
return schema
}(),
ReadContext: dsRead,
}
}
func dsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, "beginning read", map[string]interface{}{
"id": utils.IDString(d, Name),
})
zone := d.Get(AttrZone).(string)
ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutRead))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(config.GetEnvironment(meta), zone))
defer cancel()
client, err := config.GetClient(meta)
if err != nil {
return diag.FromErr(err)
}
id, byID := d.GetOk(AttrID)
name, byName := d.GetOk(AttrName)
if !byID && !byName {
return diag.Errorf(
"either %s or %s must be specified",
AttrName,
AttrID,
)
}
instance, err := client.FindInstance(
ctx,
zone, func() string {
if byID {
return id.(string)
}
return name.(string)
}(),
)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*instance.ID)
data, err := dsBuildData(instance)
if err != nil {
return diag.FromErr(err)
}
instanceType, err := client.GetInstanceType(
ctx,
zone,
*instance.InstanceTypeID,
)
if err != nil {
return diag.Errorf("unable to retrieve instance type: %s", err)
}
data[AttrType] = fmt.Sprintf(
"%s.%s",
strings.ToLower(*instanceType.Family),
strings.ToLower(*instanceType.Size),
)
rdns, err := client.GetInstanceReverseDNS(ctx, zone, *instance.ID)
if err != nil && !errors.Is(err, exoapi.ErrNotFound) {
return diag.Errorf("unable to retrieve instance reverse-dns: %s", err)
}
data[AttrReverseDNS] = strings.TrimSuffix(rdns, ".")
for key, value := range data {
err := d.Set(key, value)
if err != nil {
return diag.FromErr(err)
}
}
tflog.Debug(ctx, "read finished successfully", map[string]interface{}{
"id": utils.IDString(d, Name),
})
return nil
}
// dsBuildData builds terraform data object from egoscale API struct.
func dsBuildData(instance *exo.Instance) (map[string]interface{}, error) {
data := map[string]interface{}{}
data[AttrDeployTargetID] = instance.DeployTargetID
data[AttrDiskSize] = instance.DiskSize
data[AttrID] = instance.ID
data[AttrName] = instance.Name
data[AttrSSHKey] = instance.SSHKey
data[AttrState] = instance.State
data[AttrTemplateID] = instance.TemplateID
data[AttrZone] = instance.Zone
data[AttrIPv6] = utils.DefaultBool(instance.IPv6Enabled, false)
if instance.ElasticIPIDs != nil {
data[AttrElasticIPIDs] = *instance.ElasticIPIDs
}
if instance.AntiAffinityGroupIDs != nil {
data[AttrAntiAffinityGroupIDs] = *instance.AntiAffinityGroupIDs
}
if instance.Labels != nil {
data[AttrLabels] = *instance.Labels
}
if instance.PrivateNetworkIDs != nil {
data[AttrPrivateNetworkIDs] = *instance.PrivateNetworkIDs
}
if instance.SecurityGroupIDs != nil {
data[AttrSecurityGroupIDs] = *instance.SecurityGroupIDs
}
if instance.Manager != nil {
data[AttrManagerID] = instance.Manager.ID
data[AttrManagerType] = instance.Manager.Type
}
if instance.CreatedAt != nil {
data[AttrCreatedAt] = instance.CreatedAt.String()
}
if instance.IPv6Address != nil {
data[AttrIPv6Address] = instance.IPv6Address.String()
}
if instance.PublicIPAddress != nil {
data[AttrPublicIPAddress] = instance.PublicIPAddress.String()
}
if instance.UserData != nil {
userData, err := utils.DecodeUserData(*instance.UserData)
if err != nil {
return nil, fmt.Errorf("unable to decode user data: %w", err)
}
data[AttrUserData] = userData
}
return data, nil
}