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