-
Notifications
You must be signed in to change notification settings - Fork 95
/
data_akamai_cps_deployments.go
130 lines (114 loc) · 4.28 KB
/
data_akamai_cps_deployments.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
package cps
import (
"context"
"strconv"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v7/pkg/cps"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v7/pkg/session"
"github.com/akamai/terraform-provider-akamai/v5/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v5/pkg/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDeployments() *schema.Resource {
return &schema.Resource{
Description: "Retrieve deployed certificates for given enrollment",
ReadContext: dataSourceDeploymentsRead,
Schema: map[string]*schema.Schema{
"enrollment_id": {
Type: schema.TypeInt,
Required: true,
Description: "The unique enrollment identifier",
},
"production_certificate_rsa": {
Type: schema.TypeString,
Computed: true,
Description: "RSA certificate deployed on production network",
},
"production_certificate_ecdsa": {
Type: schema.TypeString,
Computed: true,
Description: "ECDSA certificate deployed on production network",
},
"staging_certificate_rsa": {
Type: schema.TypeString,
Computed: true,
Description: "RSA certificate deployed on staging network",
},
"staging_certificate_ecdsa": {
Type: schema.TypeString,
Computed: true,
Description: "ECDSA certificate deployed on staging network",
},
"expiry_date": {
Type: schema.TypeString,
Computed: true,
Description: "Certificate expiry date on production",
},
"auto_renewal_start_time": {
Type: schema.TypeString,
Computed: true,
Description: "Start date for the next certificate renewal process",
},
},
}
}
func dataSourceDeploymentsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
logger := meta.Log("CPS", "dataSourceDeploymentsRead")
ctx = session.ContextWithOptions(ctx, session.WithContextLog(logger))
client := inst.Client(meta)
logger.Debug("Fetching deployed certificates")
enrollmentID, err := tf.GetIntValue("enrollment_id", d)
if err != nil {
return diag.Errorf("could not get an enrollment_id: %s", err)
}
enrollment, err := client.GetEnrollment(ctx, cps.GetEnrollmentRequest{
EnrollmentID: enrollmentID,
})
if err != nil {
return diag.Errorf("could not fetch enrollment by id %d: %s", enrollmentID, err)
}
deployments, err := client.ListDeployments(ctx, cps.ListDeploymentsRequest{
EnrollmentID: enrollmentID,
})
if err != nil {
return diag.Errorf("could not fetch deployments for enrollment with id %d: %s", enrollmentID, err)
}
attrs := map[string]interface{}{
"auto_renewal_start_time": enrollment.AutoRenewalStartTime,
}
if deployments.Production != nil {
attrs["expiry_date"] = deployments.Production.PrimaryCertificate.Expiry
if deployments.Production.PrimaryCertificate.KeyAlgorithm == "ECDSA" {
attrs["production_certificate_ecdsa"] = deployments.Production.PrimaryCertificate.Certificate
} else {
attrs["production_certificate_rsa"] = deployments.Production.PrimaryCertificate.Certificate
}
if len(deployments.Production.MultiStackedCertificates) > 0 {
if deployments.Production.MultiStackedCertificates[0].KeyAlgorithm == "ECDSA" {
attrs["production_certificate_ecdsa"] = deployments.Production.MultiStackedCertificates[0].Certificate
} else {
attrs["production_certificate_rsa"] = deployments.Production.MultiStackedCertificates[0].Certificate
}
}
}
if deployments.Staging != nil {
if deployments.Staging.PrimaryCertificate.KeyAlgorithm == "ECDSA" {
attrs["staging_certificate_ecdsa"] = deployments.Staging.PrimaryCertificate.Certificate
} else {
attrs["staging_certificate_rsa"] = deployments.Staging.PrimaryCertificate.Certificate
}
if len(deployments.Staging.MultiStackedCertificates) > 0 {
if deployments.Staging.MultiStackedCertificates[0].KeyAlgorithm == "ECDSA" {
attrs["staging_certificate_ecdsa"] = deployments.Staging.MultiStackedCertificates[0].Certificate
} else {
attrs["staging_certificate_rsa"] = deployments.Staging.MultiStackedCertificates[0].Certificate
}
}
}
if err = tf.SetAttrs(d, attrs); err != nil {
return diag.Errorf("could not set attributes: %s", err)
}
d.SetId(strconv.Itoa(enrollmentID))
return nil
}