Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions apptrust/commands/application/application_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package application

import (
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
"github.com/jfrog/jfrog-cli-application/apptrust/model"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
)

func populateApplicationFromFlags(ctx *components.Context, descriptor *model.AppDescriptor) error {
descriptor.ApplicationName = ctx.GetStringFlagValue(commands.ApplicationNameFlag)

if ctx.IsFlagSet(commands.DescriptionFlag) {
description := ctx.GetStringFlagValue(commands.DescriptionFlag)
descriptor.Description = &description
}

if ctx.IsFlagSet(commands.BusinessCriticalityFlag) {
businessCriticalityStr := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag)
businessCriticality, err := utils.ValidateEnumFlag(
commands.BusinessCriticalityFlag,
businessCriticalityStr,
model.BusinessCriticalityUnspecified,
model.BusinessCriticalityValues)
if err != nil {
return err
}
descriptor.BusinessCriticality = &businessCriticality
}

if ctx.IsFlagSet(commands.MaturityLevelFlag) {
maturityLevelStr := ctx.GetStringFlagValue(commands.MaturityLevelFlag)
maturityLevel, err := utils.ValidateEnumFlag(
commands.MaturityLevelFlag,
maturityLevelStr,
model.MaturityLevelUnspecified,
model.MaturityLevelValues)
if err != nil {
return err
}
descriptor.MaturityLevel = &maturityLevel
}

if ctx.IsFlagSet(commands.LabelsFlag) {
labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag))
if err != nil {
return err
}
descriptor.Labels = &labelsMap
}

if ctx.IsFlagSet(commands.UserOwnersFlag) {
userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag))
descriptor.UserOwners = &userOwners
}

if ctx.IsFlagSet(commands.GroupOwnersFlag) {
groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag))
descriptor.GroupOwners = &groupOwners
}

return nil
}
38 changes: 4 additions & 34 deletions apptrust/commands/application/create_app_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,21 @@ func (cac *createAppCommand) buildRequestPayload(ctx *components.Context) (*mode
}

func (cac *createAppCommand) buildFromFlags(ctx *components.Context) (*model.AppDescriptor, error) {
applicationName := ctx.GetStringFlagValue(commands.ApplicationNameFlag)

project := ctx.GetStringFlagValue(commands.ProjectFlag)
if project == "" {
return nil, errorutils.CheckErrorf("--%s is mandatory", commands.ProjectFlag)
}

businessCriticalityStr := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag)
businessCriticality, err := utils.ValidateEnumFlag(
commands.BusinessCriticalityFlag,
businessCriticalityStr,
model.BusinessCriticalityUnspecified,
model.BusinessCriticalityValues)
if err != nil {
return nil, err
}

maturityLevelStr := ctx.GetStringFlagValue(commands.MaturityLevelFlag)
maturityLevel, err := utils.ValidateEnumFlag(
commands.MaturityLevelFlag,
maturityLevelStr,
model.MaturityLevelUnspecified,
model.MaturityLevelValues)
if err != nil {
return nil, err
descriptor := &model.AppDescriptor{
ProjectKey: project,
}

description := ctx.GetStringFlagValue(commands.DescriptionFlag)
userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag))
groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag))
labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag))
err := populateApplicationFromFlags(ctx, descriptor)
if err != nil {
return nil, err
}

return &model.AppDescriptor{
ApplicationName: applicationName,
Description: description,
ProjectKey: project,
MaturityLevel: maturityLevel,
BusinessCriticality: businessCriticality,
Labels: labelsMap,
UserOwners: userOwners,
GroupOwners: groupOwners,
}, nil
return descriptor, nil
}

