Files
2026-09-02 14:47:38 +08:00

107 lines
4.6 KiB
Go

package awsserversdk
import (
"context"
"net/http"
"net/url"
)
type AccessService struct{ client *Client }
type LoginInfo struct {
Users []LoginUser `json:"users"`
}
type LoginUser struct {
Username string `json:"username"`
Methods []LoginMethod `json:"methods"`
}
type LoginMethod struct {
Type string `json:"type"`
Protocol string `json:"protocol"`
Host string `json:"host"`
Port int `json:"port"`
Credential LoginCredential `json:"credential"`
}
type LoginCredential struct {
Kind string `json:"kind"`
Available bool `json:"available"`
Status string `json:"status"`
Error string `json:"error"`
Password Secret `json:"password"`
PrivateKey Secret `json:"private_key"`
KeyID string `json:"key_id"`
KeyName string `json:"key_name"`
Fingerprint string `json:"fingerprint"`
Source string `json:"source"`
}
type Credential struct {
Username string `json:"username"`
Password Secret `json:"password"`
Version int `json:"version"`
Warning string `json:"warning"`
}
type SSHKeyAssignment map[string]any
type AddSSHKeyRequest struct {
SSHKeyID string `json:"ssh_key_id"`
Username string `json:"username"`
}
type ProvisionSSHKeyRequest struct {
Name string `json:"name,omitempty"`
PublicKey string `json:"public_key,omitempty"`
Username string `json:"username"`
}
type RotateSSHKeyRequest struct {
Name string `json:"name,omitempty"`
PublicKey string `json:"public_key,omitempty"`
Username string `json:"username"`
ReplaceKeyID string `json:"replace_key_id,omitempty"`
}
type SSHPrivateKey struct {
KeyID string `json:"key_id"`
KeyName string `json:"key_name"`
Fingerprint string `json:"fingerprint"`
Algorithm string `json:"algorithm"`
Username string `json:"username"`
PrivateKey Secret `json:"private_key"`
}
func (s *AccessService) GetLoginInfo(ctx context.Context, region, instanceID string) (*LoginInfo, error) {
var result LoginInfo
err := s.client.do(ctx, http.MethodPost, instancesPath(region, instanceID)+"/login-info", map[string]any{}, &result, newID())
return &result, err
}
func (s *AccessService) RevealCredential(ctx context.Context, region, instanceID string) (*Credential, error) {
var result Credential
err := s.client.do(ctx, http.MethodPost, instancesPath(region, instanceID)+"/credentials/reveal", map[string]any{}, &result, newID())
return &result, err
}
func (s *AccessService) RotateCredential(ctx context.Context, region, instanceID string, options ...RequestOption) (*Operation, error) {
return (&InstancesService{client: s.client}).operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/credentials/rotate", map[string]any{}, options...)
}
func (s *AccessService) ListSSHKeys(ctx context.Context, region, instanceID string) (*Page[SSHKeyAssignment], error) {
var result Page[SSHKeyAssignment]
err := s.client.do(ctx, http.MethodGet, instancesPath(region, instanceID)+"/ssh-keys", nil, &result, "")
return &result, err
}
func (s *AccessService) RevealSSHKey(ctx context.Context, region, instanceID, keyID string) (*SSHPrivateKey, error) {
var result SSHPrivateKey
err := s.client.do(ctx, http.MethodPost, instancesPath(region, instanceID)+"/ssh-keys/"+url.PathEscape(keyID)+"/reveal", map[string]any{}, &result, newID())
return &result, err
}
func (s *AccessService) AddSSHKey(ctx context.Context, region, instanceID string, input AddSSHKeyRequest, options ...RequestOption) (*Operation, error) {
return (&InstancesService{client: s.client}).operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/ssh-keys", input, options...)
}
func (s *AccessService) ProvisionSSHKey(ctx context.Context, region, instanceID string, input ProvisionSSHKeyRequest, options ...RequestOption) (*Operation, error) {
return (&InstancesService{client: s.client}).operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/ssh-keys/provision", input, options...)
}
func (s *AccessService) RotateSSHKey(ctx context.Context, region, instanceID string, input RotateSSHKeyRequest, options ...RequestOption) (*Operation, error) {
return (&InstancesService{client: s.client}).operation(ctx, http.MethodPost, instancesPath(region, instanceID)+"/ssh-keys/rotate", input, options...)
}
func (s *AccessService) RemoveSSHKey(ctx context.Context, region, instanceID, keyID, username string, options ...RequestOption) (*Operation, error) {
path := instancesPath(region, instanceID) + "/ssh-keys/" + escaped(keyID)
if username != "" {
path += "?username=" + url.QueryEscape(username)
}
return (&InstancesService{client: s.client}).operation(ctx, http.MethodDelete, path, nil, options...)
}