123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package packngo
- import "fmt"
- const projectBasePath = "/projects"
- // ProjectService interface defines available project methods
- type ProjectService interface {
- List() ([]Project, *Response, error)
- Get(string) (*Project, *Response, error)
- Create(*ProjectCreateRequest) (*Project, *Response, error)
- Update(*ProjectUpdateRequest) (*Project, *Response, error)
- Delete(string) (*Response, error)
- }
- type projectsRoot struct {
- Projects []Project `json:"projects"`
- }
- // Project represents a Packet project
- type Project struct {
- ID string `json:"id"`
- Name string `json:"name,omitempty"`
- Created string `json:"created_at,omitempty"`
- Updated string `json:"updated_at,omitempty"`
- Users []User `json:"members,omitempty"`
- Devices []Device `json:"devices,omitempty"`
- SSHKeys []SSHKey `json:"ssh_keys,omitempty"`
- URL string `json:"href,omitempty"`
- }
- func (p Project) String() string {
- return Stringify(p)
- }
- // ProjectCreateRequest type used to create a Packet project
- type ProjectCreateRequest struct {
- Name string `json:"name"`
- PaymentMethod string `json:"payment_method,omitempty"`
- }
- func (p ProjectCreateRequest) String() string {
- return Stringify(p)
- }
- // ProjectUpdateRequest type used to update a Packet project
- type ProjectUpdateRequest struct {
- ID string `json:"id"`
- Name string `json:"name,omitempty"`
- PaymentMethod string `json:"payment_method,omitempty"`
- }
- func (p ProjectUpdateRequest) String() string {
- return Stringify(p)
- }
- // ProjectServiceOp implements ProjectService
- type ProjectServiceOp struct {
- client *Client
- }
- // List returns the user's projects
- func (s *ProjectServiceOp) List() ([]Project, *Response, error) {
- req, err := s.client.NewRequest("GET", projectBasePath, nil)
- if err != nil {
- return nil, nil, err
- }
- root := new(projectsRoot)
- resp, err := s.client.Do(req, root)
- if err != nil {
- return nil, resp, err
- }
- return root.Projects, resp, err
- }
- // Get returns a project by id
- func (s *ProjectServiceOp) Get(projectID string) (*Project, *Response, error) {
- path := fmt.Sprintf("%s/%s", projectBasePath, projectID)
- req, err := s.client.NewRequest("GET", path, nil)
- if err != nil {
- return nil, nil, err
- }
- project := new(Project)
- resp, err := s.client.Do(req, project)
- if err != nil {
- return nil, resp, err
- }
- return project, resp, err
- }
- // Create creates a new project
- func (s *ProjectServiceOp) Create(createRequest *ProjectCreateRequest) (*Project, *Response, error) {
- req, err := s.client.NewRequest("POST", projectBasePath, createRequest)
- if err != nil {
- return nil, nil, err
- }
- project := new(Project)
- resp, err := s.client.Do(req, project)
- if err != nil {
- return nil, resp, err
- }
- return project, resp, err
- }
- // Update updates a project
- func (s *ProjectServiceOp) Update(updateRequest *ProjectUpdateRequest) (*Project, *Response, error) {
- path := fmt.Sprintf("%s/%s", projectBasePath, updateRequest.ID)
- req, err := s.client.NewRequest("PATCH", path, updateRequest)
- if err != nil {
- return nil, nil, err
- }
- project := new(Project)
- resp, err := s.client.Do(req, project)
- if err != nil {
- return nil, resp, err
- }
- return project, resp, err
- }
- // Delete deletes a project
- func (s *ProjectServiceOp) Delete(projectID string) (*Response, error) {
- path := fmt.Sprintf("%s/%s", projectBasePath, projectID)
- req, err := s.client.NewRequest("DELETE", path, nil)
- if err != nil {
- return nil, err
- }
- resp, err := s.client.Do(req, nil)
- return resp, err
- }
|