Skip to content

Commit

Permalink
fix: refetch after creating managed certificate (#685)
Browse files Browse the repository at this point in the history
When creating a managed certificate, the complete data is only available
once the related action has completed. Instead of then returning the
initial response data, we now fetch the certificate and return this
request's response data.
  • Loading branch information
n9v9 authored Jan 29, 2024
1 parent 762cda4 commit 4864553
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
8 changes: 6 additions & 2 deletions internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, erro
if err := s.ActionProgress(cmd, s, res.Action); err != nil {
return nil, err
}
cmd.Printf("Certificate %d created\n", res.Certificate.ID)
return res.Certificate, nil
defer cmd.Printf("Certificate %d created\n", res.Certificate.ID)
cert, _, err := s.Client().Certificate().GetByID(s, res.Certificate.ID)
if err != nil {
return nil, err
}
return cert, nil
}
40 changes: 34 additions & 6 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func TestCreateManaged(t *testing.T) {
}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
fx.Client.CertificateClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(&hcloud.Certificate{
ID: 123,
Name: "test",
Type: hcloud.CertificateTypeManaged,
DomainNames: []string{"example.com"},
}, nil, nil)

out, _, err := fx.Run(cmd, []string{"--name", "test", "--type", "managed", "--domain", "example.com"})

Expand Down Expand Up @@ -71,25 +79,45 @@ func TestCreateManagedJSON(t *testing.T) {
Name: "test",
Type: hcloud.CertificateTypeManaged,
Created: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC),
NotValidBefore: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC),
NotValidAfter: time.Date(2036, 8, 12, 12, 0, 0, 0, time.UTC),
NotValidBefore: time.Time{},
NotValidAfter: time.Time{},
DomainNames: []string{"example.com"},
Labels: map[string]string{"key": "value"},
UsedBy: []hcloud.CertificateUsedByRef{{
ID: 123,
Type: hcloud.CertificateUsedByRefTypeLoadBalancer,
}},
Status: &hcloud.CertificateStatus{
Error: &hcloud.Error{
Code: "cert_error",
Message: "Certificate error",
},
Issuance: hcloud.CertificateStatusTypePending,
Renewal: hcloud.CertificateStatusTypeUnavailable,
},
},
Action: &hcloud.Action{ID: 321},
}, nil, nil)
fx.ActionWaiter.EXPECT().
ActionProgress(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 321})
fx.Client.CertificateClient.EXPECT().
GetByID(gomock.Any(), int64(123)).
Return(&hcloud.Certificate{
ID: 123,
Name: "test",
Type: hcloud.CertificateTypeManaged,
Created: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC),
NotValidBefore: time.Date(2020, 8, 24, 12, 0, 0, 0, time.UTC),
NotValidAfter: time.Date(2036, 8, 12, 12, 0, 0, 0, time.UTC),
DomainNames: []string{"example.com"},
Labels: map[string]string{"key": "value"},
UsedBy: []hcloud.CertificateUsedByRef{{
ID: 123,
Type: hcloud.CertificateUsedByRefTypeLoadBalancer,
}},
Status: &hcloud.CertificateStatus{
Issuance: hcloud.CertificateStatusTypeCompleted,
Renewal: hcloud.CertificateStatusTypeUnavailable,
},
Fingerprint: "fingerprint placeholder",
Certificate: "certificate data placeholder",
}, nil, nil)

jsonOut, out, err := fx.Run(cmd, []string{"-o=json", "--name", "test", "--type", "managed", "--domain", "example.com"})

Expand Down
14 changes: 4 additions & 10 deletions internal/cmd/certificate/testdata/managed_create_response.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"certificate": {
"certificate": "",
"certificate": "certificate data placeholder",
"created": "2020-08-24T12:00:00Z",
"domain_names": [
"example.com"
],
"fingerprint": "",
"fingerprint": "fingerprint placeholder",
"id": 123,
"labels": {
"key": "value"
Expand All @@ -14,14 +14,8 @@
"not_valid_after": "2036-08-12T12:00:00Z",
"not_valid_before": "2020-08-24T12:00:00Z",
"status": {
"error": {
"Details": null,
"code": "cert_error",
"details": null,
"message": "Certificate error"
},
"issuance": "",
"renewal": ""
"issuance": "completed",
"renewal": "unavailable"
},
"type": "managed",
"used_by": [
Expand Down

0 comments on commit 4864553

Please sign in to comment.