-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-os-vms.go
62 lines (53 loc) · 1.36 KB
/
get-os-vms.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
package main
import (
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/pagination"
log "github.com/sirupsen/logrus"
)
func GetOsVms(config *Config, c chan *[]Vm) {
log.Debug("Init os")
vms := <-c
for _, account := range config.OsAccounts {
for _, region := range account.Regions {
log.Debug("Get vms from ", account.Name, region)
authOpts := gophercloud.AuthOptions{
IdentityEndpoint: account.Auth_url,
Username: account.Username,
Password: account.Password,
TenantName: account.Project_name,
}
provider, err := openstack.AuthenticatedClient(authOpts)
if err != nil {
log.Error(err)
}
client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: region,
})
if err != nil {
log.Error(err)
}
opts := servers.ListOpts{}
pager := servers.List(client, opts)
err = pager.EachPage(func(page pagination.Page) (bool, error) {
serverList, _ := servers.ExtractServers(page)
for _, server := range serverList {
*vms = append(*vms, Vm{
server.Name,
region,
account.Name,
server.ID,
"None",
"None",
})
}
return true, nil
})
if err != nil {
log.Error(err)
}
}
}
c <- vms
}