1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
package govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// SSHKeyService is the interface to interact with the SSH Key endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/ssh
type SSHKeyService interface {
Create(ctx context.Context, sshKeyReq *SSHKeyReq) (*SSHKey, error)
Get(ctx context.Context, sshKeyID string) (*SSHKey, error)
Update(ctx context.Context, sshKeyID string, sshKeyReq *SSHKeyReq) error
Delete(ctx context.Context, sshKeyID string) error
List(ctx context.Context, options *ListOptions) ([]SSHKey, *Meta, error)
}
// SSHKeyServiceHandler handles interaction with the SSH Key methods for the Vultr API
type SSHKeyServiceHandler struct {
client *Client
}
// SSHKey represents an SSH Key on Vultr
type SSHKey struct {
ID string `json:"id"`
Name string `json:"name"`
SSHKey string `json:"ssh_key"`
DateCreated string `json:"date_created"`
}
// SSHKeyReq is the ssh key struct for create and update calls
type SSHKeyReq struct {
Name string `json:"name,omitempty"`
SSHKey string `json:"ssh_key,omitempty"`
}
type sshKeysBase struct {
SSHKeys []SSHKey `json:"ssh_keys"`
Meta *Meta `json:"meta"`
}
type sshKeyBase struct {
SSHKey *SSHKey `json:"ssh_key"`
}
// Create a ssh key
func (s *SSHKeyServiceHandler) Create(ctx context.Context, sshKeyReq *SSHKeyReq) (*SSHKey, error) {
uri := "/v2/ssh-keys"
req, err := s.client.NewRequest(ctx, http.MethodPost, uri, sshKeyReq)
if err != nil {
return nil, err
}
key := new(sshKeyBase)
if err = s.client.DoWithContext(ctx, req, key); err != nil {
return nil, err
}
return key.SSHKey, nil
}
// Get a specific ssh key.
func (s *SSHKeyServiceHandler) Get(ctx context.Context, sshKeyID string) (*SSHKey, error) {
uri := fmt.Sprintf("/v2/ssh-keys/%s", sshKeyID)
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
sshKey := new(sshKeyBase)
if err = s.client.DoWithContext(ctx, req, sshKey); err != nil {
return nil, err
}
return sshKey.SSHKey, nil
}
// Update will update the given SSH Key. Empty strings will be ignored.
func (s *SSHKeyServiceHandler) Update(ctx context.Context, sshKeyID string, sshKeyReq *SSHKeyReq) error {
uri := fmt.Sprintf("/v2/ssh-keys/%s", sshKeyID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, uri, sshKeyReq)
if err != nil {
return err
}
return s.client.DoWithContext(ctx, req, nil)
}
// Delete a specific ssh-key.
func (s *SSHKeyServiceHandler) Delete(ctx context.Context, sshKeyID string) error {
uri := fmt.Sprintf("/v2/ssh-keys/%s", sshKeyID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return s.client.DoWithContext(ctx, req, nil)
}
// List all available SSH Keys.
func (s *SSHKeyServiceHandler) List(ctx context.Context, options *ListOptions) ([]SSHKey, *Meta, error) {
uri := "/v2/ssh-keys"
req, err := s.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
sshKeys := new(sshKeysBase)
if err = s.client.DoWithContext(ctx, req, sshKeys); err != nil {
return nil, nil, err
}
return sshKeys.SSHKeys, sshKeys.Meta, nil
}
|