-
Notifications
You must be signed in to change notification settings - Fork 23
/
provider.go
271 lines (240 loc) · 8.01 KB
/
provider.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
265
266
267
268
269
270
271
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/schema"
"github.com/mitchellh/go-homedir"
)
// Provider returns a terraform.ResourceProvider.
func Provider() *schema.Provider {
provider := &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
Description: "This is the base URL to ALKS service. It must be provided, but it can also be sourced from the ALKS_URL environment variable.",
DefaultFunc: schema.EnvDefaultFunc("ALKS_URL", nil),
},
"access_key": {
Type: schema.TypeString,
Optional: true,
Description: "This is the AWS access key. It must be provided, but it can also be sourced from the ALKS_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID environment variable.",
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_ACCESS_KEY_ID",
"ALKS_ACCESS_KEY_ID",
}, nil),
},
"secret_key": {
Type: schema.TypeString,
Optional: true,
Description: "This is the AWS secret key. It must be provided, but it can also be sourced from the ALKS_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY environment variable",
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_SECRET_ACCESS_KEY",
"ALKS_SECRET_ACCESS_KEY",
}, nil),
},
"token": {
Type: schema.TypeString,
Optional: true,
Description: "This is the AWS session token. It must be provided, but it can also be sourced from the ALKS_SESSION_TOKEN or AWS_SESSION_TOKEN environment variable",
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_SESSION_TOKEN",
"ALKS_SESSION_TOKEN",
}, nil),
},
"profile": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The profile for API operations. Used in place of STS tokens.",
DefaultFunc: schema.EnvDefaultFunc("AWS_PROFILE", nil),
},
"shared_credentials_file": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The path to the shared credentials file. If not set this defaults to ~/.aws/credentials.",
DefaultFunc: schema.EnvDefaultFunc("AWS_SHARED_CREDENTIALS_FILE", nil),
},
"account": {
Type: schema.TypeString,
Optional: true,
Description: "The account which you'd like to retrieve credentials for.",
DefaultFunc: schema.EnvDefaultFunc("Account", nil),
},
"role": {
Type: schema.TypeString,
Optional: true,
Description: "The role which you'd like to retrieve credentials for.",
DefaultFunc: schema.EnvDefaultFunc("Role", nil),
},
"assume_role": assumeRoleSchema(),
"default_tags": defaultTagsSchema(),
"ignore_tags": ignoreTagsSchema(),
},
ResourcesMap: map[string]*schema.Resource{
"alks_iamrole": resourceAlksIamRole(),
"alks_iamtrustrole": resourceAlksIamTrustRole(),
"alks_ltk": resourceAlksLtk(),
},
DataSourcesMap: map[string]*schema.Resource{
"alks_keys": dataSourceAlksKeys(),
},
ConfigureContextFunc: providerConfigure,
}
return provider
}
func assumeRoleSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"role_arn": {
Type: schema.TypeString,
Optional: true,
Description: "(Required) Role ARN to assume before calling ALKS",
},
"session_name": {
Type: schema.TypeString,
Optional: true,
Description: "(Optional) Session name to use when making the AssumeRole call. See AWS SDK for more details.",
},
"external_id": {
Type: schema.TypeString,
Optional: true,
Description: "(Optional) The external ID to use when making the AssumeRole call. See AWS SDK for more details.",
},
"policy": {
Type: schema.TypeString,
Optional: true,
Description: "(Optional) Additional policy restrictions to apply to the result STS session. See AWS SDK for more details.",
},
},
},
}
}
func defaultTagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Configuration block with settings to default resource tags across all resources.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Resource tags to default across all resources",
},
},
},
}
}
func ignoreTagsSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Configuration block with settings to ignore resource tags across all resources.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"keys": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Resource tag keys to ignore across all resources.",
},
"key_prefixes": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Description: "Resource tag key prefixes to ignore across all resources.",
},
},
},
}
}
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics
config := Config{
URL: d.Get("url").(string),
AccessKey: d.Get("access_key").(string),
SecretKey: d.Get("secret_key").(string),
Token: d.Get("token").(string),
Profile: d.Get("profile").(string),
Account: d.Get("account").(string),
Role: d.Get("role").(string),
}
assumeRoleList := d.Get("assume_role").(*schema.Set).List()
if len(assumeRoleList) == 1 {
assumeRole := assumeRoleList[0].(map[string]interface{})
config.AssumeRole.RoleARN = assumeRole["role_arn"].(string)
config.AssumeRole.SessionName = assumeRole["session_name"].(string)
config.AssumeRole.ExternalID = assumeRole["external_id"].(string)
config.AssumeRole.Policy = assumeRole["policy"].(string)
}
// Set CredsFilename, expanding home directory
credsPath, err := homedir.Expand(d.Get("shared_credentials_file").(string))
if err != nil {
return nil, diag.FromErr(err)
}
config.CredsFilename = credsPath
defaultTags := expandProviderDefaultTags(d.Get("default_tags").([]interface{}))
ignoreTags := expandProviderIgnoreTags(d.Get("ignore_tags").([]interface{}))
c, err := config.Client()
if err != nil {
return nil, diag.FromErr(err)
}
alksClient := &AlksClient{}
alksClient.client = c
if defaultTags != nil {
alksClient.defaultTags = defaultTags
}
if ignoreTags != nil {
alksClient.ignoreTags = ignoreTags
}
log.Println("[INFO] Initializing ALKS client")
return alksClient, diags
}
func expandProviderDefaultTags(l []interface{}) TagMap {
defaultTags := TagMap{}
if len(l) == 0 || l[0] == nil {
return defaultTags
}
m := l[0].(map[string]interface{})
return m["tags"].(map[string]interface{})
}
func expandProviderIgnoreTags(l []interface{}) *IgnoreTags {
ignoreTags := &IgnoreTags{
Keys: TagMap{},
KeyPrefixes: TagMap{},
}
if len(l) == 0 || l[0] == nil {
return ignoreTags
}
m := l[0].(map[string]interface{})
//Aws runs these through a set similar to commented out code below, but given that it's a map, I dont see how there could be duplicates
if v, ok := m["keys"].(*schema.Set); ok {
for _, key := range v.List() {
ignoreTags.Keys[key.(string)] = ""
}
}
if v, ok := m["key_prefixes"].(*schema.Set); ok {
for _, KeyPrefix := range v.List() {
ignoreTags.KeyPrefixes[KeyPrefix.(string)] = ""
}
}
return ignoreTags
}
type AlksClient struct {
client *alks.Client
defaultTags TagMap //Not making this a pointer because I was having to check everywhere if it was nil
ignoreTags *IgnoreTags
}