Skip to content

Commit 589111b

Browse files
authored
Delete application command (#19)
1 parent 8044a89 commit 589111b

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

application/cli/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func GetJfrogApplicationCli() components.App {
4141
version.GetPromoteAppVersionCommand(appContext),
4242
application.GetCreateAppCommand(appContext),
4343
application.GetUpdateAppCommand(appContext),
44+
application.GetDeleteAppCommand(appContext),
4445
},
4546
)
4647
return appEntity
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package application
2+
3+
import (
4+
"github.com/jfrog/jfrog-cli-application/application/app"
5+
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
6+
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
7+
8+
"github.com/jfrog/jfrog-cli-application/application/commands"
9+
"github.com/jfrog/jfrog-cli-application/application/commands/utils"
10+
"github.com/jfrog/jfrog-cli-application/application/common"
11+
"github.com/jfrog/jfrog-cli-application/application/service"
12+
"github.com/jfrog/jfrog-cli-application/application/service/applications"
13+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
14+
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
15+
)
16+
17+
type deleteAppCommand struct {
18+
serverDetails *coreConfig.ServerDetails
19+
applicationService applications.ApplicationService
20+
applicationKey string
21+
}
22+
23+
func (dac *deleteAppCommand) Run() error {
24+
ctx, err := service.NewContext(*dac.serverDetails)
25+
if err != nil {
26+
return err
27+
}
28+
29+
return dac.applicationService.DeleteApplication(ctx, dac.applicationKey)
30+
}
31+
32+
func (dac *deleteAppCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
33+
return dac.serverDetails, nil
34+
}
35+
36+
func (dac *deleteAppCommand) CommandName() string {
37+
return commands.DeleteApp
38+
}
39+
40+
func (dac *deleteAppCommand) prepareAndRunCommand(ctx *components.Context) error {
41+
if len(ctx.Arguments) != 1 {
42+
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
43+
}
44+
45+
dac.applicationKey = ctx.Arguments[0]
46+
47+
var err error
48+
dac.serverDetails, err = utils.ServerDetailsByFlags(ctx)
49+
if err != nil {
50+
return err
51+
}
52+
53+
return commonCLiCommands.Exec(dac)
54+
}
55+
56+
func GetDeleteAppCommand(appContext app.Context) components.Command {
57+
cmd := &deleteAppCommand{
58+
applicationService: appContext.GetApplicationService(),
59+
}
60+
return components.Command{
61+
Name: "delete",
62+
Description: "Delete an application",
63+
Category: common.CategoryApplication,
64+
Arguments: []components.Argument{
65+
{
66+
Name: "application-key",
67+
Description: "The key of the application to delete",
68+
Optional: false,
69+
},
70+
},
71+
Action: cmd.prepareAndRunCommand,
72+
}
73+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package application
2+
3+
import (
4+
"errors"
5+
"flag"
6+
"testing"
7+
8+
mockapps "github.com/jfrog/jfrog-cli-application/application/service/applications/mocks"
9+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
10+
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/urfave/cli"
13+
"go.uber.org/mock/gomock"
14+
)
15+
16+
func TestDeleteAppCommand_Run(t *testing.T) {
17+
ctrl := gomock.NewController(t)
18+
defer ctrl.Finish()
19+
20+
serverDetails := &config.ServerDetails{Url: "https://example.com"}
21+
appKey := "app-key"
22+
23+
mockAppService := mockapps.NewMockApplicationService(ctrl)
24+
mockAppService.EXPECT().DeleteApplication(gomock.Any(), appKey).Return(nil).Times(1)
25+
26+
cmd := &deleteAppCommand{
27+
applicationService: mockAppService,
28+
serverDetails: serverDetails,
29+
applicationKey: appKey,
30+
}
31+
32+
err := cmd.Run()
33+
assert.NoError(t, err)
34+
}
35+
36+
func TestDeleteAppCommand_Run_Error(t *testing.T) {
37+
ctrl := gomock.NewController(t)
38+
defer ctrl.Finish()
39+
40+
serverDetails := &config.ServerDetails{Url: "https://example.com"}
41+
appKey := "app-key"
42+
43+
mockAppService := mockapps.NewMockApplicationService(ctrl)
44+
mockAppService.EXPECT().DeleteApplication(gomock.Any(), appKey).Return(errors.New("failed to delete application. Status code: 500")).Times(1)
45+
46+
cmd := &deleteAppCommand{
47+
applicationService: mockAppService,
48+
serverDetails: serverDetails,
49+
applicationKey: appKey,
50+
}
51+
52+
err := cmd.Run()
53+
assert.Error(t, err)
54+
assert.Equal(t, "failed to delete application. Status code: 500", err.Error())
55+
}
56+
57+
func TestDeleteAppCommand_WrongNumberOfArguments(t *testing.T) {
58+
ctrl := gomock.NewController(t)
59+
defer ctrl.Finish()
60+
app := cli.NewApp()
61+
set := flag.NewFlagSet("test", 0)
62+
ctx := cli.NewContext(app, set, nil)
63+
64+
mockAppService := mockapps.NewMockApplicationService(ctrl)
65+
cmd := &deleteAppCommand{
66+
applicationService: mockAppService,
67+
}
68+
69+
// Test with no arguments
70+
context, err := components.ConvertContext(ctx)
71+
assert.NoError(t, err)
72+
73+
err = cmd.prepareAndRunCommand(context)
74+
assert.Error(t, err)
75+
assert.Contains(t, err.Error(), "Wrong number of arguments")
76+
}

application/commands/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const (
1313
PromoteAppVersion = "promote-app-version"
1414
CreateApp = "create-app"
1515
UpdateApp = "update-app"
16+
DeleteApp = "delete-app"
1617
)
1718

1819
const (

application/http/http_client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type AppHttpClient interface {
2626
Post(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
2727
Get(path string) (resp *http.Response, body []byte, err error)
2828
Patch(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
29+
Delete(path string) (resp *http.Response, body []byte, err error)
2930
}
3031

3132
type appHttpClient struct {
@@ -138,6 +139,16 @@ func (c *appHttpClient) toJsonBytes(payload interface{}) ([]byte, error) {
138139
return jsonBytes, nil
139140
}
140141

142+
func (c *appHttpClient) Delete(path string) (resp *http.Response, body []byte, err error) {
143+
url, err := utils.BuildUrl(c.serverDetails.Url, appTrustApiPath+path, nil)
144+
if err != nil {
145+
return nil, nil, err
146+
}
147+
148+
log.Debug("Sending DELETE request to:", url)
149+
return c.client.SendDelete(url, nil, c.getJsonHttpClientDetails())
150+
}
151+
141152
func (c *appHttpClient) getJsonHttpClientDetails() *httputils.HttpClientDetails {
142153
httpClientDetails := c.authDetails.CreateHttpClientDetails()
143154
httpClientDetails.SetContentTypeApplicationJson()

application/service/applications/application_service.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
type ApplicationService interface {
1616
CreateApplication(ctx service.Context, requestBody *model.AppDescriptor) error
1717
UpdateApplication(ctx service.Context, requestBody *model.AppDescriptor) error
18+
DeleteApplication(ctx service.Context, applicationKey string) error
1819
}
1920

2021
type applicationService struct{}
@@ -53,3 +54,19 @@ func (as *applicationService) UpdateApplication(ctx service.Context, requestBody
5354
fmt.Println(string(responseBody))
5455
return nil
5556
}
57+
58+
func (as *applicationService) DeleteApplication(ctx service.Context, applicationKey string) error {
59+
endpoint := fmt.Sprintf("/v1/applications/%s", applicationKey)
60+
response, responseBody, err := ctx.GetHttpClient().Delete(endpoint)
61+
if err != nil {
62+
return err
63+
}
64+
65+
if response.StatusCode != http.StatusNoContent {
66+
return errorutils.CheckErrorf("failed to delete application. Status code: %d.\n%s",
67+
response.StatusCode, responseBody)
68+
}
69+
70+
fmt.Println("Application deleted successfully")
71+
return nil
72+
}

0 commit comments

Comments
 (0)