Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | var region, serverName, imageRef, flavorRef *string |
| 11 | var adminPass = flag.String("a", "", "Administrator password (auto-assigned if none)") |
| 12 | |
| 13 | func 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 | } |