Skip to content

Commit

Permalink
feat(certificate): allow retrying managed certificate issuance (#847)
Browse files Browse the repository at this point in the history
Somehow this API call didn't have a corresponding command in the CLI, so
this PR adds it.
  • Loading branch information
phm07 authored Aug 26, 2024
1 parent 15b26d6 commit 0223f7d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmd/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewCommand(s state.State) *cobra.Command {
LabelCmds.RemoveCobraCommand(s),
DeleteCmd.CobraCommand(s),
DescribeCmd.CobraCommand(s),
RetryCmd.CobraCommand(s),
)

return cmd
Expand Down
44 changes: 44 additions & 0 deletions internal/cmd/certificate/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package certificate

import (
"fmt"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
)

var RetryCmd = base.Cmd{
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
return &cobra.Command{
Use: "retry <certificate>",
Short: "Retry a managed certificate's issuance",
TraverseChildren: true,
DisableFlagsInUseLine: true,
}
},
Run: func(s state.State, cmd *cobra.Command, args []string) error {
idOrName := args[0]
certificate, _, err := s.Client().Certificate().Get(s, idOrName)
if err != nil {
return err
}
if certificate == nil {
return fmt.Errorf("certificate not found: %s", idOrName)
}

action, _, err := s.Client().Certificate().RetryIssuance(s, certificate)
if err != nil {
return err
}

if err := s.WaitForActions(cmd, s, action); err != nil {

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / build

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / build

cannot use s (variable of type state.State) as *cobra.Command value in argument to s.WaitForActions

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use s (variable of type state.State) as *cobra.Command value in argument to s.WaitForActions (typecheck)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use s (variable of type state.State) as *cobra.Command value in argument to s.WaitForActions) (typecheck)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use s (variable of type state.State) as *cobra.Command value in argument to s.WaitForActions) (typecheck)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / lint

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / test

cannot use cmd (variable of type *cobra.Command) as context.Context value in argument to s.WaitForActions: *cobra.Command does not implement context.Context (missing method Deadline)

Check failure on line 37 in internal/cmd/certificate/retry.go

View workflow job for this annotation

GitHub Actions / test

cannot use s (variable of type state.State) as *cobra.Command value in argument to s.WaitForActions
return err
}

cmd.Printf("Retried issuance of certificate %s\n", certificate.Name)
return nil
},
}
44 changes: 44 additions & 0 deletions internal/cmd/certificate/retry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package certificate_test

import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"

"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/testutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestRetry(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

time.Local = time.UTC

cmd := certificate.RetryCmd.CobraCommand(fx.State())

cert := &hcloud.Certificate{
ID: 123,
Name: "my-test-cert",
}

fx.ExpectEnsureToken()
fx.Client.CertificateClient.EXPECT().
Get(gomock.Any(), "123").
Return(cert, nil, nil)
fx.Client.CertificateClient.EXPECT().
RetryIssuance(gomock.Any(), cert).
Return(&hcloud.Action{ID: 456}, nil, nil)
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 456}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123"})

assert.NoError(t, err)
assert.Empty(t, errOut)
assert.Equal(t, "Retried issuance of certificate my-test-cert\n", out)
}

0 comments on commit 0223f7d

Please sign in to comment.