Skip to content

Commit 23f61fc

Browse files
authored
CLI: coder licensese delete (#3699)
Signed-off-by: Spike Curtis <spike@coder.com> Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 98a6958 commit 23f61fc

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

enterprise/cli/licenses.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"regexp"
9+
"strconv"
910
"strings"
1011

1112
"github.com/spf13/cobra"
@@ -20,13 +21,14 @@ var jwtRegexp = regexp.MustCompile(`^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_
2021

2122
func licenses() *cobra.Command {
2223
cmd := &cobra.Command{
23-
Short: "Add, remove, and list licenses",
24+
Short: "Add, delete, and list licenses",
2425
Use: "licenses",
2526
Aliases: []string{"license"},
2627
}
2728
cmd.AddCommand(
2829
licenseAdd(),
2930
licensesList(),
31+
licenseDelete(),
3032
)
3133
return cmd
3234
}
@@ -142,3 +144,29 @@ func licensesList() *cobra.Command {
142144
}
143145
return cmd
144146
}
147+
148+
func licenseDelete() *cobra.Command {
149+
cmd := &cobra.Command{
150+
Use: "delete <id>",
151+
Short: "Delete license by ID",
152+
Aliases: []string{"del", "rm"},
153+
Args: cobra.ExactArgs(1),
154+
RunE: func(cmd *cobra.Command, args []string) error {
155+
client, err := agpl.CreateClient(cmd)
156+
if err != nil {
157+
return err
158+
}
159+
id, err := strconv.ParseInt(args[0], 10, 32)
160+
if err != nil {
161+
return xerrors.Errorf("license ID must be an integer: %s", args[0])
162+
}
163+
err = client.DeleteLicense(cmd.Context(), int32(id))
164+
if err != nil {
165+
return err
166+
}
167+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "License with ID %d deleted\n", id)
168+
return nil
169+
},
170+
}
171+
return cmd
172+
}

enterprise/cli/licenses_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ func TestLicensesListReal(t *testing.T) {
191191
})
192192
}
193193

194+
func TestLicensesDeleteFake(t *testing.T) {
195+
t.Parallel()
196+
// We can't check a real license into the git repo, and can't patch out the keys from here,
197+
// so instead we have to fake the HTTP interaction.
198+
t.Run("Mainline", func(t *testing.T) {
199+
t.Parallel()
200+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
201+
defer cancel()
202+
cmd := setupFakeLicenseServerTest(t, "licenses", "delete", "55")
203+
pty := attachPty(t, cmd)
204+
errC := make(chan error)
205+
go func() {
206+
errC <- cmd.ExecuteContext(ctx)
207+
}()
208+
require.NoError(t, <-errC)
209+
pty.ExpectMatch("License with ID 55 deleted")
210+
})
211+
}
212+
213+
func TestLicensesDeleteReal(t *testing.T) {
214+
t.Parallel()
215+
t.Run("Empty", func(t *testing.T) {
216+
t.Parallel()
217+
client := coderdtest.New(t, &coderdtest.Options{APIBuilder: coderd.NewEnterprise})
218+
coderdtest.CreateFirstUser(t, client)
219+
cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(),
220+
"licenses", "delete", "1")
221+
clitest.SetupConfig(t, client, root)
222+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
223+
defer cancel()
224+
errC := make(chan error)
225+
go func() {
226+
errC <- cmd.ExecuteContext(ctx)
227+
}()
228+
err := <-errC
229+
var coderError *codersdk.Error
230+
require.True(t, xerrors.As(err, &coderError))
231+
assert.Equal(t, 404, coderError.StatusCode())
232+
assert.Contains(t, "Unknown license ID", coderError.Message)
233+
})
234+
}
235+
194236
func setupFakeLicenseServerTest(t *testing.T, args ...string) *cobra.Command {
195237
t.Helper()
196238
s := httptest.NewServer(newFakeLicenseAPI(t))
@@ -217,6 +259,7 @@ func newFakeLicenseAPI(t *testing.T) http.Handler {
217259
r.Post("/api/v2/licenses", a.postLicense)
218260
r.Get("/api/v2/licenses", a.licenses)
219261
r.Get("/api/v2/buildinfo", a.noop)
262+
r.Delete("/api/v2/licenses/{id}", a.deleteLicense)
220263
return r
221264
}
222265

@@ -282,3 +325,8 @@ func (s *fakeLicenseAPI) licenses(rw http.ResponseWriter, _ *http.Request) {
282325
err := json.NewEncoder(rw).Encode(resp)
283326
assert.NoError(s.t, err)
284327
}
328+
329+
func (s *fakeLicenseAPI) deleteLicense(rw http.ResponseWriter, r *http.Request) {
330+
assert.Equal(s.t, "55", chi.URLParam(r, "id"))
331+
rw.WriteHeader(200)
332+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy