projects.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package packngo
  2. import "fmt"
  3. const projectBasePath = "/projects"
  4. // ProjectService interface defines available project methods
  5. type ProjectService interface {
  6. List() ([]Project, *Response, error)
  7. Get(string) (*Project, *Response, error)
  8. Create(*ProjectCreateRequest) (*Project, *Response, error)
  9. Update(*ProjectUpdateRequest) (*Project, *Response, error)
  10. Delete(string) (*Response, error)
  11. }
  12. type projectsRoot struct {
  13. Projects []Project `json:"projects"`
  14. }
  15. // Project represents a Packet project
  16. type Project struct {
  17. ID string `json:"id"`
  18. Name string `json:"name,omitempty"`
  19. Created string `json:"created_at,omitempty"`
  20. Updated string `json:"updated_at,omitempty"`
  21. Users []User `json:"members,omitempty"`
  22. Devices []Device `json:"devices,omitempty"`
  23. SSHKeys []SSHKey `json:"ssh_keys,omitempty"`
  24. URL string `json:"href,omitempty"`
  25. }
  26. func (p Project) String() string {
  27. return Stringify(p)
  28. }
  29. // ProjectCreateRequest type used to create a Packet project
  30. type ProjectCreateRequest struct {
  31. Name string `json:"name"`
  32. PaymentMethod string `json:"payment_method,omitempty"`
  33. }
  34. func (p ProjectCreateRequest) String() string {
  35. return Stringify(p)
  36. }
  37. // ProjectUpdateRequest type used to update a Packet project
  38. type ProjectUpdateRequest struct {
  39. ID string `json:"id"`
  40. Name string `json:"name,omitempty"`
  41. PaymentMethod string `json:"payment_method,omitempty"`
  42. }
  43. func (p ProjectUpdateRequest) String() string {
  44. return Stringify(p)
  45. }
  46. // ProjectServiceOp implements ProjectService
  47. type ProjectServiceOp struct {
  48. client *Client
  49. }
  50. // List returns the user's projects
  51. func (s *ProjectServiceOp) List() ([]Project, *Response, error) {
  52. req, err := s.client.NewRequest("GET", projectBasePath, nil)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. root := new(projectsRoot)
  57. resp, err := s.client.Do(req, root)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return root.Projects, resp, err
  62. }
  63. // Get returns a project by id
  64. func (s *ProjectServiceOp) Get(projectID string) (*Project, *Response, error) {
  65. path := fmt.Sprintf("%s/%s", projectBasePath, projectID)
  66. req, err := s.client.NewRequest("GET", path, nil)
  67. if err != nil {
  68. return nil, nil, err
  69. }
  70. project := new(Project)
  71. resp, err := s.client.Do(req, project)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return project, resp, err
  76. }
  77. // Create creates a new project
  78. func (s *ProjectServiceOp) Create(createRequest *ProjectCreateRequest) (*Project, *Response, error) {
  79. req, err := s.client.NewRequest("POST", projectBasePath, createRequest)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. project := new(Project)
  84. resp, err := s.client.Do(req, project)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return project, resp, err
  89. }
  90. // Update updates a project
  91. func (s *ProjectServiceOp) Update(updateRequest *ProjectUpdateRequest) (*Project, *Response, error) {
  92. path := fmt.Sprintf("%s/%s", projectBasePath, updateRequest.ID)
  93. req, err := s.client.NewRequest("PATCH", path, updateRequest)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. project := new(Project)
  98. resp, err := s.client.Do(req, project)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return project, resp, err
  103. }
  104. // Delete deletes a project
  105. func (s *ProjectServiceOp) Delete(projectID string) (*Response, error) {
  106. path := fmt.Sprintf("%s/%s", projectBasePath, projectID)
  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. }