Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 4 | "flag" |
Jon Perritt | 7152a1c | 2013-12-13 17:20:22 -0600 | [diff] [blame^] | 5 | "fmt" |
Max Lincoln | 28b4956 | 2013-12-13 13:23:44 -0300 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.") |
| 10 | |
| 11 | func main() { |
| 12 | flag.Parse() |
| 13 | withIdentity(false, func(acc gophercloud.AccessProvider) { |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 14 | withServerApi(acc, func(api gophercloud.CloudServersProvider) { |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 15 | log("Creating server") |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 16 | id, err := createServer(api, "", "", "", "") |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 17 | if err != nil { |
| 18 | panic(err) |
| 19 | } |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 20 | waitForServerState(api, id, "ACTIVE") |
| 21 | defer api.DeleteServerById(id) |
Jon Perritt | 7152a1c | 2013-12-13 17:20:22 -0600 | [diff] [blame^] | 22 | |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 23 | tryAllAddresses(id, api) |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 24 | tryAddressesByNetwork("private", id, api) |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 25 | |
| 26 | log("Done") |
| 27 | }) |
| 28 | }) |
| 29 | } |
| 30 | |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 31 | func tryAllAddresses(id string, api gophercloud.CloudServersProvider) { |
| 32 | log("Getting list of all addresses...") |
| 33 | addresses, err := api.ListAddresses(id) |
Jon Perritt | 7152a1c | 2013-12-13 17:20:22 -0600 | [diff] [blame^] | 34 | if (err != nil) && (err != gophercloud.WarnUnauthoritative) { |
| 35 | panic(err) |
| 36 | } |
| 37 | if err == gophercloud.WarnUnauthoritative { |
| 38 | log("Uh oh -- got a response back, but it's not authoritative for some reason.") |
| 39 | } |
| 40 | if !*quiet { |
| 41 | fmt.Println("Addresses:") |
| 42 | fmt.Printf("%+v\n", addresses) |
| 43 | } |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func tryAddressesByNetwork(networkLabel string, id string, api gophercloud.CloudServersProvider) { |
| 47 | log("Getting list of addresses on", networkLabel, "network...") |
Jon Perritt | 7152a1c | 2013-12-13 17:20:22 -0600 | [diff] [blame^] | 48 | network, err := api.ListAddressesByNetwork(id, networkLabel) |
| 49 | if (err != nil) && (err != gophercloud.WarnUnauthoritative) { |
| 50 | panic(err) |
| 51 | } |
| 52 | if err == gophercloud.WarnUnauthoritative { |
| 53 | log("Uh oh -- got a response back, but it's not authoritative for some reason.") |
| 54 | } |
| 55 | for _, addr := range network[networkLabel] { |
Jon Perritt | 499dce1 | 2013-10-29 15:41:14 -0500 | [diff] [blame] | 56 | log("Address:", addr.Addr, " IPv", addr.Version) |
| 57 | } |
| 58 | } |
| 59 | |
Jon Perritt | 7152a1c | 2013-12-13 17:20:22 -0600 | [diff] [blame^] | 60 | func log(s ...interface{}) { |
Samuel A. Falvo II | e21808f | 2013-08-14 14:48:09 -0700 | [diff] [blame] | 61 | if !*quiet { |
| 62 | fmt.Println(s...) |
| 63 | } |
| 64 | } |