forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganizations.go
148 lines (120 loc) · 4.68 KB
/
organizations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package packngo
import "fmt"
// API documentation https://www.packet.net/developers/api/organizations/
const organizationBasePath = "/organizations"
// OrganizationService interface defines available organization methods
type OrganizationService interface {
List() ([]Organization, *Response, error)
Get(string) (*Organization, *Response, error)
Create(*OrganizationCreateRequest) (*Organization, *Response, error)
Update(*OrganizationUpdateRequest) (*Organization, *Response, error)
Delete(string) (*Response, error)
ListPaymentMethods(string) ([]PaymentMethod, *Response, error)
}
type organizationsRoot struct {
Organizations []Organization `json:"organizations"`
}
// Organization represents a Packet organization
type Organization struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Website string `json:"website,omitempty"`
Twitter string `json:"twitter,omitempty"`
Created string `json:"created_at,omitempty"`
Updated string `json:"updated_at,omitempty"`
Address Address `json:"address,omitempty"`
TaxID string `json:"tax_id,omitempty"`
MainPhone string `json:"main_phone,omitempty"`
BillingPhone string `json:"billing_phone,omitempty"`
CreditAmount float64 `json:"credit_amount,omitempty"`
Logo string `json:"logo,omitempty"`
LogoThumb string `json:"logo_thumb,omitempty"`
Projects []Project `json:"projects,omitempty"`
URL string `json:"href,omitempty"`
Users []User `json:"members,omitempty"`
Owners []User `json:"owners,omitempty"`
}
func (o Organization) String() string {
return Stringify(o)
}
// OrganizationCreateRequest type used to create a Packet organization
type OrganizationCreateRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Website string `json:"website"`
Twitter string `json:"twitter"`
Logo string `json:"logo"`
}
func (o OrganizationCreateRequest) String() string {
return Stringify(o)
}
// OrganizationUpdateRequest type used to update a Packet organization
type OrganizationUpdateRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Website string `json:"website"`
Twitter string `json:"twitter"`
Logo string `json:"logo"`
}
func (o OrganizationUpdateRequest) String() string {
return Stringify(o)
}
// OrganizationServiceOp implements OrganizationService
type OrganizationServiceOp struct {
client *Client
}
// List returns the user's organizations
func (s *OrganizationServiceOp) List() ([]Organization, *Response, error) {
root := new(organizationsRoot)
resp, err := s.client.DoRequest("GET", organizationBasePath, nil, root)
if err != nil {
return nil, resp, err
}
return root.Organizations, resp, err
}
// Get returns a organization by id
func (s *OrganizationServiceOp) Get(organizationID string) (*Organization, *Response, error) {
path := fmt.Sprintf("%s/%s", organizationBasePath, organizationID)
organization := new(Organization)
resp, err := s.client.DoRequest("GET", path, nil, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Create creates a new organization
func (s *OrganizationServiceOp) Create(createRequest *OrganizationCreateRequest) (*Organization, *Response, error) {
organization := new(Organization)
resp, err := s.client.DoRequest("POST", organizationBasePath, createRequest, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Update updates an organization
func (s *OrganizationServiceOp) Update(updateRequest *OrganizationUpdateRequest) (*Organization, *Response, error) {
path := fmt.Sprintf("%s/%s", organizationBasePath, updateRequest.ID)
organization := new(Organization)
resp, err := s.client.DoRequest("PATCH", path, updateRequest, organization)
if err != nil {
return nil, resp, err
}
return organization, resp, err
}
// Delete deletes an organizationID
func (s *OrganizationServiceOp) Delete(organizationID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", organizationBasePath, organizationID)
return s.client.DoRequest("DELETE", path, nil, nil)
}
// ListPaymentMethods returns PaymentMethods for an organization
func (s *OrganizationServiceOp) ListPaymentMethods(organizationID string) ([]PaymentMethod, *Response, error) {
url := fmt.Sprintf("%s/%s%s", organizationBasePath, organizationID, paymentMethodBasePath)
root := new(paymentMethodsRoot)
resp, err := s.client.DoRequest("GET", url, nil, root)
if err != nil {
return nil, resp, err
}
return root.PaymentMethods, resp, err
}