blob: 01140a9d706919fb54316fd3daab904802ca4915 [file] [log] [blame]
Samuel A. Falvo II2dd7d2f2014-06-30 16:18:08 -07001// +build acceptance,old
2
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07003package main
4
5import (
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07006 "flag"
Jon Perritt2be65d12013-12-13 17:21:09 -06007 "fmt"
Max Lincoln28b49562013-12-13 13:23:44 -03008 "github.com/rackspace/gophercloud"
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -07009 "os"
10 "github.com/racker/perigee"
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070011)
12
13var 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 -070014var rgn = flag.String("r", "", "Datacenter region. Leave blank for default region.")
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070015var quiet = flag.Bool("quiet", false, "Run quietly, for acceptance testing. $? non-zero if issue.")
16
17func main() {
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070018 flag.Parse()
19
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -070020 resultCode := 0
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070021 withIdentity(false, func(auth gophercloud.AccessProvider) {
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070022 withServerApi(auth, func(servers gophercloud.CloudServersProvider) {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070023 var (
Jon Perritt2be65d12013-12-13 17:21:09 -060024 err error
25 serverId string
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070026 deleteAfterwards bool
27 )
28
29 // Figure out which server to provide server details for.
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070030 if *id == "" {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070031 deleteAfterwards, serverId, err = locateAServer(servers)
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070032 if err != nil {
33 panic(err)
34 }
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070035 if deleteAfterwards {
36 defer servers.DeleteServerById(serverId)
37 }
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070038 } else {
39 serverId = *id
40 }
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070041
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070042 // Grab server details by ID, and provide a report.
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070043 s, err := servers.ServerById(serverId)
44 if err != nil {
45 panic(err)
46 }
47
Jon Perritt2be65d12013-12-13 17:21:09 -060048 configs := []string{
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070049 "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 Perritt2be65d12013-12-13 17:21:09 -060064 values := []string{
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070065 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 II9f90f772014-06-30 15:25:32 -070087
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 II02f5e832013-07-10 13:52:27 -0700105 })
Samuel A. Falvo II9f90f772014-06-30 15:25:32 -0700106 os.Exit(resultCode)
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -0700107}
Samuel A. Falvo II002b6512013-07-29 16:30:40 -0700108
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.
115func locateAServer(servers gophercloud.CloudServersProvider) (deleteAfter bool, id string, err error) {
116 ss, err := servers.ListServers()
117 if err != nil {
Jon Perritt2be65d12013-12-13 17:21:09 -0600118 return false, "", err
Samuel A. Falvo II002b6512013-07-29 16:30:40 -0700119 }
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 IIe4a3e422013-09-13 12:36:37 -0700134}