gogreen/keys.go

219 lines
5.8 KiB
Go
Raw Normal View History

2014-09-03 14:03:30 +00:00
package godo
2016-11-29 02:42:23 +00:00
import (
"context"
2016-11-29 02:42:23 +00:00
"fmt"
"net/http"
2016-11-29 02:42:23 +00:00
)
2014-09-03 14:03:30 +00:00
const keysBasePath = "v2/account/keys"
// KeysService is an interface for interfacing with the keys
2015-09-18 13:10:59 +00:00
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/SSH-Keys
type KeysService interface {
2016-11-29 02:42:23 +00:00
List(context.Context, *ListOptions) ([]Key, *Response, error)
2022-08-14 16:12:32 +00:00
GetByID(context.Context, string) (*Key, *Response, error)
2016-11-29 02:42:23 +00:00
GetByFingerprint(context.Context, string) (*Key, *Response, error)
Create(context.Context, *KeyCreateRequest) (*Key, *Response, error)
2022-08-14 16:12:32 +00:00
UpdateByID(context.Context, string, *KeyUpdateRequest) (*Key, *Response, error)
2016-11-29 02:42:23 +00:00
UpdateByFingerprint(context.Context, string, *KeyUpdateRequest) (*Key, *Response, error)
2022-08-14 16:12:32 +00:00
DeleteByID(context.Context, string) (*Response, error)
2016-11-29 02:42:23 +00:00
DeleteByFingerprint(context.Context, string) (*Response, error)
}
// KeysServiceOp handles communication with key related method of the
2014-09-03 14:03:30 +00:00
// DigitalOcean API.
type KeysServiceOp struct {
2014-09-03 14:03:30 +00:00
client *Client
}
var _ KeysService = &KeysServiceOp{}
2014-09-03 14:03:30 +00:00
// Key represents a DigitalOcean Key.
type Key struct {
2022-08-14 16:12:32 +00:00
ID string `json:"id,omitempty"`
2014-09-03 14:03:30 +00:00
Name string `json:"name,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
PublicKey string `json:"public_key,omitempty"`
}
// KeyUpdateRequest represents a request to update a DigitalOcean key.
type KeyUpdateRequest struct {
Name string `json:"name"`
}
2014-09-03 14:03:30 +00:00
type keysRoot struct {
SSHKeys []Key `json:"ssh_keys"`
2022-08-14 17:34:48 +00:00
Links []*LinkAction `json:"links"`
Meta *Meta `json:"meta"`
2014-09-03 14:03:30 +00:00
}
type keyRoot struct {
SSHKey *Key `json:"ssh_key"`
2014-09-03 14:03:30 +00:00
}
func (s Key) String() string {
return Stringify(s)
}
// KeyCreateRequest represents a request to create a new key.
type KeyCreateRequest struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}
// List all keys
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) List(ctx context.Context, opt *ListOptions) ([]Key, *Response, error) {
path := keysBasePath
path, err := addOptions(path, opt)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(keysRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
2014-09-03 14:03:30 +00:00
return root.SSHKeys, resp, err
2014-09-03 14:03:30 +00:00
}
// Performs a get given a path
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) get(ctx context.Context, path string) (*Key, *Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
return root.SSHKey, resp, err
2014-09-03 14:03:30 +00:00
}
// GetByID gets a Key by id
2022-08-14 16:12:32 +00:00
func (s *KeysServiceOp) GetByID(ctx context.Context, keyID string) (*Key, *Response, error) {
path := fmt.Sprintf("%s/%s", keysBasePath, keyID)
2016-11-29 02:42:23 +00:00
return s.get(ctx, path)
2014-09-03 14:03:30 +00:00
}
// GetByFingerprint gets a Key by fingerprint
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) GetByFingerprint(ctx context.Context, fingerprint string) (*Key, *Response, error) {
if len(fingerprint) < 1 {
return nil, nil, NewArgError("fingerprint", "cannot not be empty")
}
2014-09-03 14:03:30 +00:00
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
2016-11-29 02:42:23 +00:00
return s.get(ctx, path)
2014-09-03 14:03:30 +00:00
}
// Create a key using a KeyCreateRequest
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) Create(ctx context.Context, createRequest *KeyCreateRequest) (*Key, *Response, error) {
if createRequest == nil {
return nil, nil, NewArgError("createRequest", "cannot be nil")
}
req, err := s.client.NewRequest(ctx, http.MethodPost, keysBasePath, createRequest)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(ctx, req, root)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, resp, err
}
return root.SSHKey, resp, err
2014-09-03 14:03:30 +00:00
}
2015-06-01 14:44:30 +00:00
// UpdateByID updates a key name by ID.
2022-08-14 16:12:32 +00:00
func (s *KeysServiceOp) UpdateByID(ctx context.Context, keyID string, updateRequest *KeyUpdateRequest) (*Key, *Response, error) {
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}
2022-08-14 16:12:32 +00:00
path := fmt.Sprintf("%s/%s", keysBasePath, keyID)
2016-11-29 02:42:23 +00:00
req, err := s.client.NewRequest(ctx, "PUT", path, updateRequest)
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.SSHKey, resp, err
}
2015-06-01 14:44:30 +00:00
// UpdateByFingerprint updates a key name by fingerprint.
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) UpdateByFingerprint(ctx context.Context, fingerprint string, updateRequest *KeyUpdateRequest) (*Key, *Response, error) {
if len(fingerprint) < 1 {
return nil, nil, NewArgError("fingerprint", "cannot be empty")
}
if updateRequest == nil {
return nil, nil, NewArgError("updateRequest", "cannot be nil")
}
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
2016-11-29 02:42:23 +00:00
req, err := s.client.NewRequest(ctx, "PUT", path, updateRequest)
if err != nil {
return nil, nil, err
}
root := new(keyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.SSHKey, resp, err
}
2014-09-03 14:03:30 +00:00
// Delete key using a path
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) delete(ctx context.Context, path string) (*Response, error) {
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
2014-09-03 14:03:30 +00:00
if err != nil {
return nil, err
}
resp, err := s.client.Do(ctx, req, nil)
2014-09-03 14:03:30 +00:00
return resp, err
}
// DeleteByID deletes a key by its id
2022-08-14 16:12:32 +00:00
func (s *KeysServiceOp) DeleteByID(ctx context.Context, keyID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", keysBasePath, keyID)
2016-11-29 02:42:23 +00:00
return s.delete(ctx, path)
2014-09-03 14:03:30 +00:00
}
// DeleteByFingerprint deletes a key by its fingerprint
2016-11-29 02:42:23 +00:00
func (s *KeysServiceOp) DeleteByFingerprint(ctx context.Context, fingerprint string) (*Response, error) {
if len(fingerprint) < 1 {
return nil, NewArgError("fingerprint", "cannot be empty")
}
2014-09-03 14:03:30 +00:00
path := fmt.Sprintf("%s/%s", keysBasePath, fingerprint)
2016-11-29 02:42:23 +00:00
return s.delete(ctx, path)
2014-09-03 14:03:30 +00:00
}