sshkeys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package packngo
  2. import "fmt"
  3. const sshKeyBasePath = "/ssh-keys"
  4. // SSHKeyService interface defines available device methods
  5. type SSHKeyService interface {
  6. List() ([]SSHKey, *Response, error)
  7. Get(string) (*SSHKey, *Response, error)
  8. Create(*SSHKeyCreateRequest) (*SSHKey, *Response, error)
  9. Update(*SSHKeyUpdateRequest) (*SSHKey, *Response, error)
  10. Delete(string) (*Response, error)
  11. }
  12. type sshKeyRoot struct {
  13. SSHKeys []SSHKey `json:"ssh_keys"`
  14. }
  15. // SSHKey represents a user's ssh key
  16. type SSHKey struct {
  17. ID string `json:"id"`
  18. Label string `json:"label"`
  19. Key string `json:"key"`
  20. FingerPrint string `json:"fingerprint"`
  21. Created string `json:"created_at"`
  22. Updated string `json:"updated_at"`
  23. User User `json:"user,omitempty"`
  24. URL string `json:"href,omitempty"`
  25. }
  26. func (s SSHKey) String() string {
  27. return Stringify(s)
  28. }
  29. // SSHKeyCreateRequest type used to create an ssh key
  30. type SSHKeyCreateRequest struct {
  31. Label string `json:"label"`
  32. Key string `json:"key"`
  33. }
  34. func (s SSHKeyCreateRequest) String() string {
  35. return Stringify(s)
  36. }
  37. // SSHKeyUpdateRequest type used to update an ssh key
  38. type SSHKeyUpdateRequest struct {
  39. ID string `json:"id"`
  40. Label string `json:"label"`
  41. Key string `json:"key"`
  42. }
  43. func (s SSHKeyUpdateRequest) String() string {
  44. return Stringify(s)
  45. }
  46. // SSHKeyServiceOp implements SSHKeyService
  47. type SSHKeyServiceOp struct {
  48. client *Client
  49. }
  50. // List returns a user's ssh keys
  51. func (s *SSHKeyServiceOp) List() ([]SSHKey, *Response, error) {
  52. req, err := s.client.NewRequest("GET", sshKeyBasePath, nil)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. root := new(sshKeyRoot)
  57. resp, err := s.client.Do(req, root)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return root.SSHKeys, resp, err
  62. }
  63. // Get returns an ssh key by id
  64. func (s *SSHKeyServiceOp) Get(sshKeyID string) (*SSHKey, *Response, error) {
  65. path := fmt.Sprintf("%s/%s", sshKeyBasePath, sshKeyID)
  66. req, err := s.client.NewRequest("GET", path, nil)
  67. if err != nil {
  68. return nil, nil, err
  69. }
  70. sshKey := new(SSHKey)
  71. resp, err := s.client.Do(req, sshKey)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return sshKey, resp, err
  76. }
  77. // Create creates a new ssh key
  78. func (s *SSHKeyServiceOp) Create(createRequest *SSHKeyCreateRequest) (*SSHKey, *Response, error) {
  79. req, err := s.client.NewRequest("POST", sshKeyBasePath, createRequest)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. sshKey := new(SSHKey)
  84. resp, err := s.client.Do(req, sshKey)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return sshKey, resp, err
  89. }
  90. // Update updates an ssh key
  91. func (s *SSHKeyServiceOp) Update(updateRequest *SSHKeyUpdateRequest) (*SSHKey, *Response, error) {
  92. path := fmt.Sprintf("%s/%s", sshKeyBasePath, updateRequest.ID)
  93. req, err := s.client.NewRequest("PATCH", path, updateRequest)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. sshKey := new(SSHKey)
  98. resp, err := s.client.Do(req, sshKey)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return sshKey, resp, err
  103. }
  104. // Delete deletes an ssh key
  105. func (s *SSHKeyServiceOp) Delete(sshKeyID string) (*Response, error) {
  106. path := fmt.Sprintf("%s/%s", sshKeyBasePath, sshKeyID)
  107. req, err := s.client.NewRequest("DELETE", path, nil)
  108. if err != nil {
  109. return nil, err
  110. }
  111. resp, err := s.client.Do(req, nil)
  112. return resp, err
  113. }