func (cac *createAppCommand) loadFromSpec(ctx *components.Context) (*model.AppDescriptor, error) {
Expand Down
99 changes: 66 additions & 33 deletions apptrust/commands/application/create_app_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ func TestCreateAppCommand_Run_Flags(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

description := "Test application"
businessCriticality := "high"
maturityLevel := "production"

ctx := &components.Context{
Arguments: []string{"app-key"},
}
ctx.AddStringFlag("application-name", "test-app")
ctx.AddStringFlag("project", "test-project")
ctx.AddStringFlag("desc", "Test application")
ctx.AddStringFlag("desc", description)
ctx.AddStringFlag("business-criticality", "high")
ctx.AddStringFlag("maturity-level", "production")
ctx.AddStringFlag("maturity-level", maturityLevel)
ctx.AddStringFlag("labels", "env=prod;region=us-east")
ctx.AddStringFlag("user-owners", "john.doe;jane.smith")
ctx.AddStringFlag("group-owners", "devops;security")
Expand All @@ -36,15 +40,15 @@ func TestCreateAppCommand_Run_Flags(t *testing.T) {
ApplicationKey: "app-key",
ApplicationName: "test-app",
ProjectKey: "test-project",
Description: "Test application",
BusinessCriticality: "high",
MaturityLevel: "production",
Labels: map[string]string{
Description: &description,
BusinessCriticality: &businessCriticality,
MaturityLevel: &maturityLevel,
Labels: &map[string]string{
"env": "prod",
"region": "us-east",
},
UserOwners: []string{"john.doe", "jane.smith"},
GroupOwners: []string{"devops", "security"},
UserOwners: &[]string{"john.doe", "jane.smith"},
GroupOwners: &[]string{"devops", "security"},
}

mockAppService := mockapps.NewMockApplicationService(ctrl)
Expand Down Expand Up @@ -125,6 +129,52 @@ func TestCreateAppCommand_MissingProjectFlag(t *testing.T) {
assert.Contains(t, err.Error(), "--project is mandatory")
}

func TestCreateAppCommand_Run_FullSpecFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

ctx := &components.Context{
Arguments: []string{"app-full"},
}
ctx.AddStringFlag("url", "https://example.com")
ctx.AddStringFlag("spec", "./testfiles/full-spec.json")

expectedDescription := "A comprehensive test application"
expectedMaturityLevel := "production"
expectedBusinessCriticality := "high"
expectedPayload := &model.AppDescriptor{
ApplicationKey: "app-full",
ApplicationName: "test-app-full",
ProjectKey: "test-project",
Description: &expectedDescription,
MaturityLevel: &expectedMaturityLevel,
BusinessCriticality: &expectedBusinessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east-1",
"team": "devops",
},
UserOwners: &[]string{"john.doe", "jane.smith"},
GroupOwners: &[]string{"devops-team", "security-team"},
}

var actualPayload *model.AppDescriptor
mockAppService := mockapps.NewMockApplicationService(ctrl)
mockAppService.EXPECT().CreateApplication(gomock.Any(), gomock.Any()).
DoAndReturn(func(_ interface{}, req *model.AppDescriptor) error {
actualPayload = req
return nil
}).Times(1)

cmd := &createAppCommand{
applicationService: mockAppService,
}

err := cmd.prepareAndRunCommand(ctx)
assert.NoError(t, err)
assert.Equal(t, expectedPayload, actualPayload)
}

func TestCreateAppCommand_Run_SpecFile(t *testing.T) {
tests := []struct {
name string
Expand All @@ -144,26 +194,6 @@ func TestCreateAppCommand_Run_SpecFile(t *testing.T) {
ProjectKey: "test-project",
},
},
{
name: "full spec file",
specPath: "./testfiles/full-spec.json",
args: []string{"app-full"},
expectsPayload: &model.AppDescriptor{
ApplicationKey: "app-full",
ApplicationName: "test-app-full",
ProjectKey: "test-project",
Description: "A comprehensive test application",
MaturityLevel: "production",
BusinessCriticality: "high",
Labels: map[string]string{
"environment": "production",
"region": "us-east-1",
"team": "devops",
},
UserOwners: []string{"john.doe", "jane.smith"},
GroupOwners: []string{"devops-team", "security-team"},
},
},
{
name: "invalid spec file",
specPath: "./testfiles/invalid-spec.json",
Expand Down Expand Up @@ -193,7 +223,6 @@ func TestCreateAppCommand_Run_SpecFile(t *testing.T) {
ApplicationKey: "command-line-app-key",
ApplicationName: "test-app",
ProjectKey: "test-project",
Description: "A test application with application_key that should be ignored",
},
},
}
Expand Down Expand Up @@ -241,14 +270,18 @@ func TestCreateAppCommand_Run_SpecVars(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

expectedDescription := "A test application for production"
expectedMaturityLevel := "production"
expectedBusinessCriticality := "high"

expectedPayload := &model.AppDescriptor{
ApplicationKey: "app-with-vars",
ApplicationName: "test-app",
ProjectKey: "test-project",
Description: "A test application for production",
MaturityLevel: "production",
BusinessCriticality: "high",
Labels: map[string]string{
Description: &expectedDescription,
MaturityLevel: &expectedMaturityLevel,
BusinessCriticality: &expectedBusinessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east-1",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"application_key": "ignored-app-key",
"project_key": "test-project",
"application_name": "test-app",
"description": "A test application with application_key that should be ignored"
}
"application_name": "test-app"
}
37 changes: 4 additions & 33 deletions apptrust/commands/application/update_app_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,17 @@ func (uac *updateAppCommand) CommandName() string {

func (uac *updateAppCommand) buildRequestPayload(ctx *components.Context) (*model.AppDescriptor, error) {
applicationKey := ctx.Arguments[0]
applicationName := ctx.GetStringFlagValue(commands.ApplicationNameFlag)

businessCriticalityStr := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag)
businessCriticality, err := utils.ValidateEnumFlag(
commands.BusinessCriticalityFlag,
businessCriticalityStr,
"",
model.BusinessCriticalityValues)
if err != nil {
return nil, err
}

maturityLevelStr := ctx.GetStringFlagValue(commands.MaturityLevelFlag)
maturityLevel, err := utils.ValidateEnumFlag(
commands.MaturityLevelFlag,
maturityLevelStr,
model.MaturityLevelUnspecified,
model.MaturityLevelValues)
if err != nil {
return nil, err
descriptor := &model.AppDescriptor{
ApplicationKey: applicationKey,
}

description := ctx.GetStringFlagValue(commands.DescriptionFlag)
userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag))
groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag))
labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag))
err := populateApplicationFromFlags(ctx, descriptor)
if err != nil {
return nil, err
}

