/
helpers.go
203 lines (189 loc) · 6.05 KB
/
helpers.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
package property
import (
"errors"
"fmt"
"strings"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v6/pkg/papi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var certStatus = &schema.Resource{
Schema: map[string]*schema.Schema{
"target": {
Type: schema.TypeString,
Computed: true,
},
"hostname": {
Type: schema.TypeString,
Computed: true,
},
"production_status": {
Type: schema.TypeString,
Computed: true,
},
"staging_status": {
Type: schema.TypeString,
Computed: true,
},
},
}
var complianceRecordSchema = &schema.Resource{
Schema: map[string]*schema.Schema{
"noncompliance_reason_none": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ExactlyOneOf: []string{
"compliance_record.0.noncompliance_reason_other",
"compliance_record.0.noncompliance_reason_no_production_traffic",
"compliance_record.0.noncompliance_reason_emergency",
},
Description: fmt.Sprintf("Provides an audit record when activating on a production network with noncompliance reason as `%s`", papi.NoncomplianceReasonNone),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ticket_id": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies the ticket that describes the need for the activation",
},
"customer_email": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies the customer",
},
"peer_reviewed_by": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies person who has independently approved the activation request",
},
"unit_tested": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether the metadata to activate has been fully tested",
},
},
},
},
"noncompliance_reason_other": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: fmt.Sprintf("Provides an audit record when activating on a production network with noncompliance reason as `%s`", papi.NoncomplianceReasonOther),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ticket_id": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies the ticket that describes the need for the activation",
},
"other_noncompliance_reason": {
Type: schema.TypeString,
Optional: true,
Description: "Describes the reason why the activation must occur immediately, out of compliance with the standard procedure",
},
},
},
},
"noncompliance_reason_no_production_traffic": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: fmt.Sprintf("Provides an audit record when activating on a production network with noncompliance reason as `%s`", papi.NoncomplianceReasonNoProductionTraffic),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ticket_id": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies the ticket that describes the need for the activation",
},
},
},
},
"noncompliance_reason_emergency": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: fmt.Sprintf("Provides an audit record when activating on a production network with noncompliance reason as `%s`", papi.NoncomplianceReasonEmergency),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ticket_id": {
Type: schema.TypeString,
Optional: true,
Description: "Identifies the ticket that describes the need for the activation",
},
},
},
},
},
}
// Convert given hostnames to the map form that can be stored in a schema.ResourceData
// Setting only statuses for default certs if they exist
// TODO Set certstatus object for cps managed certs and default certs once PAPI adds support
func flattenHostnames(Hostnames []papi.Hostname) []map[string]interface{} {
var res []map[string]interface{}
for _, hn := range Hostnames {
var c []map[string]interface{}
m := map[string]interface{}{}
m["cname_from"] = hn.CnameFrom
m["cname_to"] = hn.CnameTo
m["cert_provisioning_type"] = hn.CertProvisioningType
m["edge_hostname_id"] = hn.EdgeHostnameID
m["cname_type"] = hn.CnameType
certs := map[string]interface{}{}
certs["hostname"] = hn.CertStatus.ValidationCname.Hostname
certs["target"] = hn.CertStatus.ValidationCname.Target
if len(hn.CertStatus.Staging) > 0 {
certs["staging_status"] = hn.CertStatus.Staging[0].Status
}
if len(hn.CertStatus.Production) > 0 {
certs["production_status"] = hn.CertStatus.Production[0].Status
}
c = append(c, certs)
m["cert_status"] = c
res = append(res, m)
}
return res
}
func papiErrorsToList(Errors []*papi.Error) []interface{} {
if len(Errors) == 0 {
return nil
}
var RuleErrors []interface{}
for _, err := range Errors {
if err == nil {
continue
}
RuleErrors = append(RuleErrors, papiErrorToMap(err))
}
return RuleErrors
}
func papiErrorToMap(err *papi.Error) map[string]interface{} {
if err == nil {
return nil
}
return map[string]interface{}{
"type": err.Type,
"title": err.Title,
"detail": err.Detail,
"instance": err.Instance,
"behavior_name": err.BehaviorName,
"error_location": err.ErrorLocation,
"status_code": err.StatusCode,
}
}
// NetworkAlias parses the given network name or alias and returns its full name and any error
func NetworkAlias(network string) (string, error) {
networks := map[string]papi.ActivationNetwork{
"STAGING": papi.ActivationNetworkStaging,
"STAGE": papi.ActivationNetworkStaging,
"STAG": papi.ActivationNetworkStaging,
"S": papi.ActivationNetworkStaging,
"PRODUCTION": papi.ActivationNetworkProduction,
"PROD": papi.ActivationNetworkProduction,
"P": papi.ActivationNetworkProduction,
}
networkValue, ok := networks[strings.ToUpper(network)]
if !ok {
return "", errors.New("network not recognized")
}
return string(networkValue), nil
}