Merge pull request #43 from rightscale/feature/update_keys
Add support for key updates
This commit is contained in:
commit
90214be142
41
keys.go
41
keys.go
|
@ -12,6 +12,8 @@ type KeysService interface {
|
|||
GetByID(int) (*Key, *Response, error)
|
||||
GetByFingerprint(string) (*Key, *Response, error)
|
||||
Create(*KeyCreateRequest) (*Key, *Response, error)
|
||||
UpdateByID(int, *KeyUpdateRequest) (*Key, *Response, error)
|
||||
UpdateByFingerprint(string, *KeyUpdateRequest) (*Key, *Response, error)
|
||||
DeleteByID(int) (*Response, error)
|
||||
DeleteByFingerprint(string) (*Response, error)
|
||||
}
|
||||
|
@ -32,6 +34,11 @@ type Key struct {
|
|||
PublicKey string `json:"public_key,omitempty"`
|
||||
}
|
||||
|
||||
// KeyUpdateRequest represents a request to update a DigitalOcean key.
|
||||
type KeyUpdateRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type keysRoot struct {
|
||||
SSHKeys []Key `json:"ssh_keys"`
|
||||
Links *Links `json:"links"`
|
||||
|
@ -120,6 +127,40 @@ func (s *KeysServiceOp) Create(createRequest *KeyCreateRequest) (*Key, *Response
|
|||
return &root.SSHKey, resp, err
|
||||
}
|
||||
|
||||
// Update a key name by ID
|
||||
func (s *KeysServiceOp) UpdateByID(keyID int, updateRequest *KeyUpdateRequest) (*Key, *Response, error) {
|
||||
path := fmt.Sprintf("%s/%d", keysBasePath, keyID)
|
||||
req, err := s.client.NewRequest("PUT", path, updateRequest)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
root := new(keyRoot)
|
||||
resp, err := s.client.Do(req, root)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return &root.SSHKey, resp, err
|
||||
}
|
||||
|
||||
// Update a key name by fingerprint
|
||||
func (s *KeysServiceOp) UpdateByFingerprint(fingerprint string, updateRequest *KeyUpdateRequest) (*Key, *Response, error) {
|
||||
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
|
||||
req, err := s.client.NewRequest("PUT", path, updateRequest)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
root := new(keyRoot)
|
||||
resp, err := s.client.Do(req, root)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return &root.SSHKey, resp, err
|
||||
}
|
||||
|
||||
// Delete key using a path
|
||||
func (s *KeysServiceOp) delete(path string) (*Response, error) {
|
||||
req, err := s.client.NewRequest("DELETE", path, nil)
|
||||
|
|
72
keys_test.go
72
keys_test.go
|
@ -149,6 +149,78 @@ func TestKeys_Create(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestKeys_UpdateByID(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
updateRequest := &KeyUpdateRequest{
|
||||
Name: "name",
|
||||
}
|
||||
|
||||
mux.HandleFunc("/v2/account/keys/12345", func(w http.ResponseWriter, r *http.Request) {
|
||||
expected := map[string]interface{}{
|
||||
"name": "name",
|
||||
}
|
||||
|
||||
var v map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&v)
|
||||
if err != nil {
|
||||
t.Fatalf("decode json: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(v, expected) {
|
||||
t.Errorf("Request body = %#v, expected %#v", v, expected)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, `{"ssh_key":{"id":1}}`)
|
||||
})
|
||||
|
||||
key, _, err := client.Keys.UpdateByID(12345, updateRequest)
|
||||
if err != nil {
|
||||
t.Errorf("Keys.Update returned error: %v", err)
|
||||
} else {
|
||||
if id := key.ID; id != 1 {
|
||||
t.Errorf("expected id '%d', received '%d'", 1, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeys_UpdateByFingerprint(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
updateRequest := &KeyUpdateRequest{
|
||||
Name: "name",
|
||||
}
|
||||
|
||||
mux.HandleFunc("/v2/account/keys/3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa", func(w http.ResponseWriter, r *http.Request) {
|
||||
expected := map[string]interface{}{
|
||||
"name": "name",
|
||||
}
|
||||
|
||||
var v map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&v)
|
||||
if err != nil {
|
||||
t.Fatalf("decode json: %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(v, expected) {
|
||||
t.Errorf("Request body = %#v, expected %#v", v, expected)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, `{"ssh_key":{"id":1}}`)
|
||||
})
|
||||
|
||||
key, _, err := client.Keys.UpdateByFingerprint("3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa", updateRequest)
|
||||
if err != nil {
|
||||
t.Errorf("Keys.Update returned error: %v", err)
|
||||
} else {
|
||||
if id := key.ID; id != 1 {
|
||||
t.Errorf("expected id '%d', received '%d'", 1, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeys_DestroyByID(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
|
Loading…
Reference in New Issue