blob: 705d72d38f3bb18ff42bc852ebd1005088505c0d [file] [log] [blame]
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -07001package main
2
3import (
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -07004 "flag"
Jon Perritt7152a1c2013-12-13 17:20:22 -06005 "fmt"
Max Lincoln28b49562013-12-13 13:23:44 -03006 "github.com/rackspace/gophercloud"
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -07007)
8
9var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.")
10
11func main() {
12 flag.Parse()
13 withIdentity(false, func(acc gophercloud.AccessProvider) {
Jon Perritt499dce12013-10-29 15:41:14 -050014 withServerApi(acc, func(api gophercloud.CloudServersProvider) {
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -070015 log("Creating server")
Jon Perritt499dce12013-10-29 15:41:14 -050016 id, err := createServer(api, "", "", "", "")
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -070017 if err != nil {
18 panic(err)
19 }
Jon Perritt499dce12013-10-29 15:41:14 -050020 waitForServerState(api, id, "ACTIVE")
21 defer api.DeleteServerById(id)
Jon Perritt7152a1c2013-12-13 17:20:22 -060022
Jon Perritt499dce12013-10-29 15:41:14 -050023 tryAllAddresses(id, api)
Jon Perritt499dce12013-10-29 15:41:14 -050024 tryAddressesByNetwork("private", id, api)
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -070025
26 log("Done")
27 })
28 })
29}
30
Jon Perritt499dce12013-10-29 15:41:14 -050031func tryAllAddresses(id string, api gophercloud.CloudServersProvider) {
32 log("Getting list of all addresses...")
33 addresses, err := api.ListAddresses(id)
Jon Perritt7152a1c2013-12-13 17:20:22 -060034 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 Perritt499dce12013-10-29 15:41:14 -050044}
45
46func tryAddressesByNetwork(networkLabel string, id string, api gophercloud.CloudServersProvider) {
47 log("Getting list of addresses on", networkLabel, "network...")
Jon Perritt7152a1c2013-12-13 17:20:22 -060048 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 Perritt499dce12013-10-29 15:41:14 -050056 log("Address:", addr.Addr, " IPv", addr.Version)
57 }
58}
59
Jon Perritt7152a1c2013-12-13 17:20:22 -060060func log(s ...interface{}) {
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -070061 if !*quiet {
62 fmt.Println(s...)
63 }
64}