/
resource_account.go
223 lines (191 loc) · 5.15 KB
/
resource_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package codefresh
import (
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAccount() *schema.Resource {
return &schema.Resource{
Description: `
By creating different accounts for different teams within the same company a customer can achieve complete segregation of assets between the teams.
`,
Create: resourceAccountCreate,
Read: resourceAccountRead,
Update: resourceAccountUpdate,
Delete: resourceAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Description: "The display name for the account.",
Type: schema.TypeString,
Required: true,
},
"features": {
Description: `
Features toggles for this account. Default:
* OfflineLogging: true
* ssoManagement: true
* teamsManagement: true
* abac: true
* customKubernetesCluster: true
`,
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeBool,
},
Default: map[string]bool{
"OfflineLogging": true,
"ssoManagement": true,
"teamsManagement": true,
"abac": true,
"customKubernetesCluster": true,
},
},
"limits": {
Description: "Limits for this account.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"collaborators": {
Description: "The number of collaborators allowed for this account.",
Type: schema.TypeInt,
Required: true,
},
"data_retention_weeks": {
Description: "Specifies the number of weeks for which to store the builds (default: `5`).",
Type: schema.TypeInt,
Optional: true,
Default: 5,
},
},
},
},
"build": {
Description: "Build limits for this account.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"parallel": {
Description: "The number of parallel builds allowed for this account.",
Type: schema.TypeInt,
Required: true,
},
"nodes": {
Description: "The number of nodes allowed for this account (default: `1`).",
Type: schema.TypeInt,
Default: 1,
Optional: true,
},
},
},
},
},
}
}
func resourceAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
account := *mapResourceToAccount(d)
resp, err := client.CreateAccount(&account)
if err != nil {
return err
}
d.SetId(resp.ID)
return nil
}
func resourceAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
accountID := d.Id()
if accountID == "" {
d.SetId("")
return nil
}
team, err := client.GetAccountByID(accountID)
if err != nil {
return err
}
err = mapAccountToResource(team, d)
if err != nil {
return err
}
return nil
}
func resourceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
account := *mapResourceToAccount(d)
_, err := client.UpdateAccount(&account)
if err != nil {
return err
}
return nil
}
func resourceAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cfclient.Client)
err := client.DeleteAccount(d.Id())
if err != nil {
return err
}
return nil
}
func mapAccountToResource(account *cfclient.Account, d *schema.ResourceData) error {
err := d.Set("name", account.Name)
if err != nil {
return err
}
err = d.Set("features", account.Features)
if err != nil {
return err
}
if account.Limits != nil { // On-prem API does not return 'limits' field
err = d.Set("limits", []map[string]interface{}{flattenLimits(*account.Limits)})
if err != nil {
return err
}
}
err = d.Set("build", []map[string]interface{}{flattenBuild(*account.Build)})
if err != nil {
return err
}
return nil
}
func flattenLimits(limits cfclient.Limits) map[string]interface{} {
res := make(map[string]interface{})
res["collaborators"] = limits.Collaborators.Limit
res["data_retention_weeks"] = limits.DataRetention.Weeks
return res
}
func flattenBuild(build cfclient.Build) map[string]interface{} {
res := make(map[string]interface{})
res["parallel"] = build.Parallel
res["nodes"] = build.Nodes
return res
}
func mapResourceToAccount(d *schema.ResourceData) *cfclient.Account {
account := &cfclient.Account{
ID: d.Id(),
Name: d.Get("name").(string),
}
if _, ok := d.GetOk("features"); ok {
account.SetFeatures(d.Get("features").(map[string]interface{}))
}
if _, ok := d.GetOk("limits"); ok {
account.Limits = &cfclient.Limits{
Collaborators: cfclient.Collaborators{
Limit: d.Get("limits.0.collaborators").(int),
},
DataRetention: cfclient.DataRetention{
Weeks: d.Get("limits.0.data_retention_weeks").(int),
},
}
}
if _, ok := d.GetOk("build"); ok {
account.Build = &cfclient.Build{
Parallel: d.Get("build.0.parallel").(int),
Nodes: d.Get("build.0.nodes").(int),
}
}
return account
}