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