-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_multiplexedaccount.go
264 lines (228 loc) · 7.1 KB
/
resource_multiplexedaccount.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package centrify
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
logger "github.com/marcozj/golang-sdk/logging"
vault "github.com/marcozj/golang-sdk/platform"
"github.com/marcozj/golang-sdk/restapi"
)
func resourceMultiplexedAccount() *schema.Resource {
return &schema.Resource{
Create: resourceMultiplexedAccountCreate,
Read: resourceMultiplexedAccountRead,
Update: resourceMultiplexedAccountUpdate,
Delete: resourceMultiplexedAccountDelete,
Exists: resourceMultiplexedAccountExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the multiplexed account",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Description of the multiplexed account",
},
"account1_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"account2_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"account1": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"account2": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"accounts": {
Type: schema.TypeSet,
Required: true,
MinItems: 2,
MaxItems: 2,
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"active_account": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"permission": getPermissionSchema(),
},
}
}
func resourceMultiplexedAccountExists(d *schema.ResourceData, m interface{}) (bool, error) {
logger.Infof("Checking multiplexed account exist: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
object := vault.NewMultiplexedAccount(client)
object.ID = d.Id()
err := object.Read()
if err != nil {
if strings.Contains(err.Error(), "not exist") || strings.Contains(err.Error(), "not found") {
return false, nil
}
return false, err
}
logger.Infof("Multiplexed account exists in tenant: %s", object.ID)
return true, nil
}
func resourceMultiplexedAccountRead(d *schema.ResourceData, m interface{}) error {
logger.Infof("Reading multiplexed account: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
// Create a NewMultiplexedAccount object and populate ID attribute
object := vault.NewMultiplexedAccount(client)
object.ID = d.Id()
err := object.Read()
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
if err != nil {
d.SetId("")
return fmt.Errorf(" Error reading multiplexed account: %v", err)
}
//logger.Debugf("Multiplexed account from tenant: %+v", object)
schemamap, err := vault.GenerateSchemaMap(object)
if err != nil {
return err
}
logger.Debugf("Generated Map for resourceMultiplexedAccountRead(): %+v", schemamap)
for k, v := range schemamap {
d.Set(k, v)
}
logger.Debugf("Completed reading multiplexed account: %s", object.Name)
return nil
}
func resourceMultiplexedAccountCreate(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning multiplexed account creation: %s", ResourceIDString(d))
// Enable partial state mode
d.Partial(true)
client := m.(*restapi.RestClient)
// Create a multiplexed account object and populate all attributes
object := vault.NewMultiplexedAccount(client)
err := createUpateGetMultiplexedAccountData(d, object)
if err != nil {
return err
}
resp, err := object.Create()
if err != nil {
return fmt.Errorf(" Error creating multiplexed account: %v", err)
}
id := resp.Result
if id == "" {
return fmt.Errorf(" Multiplexed account ID is not set")
}
d.SetId(id)
// Need to populate ID attribute for subsequence processes
object.ID = id
// add permissions
if _, ok := d.GetOk("permission"); ok {
_, err = object.SetPermissions(false)
if err != nil {
return fmt.Errorf(" Error setting multiplexed account permissions: %v", err)
}
}
// Creation completed
d.Partial(false)
logger.Infof("Creation of multiplexed account completed: %s", object.Name)
return resourceMultiplexedAccountRead(d, m)
}
func resourceMultiplexedAccountUpdate(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning multiplexed account update: %s", ResourceIDString(d))
// Enable partial state mode
d.Partial(true)
client := m.(*restapi.RestClient)
object := vault.NewMultiplexedAccount(client)
object.ID = d.Id()
err := createUpateGetMultiplexedAccountData(d, object)
if err != nil {
return err
}
// Deal with normal attribute changes first
if d.HasChanges("name", "description", "accounts") {
resp, err := object.Update()
if err != nil || !resp.Success {
return fmt.Errorf(" Error updating multiplexed account attribute: %v", err)
}
logger.Debugf("Updated attributes to: %v", object)
}
// Deal with Permissions
if d.HasChange("permission") {
old, new := d.GetChange("permission")
// We don't want to care the details of changes
// So, let's first remove the old permissions
var err error
if old != nil {
// do not validate old values
object.Permissions, err = expandPermissions(old, object.ValidPermissions, false)
if err != nil {
return err
}
_, err = object.SetPermissions(true)
if err != nil {
return fmt.Errorf(" Error removing multiplexed account permissions: %v", err)
}
}
if new != nil {
object.Permissions, err = expandPermissions(new, object.ValidPermissions, true)
if err != nil {
return err
}
_, err = object.SetPermissions(false)
if err != nil {
return fmt.Errorf(" Error adding multiplexed account permissions: %v", err)
}
}
}
// We succeeded, disable partial mode. This causes Terraform to save all fields again.
d.Partial(false)
logger.Infof("Updating of multiplexed account completed: %s", object.Name)
return resourceMultiplexedAccountRead(d, m)
}
func resourceMultiplexedAccountDelete(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning deletion of multiplexed account: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
object := vault.NewMultiplexedAccount(client)
object.ID = d.Id()
resp, err := object.Delete()
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
if err != nil {
return fmt.Errorf(" Error deleting multiplexed account: %v", err)
}
if resp.Success {
d.SetId("")
}
logger.Infof("Deletion of multiplexed account completed: %s", ResourceIDString(d))
return nil
}
func createUpateGetMultiplexedAccountData(d *schema.ResourceData, object *vault.MultiplexedAccount) error {
object.Name = d.Get("name").(string)
if v, ok := d.GetOk("description"); ok && d.HasChange("description") {
object.Description = v.(string)
}
object.RealAccounts = flattenSchemaSetToStringSlice(d.Get("accounts"))
// Permissions
if v, ok := d.GetOk("permission"); ok {
var err error
object.Permissions, err = expandPermissions(v, object.ValidPermissions, true)
if err != nil {
return err
}
}
return nil
}