-
Notifications
You must be signed in to change notification settings - Fork 13
/
realvalidvaluesfetcher.go
151 lines (123 loc) · 3.44 KB
/
realvalidvaluesfetcher.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
package validation
import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/identity/v3/regions"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/utils/openstack/clientconfig"
)
type realValidValuesFetcher struct{}
// NewValidValuesFetcher returns a new ValidValuesFetcher.
func NewValidValuesFetcher() ValidValuesFetcher {
return realValidValuesFetcher{}
}
// GetCloudNames gets the valid cloud names. These are read from clouds.yaml.
func (f realValidValuesFetcher) GetCloudNames() ([]string, error) {
clouds, err := clientconfig.LoadCloudsYAML()
if err != nil {
return nil, err
}
i := 0
cloudNames := make([]string, len(clouds))
for k := range clouds {
cloudNames[i] = k
i++
}
return cloudNames, nil
}
// GetRegionNames gets the valid region names.
func (f realValidValuesFetcher) GetRegionNames(cloud string) ([]string, error) {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}
conn, err := clientconfig.NewServiceClient("identity", opts)
if err != nil {
return nil, err
}
listOpts := regions.ListOpts{}
allPages, err := regions.List(conn, listOpts).AllPages()
if err != nil {
return nil, err
}
allRegions, err := regions.ExtractRegions(allPages)
if err != nil {
return nil, err
}
regionNames := make([]string, len(allRegions))
for x, region := range allRegions {
regionNames[x] = region.ID
}
return regionNames, nil
}
// GetImageNames gets the valid image names.
func (f realValidValuesFetcher) GetImageNames(cloud string) ([]string, error) {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}
conn, err := clientconfig.NewServiceClient("image", opts)
if err != nil {
return nil, err
}
listOpts := images.ListOpts{}
allPages, err := images.List(conn, listOpts).AllPages()
if err != nil {
return nil, err
}
allImages, err := images.ExtractImages(allPages)
if err != nil {
return nil, err
}
imageNames := make([]string, len(allImages))
for x, image := range allImages {
imageNames[x] = image.Name
}
return imageNames, nil
}
// GetNetworkNames gets the valid network names.
func (f realValidValuesFetcher) GetNetworkNames(cloud string) ([]string, error) {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}
conn, err := clientconfig.NewServiceClient("network", opts)
if err != nil {
return nil, err
}
listOpts := networks.ListOpts{}
allPages, err := networks.List(conn, listOpts).AllPages()
if err != nil {
return nil, err
}
allNetworks, err := networks.ExtractNetworks(allPages)
if err != nil {
return nil, err
}
networkNames := make([]string, len(allNetworks))
for x, network := range allNetworks {
networkNames[x] = network.Name
}
return networkNames, nil
}
// GetFlavorNames gets a list of valid flavor names.
func (f realValidValuesFetcher) GetFlavorNames(cloud string) ([]string, error) {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}
conn, err := clientconfig.NewServiceClient("compute", opts)
if err != nil {
return nil, err
}
listOpts := flavors.ListOpts{}
allPages, err := flavors.ListDetail(conn, listOpts).AllPages()
if err != nil {
return nil, err
}
allFlavors, err := flavors.ExtractFlavors(allPages)
if err != nil {
return nil, err
}
flavorNames := make([]string, len(allFlavors))
for i, flavor := range allFlavors {
flavorNames[i] = flavor.Name
}
return flavorNames, nil
}