Skip to content

Commit df0aeef

Browse files
committed
data.current_account
1 parent e2627f1 commit df0aeef

File tree

6 files changed

+78
-22
lines changed

6 files changed

+78
-22
lines changed

client/current_account.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88

99
// CurrentAccountUser spec
1010
type CurrentAccountUser struct {
11-
ID string
12-
UserName string
13-
Email string
11+
ID string `json:"id,omitempty"`
12+
UserName string `json:"name,omitempty"`
13+
Email string `json:"email,omitempty"`
1414
}
1515

1616
// CurrentAccount spec
1717
type CurrentAccount struct {
1818
ID string
1919
Name string
20-
Users map[string]CurrentAccountUser
20+
Users []CurrentAccountUser
2121
}
2222

2323
// GetCurrentAccount -
@@ -31,8 +31,8 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
3131
if err != nil {
3232
return nil, err
3333
}
34-
35-
currentAccountX, err := objx.FromJSON(string(userResp))
34+
userRespStr := string(userResp)
35+
currentAccountX, err := objx.FromJSON(userRespStr)
3636
if err != nil {
3737
return nil, err
3838
}
@@ -43,10 +43,10 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
4343
}
4444
currentAccount := &CurrentAccount{
4545
Name: activeAccountName,
46-
Users: make(map[string]CurrentAccountUser),
46+
Users: make([]CurrentAccountUser, 0),
4747
}
4848

49-
allAccountsI := currentAccountX.Get("account").MSISlice()
49+
allAccountsI := currentAccountX.Get("account").InterSlice()
5050
for _, accI := range(allAccountsI) {
5151
accX := objx.New(accI)
5252
if accX.Get("name").String() == activeAccountName {
@@ -73,14 +73,14 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
7373
}
7474
for _, userI := range(accountUsersI) {
7575
userX := objx.New(userI)
76-
userName := userX.Get("userX").String()
76+
userName := userX.Get("userName").String()
7777
email := userX.Get("email").String()
7878
userID := userX.Get("_id").String()
79-
currentAccount.Users[userName] = CurrentAccountUser{
79+
currentAccount.Users= append(currentAccount.Users, CurrentAccountUser{
8080
ID: userID,
8181
UserName: userName,
8282
Email: email,
83-
}
83+
})
8484
}
8585

8686
return currentAccount, nil

codefresh/data_account.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ func dataSourceAccount() *schema.Resource {
2929
}
3030
}
3131

32-
3332
func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
3433

35-
3634
client := meta.(*cfClient.Client)
3735
var account *cfClient.Account
3836
var err error
@@ -53,7 +51,6 @@ func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
5351
}
5452

5553
return mapDataAccountToResource(account, d)
56-
5754
}
5855

5956
func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData) error {

codefresh/data_current_account.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ func dataSourceCurrentAccount() *schema.Resource {
1414
Type: schema.TypeString,
1515
Optional: true,
1616
},
17-
"id": {
17+
"_id": {
1818
Type: schema.TypeString,
1919
Optional: true,
2020
},
2121
"users": {
22-
Type: schema.TypeMap,
22+
Type: schema.TypeList,
2323
Optional: true,
2424
Elem: &schema.Resource{
2525
Schema: map[string]*schema.Schema{
@@ -37,7 +37,7 @@ func dataSourceCurrentAccount() *schema.Resource {
3737
},
3838
},
3939
},
40-
}
40+
},
4141
},
4242
}
4343
}
@@ -48,7 +48,7 @@ func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) erro
4848
var currentAccount *cfClient.CurrentAccount
4949
var err error
5050

51-
currentAccount, err = client.GetCurrentAccount
51+
currentAccount, err = client.GetCurrentAccount()
5252
if err != nil {
5353
return err
5454
}
@@ -57,7 +57,7 @@ func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) erro
5757
return fmt.Errorf("data.codefresh_current_account - failed to get current_account")
5858
}
5959

60-
return mapDataCurrentAccountToResource(team, d)
60+
return mapDataCurrentAccountToResource(currentAccount, d)
6161

6262
}
6363

@@ -68,9 +68,26 @@ func mapDataCurrentAccountToResource(currentAccount *cfClient.CurrentAccount, d
6868
}
6969
d.SetId(currentAccount.ID)
7070

71-
d.Set("id", currentAccount.ID)
72-
d.Set("name", currentAccount.Name)
73-
d.Set("users", currentAccount.Users)
71+
d.Set("_id", currentAccount.ID)
72+
d.Set("name", currentAccount.Name)
73+
74+
// users := make(map[string](map[string]interface{}))
75+
// for n, user := range currentAccount.Users {
76+
// users[n] = make(map[string]interface{})
77+
// users[n]["name"] = user.UserName
78+
// users[n]["email"] = user.Email
79+
// users[n]["id"] = user.ID
80+
// }
7481

82+
// d.Set("users", []map[string](map[string]interface{}){users})
83+
users := make([](map[string]interface{}), len(currentAccount.Users))
84+
for n, user := range currentAccount.Users {
85+
users[n] = make(map[string]interface{})
86+
users[n]["name"] = user.UserName
87+
users[n]["email"] = user.Email
88+
users[n]["id"] = user.ID
89+
}
90+
91+
d.Set("users", users)
7592
return nil
7693
}

codefresh/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func Provider() *schema.Provider {
3131
"codefresh_idps": dataSourceIdps(),
3232
"codefresh_account": dataSourceAccount(),
3333
"codefresh_team": dataSourceTeam(),
34+
"codefresh_current_account": dataSourceCurrentAccount(),
3435
},
3536
ResourcesMap: map[string]*schema.Resource{
3637
"codefresh_project": resourceProject(),

docs/data/current_accont.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# current_account data module
2+
3+
Return current account (owner of the token) and its users
4+
```hcl
5+
provider "codefresh" {
6+
api_url = var.api_url
7+
token = var.token
8+
}
9+
10+
data "codefresh_current_account" "acc" {
11+
12+
}
13+
14+
15+
output "current_ac" {
16+
value = data.codefresh_current_account.acc
17+
}
18+
```
19+
20+
The output example:
21+
```
22+
Outputs:
23+
24+
current_ac = {
25+
"_id" = "5f1fd9044d0fc94ddff0d745"
26+
"id" = "5f1fd9044d0fc94ddff0d745"
27+
"name" = "acc1"
28+
"users" = [
29+
{
30+
"email" = "kosta@codefresh.io"
31+
"id" = "5f1fd9094d0fc9c656f0d75a"
32+
"name" = "user1"
33+
},
34+
{
35+
"email" = "kosta@sysadmiral.io"
36+
"id" = "5f1fd9094d0fc93b52f0d75c"
37+
"name" = "user3"
38+
},
39+
]
40+
}
41+
```
File renamed without changes.

0 commit comments

Comments
 (0)