2018-04-30 15:47:39 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-12-02 20:37:43 +00:00
|
|
|
// Userdatas represents the response of a GET /user_data
|
|
|
|
type Userdatas struct {
|
2018-04-30 15:47:39 +00:00
|
|
|
UserData []string `json:"user_data"`
|
|
|
|
}
|
|
|
|
|
2019-12-02 20:37:43 +00:00
|
|
|
// Userdata represents []byte
|
|
|
|
type Userdata []byte
|
2018-04-30 15:47:39 +00:00
|
|
|
|
|
|
|
// GetUserdatas gets list of userdata for a server
|
2019-12-02 20:37:43 +00:00
|
|
|
func (s *API) GetUserdatas(serverID string, metadata bool) (*Userdatas, error) {
|
2018-04-30 15:47:39 +00:00
|
|
|
var uri, endpoint string
|
|
|
|
|
|
|
|
endpoint = s.computeAPI
|
|
|
|
if metadata {
|
|
|
|
uri = "/user_data"
|
|
|
|
endpoint = MetadataAPI
|
|
|
|
} else {
|
|
|
|
uri = fmt.Sprintf("servers/%s/user_data", serverID)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.GetResponsePaginate(endpoint, uri, url.Values{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 20:37:43 +00:00
|
|
|
var userdatas Userdatas
|
2018-04-30 15:47:39 +00:00
|
|
|
|
|
|
|
if err = json.Unmarshal(body, &userdatas); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &userdatas, nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 20:37:43 +00:00
|
|
|
func (s *Userdata) String() string {
|
2018-04-30 15:47:39 +00:00
|
|
|
return string(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserdata gets a specific userdata for a server
|
2019-12-02 20:37:43 +00:00
|
|
|
func (s *API) GetUserdata(serverID, key string, metadata bool) (*Userdata, error) {
|
2018-04-30 15:47:39 +00:00
|
|
|
var uri, endpoint string
|
|
|
|
|
|
|
|
endpoint = s.computeAPI
|
|
|
|
if metadata {
|
|
|
|
uri = fmt.Sprintf("/user_data/%s", key)
|
|
|
|
endpoint = MetadataAPI
|
|
|
|
} else {
|
|
|
|
uri = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
resp, err := s.GetResponsePaginate(endpoint, uri, url.Values{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("no such user_data %q (%d)", key, resp.StatusCode)
|
|
|
|
}
|
2019-12-02 20:37:43 +00:00
|
|
|
var data Userdata
|
2018-04-30 15:47:39 +00:00
|
|
|
data, err = ioutil.ReadAll(resp.Body)
|
|
|
|
return &data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PatchUserdata sets a user data
|
2019-12-02 20:37:43 +00:00
|
|
|
func (s *API) PatchUserdata(serverID, key string, value []byte, metadata bool) error {
|
2018-04-30 15:47:39 +00:00
|
|
|
var resource, endpoint string
|
|
|
|
|
|
|
|
endpoint = s.computeAPI
|
|
|
|
if metadata {
|
|
|
|
resource = fmt.Sprintf("/user_data/%s", key)
|
|
|
|
endpoint = MetadataAPI
|
|
|
|
} else {
|
|
|
|
resource = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
uri := fmt.Sprintf("%s/%s", strings.TrimRight(endpoint, "/"), resource)
|
|
|
|
payload := new(bytes.Buffer)
|
|
|
|
payload.Write(value)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PATCH", uri, payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-Auth-Token", s.Token)
|
|
|
|
req.Header.Set("Content-Type", "text/plain")
|
|
|
|
req.Header.Set("User-Agent", s.userAgent)
|
|
|
|
|
2019-12-02 20:37:43 +00:00
|
|
|
resp, err := s.Client.Do(req)
|
2018-04-30 15:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNoContent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("cannot set user_data (%d)", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUserdata deletes a server user_data
|
2019-12-02 20:37:43 +00:00
|
|
|
func (s *API) DeleteUserdata(serverID, key string, metadata bool) error {
|
2018-04-30 15:47:39 +00:00
|
|
|
var url, endpoint string
|
|
|
|
|
|
|
|
endpoint = s.computeAPI
|
|
|
|
if metadata {
|
|
|
|
url = fmt.Sprintf("/user_data/%s", key)
|
|
|
|
endpoint = MetadataAPI
|
|
|
|
} else {
|
|
|
|
url = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.DeleteResponse(endpoint, url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
_, err = s.handleHTTPError([]int{http.StatusNoContent}, resp)
|
|
|
|
return err
|
|
|
|
}
|