-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_source_fastly_tls_domain.go
134 lines (116 loc) · 3.44 KB
/
data_source_fastly_tls_domain.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
package fastly
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/fastly/go-fastly/v6/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceFastlyTLSDomain() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFastlyTLSDomainsRead,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Description: "Domain name to look up activations, certificates and subscriptions for.",
Required: true,
},
"tls_activation_ids": {
Type: schema.TypeSet,
Description: "IDs of the activations associated with the domain.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tls_certificate_ids": {
Type: schema.TypeSet,
Description: "IDs of the certificates associated with the domain.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tls_subscription_ids": {
Type: schema.TypeSet,
Description: "IDs of the subscriptions associated with the domain.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceFastlyTLSDomainsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
domain, err := findTLSDomain(conn, d)
if err != nil {
return diag.FromErr(err)
}
var activations []string
for _, activation := range domain.Activations {
activations = append(activations, activation.ID)
}
var certificates []string
for _, certificate := range domain.Certificates {
certificates = append(certificates, certificate.ID)
}
var subscriptions []string
for _, subscription := range domain.Subscriptions {
subscriptions = append(subscriptions, subscription.ID)
}
d.SetId(domain.ID)
if err := d.Set("tls_activation_ids", activations); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tls_certificate_ids", certificates); err != nil {
return diag.FromErr(err)
}
if err := d.Set("tls_subscription_ids", subscriptions); err != nil {
return diag.FromErr(err)
}
return nil
}
func findTLSDomain(conn *fastly.Client, d *schema.ResourceData) (*fastly.TLSDomain, error) {
domain := d.Get("domain").(string)
filter := func(d *fastly.TLSDomain) bool {
return d.ID == domain
}
domains, err := listTLSDomains(conn, filter)
if err != nil {
return nil, err
}
if len(domains) == 0 {
return nil, fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(domains) > 1 {
return nil, fmt.Errorf("Your query returned more than one result. Please change to a more specific search criteria and try again.")
}
return domains[0], nil
}
type TLSDomainPredicate func(domain *fastly.TLSDomain) bool
func listTLSDomains(conn *fastly.Client, filters ...TLSDomainPredicate) ([]*fastly.TLSDomain, error) {
var domains []*fastly.TLSDomain
pageNumber := 1
for {
list, err := conn.ListTLSDomains(&fastly.ListTLSDomainsInput{
PageNumber: pageNumber,
})
if err != nil {
return nil, err
}
if len(list) == 0 {
break
}
pageNumber++
for _, domain := range list {
if filterTLSDomain(domain, filters) {
domains = append(domains, domain)
}
}
}
return domains, nil
}
func filterTLSDomain(domain *fastly.TLSDomain, filters []TLSDomainPredicate) bool {
for _, f := range filters {
if !f(domain) {
return false
}
}
return true
}