-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_source_fastly_tls_private_key.go
205 lines (181 loc) · 5.39 KB
/
data_source_fastly_tls_private_key.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
package fastly
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"time"
"github.com/fastly/go-fastly/v5/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceFastlyTLSPrivateKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFastlyTLSPrivateKeyRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Fastly private key ID. Conflicts with all the other filters",
ConflictsWith: []string{"name", "created_at", "key_length", "key_type", "public_key_sha1"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The human-readable name assigned to the private key when uploaded.",
Computed: true,
ConflictsWith: []string{"id"},
},
"created_at": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Timestamp (GMT) when the private key was created.",
ConflictsWith: []string{"id"},
},
"key_length": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The key length used to generate the private key.",
ConflictsWith: []string{"id"},
},
"key_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The algorithm used to generate the private key. Must be RSA.",
ConflictsWith: []string{"id"},
},
"replace": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether Fastly recommends replacing this private key.",
},
"public_key_sha1": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "A hash of the associated public key, useful for safely identifying it.",
ConflictsWith: []string{"id"},
},
},
}
}
func dataSourceFastlyTLSPrivateKeyRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
var diags diag.Diagnostics
var privateKey *fastly.PrivateKey
if v, ok := d.GetOk("id"); ok {
var err error
privateKey, err = conn.GetPrivateKey(&fastly.GetPrivateKeyInput{ID: v.(string)})
if err != nil {
return diag.FromErr(err)
}
} else {
filters := getTLSPrivateKeyFilters(d)
privateKeys, err := listTLSPrivateKeys(conn, filters...)
if err != nil {
return diag.FromErr(err)
}
if len(privateKeys) == 0 {
return diag.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(privateKeys) > 1 {
return diag.Errorf("Your query returned more than one result. Please change to a more specific search criteria and try again.")
}
privateKey = privateKeys[0]
}
if privateKey.Replace {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Fastly recommends that this private key (%s) be replaced", privateKey.ID),
})
}
err := dataSourceFastlyTLSPrivateKeySetAttributes(privateKey, d)
if err != nil {
return diag.FromErr(err)
}
return diags
}
type TLSPrivateKeyPredicate func(key *fastly.PrivateKey) bool
func getTLSPrivateKeyFilters(d *schema.ResourceData) []TLSPrivateKeyPredicate {
var filters []TLSPrivateKeyPredicate
if v, ok := d.GetOk("id"); ok {
filters = append(filters, func(key *fastly.PrivateKey) bool {
return key.ID == v.(string)
})
}
if v, ok := d.GetOk("name"); ok {
filters = append(filters, func(key *fastly.PrivateKey) bool {
return key.Name == v.(string)
})
}
if v, ok := d.GetOk("key_length"); ok {
filters = append(filters, func(key *fastly.PrivateKey) bool {
return key.KeyLength == v.(int)
})
}
if v, ok := d.GetOk("key_type"); ok {
filters = append(filters, func(key *fastly.PrivateKey) bool {
return key.KeyType == v.(string)
})
}
if v, ok := d.GetOk("public_key_sha1"); ok {
filters = append(filters, func(key *fastly.PrivateKey) bool {
return key.PublicKeySHA1 == v.(string)
})
}
return filters
}
func listTLSPrivateKeys(conn *fastly.Client, filters ...TLSPrivateKeyPredicate) ([]*fastly.PrivateKey, error) {
var privateKeys []*fastly.PrivateKey
pageNumber := 1
for {
list, err := conn.ListPrivateKeys(&fastly.ListPrivateKeysInput{
PageNumber: pageNumber,
})
if err != nil {
return nil, err
}
if len(list) == 0 {
break
}
pageNumber++
for _, privateKey := range list {
if filterPrivateKey(privateKey, filters) {
privateKeys = append(privateKeys, privateKey)
}
}
}
return privateKeys, nil
}
func dataSourceFastlyTLSPrivateKeySetAttributes(privateKey *fastly.PrivateKey, d *schema.ResourceData) error {
d.SetId(privateKey.ID)
if err := d.Set("name", privateKey.Name); err != nil {
return err
}
if err := d.Set("created_at", privateKey.CreatedAt.Format(time.RFC3339)); err != nil {
return err
}
if err := d.Set("key_length", privateKey.KeyLength); err != nil {
return err
}
if err := d.Set("key_type", privateKey.KeyType); err != nil {
return err
}
if err := d.Set("replace", privateKey.Replace); err != nil {
return err
}
if err := d.Set("public_key_sha1", privateKey.PublicKeySHA1); err != nil {
return err
}
return nil
}
func filterPrivateKey(privateKey *fastly.PrivateKey, filters []TLSPrivateKeyPredicate) bool {
for _, f := range filters {
if !f(privateKey) {
return false
}
}
return true
}