Skip to content

Commit

Permalink
feat(certificate): allow adding labels during create (#949)
Browse files Browse the repository at this point in the history
This PR adds the `--label` argument which was previously missing from
`hcloud certificate create`.
  • Loading branch information
phm07 authored Jan 3, 2025
1 parent 0998186 commit 2798902
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var CreateCmd = base.CreateCmd{
cmd.Flags().String("name", "", "Certificate name (required)")
_ = cmd.MarkFlagRequired("name")

cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

cmd.Flags().StringP("type", "t", string(hcloud.CertificateTypeUploaded),
fmt.Sprintf("Type of certificate to create. Valid choices: %v, %v",
hcloud.CertificateTypeUploaded, hcloud.CertificateTypeManaged))
Expand Down Expand Up @@ -61,6 +63,7 @@ var CreateCmd = base.CreateCmd{
func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, error) {
var (
name string
labels map[string]string
certFile, keyFile string
certPEM, keyPEM []byte
cert *hcloud.Certificate
Expand All @@ -74,6 +77,9 @@ func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, err
if name, err = cmd.Flags().GetString("name"); err != nil {
return nil, err
}
if labels, err = cmd.Flags().GetStringToString("label"); err != nil {
return nil, err
}
if certFile, err = cmd.Flags().GetString("cert-file"); err != nil {
return nil, err
}
Expand All @@ -90,6 +96,7 @@ func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, err

createOpts := hcloud.CertificateCreateOpts{
Name: name,
Labels: labels,
Type: hcloud.CertificateTypeUploaded,
Certificate: string(certPEM),
PrivateKey: string(keyPEM),
Expand All @@ -105,6 +112,7 @@ func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, err
func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, error) {
var (
name string
labels map[string]string
domains []string
res hcloud.CertificateCreateResult
err error
Expand All @@ -119,9 +127,13 @@ func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, erro
if domains, err = cmd.Flags().GetStringSlice("domain"); err != nil {
return nil, nil
}
if labels, err = cmd.Flags().GetStringToString("label"); err != nil {
return nil, err
}

createOpts := hcloud.CertificateCreateOpts{
Name: name,
Labels: labels,
Type: hcloud.CertificateTypeManaged,
DomainNames: domains,
}
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/certificate/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestCreateManaged(t *testing.T) {
fx.Client.CertificateClient.EXPECT().
CreateCertificate(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Labels: map[string]string{"foo": "bar"},
Type: hcloud.CertificateTypeManaged,
DomainNames: []string{"example.com"},
}).
Expand All @@ -53,7 +54,7 @@ func TestCreateManaged(t *testing.T) {
DomainNames: []string{"example.com"},
}, nil, nil)

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

expOut := "Certificate 123 created\n"

Expand All @@ -72,6 +73,7 @@ func TestCreateManagedJSON(t *testing.T) {
fx.Client.CertificateClient.EXPECT().
CreateCertificate(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Labels: map[string]string{},
Type: hcloud.CertificateTypeManaged,
DomainNames: []string{"example.com"},
}).
Expand Down Expand Up @@ -141,6 +143,7 @@ func TestCreateUploaded(t *testing.T) {
fx.Client.CertificateClient.EXPECT().
Create(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Labels: map[string]string{"foo": "bar"},
Type: hcloud.CertificateTypeUploaded,
Certificate: "certificate file content",
PrivateKey: "key file content",
Expand All @@ -151,7 +154,7 @@ func TestCreateUploaded(t *testing.T) {
Type: hcloud.CertificateTypeUploaded,
}, nil, nil)

out, errOut, err := fx.Run(cmd, []string{"--name", "test", "--key-file", "testdata/key.pem", "--cert-file", "testdata/cert.pem"})
out, errOut, err := fx.Run(cmd, []string{"--name", "test", "--key-file", "testdata/key.pem", "--cert-file", "testdata/cert.pem", "--label", "foo=bar"})

expOut := "Certificate 123 created\n"

Expand All @@ -170,6 +173,7 @@ func TestCreateUploadedJSON(t *testing.T) {
fx.Client.CertificateClient.EXPECT().
Create(gomock.Any(), hcloud.CertificateCreateOpts{
Name: "test",
Labels: map[string]string{},
Type: hcloud.CertificateTypeUploaded,
Certificate: "certificate file content",
PrivateKey: "key file content",
Expand Down

0 comments on commit 2798902

Please sign in to comment.