blob: 4c50e5d6a6d572073e5fd3cef244cc6dba3629b7 [file] [log] [blame]
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -07001package main
2
3import (
4 "flag"
5 "fmt"
6 "log"
7 "github.com/rackspace/gophercloud"
8)
9
10var region, serverName, imageRef, flavorRef *string
11var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)")
12
13func main() {
14 provider, username, password := getCredentials()
15 region = flag.String("r", "DFW", "Rackspace region in which to create the server")
16 serverName = flag.String("n", randomString(16), "Server name (what you see in the control panel)")
17 imageRef = flag.String("i", "", "ID of image to deploy onto the server") // TODO(sfalvo): Make this work in -quiet mode.
18 flavorRef = flag.String("f", "", "Flavor of server to deploy image upon") // TODO(sfalvo): Make this work in -quiet mode.
19
20 flag.Parse()
21
22 validations := map[string]string{
23 "an image reference (-i flag)": *imageRef,
24 "a server flavor (-f flag)": *flavorRef,
25 }
26 for flag, value := range validations {
27 if value == "" {
28 log.Fatal(fmt.Sprintf("You must provide %s", flag))
29 }
30 }
31
32 auth, err := gophercloud.Authenticate(
33 provider,
34 gophercloud.AuthOptions{
35 Username: username,
36 Password: password,
37 },
38 )
39 if err != nil {
40 panic(err)
41 }
42
43 servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{
44 Name: "cloudServersOpenStack",
45 Region: *region,
46 VersionId: "2",
47 UrlChoice: gophercloud.PublicURL,
48 })
49 if err != nil {
50 panic(err)
51 }
52
53 _, err = servers.CreateServer(gophercloud.NewServer{
54 Name: *serverName,
55 ImageRef: *imageRef,
56 FlavorRef: *flavorRef,
57 AdminPass: *adminPass,
58 })
59 if err != nil {
60 panic(err)
61 }
62
63 allServers, err := servers.ListServers()
64 if err != nil {
65 panic(err)
66 }
67
68 fmt.Printf("ID,Name,Status,Progress\n")
69 for _, i := range allServers {
70 fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress)
71 }
72}