-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_compute_hypervisor_v2.go
107 lines (86 loc) · 2.38 KB
/
data_source_viettelidc_compute_hypervisor_v2.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
package viettelidc
import (
"context"
"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/hypervisors"
)
func dataSourceComputeHypervisorV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceComputeHypervisorV2Read,
Schema: map[string]*schema.Schema{
"hostname": {
Type: schema.TypeString,
Required: true,
},
"host_ip": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"vcpus": {
Type: schema.TypeInt,
Computed: true,
},
"memory": {
Type: schema.TypeInt,
Computed: true,
},
"disk": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceComputeHypervisorV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
region := GetRegion(d, config)
computeClient, err := config.ComputeV2Client(region)
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
allPages, err := hypervisors.List(computeClient, hypervisors.ListOpts{}).AllPages()
if err != nil {
return diag.Errorf("Error listing compute hypervisors: %s", err)
}
allHypervisors, err := hypervisors.ExtractHypervisors(allPages)
if err != nil {
return diag.Errorf("Error extracting compute hypervisors: %s", err)
}
name := d.Get("hostname").(string)
var refinedHypervisors []hypervisors.Hypervisor
for _, hypervisor := range allHypervisors {
if hypervisor.HypervisorHostname == name {
refinedHypervisors = append(refinedHypervisors, hypervisor)
}
}
if len(refinedHypervisors) < 1 {
return diag.Errorf("Could not find any hypervisor with this name: %s", name)
}
if len(refinedHypervisors) > 1 {
return diag.Errorf("More than one hypervisor found with this name: %s", name)
}
h := refinedHypervisors[0]
d.SetId(h.ID)
d.Set("hostname", h.HypervisorHostname)
d.Set("host_ip", h.HostIP)
d.Set("state", h.State)
d.Set("status", h.Status)
d.Set("type", h.HypervisorType)
d.Set("vcpus", h.VCPUs)
d.Set("memory", h.MemoryMB)
d.Set("disk", h.LocalGB)
return nil
}