Skip to content

Commit

Permalink
SCALRCORE-31981 add SSH key link client
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanMytsko committed Nov 5, 2024
1 parent cd0e1b0 commit 43cef5a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scalr.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type Client struct {
Workspaces Workspaces
RunScheduleRules RunScheduleRules
SSHKeys SSHKeys
SSHKeysLinks SSHKeysLinks
}

// NewClient creates a new Scalr API client.
Expand Down Expand Up @@ -249,6 +250,7 @@ func NewClient(cfg *Config) (*Client, error) {
client.RunScheduleRules = &runScheduleRules{client: client}
client.EventBridgeIntegrations = &eventBridgeIntegrations{client: client}
client.SSHKeys = &sshKeys{client: client}
client.SSHKeysLinks = &sshKeysLinks{client: client}
return client, nil
}

Expand Down
60 changes: 60 additions & 0 deletions ssh_key_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package scalr

import (
"context"
"fmt"
"net/url"
)

// Compile-time proof of interface implementation.
var _ SSHKeysLinks = (*sshKeysLinks)(nil)

type SSHKeysLinks interface {
Create(ctx context.Context, workspaceID string, sshKeyID string) (*SSHKeysLink, error)
Delete(ctx context.Context, workspaceID string) error
}

// sshKeysLinks implements SSHKeysLinks.
type sshKeysLinks struct {
client *Client
}

// SSHKeysLink represents a single SSH key workspace link.
type SSHKeysLink struct {
SSHKeyID string `jsonapi:"attr,ssh-key"`
}

type SSHKeysLinkCreateOptions struct {
SSHKeyID string `json:"ssh-key"`
}

// Create creates a SSH key workspace link.
func (s *sshKeysLinks) Create(ctx context.Context, workspaceID string, sshKeyID string) (*SSHKeysLink, error) {
urlPath := fmt.Sprintf("workspaces/%s/ssh-key-links", url.QueryEscape(workspaceID))
linkOptions := SSHKeysLinkCreateOptions{
SSHKeyID: sshKeyID,
}
req, err := s.client.newJsonRequest("POST", urlPath, linkOptions)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/vnd.api+json")

link := &SSHKeysLink{}
if err := s.client.do(ctx, req, link); err != nil {
return nil, err
}

return link, nil
}

// Delete deletes a SSH key workspace link.
func (s *sshKeysLinks) Delete(ctx context.Context, workspaceID string) error {
urlPath := fmt.Sprintf("workspaces/%s/ssh-key-links/", url.QueryEscape(workspaceID))
req, err := s.client.newRequest("DELETE", urlPath, nil)
if err != nil {
return err
}

return s.client.do(ctx, req, nil)
}
1 change: 1 addition & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type Workspace struct {
ModuleVersion *ModuleVersion `jsonapi:"relation,module-version,omitempty"`
Tags []*Tag `jsonapi:"relation,tags"`
ConfigurationVersion *ConfigurationVersion `jsonapi:"relation,configuration-version"`
SSHKey *SSHKey `jsonapi:"relation,ssh-key"`
}

// Hooks contains the custom hooks field.
Expand Down

0 comments on commit 43cef5a

Please sign in to comment.