-
Notifications
You must be signed in to change notification settings - Fork 9
/
account.go
123 lines (101 loc) · 2.78 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
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
package cloud66
import (
"fmt"
"net/http"
"strconv"
"time"
)
type UnmanagedServer struct {
Vendor string `json:"vendor"`
Id string `json:"id"`
}
type Account struct {
Id int `json:"id"`
Owner string `json:"owner"`
Name string `json:"friendly_name"`
StackCount int `json:"stack_count"`
UsedClouds []string `json:"used_clouds"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CurrentAccount bool `json:"current_account"`
UnmanagedServers []UnmanagedServer `json:"unmanaged_servers"`
ServerRegistration string `json:"server_registration_script"`
ConfigStoreNamespace string `json:"configstore_namespace"`
}
type OTP struct {
OK bool `json:"ok"`
Code string `json:"otp"`
}
func (c *Client) AccountInfo(accountId int, getUnmanaged bool) (*Account, error) {
queryStrings := make(map[string]string)
queryStrings["include_servers"] = strconv.FormatBool(getUnmanaged)
req, err := c.NewRequest("GET", fmt.Sprintf("/accounts/%d.json", accountId), nil, queryStrings)
if err != nil {
return nil, err
}
var accountRes *Account
return accountRes, c.DoReq(req, &accountRes, nil)
}
func (c *Client) AccountInfos() ([]Account, error) {
queryStrings := make(map[string]string)
queryStrings["page"] = "1"
var p Pagination
var result []Account
var accountRes []Account
for {
req, err := c.NewRequest("GET", "/accounts.json", nil, queryStrings)
if err != nil {
return nil, err
}
accountRes = nil
err = c.DoReq(req, &accountRes, &p)
if err != nil {
return nil, err
}
result = append(result, accountRes...)
if p.Current < p.Next {
queryStrings["page"] = strconv.Itoa(p.Next)
} else {
break
}
}
return result, nil
}
// FetchJWT pulls a JWT token for the given aud and scope from Cloud 66
func (c *Client) FetchJWT(aud string, scope string) (string, error) {
var req *http.Request
var err error
params := struct {
Scope string `json:"scope"`
Aud string `json:"aud"`
}{
Scope: scope,
Aud: aud,
}
req, err = c.NewRequest("POST", "/jwt/fetch.json", params, nil)
if err != nil {
return "", err
}
var jwt string
err = c.DoReq(req, &jwt, nil)
if err != nil {
return "", err
}
return jwt, nil
}
// AccountOTP returns the OTP from the server. If accountId and stackId are passed
// (!= 0) then they will be passed up
func (c *Client) AccountOTP() (string, error) {
var req *http.Request
var err error
req, err = c.NewRequest("GET", "/accounts/otp.json", nil, nil)
if err != nil {
return "", err
}
var otp *OTP
err = c.DoReq(req, &otp, nil)
if err != nil {
return "", err
}
return otp.Code, nil
}