Samuel A. Falvo II | 659e14b | 2013-07-16 12:04:54 -0700 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "flag" |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
| 8 | |
| 9 | var quiet = flag.Bool("quiet", false, "Quiet mode for acceptance testing. $? non-zero on error though.") |
| 10 | var rgn = flag.String("r", "DFW", "Datacenter region to interrogate.") |
| 11 | |
| 12 | func main() { |
| 13 | provider, username, password := getCredentials() |
| 14 | flag.Parse() |
| 15 | |
| 16 | // Authenticate initially against the service. |
| 17 | auth, err := gophercloud.Authenticate( |
| 18 | provider, |
| 19 | gophercloud.AuthOptions{ |
| 20 | Username: username, |
| 21 | Password: password, |
| 22 | }, |
| 23 | ) |
| 24 | if err != nil { |
| 25 | panic(err) |
| 26 | } |
| 27 | |
| 28 | // Cache our initial authentication token. |
| 29 | token1 := auth.AuthToken() |
| 30 | |
| 31 | // Acquire access to the cloud servers API. |
| 32 | servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{ |
| 33 | Name: "cloudServersOpenStack", |
| 34 | Region: *rgn, |
| 35 | VersionId: "2", |
| 36 | UrlChoice: gophercloud.PublicURL, |
| 37 | }) |
| 38 | if err != nil { |
| 39 | panic(err) |
| 40 | } |
| 41 | |
| 42 | // Just to confirm everything works, we should be able to list images without error. |
| 43 | _, err = servers.ListImages() |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | |
| 48 | // Revoke our current authentication token. |
| 49 | auth.Revoke(auth.AuthToken()) |
| 50 | |
| 51 | // Attempt to list images again. This should _succeed_, because we enabled re-authentication. |
| 52 | _, err = servers.ListImages() |
| 53 | if err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | |
| 57 | // However, our new authentication token should differ. |
| 58 | token2 := auth.AuthToken() |
| 59 | |
| 60 | if !*quiet { |
| 61 | fmt.Println("Old authentication token: ", token1) |
| 62 | fmt.Println("New authentication token: ", token2) |
| 63 | } |
| 64 | } |