Skip to content

Commit

Permalink
feat: Add ability to create, get and delete SSH Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewPierson committed Mar 11, 2024
1 parent 9766aa6 commit cb2d4de
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
35 changes: 35 additions & 0 deletions pkg/api/ssh/key/create.go
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
}
31 changes: 31 additions & 0 deletions pkg/api/ssh/key/delete.go
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
}
22 changes: 22 additions & 0 deletions pkg/api/ssh/key/get.go
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
}
12 changes: 11 additions & 1 deletion pkg/api/ssh/key/request.go
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"`
}
)
23 changes: 23 additions & 0 deletions pkg/api/ssh/key/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,27 @@ type (
} `json:"return"`
models.APIResponse
}
// CreateResponse represents a result of the create an SSH Key call.
CreateResponse struct {
Return struct {
KeyID string `json:"key_id"`
} `json:"return"`
models.APIResponse
}
// GetResponse represents a SSH Keys information.
GetResponse struct {
Return struct {
ID string `json:"id"`
ClientID string `json:"client_id"`
Label string `json:"label"`
Content string `json:"content"`
DateAdded string `json:"date_added"`
DateUpdated string `json:"date_updated"`
} `json:"return"`
models.APIResponse
}
// DeleteResponse represents a result of the delete an SSH Key call.
DeleteResponse struct {
models.APIResponse
}
)

0 comments on commit cb2d4de

Please sign in to comment.