blob: 2ce7bbed1ff64d14e197603c1a88081ea2079b1b [file] [log] [blame]
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -07001package main
2
3import (
4 "flag"
5 "fmt"
6 "github.com/rackspace/gophercloud"
Samuel A. Falvo II80699602013-07-25 23:35:57 -07007 "time"
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -07008)
9
10var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.")
11var serverId = flag.String("i", "", "ID of server whose admin password is to be changed.")
12var newPass = flag.String("p", "", "New password for the server.")
13
14func main() {
15 provider, username, password := getCredentials()
16 flag.Parse()
17
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -070018 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 II80699602013-07-25 23:35:57 -070039 // 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 II5c305e12013-07-25 19:19:43 -070065 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}