Samuel A. Falvo II | 2dd7d2f | 2014-06-30 16:18:08 -0700 | [diff] [blame^] | 1 | // +build acceptance,old |
| 2 | |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 3 | package main |
| 4 | |
| 5 | import ( |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 6 | "flag" |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 7 | "fmt" |
Max Lincoln | 28b4956 | 2013-12-13 13:23:44 -0300 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 9f90f77 | 2014-06-30 15:25:32 -0700 | [diff] [blame] | 9 | "os" |
| 10 | "github.com/racker/perigee" |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | var id = flag.String("i", "", "Server ID to get info on. Defaults to first server in your account if unspecified.") |
Samuel A. Falvo II | e4a3e42 | 2013-09-13 12:36:37 -0700 | [diff] [blame] | 14 | var rgn = flag.String("r", "", "Datacenter region. Leave blank for default region.") |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 15 | var quiet = flag.Bool("quiet", false, "Run quietly, for acceptance testing. $? non-zero if issue.") |
| 16 | |
| 17 | func main() { |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 18 | flag.Parse() |
| 19 | |
Samuel A. Falvo II | 9f90f77 | 2014-06-30 15:25:32 -0700 | [diff] [blame] | 20 | resultCode := 0 |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 21 | withIdentity(false, func(auth gophercloud.AccessProvider) { |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 22 | withServerApi(auth, func(servers gophercloud.CloudServersProvider) { |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 23 | var ( |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 24 | err error |
| 25 | serverId string |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 26 | deleteAfterwards bool |
| 27 | ) |
| 28 | |
| 29 | // Figure out which server to provide server details for. |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 30 | if *id == "" { |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 31 | deleteAfterwards, serverId, err = locateAServer(servers) |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 32 | if err != nil { |
| 33 | panic(err) |
| 34 | } |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 35 | if deleteAfterwards { |
| 36 | defer servers.DeleteServerById(serverId) |
| 37 | } |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 38 | } else { |
| 39 | serverId = *id |
| 40 | } |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 41 | |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 42 | // Grab server details by ID, and provide a report. |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 43 | s, err := servers.ServerById(serverId) |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 48 | configs := []string{ |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 49 | "Access IPv4: %s\n", |
| 50 | "Access IPv6: %s\n", |
| 51 | " Created: %s\n", |
| 52 | " Flavor: %s\n", |
| 53 | " Host ID: %s\n", |
| 54 | " ID: %s\n", |
| 55 | " Image: %s\n", |
| 56 | " Name: %s\n", |
| 57 | " Progress: %s\n", |
| 58 | " Status: %s\n", |
| 59 | " Tenant ID: %s\n", |
| 60 | " Updated: %s\n", |
| 61 | " User ID: %s\n", |
| 62 | } |
| 63 | |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 64 | values := []string{ |
Samuel A. Falvo II | 41856d6 | 2013-07-29 15:48:22 -0700 | [diff] [blame] | 65 | s.AccessIPv4, |
| 66 | s.AccessIPv6, |
| 67 | s.Created, |
| 68 | s.Flavor.Id, |
| 69 | s.HostId, |
| 70 | s.Id, |
| 71 | s.Image.Id, |
| 72 | s.Name, |
| 73 | fmt.Sprintf("%d", s.Progress), |
| 74 | s.Status, |
| 75 | s.TenantId, |
| 76 | s.Updated, |
| 77 | s.UserId, |
| 78 | } |
| 79 | |
| 80 | if !*quiet { |
| 81 | fmt.Println("Server info:") |
| 82 | for i, _ := range configs { |
| 83 | fmt.Printf(configs[i], values[i]) |
| 84 | } |
| 85 | } |
| 86 | }) |
Samuel A. Falvo II | 9f90f77 | 2014-06-30 15:25:32 -0700 | [diff] [blame] | 87 | |
| 88 | // Negative test -- We should absolutely never panic for a server that doesn't exist. |
| 89 | withServerApi(auth, func(servers gophercloud.CloudServersProvider) { |
| 90 | _, err := servers.ServerById(randomString("garbage", 32)) |
| 91 | if err == nil { |
| 92 | fmt.Printf("Expected a 404 response when looking for a server known not to exist\n") |
| 93 | resultCode = 1 |
| 94 | } |
| 95 | perigeeError, ok := err.(*perigee.UnexpectedResponseCodeError) |
| 96 | if !ok { |
| 97 | fmt.Printf("Unexpected error type\n") |
| 98 | resultCode = 1 |
| 99 | } else { |
| 100 | if perigeeError.Actual != 404 { |
| 101 | fmt.Printf("Expected a 404 error code\n") |
| 102 | } |
| 103 | } |
| 104 | }) |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 105 | }) |
Samuel A. Falvo II | 9f90f77 | 2014-06-30 15:25:32 -0700 | [diff] [blame] | 106 | os.Exit(resultCode) |
Samuel A. Falvo II | 02f5e83 | 2013-07-10 13:52:27 -0700 | [diff] [blame] | 107 | } |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 108 | |
| 109 | // locateAServer queries the set of servers owned by the user. If at least one |
| 110 | // exists, the first found is picked, and its ID is returned. Otherwise, a new |
| 111 | // server will be created, and its ID returned. |
| 112 | // |
| 113 | // deleteAfter will be true if the caller should schedule a call to DeleteServerById() |
| 114 | // to clean up. |
| 115 | func locateAServer(servers gophercloud.CloudServersProvider) (deleteAfter bool, id string, err error) { |
| 116 | ss, err := servers.ListServers() |
| 117 | if err != nil { |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 118 | return false, "", err |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if len(ss) > 0 { |
| 122 | // We could just cheat and dump the server details from ss[0]. |
| 123 | // But, that tests ListServers(), and not ServerById(). So, we |
| 124 | // elect not to cheat. |
| 125 | return false, ss[0].Id, nil |
| 126 | } |
| 127 | |
| 128 | serverId, err := createServer(servers, "", "", "", "") |
| 129 | if err != nil { |
| 130 | return false, "", err |
| 131 | } |
| 132 | err = waitForServerState(servers, serverId, "ACTIVE") |
| 133 | return true, serverId, err |
Samuel A. Falvo II | e4a3e42 | 2013-09-13 12:36:37 -0700 | [diff] [blame] | 134 | } |