Samuel A. Falvo II | 5c305e1 | 2013-07-25 19:19:43 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 8069960 | 2013-07-25 23:35:57 -0700 | [diff] [blame] | 7 | "time" |
Samuel A. Falvo II | 5c305e1 | 2013-07-25 19:19:43 -0700 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.") |
| 11 | var serverId = flag.String("i", "", "ID of server whose admin password is to be changed.") |
| 12 | var newPass = flag.String("p", "", "New password for the server.") |
| 13 | |
| 14 | func main() { |
| 15 | provider, username, password := getCredentials() |
| 16 | flag.Parse() |
| 17 | |
Samuel A. Falvo II | 5c305e1 | 2013-07-25 19:19:43 -0700 | [diff] [blame] | 18 | acc, err := gophercloud.Authenticate( |
| 19 | provider, |
| 20 | gophercloud.AuthOptions{ |
| 21 | Username: username, |
| 22 | Password: password, |
| 23 | }, |
| 24 | ) |
| 25 | if err != nil { |
| 26 | panic(err) |
| 27 | } |
| 28 | |
| 29 | api, err := gophercloud.ServersApi(acc, gophercloud.ApiCriteria{ |
| 30 | Name: "cloudServersOpenStack", |
| 31 | Region: "DFW", |
| 32 | VersionId: "2", |
| 33 | UrlChoice: gophercloud.PublicURL, |
| 34 | }) |
| 35 | if err != nil { |
| 36 | panic(err) |
| 37 | } |
| 38 | |
Samuel A. Falvo II | 8069960 | 2013-07-25 23:35:57 -0700 | [diff] [blame] | 39 | // If user doesn't explicitly provide a server ID, create one dynamically. |
| 40 | if *serverId == "" { |
| 41 | var err error |
| 42 | *serverId, err = createServer(api, "", "", "", "") |
| 43 | if err != nil { |
| 44 | panic(err) |
| 45 | } |
| 46 | |
| 47 | // Wait for server to finish provisioning. |
| 48 | for { |
| 49 | s, err := api.ServerById(*serverId) |
| 50 | if err != nil { |
| 51 | panic(err) |
| 52 | } |
| 53 | if s.Status == "ACTIVE" { |
| 54 | break |
| 55 | } |
| 56 | time.Sleep(10 * time.Second) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // If no password is provided, create one dynamically. |
| 61 | if *newPass == "" { |
| 62 | *newPass = randomString("", 16) |
| 63 | } |
| 64 | |
Samuel A. Falvo II | 5c305e1 | 2013-07-25 19:19:43 -0700 | [diff] [blame] | 65 | err = api.SetAdminPassword(*serverId, *newPass) |
| 66 | if err != nil { |
| 67 | panic(err) |
| 68 | } |
| 69 | |
| 70 | if !*quiet { |
| 71 | fmt.Println("Password change request submitted.") |
| 72 | } |
| 73 | } |