Add create-server acceptance test and support code
This check-in is not complete; it will break acceptance tests. The
problem is that I cannot run the test in full-quiet mode yet; I need to
support listing of images and flavors before I can do that. That will
allow the acceptance test to choose a server flavor and OS image
appropriate for the acceptance test.
diff --git a/acceptance/04-create-server.go b/acceptance/04-create-server.go
new file mode 100644
index 0000000..4c50e5d
--- /dev/null
+++ b/acceptance/04-create-server.go
@@ -0,0 +1,72 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "github.com/rackspace/gophercloud"
+)
+
+var region, serverName, imageRef, flavorRef *string
+var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)")
+
+func main() {
+ provider, username, password := getCredentials()
+ region = flag.String("r", "DFW", "Rackspace region in which to create the server")
+ serverName = flag.String("n", randomString(16), "Server name (what you see in the control panel)")
+ imageRef = flag.String("i", "", "ID of image to deploy onto the server") // TODO(sfalvo): Make this work in -quiet mode.
+ flavorRef = flag.String("f", "", "Flavor of server to deploy image upon") // TODO(sfalvo): Make this work in -quiet mode.
+
+ flag.Parse()
+
+ validations := map[string]string{
+ "an image reference (-i flag)": *imageRef,
+ "a server flavor (-f flag)": *flavorRef,
+ }
+ for flag, value := range validations {
+ if value == "" {
+ log.Fatal(fmt.Sprintf("You must provide %s", flag))
+ }
+ }
+
+ auth, err := gophercloud.Authenticate(
+ provider,
+ gophercloud.AuthOptions{
+ Username: username,
+ Password: password,
+ },
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ servers, err := gophercloud.ServersApi(auth, gophercloud.ApiCriteria{
+ Name: "cloudServersOpenStack",
+ Region: *region,
+ VersionId: "2",
+ UrlChoice: gophercloud.PublicURL,
+ })
+ if err != nil {
+ panic(err)
+ }
+
+ _, err = servers.CreateServer(gophercloud.NewServer{
+ Name: *serverName,
+ ImageRef: *imageRef,
+ FlavorRef: *flavorRef,
+ AdminPass: *adminPass,
+ })
+ if err != nil {
+ panic(err)
+ }
+
+ allServers, err := servers.ListServers()
+ if err != nil {
+ panic(err)
+ }
+
+ fmt.Printf("ID,Name,Status,Progress\n")
+ for _, i := range allServers {
+ fmt.Printf("%s,\"%s\",%s,%d\n", i.Id, i.Name, i.Status, i.Progress)
+ }
+}
diff --git a/acceptance/libargs.go b/acceptance/libargs.go
index 7b50f46..0af62d4 100644
--- a/acceptance/libargs.go
+++ b/acceptance/libargs.go
@@ -3,6 +3,7 @@
import (
"fmt"
"os"
+ "crypto/rand"
)
// getCredentials will verify existence of needed credential information
@@ -23,3 +24,17 @@
return
}
+
+// randomString generates a string of given length, but random content.
+// All content will be within the ASCII graphic character set.
+// (Implementation from Even Shaw's contribution on
+// http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go).
+func randomString(n int) string {
+ const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ var bytes = make([]byte, n)
+ rand.Read(bytes)
+ for i, b := range bytes {
+ bytes[i] = alphanum[b % byte(len(alphanum))]
+ }
+ return string(bytes)
+}
\ No newline at end of file