devices.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package packngo
  2. import "fmt"
  3. const deviceBasePath = "/devices"
  4. // DeviceService interface defines available device methods
  5. type DeviceService interface {
  6. List(ProjectID string) ([]Device, *Response, error)
  7. Get(string) (*Device, *Response, error)
  8. Create(*DeviceCreateRequest) (*Device, *Response, error)
  9. Delete(string) (*Response, error)
  10. Reboot(string) (*Response, error)
  11. PowerOff(string) (*Response, error)
  12. PowerOn(string) (*Response, error)
  13. Lock(string) (*Response, error)
  14. Unlock(string) (*Response, error)
  15. }
  16. type devicesRoot struct {
  17. Devices []Device `json:"devices"`
  18. }
  19. // Device represents a Packet device
  20. type Device struct {
  21. ID string `json:"id"`
  22. Href string `json:"href,omitempty"`
  23. Hostname string `json:"hostname,omitempty"`
  24. State string `json:"state,omitempty"`
  25. Created string `json:"created_at,omitempty"`
  26. Updated string `json:"updated_at,omitempty"`
  27. Locked bool `json:"locked,omitempty"`
  28. BillingCycle string `json:"billing_cycle,omitempty"`
  29. Tags []string `json:"tags,omitempty"`
  30. Network []*IPAddress `json:"ip_addresses"`
  31. OS *OS `json:"operating_system,omitempty"`
  32. Plan *Plan `json:"plan,omitempty"`
  33. Facility *Facility `json:"facility,omitempty"`
  34. Project *Project `json:"project,omitempty"`
  35. ProvisionPer float32 `json:"provisioning_percentage,omitempty"`
  36. }
  37. func (d Device) String() string {
  38. return Stringify(d)
  39. }
  40. // DeviceCreateRequest type used to create a Packet device
  41. type DeviceCreateRequest struct {
  42. HostName string `json:"hostname"`
  43. Plan string `json:"plan"`
  44. Facility string `json:"facility"`
  45. OS string `json:"operating_system"`
  46. BillingCycle string `json:"billing_cycle"`
  47. ProjectID string `json:"project_id"`
  48. UserData string `json:"userdata"`
  49. Tags []string `json:"tags"`
  50. }
  51. func (d DeviceCreateRequest) String() string {
  52. return Stringify(d)
  53. }
  54. // DeviceActionRequest type used to execute actions on devices
  55. type DeviceActionRequest struct {
  56. Type string `json:"type"`
  57. }
  58. func (d DeviceActionRequest) String() string {
  59. return Stringify(d)
  60. }
  61. // IPAddress used to execute actions on devices
  62. type IPAddress struct {
  63. Family int `json:"address_family"`
  64. Cidr int `json:"cidr"`
  65. Address string `json:"address"`
  66. Gateway string `json:"gateway"`
  67. Public bool `json:"public"`
  68. }
  69. func (n IPAddress) String() string {
  70. return Stringify(n)
  71. }
  72. // DeviceServiceOp implements DeviceService
  73. type DeviceServiceOp struct {
  74. client *Client
  75. }
  76. // List returns devices on a project
  77. func (s *DeviceServiceOp) List(projectID string) ([]Device, *Response, error) {
  78. path := fmt.Sprintf("%s/%s/devices?include=facility", projectBasePath, projectID)
  79. req, err := s.client.NewRequest("GET", path, nil)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. root := new(devicesRoot)
  84. resp, err := s.client.Do(req, root)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return root.Devices, resp, err
  89. }
  90. // Get returns a device by id
  91. func (s *DeviceServiceOp) Get(deviceID string) (*Device, *Response, error) {
  92. path := fmt.Sprintf("%s/%s?include=facility", deviceBasePath, deviceID)
  93. req, err := s.client.NewRequest("GET", path, nil)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. device := new(Device)
  98. resp, err := s.client.Do(req, device)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return device, resp, err
  103. }
  104. // Create creates a new device
  105. func (s *DeviceServiceOp) Create(createRequest *DeviceCreateRequest) (*Device, *Response, error) {
  106. path := fmt.Sprintf("%s/%s/devices", projectBasePath, createRequest.ProjectID)
  107. req, err := s.client.NewRequest("POST", path, createRequest)
  108. if err != nil {
  109. return nil, nil, err
  110. }
  111. device := new(Device)
  112. resp, err := s.client.Do(req, device)
  113. if err != nil {
  114. return nil, resp, err
  115. }
  116. return device, resp, err
  117. }
  118. // Delete deletes a device
  119. func (s *DeviceServiceOp) Delete(deviceID string) (*Response, error) {
  120. path := fmt.Sprintf("%s/%s", deviceBasePath, deviceID)
  121. req, err := s.client.NewRequest("DELETE", path, nil)
  122. if err != nil {
  123. return nil, err
  124. }
  125. resp, err := s.client.Do(req, nil)
  126. return resp, err
  127. }
  128. // Reboot reboots on a device
  129. func (s *DeviceServiceOp) Reboot(deviceID string) (*Response, error) {
  130. path := fmt.Sprintf("%s/%s/actions", deviceBasePath, deviceID)
  131. action := &DeviceActionRequest{Type: "reboot"}
  132. req, err := s.client.NewRequest("POST", path, action)
  133. if err != nil {
  134. return nil, err
  135. }
  136. resp, err := s.client.Do(req, nil)
  137. return resp, err
  138. }
  139. // PowerOff powers on a device
  140. func (s *DeviceServiceOp) PowerOff(deviceID string) (*Response, error) {
  141. path := fmt.Sprintf("%s/%s/actions", deviceBasePath, deviceID)
  142. action := &DeviceActionRequest{Type: "power_off"}
  143. req, err := s.client.NewRequest("POST", path, action)
  144. if err != nil {
  145. return nil, err
  146. }
  147. resp, err := s.client.Do(req, nil)
  148. return resp, err
  149. }
  150. // PowerOn powers on a device
  151. func (s *DeviceServiceOp) PowerOn(deviceID string) (*Response, error) {
  152. path := fmt.Sprintf("%s/%s/actions", deviceBasePath, deviceID)
  153. action := &DeviceActionRequest{Type: "power_on"}
  154. req, err := s.client.NewRequest("POST", path, action)
  155. if err != nil {
  156. return nil, err
  157. }
  158. resp, err := s.client.Do(req, nil)
  159. return resp, err
  160. }
  161. type lockDeviceType struct {
  162. Locked bool `json:"locked"`
  163. }
  164. // Lock sets a device to "locked"
  165. func (s *DeviceServiceOp) Lock(deviceID string) (*Response, error) {
  166. path := fmt.Sprintf("%s/%s", deviceBasePath, deviceID)
  167. action := lockDeviceType{Locked: true}
  168. req, err := s.client.NewRequest("PATCH", path, action)
  169. if err != nil {
  170. return nil, err
  171. }
  172. resp, err := s.client.Do(req, nil)
  173. return resp, err
  174. }
  175. // Unlock sets a device to "locked"
  176. func (s *DeviceServiceOp) Unlock(deviceID string) (*Response, error) {
  177. path := fmt.Sprintf("%s/%s", deviceBasePath, deviceID)
  178. action := lockDeviceType{Locked: false}
  179. req, err := s.client.NewRequest("PATCH", path, action)
  180. if err != nil {
  181. return nil, err
  182. }
  183. resp, err := s.client.Do(req, nil)
  184. return resp, err
  185. }