blob: eae17755f33bee87ca7bf32867ed62be6978cbfc [file] [log] [blame]
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07001package main
2
3import (
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07004 "flag"
Jon Perritt2be65d12013-12-13 17:21:09 -06005 "fmt"
Max Lincoln28b49562013-12-13 13:23:44 -03006 "github.com/rackspace/gophercloud"
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -07007 "os"
8 "github.com/racker/perigee"
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07009)
10
11var id = flag.String("i", "", "Server ID to get info on. Defaults to first server in your account if unspecified.")
Samuel A. Falvo IIe4a3e422013-09-13 12:36:37 -070012var rgn = flag.String("r", "", "Datacenter region. Leave blank for default region.")
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070013var quiet = flag.Bool("quiet", false, "Run quietly, for acceptance testing. $? non-zero if issue.")
14
15func main() {
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070016 flag.Parse()
17
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -070018 resultCode := 0
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070019 withIdentity(false, func(auth gophercloud.AccessProvider) {
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070020 withServerApi(auth, func(servers gophercloud.CloudServersProvider) {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070021 var (
Jon Perritt2be65d12013-12-13 17:21:09 -060022 err error
23 serverId string
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070024 deleteAfterwards bool
25 )
26
27 // Figure out which server to provide server details for.
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070028 if *id == "" {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070029 deleteAfterwards, serverId, err = locateAServer(servers)
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070030 if err != nil {
31 panic(err)
32 }
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070033 if deleteAfterwards {
34 defer servers.DeleteServerById(serverId)
35 }
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070036 } else {
37 serverId = *id
38 }
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070039
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070040 // Grab server details by ID, and provide a report.
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070041 s, err := servers.ServerById(serverId)
42 if err != nil {
43 panic(err)
44 }
45
Jon Perritt2be65d12013-12-13 17:21:09 -060046 configs := []string{
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070047 "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 Perritt2be65d12013-12-13 17:21:09 -060062 values := []string{
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070063 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 II9f90f772014-06-30 15:25:32 -070085
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 II02f5e832013-07-10 13:52:27 -0700103 })
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -0700104 os.Exit(resultCode)
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700105}
Samuel A. Falvo II002b6512013-07-29 16:30:40 -0700106
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.
113func locateAServer(servers gophercloud.CloudServersProvider) (deleteAfter bool, id string, err error) {
114 ss, err := servers.ListServers()
115 if err != nil {
Jon Perritt2be65d12013-12-13 17:21:09 -0600116 return false, "", err
Samuel A. Falvo II002b6512013-07-29 16:30:40 -0700117 }
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 IIe4a3e422013-09-13 12:36:37 -0700132}