From 2798902d53d5cb7500ba67a52a50095e0bb11b65 Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:21:13 +0100 Subject: [PATCH] feat(certificate): allow adding labels during create (#949) This PR adds the `--label` argument which was previously missing from `hcloud certificate create`. --- internal/cmd/certificate/create.go | 12 ++++++++++++ internal/cmd/certificate/create_test.go | 8 ++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 9835b16e..e68062ee 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -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)) @@ -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 @@ -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 } @@ -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), @@ -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 @@ -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, } diff --git a/internal/cmd/certificate/create_test.go b/internal/cmd/certificate/create_test.go index b350cd87..33896ec2 100644 --- a/internal/cmd/certificate/create_test.go +++ b/internal/cmd/certificate/create_test.go @@ -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"}, }). @@ -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" @@ -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"}, }). @@ -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", @@ -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" @@ -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",