|
@@ -0,0 +1,88 @@
|
|
|
+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)
|
|
|
+
|
|
|
+ file, err := os.Create("./instance_list.csv")
|
|
|
+ checkErr(err)
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ _, err = file.WriteString("InstanceID, InstanceIP, InstanceName\n")
|
|
|
+ checkErr(err)
|
|
|
+
|
|
|
+ allServers := getAllServers(computeClient)
|
|
|
+ for _, server := range allServers {
|
|
|
+ addresses := server.Addresses["VM network"].([]interface{})
|
|
|
+ addressDetails := addresses[0].(map[string]interface{})
|
|
|
+ serverIP := addressDetails["addr"]
|
|
|
+
|
|
|
+ str := fmt.Sprintf("%v,%v,%v\n", server.ID, serverIP, server.Name)
|
|
|
+ _, err := file.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
|
|
|
+}
|