-
Notifications
You must be signed in to change notification settings - Fork 3
/
account.go
79 lines (64 loc) · 1.82 KB
/
account.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
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ Accounts = (*accounts)(nil)
// Accounts describes methods for updating and reading account that the
// Scalr IACP API supports.
type Accounts interface {
Read(ctx context.Context, account string) (*Account, error)
Update(ctx context.Context, account string, options AccountUpdateOptions) (*Account, error)
}
// accounts implements Accounts.
type accounts struct {
client *Client
}
// Account represents a Scalr IACP account.
type Account struct {
ID string `jsonapi:"primary,accounts"`
Name string `jsonapi:"attr,name"`
AllowedIPs []string `jsonapi:"attr,allowed-ips"`
}
// Read a account by its ID.
func (s *accounts) Read(ctx context.Context, accountID string) (*Account, error) {
if !validStringID(&accountID) {
return nil, errors.New("invalid value for account ID")
}
u := fmt.Sprintf("accounts/%s", url.QueryEscape(accountID))
req, err := s.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
a := &Account{}
err = s.client.do(ctx, req, a)
if err != nil {
return nil, err
}
return a, nil
}
type AccountUpdateOptions struct {
ID string `jsonapi:"primary,accounts"`
AllowedIPs *[]string `jsonapi:"attr,allowed-ips,omitempty"`
}
func (s *accounts) Update(ctx context.Context, accountID string, options AccountUpdateOptions) (*Account, error) {
if !validStringID(&accountID) {
return nil, errors.New("invalid value for account ID")
}
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("accounts/%s", url.QueryEscape(accountID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
a := &Account{}
err = s.client.do(ctx, req, a)
if err != nil {
return nil, err
}
return a, nil
}