Skip to content

Commit e2627f1

Browse files
committed
current account
1 parent 16485e9 commit e2627f1

File tree

18 files changed

+335
-2
lines changed

18 files changed

+335
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
terraform-provider-codefresh
22
dist/
3+
4+
**/.terraform
5+
**/terraform.tfstate
6+
**/terraform.tfstate.backup
7+
tests/

client/current_account.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
"encoding/json"
6+
"github.com/stretchr/objx"
7+
)
8+
9+
// CurrentAccountUser spec
10+
type CurrentAccountUser struct {
11+
ID string
12+
UserName string
13+
Email string
14+
}
15+
16+
// CurrentAccount spec
17+
type CurrentAccount struct {
18+
ID string
19+
Name string
20+
Users map[string]CurrentAccountUser
21+
}
22+
23+
// GetCurrentAccount -
24+
func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
25+
26+
// get and parse current account
27+
userResp, err := client.RequestAPI(&RequestOptions{
28+
Path: "/user",
29+
Method: "GET",
30+
})
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
currentAccountX, err := objx.FromJSON(string(userResp))
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
activeAccountName := currentAccountX.Get("activeAccountName").String()
41+
if activeAccountName == "" {
42+
return nil, fmt.Errorf("GetCurrentAccount - cannot get activeAccountName")
43+
}
44+
currentAccount := &CurrentAccount{
45+
Name: activeAccountName,
46+
Users: make(map[string]CurrentAccountUser),
47+
}
48+
49+
allAccountsI := currentAccountX.Get("account").MSISlice()
50+
for _, accI := range(allAccountsI) {
51+
accX := objx.New(accI)
52+
if accX.Get("name").String() == activeAccountName {
53+
currentAccount.ID = accX.Get("id").String()
54+
break
55+
}
56+
}
57+
if currentAccount.ID == "" {
58+
return nil, fmt.Errorf("GetCurrentAccount - cannot get activeAccountName")
59+
}
60+
61+
// get and parse account users
62+
accountUsersResp, err := client.RequestAPI(&RequestOptions{
63+
Path: fmt.Sprintf("/accounts/%s/users", currentAccount.ID),
64+
Method: "GET",
65+
})
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
accountUsersI := make([]interface{}, 0)
71+
if e := json.Unmarshal(accountUsersResp, &accountUsersI); e != nil {
72+
return nil, fmt.Errorf("Cannot unmarshal accountUsers responce for accountId=%s: %v", currentAccount.ID, e)
73+
}
74+
for _, userI := range(accountUsersI) {
75+
userX := objx.New(userI)
76+
userName := userX.Get("userX").String()
77+
email := userX.Get("email").String()
78+
userID := userX.Get("_id").String()
79+
currentAccount.Users[userName] = CurrentAccountUser{
80+
ID: userID,
81+
UserName: userName,
82+
Email: email,
83+
}
84+
}
85+
86+
return currentAccount, nil
87+
}

codefresh/data_current_account.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceCurrentAccount() *schema.Resource {
10+
return &schema.Resource{
11+
Read: dataSourceCurrentAccountRead,
12+
Schema: map[string]*schema.Schema{
13+
"name": {
14+
Type: schema.TypeString,
15+
Optional: true,
16+
},
17+
"id": {
18+
Type: schema.TypeString,
19+
Optional: true,
20+
},
21+
"users": {
22+
Type: schema.TypeMap,
23+
Optional: true,
24+
Elem: &schema.Resource{
25+
Schema: map[string]*schema.Schema{
26+
"id": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"name": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
},
34+
"email": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
},
39+
},
40+
}
41+
},
42+
}
43+
}
44+
45+
46+
func dataSourceCurrentAccountRead(d *schema.ResourceData, meta interface{}) error {
47+
client := meta.(*cfClient.Client)
48+
var currentAccount *cfClient.CurrentAccount
49+
var err error
50+
51+
currentAccount, err = client.GetCurrentAccount
52+
if err != nil {
53+
return err
54+
}
55+
56+
if currentAccount == nil {
57+
return fmt.Errorf("data.codefresh_current_account - failed to get current_account")
58+
}
59+
60+
return mapDataCurrentAccountToResource(team, d)
61+
62+
}
63+
64+
func mapDataCurrentAccountToResource(currentAccount *cfClient.CurrentAccount, d *schema.ResourceData) error {
65+
66+
if currentAccount == nil || currentAccount.ID == "" {
67+
return fmt.Errorf("data.codefresh_current_account - failed to mapDataCurrentAccountToResource")
68+
}
69+
d.SetId(currentAccount.ID)
70+
71+
d.Set("id", currentAccount.ID)
72+
d.Set("name", currentAccount.Name)
73+
d.Set("users", currentAccount.Users)
74+
75+
return nil
76+
}

