12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package main
- import (
- "fmt"
- "os"
- "github.com/alyu/configparser"
- "github.com/gophercloud/gophercloud"
- "github.com/gophercloud/gophercloud/openstack"
- "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
- )
- func main() {
- config, err := configparser.Read("./identity.conf")
- checkErr(err)
- section, err := config.Section("openstack")
- checkErr(err)
- region := section.ValueOf("region")
- provider := createProvider(config)
- computeClient, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
- Region: region,
- })
- checkErr(err)
- allServers := getAllServers(computeClient)
- for _, server := range allServers {
- addresses, ok := server.Addresses[section.ValueOf("network_name")].([]interface{})
- if !ok {
- continue
- }
- addressDetails := addresses[0].(map[string]interface{})
- serverIP := addressDetails["addr"]
- str := fmt.Sprintf("%v,%v,%v\n", server.ID, serverIP, server.Name)
- _, err := os.Stdout.WriteString(str)
- checkErr(err)
- }
- }
- func createProvider(config *configparser.Configuration) *gophercloud.ProviderClient {
- section, err := config.Section("openstack")
- checkErr(err)
- opts := gophercloud.AuthOptions{
- IdentityEndpoint: section.ValueOf("identity_endpoint"),
- DomainName: section.ValueOf("domain_name"),
- TenantName: section.ValueOf("project_name"),
- Username: section.ValueOf("username"),
- Password: section.ValueOf("password"),
- }
- provider, err := openstack.AuthenticatedClient(opts)
- checkErr(err)
- return provider
- }
- func getAllServers(client *gophercloud.ServiceClient) []servers.Server {
- listOpts := servers.ListOpts{
- AllTenants: true,
- }
- allPages, err := servers.List(client, listOpts).AllPages()
- checkErr(err)
- allServers, err := servers.ExtractServers(allPages)
- checkErr(err)
- return allServers
- }
- // H E L P E R S
- func checkErr(err error) {
- if err != nil {
- panic(err)
- }
- }
- func pointerFromBool(b bool) *bool {
- return &b
- }
|