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