blob: 88d79789d97ed4223727d2e8b7298763b29bf8eb [file] [log] [blame]
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07001package main
2
3import (
4 "fmt"
5 "flag"
6 "github.com/rackspace/gophercloud"
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -07007)
8
9var id = flag.String("i", "", "Server ID to get info on. Defaults to first server in your account if unspecified.")
10var rgn = flag.String("r", "DFW", "Datacenter region")
11var quiet = flag.Bool("quiet", false, "Run quietly, for acceptance testing. $? non-zero if issue.")
12
13func main() {
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070014 flag.Parse()
15
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070016 withIdentity(false, func(auth gophercloud.AccessProvider) {
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070017 withServerApi(auth, func(servers gophercloud.CloudServersProvider) {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070018 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 II41856d62013-07-29 15:48:22 -070025 if *id == "" {
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070026 deleteAfterwards, serverId, err = locateAServer(servers)
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070027 if err != nil {
28 panic(err)
29 }
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070030 if deleteAfterwards {
31 defer servers.DeleteServerById(serverId)
32 }
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070033 } else {
34 serverId = *id
35 }
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070036
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070037 // Grab server details by ID, and provide a report.
Samuel A. Falvo II41856d62013-07-29 15:48:22 -070038 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 II02f5e832013-07-10 13:52:27 -070082 })
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070083}
Samuel A. Falvo II002b6512013-07-29 16:30:40 -070084
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.
91func 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
110}