Skip to content

Commit

Permalink
data.current_account
Browse files Browse the repository at this point in the history
  • Loading branch information
kosta709 committed Jul 28, 2020
1 parent e2627f1 commit df0aeef
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
22 changes: 11 additions & 11 deletions client/current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (

// CurrentAccountUser spec
type CurrentAccountUser struct {
ID string
UserName string
Email string
ID string `json:"id,omitempty"`
UserName string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}

// CurrentAccount spec
type CurrentAccount struct {
ID string
Name string
Users map[string]CurrentAccountUser
Users []CurrentAccountUser
}

// GetCurrentAccount -
Expand All @@ -31,8 +31,8 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
if err != nil {
return nil, err
}

currentAccountX, err := objx.FromJSON(string(userResp))
userRespStr := string(userResp)
currentAccountX, err := objx.FromJSON(userRespStr)
if err != nil {
return nil, err
}
Expand All @@ -43,10 +43,10 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
}
currentAccount := &CurrentAccount{
Name: activeAccountName,
Users: make(map[string]CurrentAccountUser),
Users: make([]CurrentAccountUser, 0),
}

allAccountsI := currentAccountX.Get("account").MSISlice()
allAccountsI := currentAccountX.Get("account").InterSlice()
for _, accI := range(allAccountsI) {
accX := objx.New(accI)
if accX.Get("name").String() == activeAccountName {
Expand All @@ -73,14 +73,14 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
}
for _, userI := range(accountUsersI) {
userX := objx.New(userI)
userName := userX.Get("userX").String()
userName := userX.Get("userName").String()
email := userX.Get("email").String()
userID := userX.Get("_id").String()
currentAccount.Users[userName] = CurrentAccountUser{
currentAccount.Users= append(currentAccount.Users, CurrentAccountUser{
ID: userID,
UserName: userName,
Email: email,
}
})
}

return currentAccount, nil
Expand Down
3 changes: 0 additions & 3 deletions codefresh/data_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ func dataSourceAccount() *schema.Resource {
}
}


func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {


client := meta.(*cfClient.Client)
var account *cfClient.Account
var err error
Expand All @@ -53,7 +51,6 @@ func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
}

return mapDataAccountToResource(account, d)

}

func mapDataAccountToResource(account *cfClient.Account, d *schema.ResourceData) error {
Expand Down
33 changes: 25 additions & 8 deletions codefresh/data_current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func dataSourceCurrentAccount() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"id": {
"_id": {
Type: schema.TypeString,
Optional: true,
},
"users": {
Type: schema.TypeMap,
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -37,7 +37,7 @@ func dataSourceCurrentAccount() *schema.Resource {
},
},
},
}
},
},
}
}
Expand All @@ -48,7 +48,7 @@ func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) erro
var currentAccount *cfClient.CurrentAccount
var err error

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

return mapDataCurrentAccountToResource(team, d)
return mapDataCurrentAccountToResource(currentAccount, d)

}

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

d.Set("id", currentAccount.ID)
d.Set("name", currentAccount.Name)
d.Set("users", currentAccount.Users)
d.Set("_id", currentAccount.ID)
d.Set("name", currentAccount.Name)

// users := make(map[string](map[string]interface{}))
// for n, user := range currentAccount.Users {
// users[n] = make(map[string]interface{})
// users[n]["name"] = user.UserName
// users[n]["email"] = user.Email
// users[n]["id"] = user.ID
// }

// d.Set("users", []map[string](map[string]interface{}){users})
users := make([](map[string]interface{}), len(currentAccount.Users))
for n, user := range currentAccount.Users {
users[n] = make(map[string]interface{})
users[n]["name"] = user.UserName
users[n]["email"] = user.Email
users[n]["id"] = user.ID
}

d.Set("users", users)
return nil
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Provider() *schema.Provider {
"codefresh_idps": dataSourceIdps(),
"codefresh_account": dataSourceAccount(),
"codefresh_team": dataSourceTeam(),
"codefresh_current_account": dataSourceCurrentAccount(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_project": resourceProject(),
Expand Down
41 changes: 41 additions & 0 deletions docs/data/current_accont.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# current_account data module

Return current account (owner of the token) and its users
```hcl
provider "codefresh" {
api_url = var.api_url
token = var.token
}
data "codefresh_current_account" "acc" {
}
output "current_ac" {
value = data.codefresh_current_account.acc
}
```

The output example:
```
Outputs:
current_ac = {
"_id" = "5f1fd9044d0fc94ddff0d745"
"id" = "5f1fd9044d0fc94ddff0d745"
"name" = "acc1"
"users" = [
{
"email" = "[email protected]"
"id" = "5f1fd9094d0fc9c656f0d75a"
"name" = "user1"
},
{
"email" = "[email protected]"
"id" = "5f1fd9094d0fc93b52f0d75c"
"name" = "user3"
},
]
}
```
File renamed without changes.

0 comments on commit df0aeef

Please sign in to comment.