codefresh/resource_team.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codefresh
22

33
import (
4+
"fmt"
45
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
56
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
67
)
@@ -136,6 +137,9 @@ func resourceTeamDelete(d *schema.ResourceData, meta interface{}) error {
136137

137138
func mapTeamToResource(team *cfClient.Team, d *schema.ResourceData) error {
138139

140+
if team == nil {
141+
return fmt.Errorf("mapTeamToResource - cannot find team")
142+
}
139143
err := d.Set("name", team.Name)
140144
if err != nil {
141145
return err

docs/modules/account_token.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# modules account_token and account_tokens
2+
3+
To operate with Teams and Permission we should use token generated for Codefresh Account user (not adminCF token)
4+
5+
6+
[account_token](../../tf_modules/account_token) - creates and outputs token of single account, for usage in aliased providers
7+
8+
[account_tokens](../../tf_modules/account_tokens) - creates and outputs token for multiple accounts, for usage in other per-account configurations
9+
10+
### Example - account_token
11+
```hcl
12+
module "account_token" "acc1_token" {
13+
source = "../../tf_modules/account_token"
14+
account_name = "acc1"
15+
}
16+
17+
provider "codefresh" {
18+
alias = "acc1"
19+
api_url = var.api_url
20+
token = module.change_account.acc1_token.token
21+
}
22+
23+
resource "codefresh_team" "developers" {
24+
provider = codefresh.acc1
25+
name = "developers"
26+
account_id = data.codefresh_account.acc.id
27+
28+
users = [
29+
data.codefresh_user.user.id
30+
]
31+
}
32+
33+
resource "codefresh_permission" "permission" {
34+
for_each = toset(["run", "create", "update", "delete", "read", "approve"])
35+
provider = codefresh.acc1
36+
team = codefresh_team.developers.id
37+
action = each.value
38+
resource = "pipeline"
39+
tags = [ "*", "untagged"]
40+
}
41+
42+
```
43+
44+
### [Example account-tokens](../../examples/account_tokens)

docs/modules/accounts_users.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# module account_users
2+
3+
[accounts_users source](../../tf_modules/accounts_users)
4+
[accounts_users example](../../examples/accounts_users)

docs/modules/permissions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# module permissions
2+

docs/modules/teams.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# module teams

examples/account_tokens/main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable api_url {
2+
type = string
3+
}
4+
5+
#
6+
variable token {
7+
type = string
8+
default = ""
9+
}
10+
11+
## Set of account names
12+
variable accounts {
13+
type = set(string)
14+
}
15+
16+
module "account_tokens" {
17+
source = "../../tf_modules/account_tokens"
18+
api_url = var.api_url
19+
accounts = var.accounts
20+
}
21+
22+
output "account_tokens" {
23+
value = module.account_tokens.tokens
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
api_url = "https://my-codefresh-example.com/api"
2+
3+
accounts = [
4+
"acc1", "acc2"
5+
]

0 commit comments

Comments
 (0)