Skip to content

Commit 3e20eeb

Browse files
committed
tf_module access_control and examples
1 parent 11f52ba commit 3e20eeb

File tree

7 files changed

+183
-4
lines changed

7 files changed

+183
-4
lines changed

codefresh/provider.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ func Provider() *schema.Provider {
2222
},
2323
"token": {
2424
Type: schema.TypeString,
25-
Required: true,
26-
DefaultFunc: schema.EnvDefaultFunc("CODEFRESH_API_KEY", ""),
25+
Optional: true,
2726
},
2827
},
2928
DataSourcesMap: map[string]*schema.Resource{
@@ -49,6 +48,8 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
4948

5049
apiURL := d.Get("api_url").(string)
5150
token := d.Get("token").(string)
52-
51+
if token == "" {
52+
token = os.Getenv("CODEFRESH_API_KEY")
53+
}
5354
return cfClient.NewClient(apiURL, token), nil
5455
}

codefresh/resource_account.go

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func resourceAccount() *schema.Resource {
5353
"data_retention_weeks": {
5454
Type: schema.TypeInt,
5555
Optional: true,
56+
Default: 5,
5657
},
5758
},
5859
},
@@ -68,6 +69,7 @@ func resourceAccount() *schema.Resource {
6869
},
6970
"nodes": {
7071
Type: schema.TypeInt,
72+
Default: 1,
7173
Optional: true,
7274
},
7375
},

docs/resources/account.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ resource "codefresh_account" "test" {
1818
parallel = 27
1919
}
2020
21+
features = {
22+
OfflineLogging = true,
23+
ssoManagement = true,
24+
teamsManagement = true,
25+
abac = true,
26+
customKubernetesCluster = true,
27+
launchDarklyManagement = false,
28+
}
2129
}
2230
```
2331

@@ -26,7 +34,7 @@ resource "codefresh_account" "test" {
2634
- `name` - (Required) The display name for the account.
2735
- `limits` - (Optional) A collection of `limits` blocks as documented below.
2836
- `build` - (Optional) A collection of `build` blocks as documented below.
29-
37+
- `features` - (Optional) map of supported features toggles
3038
---
3139

3240
`limits` supports the following:

tf_modules/access_control/main.tf

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
data "codefresh_idps" "idps" {
2+
for_each = var.default_idps
3+
_id = lookup(each.value, "_id", "")
4+
display_name = lookup(each.value, "display_name", "")
5+
client_name = lookup(each.value, "client_name", "")
6+
client_type = lookup(each.value, "client_type", "")
7+
}
8+
9+
resource "codefresh_account" "acc" {
10+
for_each = var.accounts
11+
name = each.key
12+
13+
features = var.default_account_features
14+
15+
limits {
16+
collaborators = lookup(var.default_acccount_limits, "collaborators", 10)
17+
}
18+
19+
build {
20+
parallel = lookup(var.default_acccount_limits, "parallel_builds", 1)
21+
}
22+
23+
}
24+
25+
resource "codefresh_idp_accounts" "acc_idp" {
26+
for_each = var.default_idps
27+
idp_id = data.codefresh_idps.idps[each.key].id
28+
account_ids = values(codefresh_account.acc)[*].id
29+
}
30+
31+
resource "codefresh_user" "users" {
32+
for_each = var.users
33+
user_name = each.key
34+
email = each.value.email
35+
36+
accounts = [
37+
for acc_name in each.value.accounts: codefresh_account.acc[acc_name].id
38+
]
39+
40+
activate = true
41+
42+
roles = each.value.global_admin ? ["Admin","User"] : ["User"]
43+
44+
dynamic "login" {
45+
for_each = var.default_idps
46+
content {
47+
idp_id = data.codefresh_idps.idps[login.key].id
48+
sso = login.value.sso
49+
}
50+
}
51+
52+
personal {
53+
first_name = each.value.personal.first_name
54+
last_name = each.value.personal.last_name
55+
}
56+
}
57+
58+
resource "codefresh_account_admins" "acc_admins" {
59+
for_each = toset(flatten([
60+
for u in var.users:
61+
u.admin_of_accounts if length(u.admin_of_accounts) > 0
62+
]))
63+
64+
account_id = codefresh_account.acc[each.value].id
65+
users = [
66+
for k, u in var.users:
67+
codefresh_user.users[k].id if contains(u.admin_of_accounts, each.key)
68+
]
69+
}

tf_modules/access_control/output.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "idps" {
2+
value = {
3+
for idp in data.codefresh_idps.idps:
4+
idp.id => { client_name = idp.client_name,
5+
display_name = idp.display_name
6+
}
7+
}
8+
}
9+
output "accounts" {
10+
value = {
11+
for acc in codefresh_account.acc:
12+
acc.id => acc.name
13+
}
14+
}

tf_modules/access_control/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.token
4+
}

tf_modules/access_control/vars.tf

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
variable api_url {
2+
type = string
3+
}
4+
5+
#
6+
variable token {
7+
type = string
8+
default = ""
9+
}
10+
11+
variable default_account_features {
12+
type = map(any)
13+
default = {
14+
OfflineLogging = true,
15+
ssoManagement = true,
16+
teamsManagement = true,
17+
abac = true,
18+
customKubernetesCluster = true,
19+
launchDarklyManagement = false,
20+
}
21+
}
22+
23+
variable default_acccount_limits {
24+
type = map(any)
25+
default = {
26+
collaborators = 100
27+
parallel_builds = 10
28+
}
29+
}
30+
31+
variable default_idps {
32+
type = map(any)
33+
default = {
34+
local = {
35+
display_name = "local"
36+
sso = false
37+
}
38+
}
39+
}
40+
41+
# map of accounts indexed by unique account name
42+
# accounts = {
43+
# acc1 = {
44+
# }
45+
# acc2 = {
46+
# limits = {
47+
# collaborators = 50
48+
# parallel_builds = 5
49+
# }
50+
# }
51+
# }
52+
variable accounts {
53+
type = map(any)
54+
}
55+
56+
# map of users:
57+
# users = {
58+
# user1 = {
59+
# email = "[email protected]"
60+
# personal = {
61+
# first_name = "Q"
62+
# last_name = "D"
63+
# }
64+
# accounts = ["acc1", "acc2"]
65+
# global_admin = false
66+
# }
67+
# user2 = {
68+
69+
# email = "[email protected]"
70+
# personal = {
71+
# first_name = "Q"
72+
# last_name = "D"
73+
# }
74+
# accounts = ["acc1", "acc2"]
75+
# global_admin = true
76+
# }
77+
# }
78+
variable users {
79+
//type = map(any)
80+
}
81+

0 commit comments

Comments
 (0)