-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcurrent_account.go
118 lines (104 loc) · 3.2 KB
/
current_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
package cfclient
import (
"encoding/json"
"fmt"
"github.com/stretchr/objx"
"golang.org/x/exp/slices"
)
// CurrentAccountUser spec
type CurrentAccountUser struct {
ID string `json:"id,omitempty"`
UserName string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Status string `json:"status,omitempty"`
}
// CurrentAccount spec
type CurrentAccount struct {
ID string
Name string
Users []CurrentAccountUser
Admins []CurrentAccountUser
FeatureFlags map[string]bool
}
// GetCurrentAccount -
func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
// get and parse current account
userResp, err := client.RequestAPI(&RequestOptions{
Path: "/user",
Method: "GET",
})
if err != nil {
return nil, err
}
userRespStr := string(userResp)
currentAccountX, err := objx.FromJSON(userRespStr)
if err != nil {
return nil, err
}
activeAccountName := currentAccountX.Get("activeAccountName").String()
if activeAccountName == "" {
return nil, fmt.Errorf("GetCurrentAccount - cannot get activeAccountName")
}
currentAccount := &CurrentAccount{
Name: activeAccountName,
Users: make([]CurrentAccountUser, 0),
Admins: make([]CurrentAccountUser, 0),
FeatureFlags: make(map[string]bool),
}
accountAdminsIDs := make([]string, 0)
allAccountsI := currentAccountX.Get("account").InterSlice()
for _, accI := range allAccountsI {
accX := objx.New(accI)
if accX.Get("name").String() == activeAccountName {
currentAccount.ID = accX.Get("id").String()
admins := accX.Get("admins").InterSlice()
for _, adminI := range admins {
accountAdminsIDs = append(accountAdminsIDs, adminI.(string))
}
featureFlags := accX.Get("features").ObjxMap()
for k, v := range featureFlags {
currentAccount.FeatureFlags[k] = v.(bool)
}
break
}
}
if currentAccount.ID == "" {
return nil, fmt.Errorf("GetCurrentAccount - cannot get activeAccountName")
}
// get and parse account users
accountUsersResp, err := client.RequestAPI(&RequestOptions{
Path: fmt.Sprintf("/accounts/%s/users", currentAccount.ID),
Method: "GET",
})
if err != nil {
return nil, err
}
accountUsersI := make([]interface{}, 0)
if e := json.Unmarshal(accountUsersResp, &accountUsersI); e != nil {
return nil, fmt.Errorf("cannot unmarshal accountUsers responce for accountId=%s: %v", currentAccount.ID, e)
}
for _, userI := range accountUsersI {
userX := objx.New(userI)
userName := userX.Get("userName").String()
email := userX.Get("email").String()
status := userX.Get("status").String()
userID := userX.Get("_id").String()
currentAccount.Users = append(currentAccount.Users, CurrentAccountUser{
ID: userID,
UserName: userName,
Email: email,
Status: status,
})
// If user exists in Admin list append it to addmins as well. This assumes that a user cannot be an admin without being a regular user too.
// Which is indeed the case currenty in Codefresh
if slices.Contains(accountAdminsIDs, userID) {
currentAccount.Admins = append(currentAccount.Admins, CurrentAccountUser{
ID: userID,
UserName: userName,
Email: email,
Status: status,
})
}
}
return currentAccount, nil
}