-
Notifications
You must be signed in to change notification settings - Fork 23
/
resource_alks_ltk.go
200 lines (158 loc) · 5 KB
/
resource_alks_ltk.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
package main
import (
"context"
"log"
"github.com/Cox-Automotive/alks-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAlksLtk() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAlksLtkCreate,
ReadContext: resourceAlksLtkRead,
UpdateContext: resourceAlksLtkUpdate,
DeleteContext: resourceAlksLtkDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"iam_username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"iam_user_arn": {
Type: schema.TypeString,
Computed: true,
},
"access_key": {
Sensitive: true,
Type: schema.TypeString,
Computed: true,
},
"secret_key": {
Sensitive: true,
Type: schema.TypeString,
Computed: true,
},
"tags": TagsSchema(),
"tags_all": TagsSchemaComputed(),
},
CustomizeDiff: customdiff.All(
SetTagsDiff,
),
}
}
func resourceAlksLtkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS LTK User Create")
var iamUsername = d.Get("iam_username").(string)
var tags = d.Get("tags").(map[string]interface{})
providerStruct := meta.(*AlksClient)
client := providerStruct.client
allTags := tagMapToSlice(combineTagMaps(providerStruct.defaultTags, tags))
options := &alks.IamUserOptions{
IamUserName: &iamUsername,
Tags: &allTags,
}
if err := validateIAMEnabled(client); err != nil {
return diag.FromErr(err)
}
resp, err := client.CreateIamUser(options)
if err != nil {
return diag.FromErr(err)
}
d.SetId(iamUsername)
_ = d.Set("iam_user_arn", resp.IAMUserArn)
_ = d.Set("access_key", resp.AccessKey)
_ = d.Set("secret_key", resp.SecretKey)
log.Printf("[INFO] alks_ltk.id: %v", d.Id())
return resourceAlksLtkRead(ctx, d, meta)
}
func resourceAlksLtkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS LTK User Read")
providerStruct := meta.(*AlksClient)
client := providerStruct.client
defaultTags := providerStruct.defaultTags
ignoreTags := providerStruct.ignoreTags
// Check if role exists.
if d.Id() == "" || d.Id() == "none" {
return nil
}
resp, err := client.GetIamUser(d.Id())
if err != nil {
//If error is 404, UserNotFound, we log it and let terraform decide how to handle it.
//All other errors cause a failure
if err.StatusCode == 404 {
log.Printf("[Error] %s", err)
d.SetId("")
return nil
}
return diag.FromErr(err)
}
log.Printf("[INFO] alks_ltk.id: %v", d.Id())
_ = d.Set("iam_username", resp.User.UserName)
_ = d.Set("access_key", resp.User.AccessKey)
allTags := tagSliceToMap(resp.User.Tags)
localTags := removeIgnoredTags(allTags, *ignoreTags)
if err := d.Set("tags_all", localTags); err != nil {
return diag.FromErr(err)
}
userSpecificTags := removeDefaultTags(localTags, defaultTags)
if err := d.Set("tags", userSpecificTags); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceAlksLtkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS LTK Update")
// enable partial state mode
d.Partial(true)
if d.HasChange("tags_all") {
// try updating enable_alks_access
if err := updateUserTags(d, meta); err != nil {
return diag.FromErr(err)
}
}
d.Partial(false)
return resourceAlksLtkRead(ctx, d, meta)
}
func resourceAlksLtkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] ALKS LTK User Delete")
providerStruct := meta.(*AlksClient)
client := providerStruct.client
if err := validateIAMEnabled(client); err != nil {
return diag.FromErr(err)
}
if _, err := client.DeleteIamUser(d.Id()); err != nil {
return diag.FromErr(err)
}
return nil
}
func updateUserTags(d *schema.ResourceData, meta interface{}) error {
providerStruct := meta.(*AlksClient)
client := providerStruct.client
if err := validateIAMEnabled(client); err != nil {
return err
}
//Do a read to get existing tags. If any of those are in ignore_tags, then they are externally managed
//and they should be included in the update so they don't get removed.
resp, err := client.GetIamUser(d.Id())
if err != nil {
return err
}
existingTags := tagSliceToMap(resp.User.Tags)
externalTags := getExternalyManagedTags(existingTags, *providerStruct.ignoreTags)
internalTags := d.Get("tags_all").(map[string]interface{})
//Tags includes default tags, role specific tags, and tags that exist externally on the role itself and are specified in ignored_tags
tags := tagMapToSlice(combineTagMaps(internalTags, externalTags))
options := alks.IamUserOptions{
IamUserName: &resp.User.UserName,
Tags: &tags,
}
if _, err := client.UpdateIamUser(&options); err != nil {
return err
}
return nil
}