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

Diff for: .gitignore

+5
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/

Diff for: client/current_account.go

+87
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+
}

Diff for: codefresh/data_current_account.go

+76
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+
}

Diff for: codefresh/resource_team.go

+4
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

Diff for: docs/modules/account_token.md

+44
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)

Diff for: docs/modules/accounts_users.md

+4
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)

Diff for: docs/modules/permissions.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# module permissions
2+

Diff for: docs/modules/teams.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# module teams

Diff for: examples/account_tokens/main.tf

+24
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+
}

Diff for: examples/account_tokens/terraform.tfvars

+5
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+
]

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.0-rc.2.0.20200717132200-7435e2abc9d1
1111
github.com/imdario/mergo v0.3.9
12+
github.com/stretchr/objx v0.1.1
1213
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 // indirect
1314
)
1415

Diff for: tf_modules/change_account/main.tf renamed to tf_modules/account_token/main.tf

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
data "codefresh_account" "acc" {
2-
name = var.account_id
2+
name = var.account_name != ""? var.account_name : ""
3+
_id = var.account_id != ""? var.account_id : ""
4+
35
}
46

57
resource "random_string" "random" {
@@ -36,4 +38,12 @@ resource "codefresh_api_key" "new" {
3638

3739
output "token" {
3840
value = codefresh_api_key.new.token
41+
}
42+
43+
output "account_name" {
44+
value = data.codefresh_account.acc.name
45+
}
46+
47+
output "account_id" {
48+
value = data.codefresh_account.acc.id
3949
}
File renamed without changes.

Diff for: tf_modules/change_account/vars.tf renamed to tf_modules/account_token/vars.tf

+6
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ variable token {
1010

1111
variable account_id {
1212
type = string
13+
default = ""
1314
}
15+
16+
variable account_name {
17+
type = string
18+
default = ""
19+
}

Diff for: tf_modules/account_tokens/main.tf

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
data "codefresh_account" "acc" {
2+
for_each = var.accounts
3+
name = each.value
4+
}
5+
6+
resource "random_string" "random" {
7+
for_each = var.accounts
8+
length = 5
9+
special = false
10+
}
11+
12+
resource "codefresh_api_key" "new" {
13+
for_each = var.accounts
14+
account_id = data.codefresh_account.acc[each.value].id
15+
user_id = data.codefresh_account.acc[each.value].admins[0]
16+
name = "tfkey_${random_string.random[each.value].result}"
17+
18+
scopes = [
19+
"agent",
20+
"agents",
21+
"audit",
22+
"build",
23+
"cluster",
24+
"clusters",
25+
"environments-v2",
26+
"github-action",
27+
"helm",
28+
"kubernetes",
29+
"pipeline",
30+
"project",
31+
"repos",
32+
"runner-installation",
33+
"step-type",
34+
"step-types",
35+
"view",
36+
"workflow",
37+
]
38+
}
39+
40+
output "tokens" {
41+
value = {
42+
for acc, token in codefresh_api_key.new:
43+
acc => token.token
44+
}
45+
}

Diff for: tf_modules/account_tokens/provider.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "codefresh" {
2+
api_url = var.api_url
3+
token = var.admin_token
4+
}

Diff for: tf_modules/account_tokens/vars.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable api_url {
2+
type = string
3+
}
4+
5+
#
6+
variable admin_token {
7+
type = string
8+
default = ""
9+
}
10+
11+
12+
## Set of account names
13+
variable accounts {
14+
type = set(string)
15+
}

0 commit comments

Comments
 (0)