-
Notifications
You must be signed in to change notification settings - Fork 140
/
data_source_fastly_tls_activation.go
169 lines (146 loc) · 4.28 KB
/
data_source_fastly_tls_activation.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
package fastly
import (
"context"
"github.com/fastly/go-fastly/v5/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"time"
)
func dataSourceFastlyTLSActivation() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFastlyTLSActivationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Fastly Activation ID. Conflicts with all other filters.",
ConflictsWith: []string{"certificate_id", "configuration_id", "domain"},
},
"certificate_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"id"},
Description: "ID of the TLS Certificate used.",
},
"configuration_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"id"},
Description: "ID of the TLS Configuration used.",
},
"domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"id"},
Description: "Domain that TLS was enabled on.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Timestamp (GMT) when TLS was enabled.",
},
},
}
}
func dataSourceFastlyTLSActivationRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
var activation *fastly.TLSActivation
if v, ok := d.GetOk("id"); ok {
foundActivation, err := conn.GetTLSActivation(&fastly.GetTLSActivationInput{
ID: v.(string),
})
if err != nil {
return diag.FromErr(err)
}
activation = foundActivation
} else {
filters := getTLSActivationFilters(d)
activations, err := listTLSActivations(conn, filters...)
if err != nil {
return diag.FromErr(err)
}
if len(activations) == 0 {
return diag.Errorf("Your query returned no results. Please change your search criteria and try again")
}
if len(activations) > 1 {
return diag.Errorf("Your query returned more than one result. Please change to a more specific search criteria")
}
activation = activations[0]
}
err := dataSourceFastlyTLSActivationSetAttributes(activation, d)
if err != nil {
return diag.FromErr(err)
}
return nil
}
type TLSActivationPredicate func(activation *fastly.TLSActivation) bool
func getTLSActivationFilters(d *schema.ResourceData) []TLSActivationPredicate {
var filters []TLSActivationPredicate
if v, ok := d.GetOk("certificate_id"); ok {
filters = append(filters, func(c *fastly.TLSActivation) bool {
return c.Certificate.ID == v.(string)
})
}
if v, ok := d.GetOk("configuration_id"); ok {
filters = append(filters, func(c *fastly.TLSActivation) bool {
return c.Configuration.ID == v.(string)
})
}
if v, ok := d.GetOk("domain"); ok {
filters = append(filters, func(c *fastly.TLSActivation) bool {
return c.Domain.ID == v.(string)
})
}
return filters
}
func listTLSActivations(conn *fastly.Client, filters ...TLSActivationPredicate) ([]*fastly.TLSActivation, error) {
var activations []*fastly.TLSActivation
pageNumber := 1
for {
list, err := conn.ListTLSActivations(&fastly.ListTLSActivationsInput{
PageNumber: pageNumber,
PageSize: 10,
})
if err != nil {
return nil, err
}
if len(list) == 0 {
break
}
pageNumber++
for _, activation := range list {
if filterTLSActivations(activation, filters) {
activations = append(activations, activation)
}
}
}
return activations, nil
}
func dataSourceFastlyTLSActivationSetAttributes(activation *fastly.TLSActivation, d *schema.ResourceData) error {
d.SetId(activation.ID)
if err := d.Set("certificate_id", activation.Certificate.ID); err != nil {
return err
}
if err := d.Set("configuration_id", activation.Configuration.ID); err != nil {
return err
}
if err := d.Set("domain", activation.Domain.ID); err != nil {
return err
}
if err := d.Set("created_at", activation.CreatedAt.Format(time.RFC3339)); err != nil {
return err
}
return nil
}
func filterTLSActivations(config *fastly.TLSActivation, filters []TLSActivationPredicate) bool {
for _, f := range filters {
if !f(config) {
return false
}
}
return true
}