blob: 9d29b0ec0d034425bcacb2e3ec6d7e7e4a0d15bf [file] [log] [blame]
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -07001package main
2
3import (
4 "flag"
5 "fmt"
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -07006 "github.com/rackspace/gophercloud"
7)
8
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -07009var provider, username, password string
10
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070011var region, serverName, imageRef, flavorRef *string
12var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)")
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070013var quiet = flag.Bool("quiet", false, "Quiet mode for acceptance tests. $? non-zero if error.")
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070014
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070015func configure() {
16 provider, username, password = getCredentials()
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070017 region = flag.String("r", "DFW", "Rackspace region in which to create the server")
Samuel A. Falvo IIe3b2d7a2013-07-12 11:08:02 -070018 serverName = flag.String("n", randomString("ACPTTEST--", 16), "Server name (what you see in the control panel)")
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070019 imageRef = flag.String("i", "", "ID of image to deploy onto the server")
20 flavorRef = flag.String("f", "", "Flavor of server to deploy image upon")
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070021
22 flag.Parse()
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070023}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070024
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070025func aSuitableImage(api gophercloud.CloudServersProvider) string {
26 images, err := api.ListImages()
27 if err != nil {
28 panic(err)
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070029 }
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070030
31 // TODO(sfalvo):
32 // Works for Rackspace, might not work for your provider!
33 // Need to figure out why ListImages() provides 0 values for
34 // Ram and Disk fields.
35 //
36 // Until then, just return Ubuntu 12.04 LTS.
37 for i := 0; i < len(images); i++ {
38 if images[i].Id == "6a668bb8-fb5d-407a-9a89-6f957bced767" {
39 return images[i].Id
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070040 }
41 }
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070042 panic("Image 6a668bb8-fb5d-407a-9a89-6f957bced767 (Ubuntu 12.04 LTS) not found.")
43}
44
45func aSuitableFlavor(api gophercloud.CloudServersProvider) string {
46 flavors, err := api.ListFlavors()
47 if err != nil {
48 panic(err)
49 }
50
51 // TODO(sfalvo):
52 // Works for Rackspace, might not work for your provider!
53 // Need to figure out why ListFlavors() provides 0 values for
54 // Ram and Disk fields.
55 //
56 // Until then, just return Ubuntu 12.04 LTS.
57 for i := 0; i < len(flavors); i++ {
58 if flavors[i].Id == "2" {
59 return flavors[i].Id
60 }
61 }
62 panic("Flavor 2 (512MB 1-core 20GB machine) not found.")
63}
64
65func main() {
66 configure()
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070067
68 auth, err := gophercloud.Authenticate(
69 provider,
70 gophercloud.AuthOptions{
71 Username: username,
72 Password: password,
73 },
74 )
75 if err != nil {
76 panic(err)
77 }
78
79 servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{
80 Name: "cloudServersOpenStack",
81 Region: *region,
82 VersionId: "2",
83 UrlChoice: gophercloud.PublicURL,
84 })
85 if err != nil {
86 panic(err)
87 }
88
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -070089 if *imageRef == "" {
90 *imageRef = aSuitableImage(servers)
91 }
92
93 if *flavorRef == "" {
94 *flavorRef = aSuitableFlavor(servers)
95 }
96
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070097 _, err = servers.CreateServer(gophercloud.NewServer{
98 Name: *serverName,
99 ImageRef: *imageRef,
100 FlavorRef: *flavorRef,
101 AdminPass: *adminPass,
102 })
103 if err != nil {
104 panic(err)
105 }
106
107 allServers, err := servers.ListServers()
108 if err != nil {
109 panic(err)
110 }
111
Samuel A. Falvo II4b0ba752013-07-11 17:42:26 -0700112 if !*quiet {
113 fmt.Printf("ID,Name,Status,Progress\n")
114 for _, i := range allServers {
115 fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress)
116 }
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700117 }
118}