-
Notifications
You must be signed in to change notification settings - Fork 40
/
subaccounts.go
211 lines (179 loc) · 5.89 KB
/
subaccounts.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package gosparkpost
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// SubaccountsPathFormat provides an easy way to fill out the path including the version.
var SubaccountsPathFormat = "/api/v%d/subaccounts"
// SubaccountGrants contains the grants that will be given to new subaccounts by default.
var SubaccountGrants = []string{
"smtp/inject",
"sending_domains/manage",
"message_events/view",
"suppression_lists/manage",
"tracking_domains/view",
"tracking_domains/manage",
"transmissions/view",
"transmissions/modify",
}
// SubaccountStatuses contains valid subaccount statuses.
var SubaccountStatuses = []string{
"active",
"suspended",
"terminated",
}
// Subaccount is the JSON structure accepted by and returned from the SparkPost Subaccounts API.
type Subaccount struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
KeyLabel string `json:"key_label,omitempty"`
Grants []string `json:"key_grants,omitempty"`
ShortKey string `json:"short_key,omitempty"`
Status string `json:"status,omitempty"`
ComplianceStatus string `json:"compliance_status,omitempty"`
IPPool string `json:"ip_pool,omitempty"`
}
// SubaccountCreate attempts to create a subaccount using the provided object
func (c *Client) SubaccountCreate(s *Subaccount) (res *Response, err error) {
return c.SubaccountCreateContext(context.Background(), s)
}
// SubaccountCreateContext is the same as SubaccountCreate, and it allows the caller to pass in a context
// New subaccounts will have all grants in SubaccountGrants, unless s.Grants is non-nil.
func (c *Client) SubaccountCreateContext(ctx context.Context, s *Subaccount) (res *Response, err error) {
// enforce required parameters
if s == nil {
err = errors.New("Create called with nil Subaccount")
return
}
if len(s.Grants) == 0 {
s.Grants = SubaccountGrants
}
// Marshaling a static type won't fail
jsonBytes, _ := json.Marshal(s)
path := fmt.Sprintf(SubaccountsPathFormat, c.Config.ApiVersion)
res, err = c.HttpPost(ctx, c.Config.BaseUrl+path, jsonBytes)
if err != nil {
return
}
if _, err = res.AssertJson(); err != nil {
return
}
err = res.ParseResponse()
if err != nil {
return
}
if Is2XX(res.HTTP.StatusCode) {
var ok bool
var results map[string]interface{}
if results, ok = res.Results.(map[string]interface{}); !ok {
err = errors.New("Unexpected response to Subaccount creation (results)")
} else if f, ok := results["subaccount_id"].(float64); !ok {
err = errors.New("Unexpected response to Subaccount creation (subaccount_id)")
} else {
s.ID = int(f)
if s.ShortKey, ok = results["short_key"].(string); !ok {
err = errors.New("Unexpected response to Subaccount creation (short_key)")
}
}
} else {
err = res.HTTPError()
}
return
}
// SubaccountUpdate updates a subaccount with the specified id.
// It marshals and sends all the subaccount fields, ignoring the read-only ones.
func (c *Client) SubaccountUpdate(s *Subaccount) (res *Response, err error) {
return c.SubaccountUpdateContext(context.Background(), s)
}
// SubaccountUpdateContext is the same as SubaccountUpdate, and it allows the caller to provide a context
func (c *Client) SubaccountUpdateContext(ctx context.Context, s *Subaccount) (res *Response, err error) {
if s == nil {
err = errors.New("Subaccount Update called with nil Subaccount")
} else if s.Status != "" {
found := false
for _, v := range SubaccountStatuses {
if s.Status == v {
found = true
}
}
if !found {
err = errors.New("Not a valid subaccount status")
}
}
if err != nil {
return
}
// Marshaling a static type won't fail
jsonBytes, _ := json.Marshal(s)
path := fmt.Sprintf(SubaccountsPathFormat, c.Config.ApiVersion)
url := fmt.Sprintf("%s%s/%d", c.Config.BaseUrl, path, s.ID)
return c.HttpPutJson(ctx, url, jsonBytes)
}
// Subaccounts returns metadata for all Subaccounts in the system.
func (c *Client) Subaccounts() (subaccounts []Subaccount, res *Response, err error) {
return c.SubaccountsContext(context.Background())
}
// SubaccountsContext is the same as Subaccounts, and it allows the caller to provide a context
func (c *Client) SubaccountsContext(ctx context.Context) (subaccounts []Subaccount, res *Response, err error) {
path := fmt.Sprintf(SubaccountsPathFormat, c.Config.ApiVersion)
res, err = c.HttpGet(ctx, c.Config.BaseUrl+path)
if err != nil {
return
}
var body []byte
if body, err = res.AssertJson(); err != nil {
return
}
if Is2XX(res.HTTP.StatusCode) {
slist := map[string][]Subaccount{}
err = json.Unmarshal(body, &slist)
if err != nil {
} else if list, ok := slist["results"]; ok {
subaccounts = list
} else {
err = errors.New("Unexpected response to Subaccount list")
}
} else {
err = res.ParseResponse()
if err == nil {
err = res.HTTPError()
}
}
return
}
// Subaccount looks up a subaccount using the provided id
func (c *Client) Subaccount(id int) (subaccount *Subaccount, res *Response, err error) {
return c.SubaccountContext(context.Background(), id)
}
// SubaccountContext is the same as Subaccount, and it accepts a context.Context
func (c *Client) SubaccountContext(ctx context.Context, id int) (subaccount *Subaccount, res *Response, err error) {
path := fmt.Sprintf(SubaccountsPathFormat, c.Config.ApiVersion)
u := fmt.Sprintf("%s%s/%d", c.Config.BaseUrl, path, id)
res, err = c.HttpGet(ctx, u)
if err != nil {
return
}
var body []byte
if body, err = res.AssertJson(); err != nil {
return
}
if Is2XX(res.HTTP.StatusCode) {
slist := map[string]Subaccount{}
err = json.Unmarshal(body, &slist)
if err != nil {
} else if s, ok := slist["results"]; ok {
subaccount = &s
} else {
err = errors.New("Unexpected response to Subaccount")
}
} else {
err = res.ParseResponse()
if err == nil {
err = res.HTTPError()
}
}
return
}