return &model.AppDescriptor{
ApplicationKey: applicationKey,
ApplicationName: applicationName,
Description: description,
MaturityLevel: maturityLevel,
BusinessCriticality: businessCriticality,
Labels: labelsMap,
UserOwners: userOwners,
GroupOwners: groupOwners,
}, nil
return descriptor, nil
}

func (uac *updateAppCommand) prepareAndRunCommand(ctx *components.Context) error {
Expand Down
30 changes: 18 additions & 12 deletions apptrust/commands/application/update_app_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ func TestUpdateAppCommand_Run(t *testing.T) {

serverDetails := &config.ServerDetails{Url: "https://example.com"}
appKey := "app-key"
description := "Updated description"
maturityLevel := "production"
businessCriticality := "high"
requestPayload := &model.AppDescriptor{
ApplicationKey: appKey,
ApplicationName: "app-name",
Description: "Updated description",
MaturityLevel: "production",
BusinessCriticality: "high",
Labels: map[string]string{
Description: &description,
MaturityLevel: &maturityLevel,
BusinessCriticality: &businessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east",
},
UserOwners: []string{"JohnD", "Dave Rice"},
GroupOwners: []string{"DevOps"},
UserOwners: &[]string{"JohnD", "Dave Rice"},
GroupOwners: &[]string{"DevOps"},
}

mockAppService := mockapps.NewMockApplicationService(ctrl)
Expand All @@ -54,18 +57,21 @@ func TestUpdateAppCommand_Run_Error(t *testing.T) {

serverDetails := &config.ServerDetails{Url: "https://example.com"}
appKey := "app-key"
description := "Updated description"
maturityLevel := "production"
businessCriticality := "high"
requestPayload := &model.AppDescriptor{
ApplicationKey: appKey,
ApplicationName: "app-name",
Description: "Updated description",
MaturityLevel: "production",
BusinessCriticality: "high",
Labels: map[string]string{
Description: &description,
MaturityLevel: &maturityLevel,
BusinessCriticality: &businessCriticality,
Labels: &map[string]string{
"environment": "production",
"region": "us-east",
},
UserOwners: []string{"JohnD", "Dave Rice"},
GroupOwners: []string{"DevOps"},
UserOwners: &[]string{"JohnD", "Dave Rice"},
GroupOwners: &[]string{"DevOps"},
}

mockAppService := mockapps.NewMockApplicationService(ctrl)
Expand Down
Loading