Repository navigation
Expand file tree
/
Copy pathdata_source_openstack_compute_instance_v2.go
More file actions
240 lines (212 loc) · 6.02 KB
/
Copy pathdata_source_openstack_compute_instance_v2.go
File metadata and controls
240 lines (212 loc) · 6.02 KB
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
package openstack
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tags"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
)
func dataSourceComputeInstanceV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceComputeInstanceV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"image_id": {
Type: schema.TypeString,
Computed: true,
},
"image_name": {
Type: schema.TypeString,
Computed: true,
},
"flavor_id": {
Type: schema.TypeString,
Computed: true,
},
"flavor_name": {
Type: schema.TypeString,
Computed: true,
},
"user_data": {
Type: schema.TypeString,
Optional: true,
Computed: true,
// just stash the hash for state & diff comparisons
},
"security_groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"network": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"port": {
Type: schema.TypeString,
Computed: true,
},
"fixed_ip_v4": {
Type: schema.TypeString,
Computed: true,
},
"fixed_ip_v6": {
Type: schema.TypeString,
Computed: true,
},
"mac": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"access_ip_v4": {
Type: schema.TypeString,
Computed: true,
},
"access_ip_v6": {
Type: schema.TypeString,
Computed: true,
},
"key_pair": {
Type: schema.TypeString,
Computed: true,
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
},
"power_state": {
Type: schema.TypeString,
Computed: true,
},
"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceComputeInstanceV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
log.Print("[DEBUG] Creating compute client")
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
id := d.Get("id").(string)
log.Printf("[DEBUG] Attempting to retrieve server %s", id)
server, err := servers.Get(computeClient, id).Extract()
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "server"))
}
log.Printf("[DEBUG] Retrieved Server %s: %+v", id, server)
d.SetId(server.ID)
d.Set("name", server.Name)
d.Set("image_id", server.Image["ID"])
// Get the instance network and address information
networks, err := flattenInstanceNetworks(d, meta)
if err != nil {
return diag.FromErr(err)
}
// Determine the best IPv4 and IPv6 addresses to access the instance with
hostv4, hostv6 := getInstanceAccessAddresses(d, networks)
// AccessIPv4/v6 isn't standard in OpenStack, but there have been reports
// of them being used in some environments.
if server.AccessIPv4 != "" && hostv4 == "" {
hostv4 = server.AccessIPv4
}
if server.AccessIPv6 != "" && hostv6 == "" {
hostv6 = server.AccessIPv6
}
log.Printf("[DEBUG] Setting networks: %+v", networks)
d.Set("network", networks)
d.Set("access_ip_v4", hostv4)
d.Set("access_ip_v6", hostv6)
d.Set("metadata", server.Metadata)
secGrpNames := []string{}
for _, sg := range server.SecurityGroups {
secGrpNames = append(secGrpNames, sg["name"].(string))
}
log.Printf("[DEBUG] Setting security groups: %+v", secGrpNames)
d.Set("security_groups", secGrpNames)
flavorID, ok := server.Flavor["id"].(string)
if !ok {
return diag.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor)
}
d.Set("flavor_id", flavorID)
d.Set("key_pair", server.KeyName)
flavor, err := flavors.Get(computeClient, flavorID).Extract()
if err != nil {
return diag.FromErr(err)
}
d.Set("flavor_name", flavor.Name)
// Set the instance's image information appropriately
if err := setImageInformation(computeClient, server, d); err != nil {
return diag.FromErr(err)
}
// Build a custom struct for the availability zone extension
var serverWithAZ struct {
servers.Server
availabilityzones.ServerAvailabilityZoneExt
}
// Do another Get so the above work is not disturbed.
err = servers.Get(computeClient, d.Id()).ExtractInto(&serverWithAZ)
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "server"))
}
// Set the availability zone
d.Set("availability_zone", serverWithAZ.AvailabilityZone)
// Set the region
d.Set("region", GetRegion(d, config))
// Set the current power_state
currentStatus := strings.ToLower(server.Status)
switch currentStatus {
case "active", "shutoff", "error", "migrating", "shelved_offloaded", "shelved":
d.Set("power_state", currentStatus)
default:
return diag.Errorf("Invalid power_state for instance %s: %s", d.Id(), server.Status)
}
// Populate tags.
computeClient.Microversion = computeV2TagsExtensionMicroversion
instanceTags, err := tags.List(computeClient, server.ID).Extract()
if err != nil {
log.Printf("[DEBUG] Unable to get tags for openstack_compute_instance_v2: %s", err)
} else {
d.Set("tags", instanceTags)
}
return nil
}