Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame^] | 5 | "fmt" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // ListResult abstracts the raw results of making a List() request against the |
| 9 | // API. As OpenStack extensions may freely alter the response bodies of |
| 10 | // structures returned to the client, you may only safely access the data |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 11 | // provided through separate, type-safe accessors or methods. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 12 | type ListResult map[string]interface{} |
| 13 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame^] | 14 | // ServerResult abstracts a single server description, |
| 15 | // as returned by the OpenStack provider. |
| 16 | // As OpenStack extensions may freely alter the response bodies of the |
| 17 | // structures returned to the client, |
| 18 | // you may only safely access the data provided through |
| 19 | // separate, type-safe accessors or methods. |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 20 | type ServerResult map[string]interface{} |
| 21 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 22 | // List makes a request against the API to list servers accessible to you. |
| 23 | func List(c *Client) (ListResult, error) { |
| 24 | var lr ListResult |
| 25 | |
| 26 | h, err := c.getListHeaders() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | err = perigee.Get(c.getListUrl(), perigee.Options{ |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 32 | Results: &lr, |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 33 | MoreHeaders: h, |
| 34 | }) |
| 35 | return lr, err |
| 36 | } |
| 37 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 38 | // Create requests a server to be provisioned to the user in the current tenant. |
| 39 | func Create(c *Client, opts map[string]interface{}) (ServerResult, error) { |
| 40 | var sr ServerResult |
| 41 | |
| 42 | h, err := c.getCreateHeaders() |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | err = perigee.Post(c.getCreateUrl(), perigee.Options{ |
| 48 | Results: &sr, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 49 | ReqBody: map[string]interface{}{ |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 50 | "server": opts, |
| 51 | }, |
| 52 | MoreHeaders: h, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 53 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 54 | }) |
| 55 | return sr, err |
| 56 | } |
| 57 | |
| 58 | // Delete requests that a server previously provisioned be removed from your account. |
| 59 | func Delete(c *Client, id string) error { |
| 60 | h, err := c.getDeleteHeaders() |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | err = perigee.Delete(c.getDeleteUrl(id), perigee.Options{ |
| 66 | MoreHeaders: h, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 67 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 68 | }) |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | // GetDetail requests details on a single server, by ID. |
| 73 | func GetDetail(c *Client, id string) (ServerResult, error) { |
| 74 | var sr ServerResult |
| 75 | |
| 76 | h, err := c.getDetailHeaders() |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | err = perigee.Get(c.getDetailUrl(id), perigee.Options{ |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 82 | Results: &sr, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 83 | MoreHeaders: h, |
| 84 | }) |
| 85 | return sr, err |
| 86 | } |
| 87 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 88 | // Update requests that various attributes of the indicated server be changed. |
| 89 | func Update(c *Client, id string, opts map[string]interface{}) (ServerResult, error) { |
| 90 | var sr ServerResult |
| 91 | |
| 92 | h, err := c.getUpdateHeaders() |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | err = perigee.Put(c.getUpdateUrl(id), perigee.Options{ |
| 98 | Results: &sr, |
| 99 | ReqBody: map[string]interface{}{ |
| 100 | "server": opts, |
| 101 | }, |
| 102 | MoreHeaders: h, |
| 103 | }) |
| 104 | return sr, err |
| 105 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 106 | |
| 107 | // ChangeAdminPassword alters the administrator or root password for a specified |
| 108 | // server. |
| 109 | func ChangeAdminPassword(c *Client, id, newPassword string) error { |
| 110 | h, err := c.getActionHeaders() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
| 116 | ReqBody: struct{C map[string]string `json:"changePassword"`}{ |
| 117 | map[string]string{"adminPass": newPassword}, |
| 118 | }, |
| 119 | MoreHeaders: h, |
| 120 | OkCodes: []int{202}, |
| 121 | }) |
| 122 | return err |
| 123 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame^] | 124 | |
| 125 | // ArgumentError errors occur when an argument supplied to a package function |
| 126 | // fails to fall within acceptable values. For example, the Reboot() function |
| 127 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 128 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 129 | // other string values instead, perhaps in an effort to break the API of their |
| 130 | // provider. Reboot() returns this error in this situation. |
| 131 | // |
| 132 | // Function identifies which function was called/which function is generating |
| 133 | // the error. |
| 134 | // Argument identifies which formal argument was responsible for producing the |
| 135 | // error. |
| 136 | // Value provides the value as it was passed into the function. |
| 137 | type ArgumentError struct { |
| 138 | Function, Argument string |
| 139 | Value interface{} |
| 140 | } |
| 141 | |
| 142 | // Error yields a useful diagnostic for debugging purposes. |
| 143 | func (e *ArgumentError) Error() string { |
| 144 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 145 | } |
| 146 | |
| 147 | func (e *ArgumentError) String() string { |
| 148 | return e.Error() |
| 149 | } |
| 150 | |
| 151 | // These constants determine how a server should be rebooted. |
| 152 | // See the Reboot() function for further details. |
| 153 | const ( |
| 154 | SoftReboot = "SOFT" |
| 155 | HardReboot = "HARD" |
| 156 | OSReboot = SoftReboot |
| 157 | PowerCycle = HardReboot |
| 158 | ) |
| 159 | |
| 160 | // Reboot requests that a given server reboot. |
| 161 | // Two methods exist for rebooting a server: |
| 162 | // |
| 163 | // HardReboot (aka PowerCycle) -- restarts the server instance by physically |
| 164 | // cutting power to the machine, or if a VM, terminating it at the hypervisor |
| 165 | // level. It's done. Caput. Full stop. Then, after a brief while, power is |
| 166 | // restored or the VM instance restarted. |
| 167 | // |
| 168 | // SoftReboot (aka OSReboot). This approach simply tells the OS to restart |
| 169 | // under its own procedures. E.g., in Linux, asking it to enter runlevel 6, |
| 170 | // or executing "sudo shutdown -r now", or by wasking Windows to restart the |
| 171 | // machine. |
| 172 | func Reboot(c *Client, id, how string) error { |
| 173 | if (how != SoftReboot) && (how != HardReboot) { |
| 174 | return &ArgumentError{ |
| 175 | Function: "Reboot", |
| 176 | Argument: "how", |
| 177 | Value: how, |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | h, err := c.getActionHeaders() |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | |
| 186 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
| 187 | ReqBody: struct{C map[string]string `json:"reboot"`}{ |
| 188 | map[string]string{"type": how}, |
| 189 | }, |
| 190 | MoreHeaders: h, |
| 191 | OkCodes: []int{202}, |
| 192 | }) |
| 193 | return err |
| 194 | } |