api: sys methods

This commit is contained in:
Mitchell Hashimoto 2015-03-11 19:33:20 +01:00
parent c6009345d1
commit 798689fb8d
4 changed files with 73 additions and 3 deletions

View File

@ -91,7 +91,7 @@ The following HTTP status codes are used throughout the API.
# Group Seal/Unseal
## Seal [/sys/seal]
## Seal Status [/sys/seal-status]
### Seal Status [GET]
Returns the status of whether the vault is currently
sealed or not, as well as the progress of unsealing.
@ -116,6 +116,7 @@ The response has the following attributes:
"progress": 1
}
## Seal [/sys/seal]
### Seal [PUT]
Seal the vault.

View File

@ -67,7 +67,7 @@ func (c *Client) NewRequest(method, path string) *Request {
// RawRequest performs the raw request given. This request may be against
// a Vault server not configured with this client. This is an advanced operation
// that generally won't need to be called externally.
func (c *Client) RawRequest(r *Request) (*http.Response, error) {
func (c *Client) RawRequest(r *Request) (*Response, error) {
req, err := r.ToHTTP()
if err != nil {
return nil, err
@ -78,5 +78,5 @@ func (c *Client) RawRequest(r *Request) (*http.Response, error) {
return nil, err
}
return resp, nil
return &Response{Response: resp}, nil
}

19
api/response.go Normal file
View File

@ -0,0 +1,19 @@
package api
import (
"encoding/json"
"net/http"
)
// Response is a raw response that wraps an HTTP response.
type Response struct {
*http.Response
}
// DecodeJSON will decode the response body to a JSON structure. This
// will consume the response body, but will not close it. Close must
// still be called.
func (r *Response) DecodeJSON(out interface{}) error {
dec := json.NewDecoder(r.Body)
return dec.Decode(out)
}

View File

@ -9,3 +9,53 @@ type Sys struct {
func (c *Client) Sys() *Sys {
return &Sys{c: c}
}
func (c *Sys) SealStatus() (*SealStatusResponse, error) {
r := c.c.NewRequest("GET", "/sys/seal-status")
resp, err := c.c.RawRequest(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SealStatusResponse
err = resp.DecodeJSON(&result)
return &result, err
}
func (c *Sys) Seal() error {
r := c.c.NewRequest("PUT", "/sys/seal")
resp, err := c.c.RawRequest(r)
defer resp.Body.Close()
return err
}
func (c *Sys) Unseal(shard string) (*SealStatusResponse, error) {
body := map[string]interface{}{"key": shard}
r := c.c.NewRequest("PUT", "/sys/unseal")
if err := r.SetJSONBody(body); err != nil {
return nil, err
}
resp, err := c.c.RawRequest(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result SealStatusResponse
err = resp.DecodeJSON(&result)
return &result, err
}
// Structures for the requests/resposne are all down here. They aren't
// individually documentd because the map almost directly to the raw HTTP API
// documentation. Please refer to that documentation for more details.
type SealStatusResponse struct {
Sealed bool
T int
N int
Progress int
}