Skip to content

Commit f7aaa3a

Browse files
committed
receive context as the 1st parameter
1 parent 130ec30 commit f7aaa3a

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ goacm is a simple package for using AWS Certificate Manager from applications im
1818
## Create goacm client
1919

2020
```go
21-
g, err := goacm.NewGoACM("ap-northeast-1")
21+
ctx := context.TODO()
22+
g, err := goacm.NewGoACM(ctx, "ap-northeast-1")
2223
if err != nil {
2324
fmt.Println(err.Error())
2425
return
@@ -28,7 +29,8 @@ if err != nil {
2829
## List Certificats
2930

3031
```go
31-
if certificates, err := goacm.ListCertificates(g.ACMClient); err != nil {
32+
ctx := context.TODO()
33+
if certificates, err := goacm.ListCertificates(ctx, g.ACMClient); err != nil {
3234
fmt.Println(err.Error())
3335
} else {
3436
fmt.Println("DomainName\tStatus\tARN")
@@ -42,7 +44,8 @@ if certificates, err := goacm.ListCertificates(g.ACMClient); err != nil {
4244

4345
```go
4446
arn := "arn:aws:acm:ap-northeast-1:000000000000:certificate/xxxxxxxx-1111-1111-1111-11111111xxxx"
45-
c, err := goacm.GetCertificate(g.ACMClient, arn)
47+
ctx := context.TODO()
48+
c, err := goacm.GetCertificate(ctx, g.ACMClient, arn)
4649
if err != nil {
4750
fmt.Println(err.Error())
4851
return
@@ -60,7 +63,8 @@ Request an ACM Certificate and create a RecordSet in Route 53 to validate the do
6063
method := "DNS"
6164
targetDomain := "sample.exapmle.com"
6265
hostedDomain := "example.com"
63-
res, err := goacm.IssueCertificate(g.ACMClient, g.Route53Client, method, targetDomain, hostedDomain)
66+
ctx := context.TODO()
67+
res, err := goacm.IssueCertificate(ctx, g.ACMClient, g.Route53Client, method, targetDomain, hostedDomain)
6468
if err != nil {
6569
fmt.Println(err.Error())
6670
return
@@ -75,7 +79,8 @@ Delete the Route 53 RecordSet that was created for ACM Certificate and Domain va
7579

7680
```go
7781
arn := "arn:aws:acm:ap-northeast-1:000000000000:certificate/xxxxxxxx-1111-1111-1111-11111111xxxx"
78-
if err := goacm.DeleteCertificate(g.ACMClient, g.Route53Client, arn); err != nil {
82+
ctx := context.TODO()
83+
if err := goacm.DeleteCertificate(ctx, g.ACMClient, g.Route53Client, arn); err != nil {
7984
fmt.Println(err.Error())
8085
}
8186
```

goacm.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type GoACM struct {
2323
}
2424

2525
// NewGoACM returns a new GoACM object.
26-
func NewGoACM(region string) (*GoACM, error) {
27-
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
26+
func NewGoACM(ctx context.Context, region string) (*GoACM, error) {
27+
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
2828
if err != nil {
2929
return nil, err
3030
}
@@ -37,9 +37,9 @@ func NewGoACM(region string) (*GoACM, error) {
3737
}
3838

3939
// ListCertificateSummaries returns a list of certificate summary.
40-
func ListCertificateSummaries(api ACMListCertificatesAPI) ([]acmTypes.CertificateSummary, error) {
40+
func ListCertificateSummaries(ctx context.Context, api ACMListCertificatesAPI) ([]acmTypes.CertificateSummary, error) {
4141
in := acm.ListCertificatesInput{}
42-
out, err := api.ListCertificates(context.TODO(), &in)
42+
out, err := api.ListCertificates(ctx, &in)
4343
if err != nil {
4444
return nil, err
4545
}
@@ -48,11 +48,11 @@ func ListCertificateSummaries(api ACMListCertificatesAPI) ([]acmTypes.Certificat
4848
}
4949

5050
// GetCertificate returns the details of the certificate.
51-
func GetCertificate(api ACMDescribeCertificateAPI, arn string) (Certificate, error) {
51+
func GetCertificate(ctx context.Context, api ACMDescribeCertificateAPI, arn string) (Certificate, error) {
5252
in := acm.DescribeCertificateInput{
5353
CertificateArn: aws.String(arn),
5454
}
55-
out, err := api.DescribeCertificate(context.TODO(), &in)
55+
out, err := api.DescribeCertificate(ctx, &in)
5656
if err != nil {
5757
return Certificate{}, err
5858
}
@@ -81,15 +81,15 @@ func GetCertificate(api ACMDescribeCertificateAPI, arn string) (Certificate, err
8181
}
8282

8383
// ListCertificates returns list of certificate.
84-
func ListCertificates(api ACMAPI) ([]Certificate, error) {
85-
summary, err := ListCertificateSummaries(api)
84+
func ListCertificates(ctx context.Context, api ACMAPI) ([]Certificate, error) {
85+
summary, err := ListCertificateSummaries(ctx, api)
8686
if err != nil {
8787
return nil, err
8888
}
8989

9090
var cList []Certificate
9191
for _, s := range summary {
92-
c, err := GetCertificate(api, aws.ToString(s.CertificateArn))
92+
c, err := GetCertificate(ctx, api, aws.ToString(s.CertificateArn))
9393
if err != nil {
9494
fmt.Println(err.Error())
9595
continue
@@ -101,15 +101,15 @@ func ListCertificates(api ACMAPI) ([]Certificate, error) {
101101
}
102102

103103
// DeleteCertificate returns an error if deleting the certificate fails.
104-
func DeleteCertificate(aAPI ACMAPI, rAPI Route53API, arn string) error {
105-
c, err := GetCertificate(aAPI, arn)
104+
func DeleteCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API, arn string) error {
105+
c, err := GetCertificate(ctx, aAPI, arn)
106106
if err != nil {
107107
return err
108108
}
109109

110110
// Delete Route 53 Record that validate domain.
111111
if c.ValidationMethod == string(types.ValidationMethodDns) {
112-
if err := DeleteRoute53RecordSet(aAPI, rAPI, c.ValidationRecordSet); err != nil {
112+
if err := DeleteRoute53RecordSet(ctx, aAPI, rAPI, c.ValidationRecordSet); err != nil {
113113
return err
114114
}
115115
}
@@ -118,15 +118,15 @@ func DeleteCertificate(aAPI ACMAPI, rAPI Route53API, arn string) error {
118118
CertificateArn: aws.String(arn),
119119
}
120120

121-
if _, err := aAPI.DeleteCertificate(context.TODO(), &in); err != nil {
121+
if _, err := aAPI.DeleteCertificate(ctx, &in); err != nil {
122122
return err
123123
}
124124

125125
return nil
126126
}
127127

128128
// IssueCertificate issues an SSL certificate for the specified domain.
129-
func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hostedDomain string) (IssueCertificateResult, error) {
129+
func IssueCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API, method, targetDomain, hostedDomain string) (IssueCertificateResult, error) {
130130
var result IssueCertificateResult = IssueCertificateResult{
131131
DomainName: targetDomain,
132132
HostedDomainName: hostedDomain,
@@ -144,7 +144,7 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
144144
},
145145
},
146146
}
147-
r, err := aAPI.RequestCertificate(context.TODO(), &reqIn)
147+
r, err := aAPI.RequestCertificate(ctx, &reqIn)
148148
if err != nil {
149149
return IssueCertificateResult{}, err
150150
}
@@ -160,13 +160,13 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
160160
dcIn := acm.DescribeCertificateInput{
161161
CertificateArn: r.CertificateArn,
162162
}
163-
c, err := aAPI.DescribeCertificate(context.TODO(), &dcIn)
163+
c, err := aAPI.DescribeCertificate(ctx, &dcIn)
164164
if err != nil {
165165
return IssueCertificateResult{}, err
166166
}
167167
if c.Certificate.DomainValidationOptions == nil {
168168
errMsg := "DomainValidationOptions dose not exists"
169-
if err := RollbackIssueCertificate(aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
169+
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
170170
errMsg += fmt.Sprintf("; Failed to rollback to issue certificate: %v", err)
171171
} else {
172172
errMsg += "; rollbacked to issue certificate"
@@ -181,10 +181,10 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
181181
result.ValidationRecordValue = *vRecordValue
182182

183183
lhzIn := route53.ListHostedZonesInput{}
184-
h, err := rAPI.ListHostedZones(context.TODO(), &lhzIn)
184+
h, err := rAPI.ListHostedZones(ctx, &lhzIn)
185185
if err != nil {
186186
errMsg := err.Error()
187-
if err := RollbackIssueCertificate(aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
187+
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
188188
errMsg += fmt.Sprintf("; Failed to rollback to issue certificate: %v", err)
189189
} else {
190190
errMsg += "; rollbacked to issue certificate"
@@ -200,7 +200,7 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
200200
}
201201
if hzID == "" {
202202
errMsg := "Cannot get hosted zone ID"
203-
if err := RollbackIssueCertificate(aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
203+
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
204204
errMsg += fmt.Sprintf("; Failed to rollback to issue certificate: %v", err)
205205
} else {
206206
errMsg += "; rollbacked to issue certificate"
@@ -231,10 +231,10 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
231231
},
232232
}
233233

234-
_, err = rAPI.ChangeResourceRecordSets(context.TODO(), &crsIn)
234+
_, err = rAPI.ChangeResourceRecordSets(ctx, &crsIn)
235235
if err != nil {
236236
errMsg := err.Error()
237-
if err := RollbackIssueCertificate(aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
237+
if err := RollbackIssueCertificate(ctx, aAPI, rAPI, *c.Certificate.CertificateArn); err != nil {
238238
errMsg += fmt.Sprintf("; Failed to rollback to issue certificate: %v", err)
239239
} else {
240240
errMsg += "; rollbacked to issue certificate"
@@ -246,14 +246,14 @@ func IssueCertificate(aAPI ACMAPI, rAPI Route53API, method, targetDomain, hosted
246246
}
247247

248248
// RollbackIssueCertificate rollbacks to issue an SSL certificate.
249-
func RollbackIssueCertificate(aAPI ACMAPI, rAPI Route53API, arn string) error {
250-
return DeleteCertificate(aAPI, rAPI, arn)
249+
func RollbackIssueCertificate(ctx context.Context, aAPI ACMAPI, rAPI Route53API, arn string) error {
250+
return DeleteCertificate(ctx, aAPI, rAPI, arn)
251251
}
252252

253253
// DeleteRoute53RecordSet deletes a Route 53 record set.
254-
func DeleteRoute53RecordSet(aAPI ACMAPI, rAPI Route53API, rs RecordSet) error {
254+
func DeleteRoute53RecordSet(ctx context.Context, aAPI ACMAPI, rAPI Route53API, rs RecordSet) error {
255255
lhzIn := route53.ListHostedZonesInput{}
256-
h, err := rAPI.ListHostedZones(context.TODO(), &lhzIn)
256+
h, err := rAPI.ListHostedZones(ctx, &lhzIn)
257257
if err != nil {
258258
return err
259259
}
@@ -273,7 +273,7 @@ func DeleteRoute53RecordSet(aAPI ACMAPI, rAPI Route53API, rs RecordSet) error {
273273
StartRecordName: aws.String(rs.Name),
274274
MaxItems: aws.Int32(1),
275275
}
276-
r, err := rAPI.ListResourceRecordSets(context.TODO(), &lrrsIn)
276+
r, err := rAPI.ListResourceRecordSets(ctx, &lrrsIn)
277277
if err != nil {
278278
return err
279279
}
@@ -308,7 +308,7 @@ func DeleteRoute53RecordSet(aAPI ACMAPI, rAPI Route53API, rs RecordSet) error {
308308
},
309309
}
310310

311-
_, err = rAPI.ChangeResourceRecordSets(context.TODO(), &crsIn)
311+
_, err = rAPI.ChangeResourceRecordSets(ctx, &crsIn)
312312

313313
if err != nil {
314314
return err

goacm_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goacm
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67

@@ -58,7 +59,8 @@ func TestGetCertificate(t *testing.T) {
5859

5960
for _, tt := range cases {
6061
t.Run(tt.name, func(t *testing.T) {
61-
c, err := GetCertificate(tt.acmClient(t), tt.arn)
62+
ctx := context.TODO()
63+
c, err := GetCertificate(ctx, tt.acmClient(t), tt.arn)
6264
if tt.wantErr {
6365
assert.Error(t, err)
6466
return
@@ -104,7 +106,8 @@ func TestListCertificateSummaries(t *testing.T) {
104106

105107
for _, tt := range cases {
106108
t.Run(tt.name, func(t *testing.T) {
107-
c, err := ListCertificateSummaries(tt.acmClient(t))
109+
ctx := context.TODO()
110+
c, err := ListCertificateSummaries(ctx, tt.acmClient(t))
108111
if tt.wantErr {
109112
assert.Error(t, err)
110113
return
@@ -168,7 +171,8 @@ func TestListCertificates(t *testing.T) {
168171

169172
for _, tt := range cases {
170173
t.Run(tt.name, func(t *testing.T) {
171-
c, err := ListCertificates(tt.acmClient(t))
174+
ctx := context.TODO()
175+
c, err := ListCertificates(ctx, tt.acmClient(t))
172176
if tt.wantErr {
173177
assert.Error(t, err)
174178
return
@@ -244,7 +248,8 @@ func TestDeleteCertificate(t *testing.T) {
244248

245249
for _, tt := range cases {
246250
t.Run(tt.name, func(t *testing.T) {
247-
err := DeleteCertificate(tt.acmClient(t), tt.route53Client(t), tt.arn)
251+
ctx := context.TODO()
252+
err := DeleteCertificate(ctx, tt.acmClient(t), tt.route53Client(t), tt.arn)
248253
if tt.wantErr {
249254
assert.Error(t, err)
250255
return

0 commit comments

Comments
 (0)