-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to create, get and delete SSH Keys
- Loading branch information
1 parent
9766aa6
commit cb2d4de
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package key | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/sitehostnz/gosh/pkg/utils" | ||
) | ||
|
||
// Create an SSH Key. | ||
func (s *Client) Create(ctx context.Context, opts CreateRequest) (response CreateResponse, err error) { | ||
u := "ssh/key/add.json" | ||
|
||
keys := []string{ | ||
"label", | ||
"content", | ||
"params[custom_image_access]", | ||
} | ||
|
||
values := url.Values{} | ||
values.Add("label", opts.Label) | ||
values.Add("content", opts.Content) | ||
values.Add("params[custom_image_access]", opts.CustomImageAccess) | ||
|
||
req, err := s.client.NewRequest("POST", u, utils.Encode(values, keys)) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
if err := s.client.Do(ctx, req, &response); err != nil { | ||
return response, err | ||
} | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package key | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/sitehostnz/gosh/pkg/utils" | ||
) | ||
|
||
// Delete an SSH Key with the provided ID. | ||
func (s *Client) Delete(ctx context.Context, request DeleteRequest) (response DeleteResponse, err error) { | ||
u := "ssh/key/remove.json" | ||
|
||
keys := []string{ | ||
"key_id", | ||
} | ||
|
||
values := url.Values{} | ||
values.Add("key_id", request.ID) | ||
|
||
req, err := s.client.NewRequest("POST", u, utils.Encode(values, keys)) | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
if err := s.client.Do(ctx, req, &response); err != nil { | ||
return response, err | ||
} | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package key | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// Get information about the SSH Key. | ||
func (s *Client) Get(ctx context.Context, request GetRequest) (response GetResponse, err error) { | ||
u := fmt.Sprintf("ssh/key/get.json?key_id=%v", request.ID) | ||
|
||
req, err := s.client.NewRequest("GET", u, "") | ||
if err != nil { | ||
return response, err | ||
} | ||
|
||
if err := s.client.Do(ctx, req, &response); err != nil { | ||
return response, err | ||
} | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
package key | ||
|
||
type ( | ||
// GetRequest represents a request to get a specific SSH key. | ||
// GetRequest represents a request to get a specific SSH Key. | ||
GetRequest struct { | ||
ID string `json:"key_id"` | ||
} | ||
// CreateRequest represents a request to create an SSH Key. | ||
CreateRequest struct { | ||
Label string `json:"label"` | ||
Content string `json:"content"` | ||
CustomImageAccess string `json:"custom_image_access"` | ||
} | ||
// DeleteRequest represents a request to delete a specific SSH Key. | ||
DeleteRequest struct { | ||
ID string `json:"key_id"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters